0

I want to call the ERC20 contract with other contracts and transfer the corresponding balance to the contract caller. What should I do?

0

You need to have some tokens in you caller contract and then you can send the msg.sender some tokens.Following is the code you are loooking for:

contract ERC20 {
    function totalSupply() public constant returns (uint);
    function balanceOf(address tokenOwner) public constant returns (uint balance);
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);

    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}

contract CallerContract{
      ERC20 public token = ERC20(0x738A6a902ff347fB8e28DEEf824a4754235f0b8B); // contarct add of erc-20 token

      function sendERC20(uint coints) {
        ERC20.transfer(msg.sender, coins);
      }

}
4
  • Thanks for the answer, but it still doesn't work. I used OpenZeppelin and I created a standard token. please refer to the : gist.github.com/52812c6adfb7e5cf9b2343ae09651081 – Jacob Sun Mar 5 '18 at 5:19
  • What do you mean by it didn't work? Did you got any errors? – Prashant Prabhakar Singh Mar 7 '18 at 4:43
  • yes, I got a error: Member "transfer" not found or not visible after argument-dependent lookup in type. thank you for your reply :) – Jacob Sun Mar 7 '18 at 8:51
  • Did you pass your token address in ERC20(your_address_here)? As I can see the implementation you followed has transfer function and every erc-20 standard has. So unless you specified an incorrect address, it should work. – Prashant Prabhakar Singh Mar 7 '18 at 12:10
0

You can call the ERC20 contract from your new contract. Then you can access the balance of any address. Note that there is no need to try to copy the whole balanceOf, that mapping variable can just be accessed from your new contract every time that you need. Below there is an example of how to do this. Assume your ERC20 contract is called MyERC20 and your new contract is called MyERC20Copy

contract MyERC20 {
...
}

contract MyERC20Copy public { 
    MyERC20 m = MyERC20(contractaddress_for_MyERC20);

    function MyERC20Copy() { 
    // constructor
    } 

    function getERC20Balance(address target) public {
        unit balance = m.balanceOf[target]; 
    }
}

I hope this helps.

1
  • Welcome to the community. The OP is not asking about getting balance, instead he is willing to transfer erc-20 token. This answer is similar to mine. I guess you can add a comment to the answer. This doesn't help OP. – Prashant Prabhakar Singh Mar 7 '18 at 4:46

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.