0

I struggle understanding conversion. I found a lot of questions here and online that state that converting between substrate primitives and rust primitives can easily be handled with into() calls or the Saturated package or other stuff.

I'm trying this with https://github.com/paritytech/substrate/blob/master/frame/assets/src/lib.rs#L197

There is this (hopelessly outdated) example here: https://github.com/paritytech/substrate/tree/master/frame/assets#simple-code-snippet

... that does not work with the current version of pallet assets for ages.

However, so I have:

type AssetIdOf<T> = <T as pallet_assets::Config>::AssetId;

...

#[pallet::storage]
#[pallet::getter(fn get_next_asset_id)]
pub type NextAssetId<T> = StorageValue<_, AssetIdOf<T>, ValueQuery>;

and basically all I want to do is add 1 to NextAssetId.

I tried everything but it says that it missed From<u32> (on into) or can't add if I do something like <NextAssetId<T>>::mutate(|asset_id| *asset_id += 1); or even <NextAssetId<T>>::mutate(|asset_id| *asset_id += One::one()); as in a very old commit on the repo.

I'm not sure if i attack this problem correctly at all.

1 Answer 1

1

I think that in order to achieve what you're trying to do, you need to put an explicit trait bound in your pallet's config, enforcing AssetId type to be incrementable.

I would do it like this (assuming we're tightly coupling):

use frame_support::sp_runtime::traits::{Saturating, One};

...

#[pallet::config]
pub trait Config: pallet_assets::Config {
    type AssetId: IsType<<T as pallet_assets::Config>::AssetId>
        + Saturating
        + One;
}

type AssetIdOf<T> = <T as Config>::AssetId;

Then you should be able to increment values of type AssetId like this:

asset_id.saturating_inc();
2
  • This works, but I have to add Parameter, Default, MaxEncodedLen as traits for the storage. What I don't understand: Why do I have to add them again? Isn't the IsType bit defining it as AssetId from config which in turn already has this trait?
    – shredding
    Oct 25, 2022 at 16:38
  • 1
    @shredding I think IsType only allows to convert back and forth, but isn't so smart to tell the compiler that type A implements all the traits which type B does. You would probably need a macro for that. Oct 26, 2022 at 7:29

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.