2

When you declare a storage say

pub type Something<T> = StorageValue<_, u32>;

We get query function "something".

But I want to write some specific functions that reads storage and supplies it to frontend.

Say:

fn get_something() -> u32 {
       match <Something<T>>::get() {
                None => Err(Error::<T>::NoneValue)?,
                Some(value) => {
                    value + 1
                },
            }
    }

Solidity has view functions. How to write view functions or custom queries in substrate?

1 Answer 1

1

We get query function "something".

You don't get this out of the box. You will get this only when you declare #[pallet::getter(fn something)] on top of the storage item. This is totally optional, and adds a fn something to impl<T: Config> Pallet<T> {}. You can simply skip the #[pallet::getter(fn something)], and add your custom getter/view function:

impl<T: Config> Pallet<T> {
  fn get_something() -> u32 {
    // custom logic
  } 
}
2
  • Ya, I have done it, not sure why its not showing on pallet inspector substrate-node-template Apr 7, 2022 at 13:54
  • No. I am unable to make it work. Apr 7, 2022 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.