0

How can I call transfer function of a ERC20 token contract from another contract? in this question there is one answer from pabloruiz55 but in this example or demo code ERC20 token = ERC20(_tokenAddress);

What exactly is ERC20 here? i Is it an ABI of the other smart contract, is it any other imported smart contract or a place holder to explain the concept? If it is a placeholder can you please explain what I should replace it with in order to transfer tokens?

0

assume that the ERC20 token that you want to transfer is called ERC20Token (thats the name of the contract)

you can import ERC20Token contract (or the interface of it) in your contract and then do this, for instance if you want to transfer tokens that belong to your contract to someone else:

function interactWithERC20Token (address tokenContractAddress, address _to, value) public {
    ERC20Token myInstance = ECR20Token(tokenContractAddress);
    myInstance.transfer(_to,value);
}

tokenContractAddress is the address of the token Contract. This assumes that your contract owns tokens.

In the answer that you presented, they just used "ERC20" as the name of the ERC20 token contract.

Finally you can call all the functions of ERC20Token using myInstance.<function>.

This is just an example of how to interact with the token contract. The function that I used as an example will allow anyone to take tokens from your contract so don't use this literally.

  • thank you! also do i have to paste abi of that deployed contract when i will write this code ERC20Token myInstance = ECR20Token(tokenContractAddress); – Akshay Pilankar Apr 19 '18 at 11:14
  • No. In solidity, you just need to either copy the token contract inside your contract, import it, or just to add the interface of the token contract. The interface is just the definition of all the functions in the token contract without the implementation. Using the interface instead of importing of copying the whole token contract makes the deployment of your contract cheaper in terms of gas. Hope this help you. – Jaime Apr 19 '18 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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