4

Hello Substrate and Polkadot community,

I am working on creating a decentralized exchange (DEX) using Substrate pallets and I've encountered an issue related to transferring tokens from the contract owner to the pallet address.

To add liquidity for a token pair (e.g., XXX/USDT) in my DEX, users need to provide the contract address of the token (e.g., XXX). Before initiating the transfer, I want to ensure that the user is indeed the owner of the contract. Once the ownership is verified, I need to transfer tokens from the contract address (e.g., 5xsdf6s5d4f6sd4f65sd4fs65df46s5d4f6s5d4f) to the pallet address.

Could anyone provide guidance or sample code on how to achieve this token transfer from the contract owner to the pallet address while ensuring the user is the actual owner of the contract?

Any help or suggestions would be greatly appreciated. Thank you!

Update

I want to transfer token with this code:

#[pallet::call_index(9)]
#[pallet::weight(10_000)]
pub fn total_supply(
    origin: OriginFor<T>,
    token: T::AccountId,
    currency: T::AccountId,
    currency_amount: BalanceOf<T>,
    token_amount: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
    let who = ensure_signed(origin)?;
     
    T::Currency::transfer(&who, &T::PalletId::get().into_account(), 
    T::MinDeposit::get(), ExistenceRequirement::KeepAlive)?; 
}

but in this code transfer native token of the chain and I need to transfer from XXX token to the pallet account.

3
  • You have ink! smart contracts running that want to interact with a pallet in your node?
    – Alex Bean
    Commented Apr 10, 2023 at 9:12
  • @AlexBean I using ink smart contract . But i want to know , can i accept ink! And solidity at the same time ?
    – Mr Coder
    Commented Apr 10, 2023 at 9:51
  • Answered! And about the ink! - solidity, I am not very familiar but take a look into this post: forum.polkadot.network/t/…
    – Alex Bean
    Commented Apr 10, 2023 at 13:08

1 Answer 1

3

A Smart Contract account doesn't have a private key that allows you to make a transfer as normal account do. So you have to implement a function that does this transfer in the smart contract itself.

In this call the first thing you want to do is check the contract owner of your Smart Contract. You can get the owner in the constructor and store it in the storage of the SC.

A basic example:

#[ink(storage)]
    pub struct YourStorage {
        /// Owner of the Smart Contract
        owner: AccountId,
    }

And in the constructor, get the owner:

#[ink(constructor, payable)]
        pub fn new() -> Self {
            let owner = Self::env().caller();
            Self {
                owner,
            }
        }

Then in the call of your Smart Contract you can check if the caller of that call is the same Account you have stored before as the owner:

let caller = Self::env().caller();
// Only owner of the SC can call this message.
if caller != self.owner {
    return Err(Error::BadOrigin);
}

In that call, after the check that the caller is the owner of the smart contract, you can do the transfer into the pallet runtime account.

Self::env().transfer(pallet_address, amount)?;

The pallet address is calculated from the pallet id, see this explanation about how this calculation is done here: About pallet's account

For the Smart Contract to have funds you have to send funds previously, check this old question on StackExchange: How to deposit into an Ink! Smart Contract:

4
  • Thanks but its nit my answer . I want to transfer token from contract to pallet id , into runtime pallet .
    – Mr Coder
    Commented Apr 10, 2023 at 13:20
  • Form example . I want to add XXX token to liqudity and i have to transfer token from owner of contract to pallet id . Transfer XXX token to pallet id .
    – Mr Coder
    Commented Apr 10, 2023 at 13:21
  • Transfer from the Smart Contract to pallet account? Updated!
    – Alex Bean
    Commented Apr 10, 2023 at 13:53
  • i updated the question . please check it
    – Mr Coder
    Commented Apr 10, 2023 at 15:36

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.