1

I was making a function to store the some data in the pallet. #[pallet::storage] #[pallet::getter(fn number)] // Learn more about declaring storage items: // https://docs.substrate.io/main-docs/build/runtime-storage/#declaring-storage-items

pub type Number<T> = StorageMap<_, Blake2_128Concat,u32,Vec<u8>,ValueQuery>;

#[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
        pub fn store_data(origin: OriginFor<T>, number: u32, data: Vec<u8>) -> DispatchResult {
            // Check that the extrinsic was signed and get the signer.
            // This function will return an error if the extrinsic is not signed.
            // https://docs.substrate.io/main-docs/build/origins/
            let who = ensure_signed(origin)?;

            // Update storage.
            <Number<T>>::insert(number,data.clone());

            // Emit an event.
            Self::deposit_event(Event::SomethingStored ( who.clone(),number.clone(),data.clone() ));
            // Return a successful DispatchResultWithPostInfo
            Ok(())
        }

And i was making a function in which i want to print the current existing data of the particular storage.

    #[pallet::weight(10_000 + T::DbWeight::get().writes(1).ref_time())]
    pub fn show_data(origin: OriginFor<T>,number: u32, data: Vec<u8> ) -> DispatchResult {
        // Check that the extrinsic was signed and get the signer.
        // This function will return an error if the extrinsic is not signed.
        let who = ensure_signed(origin)?;
    
        // Update storage.
         <Number<T>>::get(number);
        <Number<T>>::get(data.clone());
    
    
        // Emit an event.
        Self::deposit_event(Event::Data(who.clone(),number.clone(),data.clone()));
    
        Ok(())
    }

In the number(u32) is working fine. But in the get(Vec) is showing the trait EncodeLike<u32> is not implemented for Vec<u8>

1 Answer 1

1

The StorageValue Number is a data structure similar to traditional hash maps with key-to-value mapping (See the docs).

The method get loads the value associated with the given key from the map.

fn get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query

And the insert stores a value to be associated with the given key from the map:

fn insert<KeyArg: EncodeLike<K>, ValArg: EncodeLike<V>>(key: KeyArg, val: ValArg)

Therefore in your case it makes sense to retrieve the value from the number, because you are storing number as a key:

<Number<T>>::insert(number,data.clone());
let data_from_storage = <Number<T>>::get(number);

But you can not do it the other way around (retrieve the key from the data)

    <Number<T>>::get(data.clone());

This will work in case you want to use the Vec as a Key and the number as a Value (But not sure if it makes sense):

#[pallet::storage]
#[pallet::getter(fn number)]
pub type Number<T> = StorageMap<_, Blake2_128Concat,Vec<u8>, u32, ValueQuery>;

Then use Vec as a key:

<Number<T>>::insert(data.clone(), number);

And you can retrieve the number:

let number_from_storage = <Number<T>>::get(data.clone());

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.