1

Here is my scenario: One account will maintain 3 balances of Token A, Token B, Token C. When Alice sends x Token A to Bob, it should deposit to Bob's balance for the same kind of token, means Token A. Similarly for Token C, but Token B is not transferable.

Inside the same about, It is possible to convert Token A into Token C with a conversion rate. So, how should I implement them? and What I did so far is?

#[frame_support::pallet]
pub mod pallet {
    
    use frame_system::pallet_prelude::*;
    use frame_support::traits::Currency;
    use frame_support::serde::{Deserialize, Serialize};

    #[pallet::pallet]
    pub struct Pallet<T>(_);

    //#[cfg(feature = "std")]
    type TokenABalanceOf<T> = <<T as Config>::Token as Currency<<T as frame_system::Config>::AccountId>>::Balance;
    type TokenBBalanceOf<T> = <<T as Config>::TokenB as Currency<<T as frame_system::Config>::AccountId>>::Balance;
    type TokenCBalanceOf<T> = <<T as Config>::TokenC as Currency<<T as frame_system::Config>::AccountId>>::Balance;


    pub trait TokenAManagement<T:Config> {
        // fn convert_to_TokenC(from: T::&AccountId, amount: TokenBalanceOf<T>) -> DispatchResult;
        fn withdraw(owner: T::AccountId, amount: TokenBalanceOf<T>) -> DispatchResult;
        // fn transfer(from: &T::AccountId, to: &AccountId, value: TokenBalanceOf<T>) -> DispatchResult;
        fn deposit(owner: T::AccountId, amount: u32) -> DispatchResult;
    }

    pub trait TokenBManagement<T:Config> {
        fn deposit(owner: T::AccountId, amount: u32) -> DispatchResult;
        // fn lock(owner: T::AccountId, amount: TokenCBalanceOf<T>) -> DispatchResult;
        // fn burn(owner: T::AccountId, amount: TokenCBalanceOf<T>) -> DispatchResult;
    }

    pub trait TokenBManagement<T:Config> {
        fn withdraw(owner: T::AccountId, amount: u32) -> DispatchResult;
        fn deposit(owner: T::AccountId, amount: u32) -> DispatchResult;
    }



    #[pallet::config]
    pub trait Config: frame_system::Config + pallet_transaction_payment::Config {
        /// Because this pallet emits events, it depends on the runtime's definition of an event.
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

        type TokenA : Currency<Self::AccountId>;
        type TokenB : Currency<Self::AccountId>;
        type TokenC : Currency<Self::AccountId>;

        type TokenAManagement: TokenAManagement<Self>;
        type TokenBManagement: TokenBManagement<Self>;
        type TokenCManagement: TokenCManagement<Self>;
    
        
    }
}
    

My code is a mess. Could you please give me some idea to implement them more efficiently?

1 Answer 1

1

There exists pallet-assets to manage an arbitrary number of different distinct assets. Alternatively there also is pallet-nfts for non-fungible assets.

Assets are addressed via an ID which makes it possible to easily write generic functions that operate on any asset. Additionally to this there is pallet-asset-conversion which is a Uniswap-like pallet for converting assets.

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.