2

I'm working on a Substrate pallet, and I have a custom associated type AssetId. I need to convert an AccountId into this AssetId. My Config trait looks like this:

#[pallet::config]
pub trait Config: frame_system::Config + TypeInfo + pallet_contracts::Config {
    /// Pallet ID.
    #[pallet::constant]
    type PalletId: Get<PalletId>;

    /// The overarching event type.
    type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

    /// The currency trait.
    type Currency: Currency<Self::AccountId>;

    /// The balance type for assets (i.e. tokens).
    type AssetBalance: Balance
        + FixedPointOperand
        + MaxEncodedLen
        + MaybeSerializeDeserialize
        + TypeInfo;

    // Two-way conversion between asset and currency balances
    type AssetToCurrencyBalance: Convert<Self::AssetBalance, BalanceOf<Self>>;
    type CurrencyToAssetBalance: Convert<BalanceOf<Self>, Self::AssetBalance>;

    /// The asset ID type.
    type AssetId: MaybeSerializeDeserialize
        + MaxEncodedLen
        + TypeInfo
        + Clone
        + Debug
        + PartialEq
        + EncodeLike
        + Decode;

    /// The type for tradable assets.
    type Assets: Inspect<Self::AccountId, AssetId = Self::AssetId, Balance = Self::AssetBalance>
        + Transfer<Self::AccountId>;

    /// The type for liquidity tokens.
    type AssetRegistry: Inspect<Self::AccountId, AssetId = Self::AccountId, Balance = Self::AssetBalance>
        + Mutate<Self::AccountId>
        + Create<Self::AccountId>
        + Destroy<Self::AccountId>;

    /// Information on runtime weights.
    type WeightInfo: WeightInfo;

    /// Provider fee numerator.
    #[pallet::constant]
    type ProviderFeeNumerator: Get<BalanceOf<Self>>;

    /// Provider fee denominator.
    #[pallet::constant]
    type ProviderFeeDenominator: Get<BalanceOf<Self>>;

    /// Minimum currency deposit for a new exchange.
    #[pallet::constant]
    type MinDeposit: Get<BalanceOf<Self>>;
}

this is the function i need to convert <T as SysConfig>::AccountId to <T as pallet::Config>::AssetId .

#[pallet::call_index(0)]
        #[pallet::weight(<T as Config>::WeightInfo::create_exchange())]
        #[transactional]
        pub fn create_exchange(
            origin: OriginFor<T>,
            asset_id:  AssetIdOf<T>,
            liquidity_token_id:  AssetIdOf<T>,
            currency_amount: BalanceOf<T>,
            token_amount: BalanceOf<T>,
        ) -> DispatchResult {
            // -------------------------- Validation part --------------------------
            let caller = ensure_signed(origin)?;
    

            let exchange = Exchange {
                asset_id: asset_id.clone(),
                currency_reserve: <BalanceOf<T>>::zero(),
                token_reserve: <AssetBalanceOf<T>>::zero(),
                liquidity_token_id: liquidity_token_id.clone(),
            };

            
            Ok(())
        }

and this is my strct :

#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, MaxEncodedLen, TypeInfo)]
    pub struct Exchange<T: Config> {
        pub asset_id: T::AccountId,
        pub currency_reserve: BalanceOf<T>,
        pub token_reserve: AssetBalanceOf<T>,
        pub liquidity_token_id: T::AccountId,
    }

i have to use T::AccountId for call contract for some actions :

let result = pallet_contracts::Pallet::<T>::bare_call(
            origin.clone(),
            contract_address.clone(),
            contracts_value,
            gas_limit,
            None,
            data,
            false,
            pallet_contracts::Determinism::Deterministic,
        );

and use AssetId for using T::AssetRegistry::mint_into .

T::AssetRegistry::mint_into(
            exchange.liquidity_token_id.clone(),
            &provider,
            liquidity_minted,
        )?;

when i need to rin this code it show me this error :

mismatched types expected associated type <T as pallet::Config>::AssetId found associated type <T as SysConfig>::AccountId

Repository

how can i sovle this problem ?

4
  • Can you link the repo? Or create a reproducible example.
    – Bruno
    May 8 at 20:53
  • @Bruno this is repo bro .
    – Mr Coder
    May 9 at 6:34
  • 1
    I do not think it makes sense to convert a 32 byte accountId to an assetId that is usually u32/u128. What are you trying to achieve? Perhaps you want a mapping between AccountId and AccountId that owns the asset?
    – Bruno
    May 9 at 14:55

1 Answer 1

1

if I understand you right, you probably want to have some account id associated to a particular exchange from your example. You can create a system account for it, for example combining some prefix sacc/exch + asset_id.

System Accounts

Example

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.