1

In my runtime I'm using pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;. Within my pallet I also need this type to implement From<SomeType>. I attempted to add type CustomAccountId: From<SomeType>; to my pallet's config, but encountered numerous errors. I'm not even sure if I'm on the right track. What is the proper way to achieve this?

Edit: To be more specific, I have the following type definition in my pallet, which is also used in a StorageMap

type AccountId: IsType<<Self as frame_system::Config>::AccountId> + From<H160>;

// some code...

#[pallet::storage]
pub(super) type AssetOwner<T: Config> = StorageMap<_, Blake2_128Concat, U256, <T as pallet::Config>::AccountId, OptionQuery>;

Some errors I got:

error[E0599]: the function or associated item `get` exists for struct `StorageMap<_GeneratedPrefixForStorageAssetOwner<T>, Blake2_128Concat, U256, <T as Config>::AccountId>`, but its trait bounds were not satisfied
...
error[E0277]: the trait bound `<T as pallet::Config>::AccountId: std::clone::Clone` is not satisfied
...
the trait bound `<T as pallet::Config>::AccountId: WrapperTypeEncode` is not satisfied
...
error[E0369]: binary operation `==` cannot be applied to type `&<T as pallet::Config>::AccountId`
...

These errors make me suspect that I'm not extending the AccountId type from the runtime config correctly. What is the proper way to achieve this functionality?

5
  • Could you include what you need it for and provide the errors that you have encountered? Commented Aug 29, 2023 at 7:05
  • @DaanvanderPlas I would like to "extend" the AccountId type used within the pallet so that it also has to implement From<H160> and Into<H160>, enabling conversion between the T::AccountId type and H160.
    – magecnion
    Commented Aug 29, 2023 at 10:32
  • Can you paste the error and share the implementation? will be easy if we can replicate your issue
    – Alex Bean
    Commented Aug 29, 2023 at 14:24
  • Could you give a link to your repo? Commented Aug 30, 2023 at 9:54
  • @DaanvanderPlas sure: github.com/freeverseio/laos-ownership-node/blob/refactor/…
    – magecnion
    Commented Aug 30, 2023 at 10:11

1 Answer 1

1
+50

Outdated

The error message is still unclear. It appears that the error originated from the runtime instead of the pallet. When passing AccountId = YourImplementation, YourImplementation must meet all trait bounds.

You can check how other projects implement their account id here: link.


For example, this line #[derive(Clone)] at line 30 will satisfy the following error: ``` error[E0277]: the trait bound <T as pallet::Config>::AccountId: std::clone::Clone is not satisfied


I believe so.

After reviewing your source code, it appears that the IsType function removes the trait bound of frame_system::Config::AccountId. It seems there is no straightforward way to extend the associated type for this.

If you really want to do this that way, try:

type AccountId: IsType<<Self as frame_system::Config>::AccountId> + From<H160> + Parameter + Member + MaxEncodedLen;

This almost re-define a new AccountId.

Check: https://github.com/freeverseio/laos-ownership-node/pull/143.


There is another way, use the frame_system::Config::AccountId directly and provide a converter for it. For example:

trait Config: frame_system::Config {
    type H160ToAccountId: Convert<Self::AccountId, H160>
}

trait Convert<A, B> {
    fn convert(a: A) -> B;
}

enum H160ToAccountIdConverter {}
impl Convert<AccountId, H160> for H160ToAccountIdConverter {
    ...
}

impl your_pallet::Config for Runtime {
    type H160ToAccountId = H160ToAccountIdConverter;
}
2
  • I'm currently working within the pallet directory to test its functionality. Despite adding #[derive(Clone, Decode, Encode, TypeInfo, Debug, Eq, PartialEq, MaxEncodedLen, Ord, PartialOrd, derive_more::Display, Serialize, Deserialize)] to my test AccountId struct, I'm still getting the errors stated above
    – magecnion
    Commented Aug 30, 2023 at 10:16
  • Updated my answer.
    – AurevoirXavier
    Commented Aug 30, 2023 at 11:22

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