12

When declaring storage items inside the runtime, we can automatically generate getters to query that storage item, for example (https://docs.substrate.io/v3/runtime/storage/#declaring-`storage-items`)`:

#[pallet::storage]
#[pallet::getter(fn some_primitive_value)]
pub(super) type SomePrimitiveValue<T> = StorageValue<_, u32, ValueQuery>;

Is there a simple way to generate a setter in the same way? While a simple setter is likely undesirable on all storage items, it would be handy to have a shorthand way to set storage values from root or signed accounts:

#[pallet::root_setter(fn some_primitive_value)]
#[pallet::signed_setter(fn some_primitive_value)]

1 Answer 1

8

There is not a way with our macros, and generally I would suggest not even to use this feature.

When you include #[pallet::getter(fn some_primitive_value)] all that is happening is the generation of the following function:

fn some_primitive_value() -> u32 {
    SomePrimitiveValue::<T>::get()
}

So really you have not saved much code writing, and you have created some macro magic abstraction.

Similarly, if you wanted to write a "setter", you can just write:

fn set_some_primitive_value(value: u32) {
    SomePrimitiveValue::<T>::put(value)
}
3
  • So using the pallet::getter macro is fully optional? Mar 22, 2022 at 16:45
  • 1
    yes. it is exactly equivalent to writing the function I wrote above inside imp<T: Config> for Pallet<T>
    – Shawn Tabrizi
    Mar 22, 2022 at 16:53
  • 1
    Yes, this is also the practice adopted by Phala Network
    – h4x3rotab
    Mar 23, 2022 at 6:34

Your Answer

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

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