0

I'm struggling to transfer my ERC20 token toward a contract address. My code is like this. What's wrong with my code? the code is like this.

Contract A{
  function deposit(uint amount) public {
    ERC20.transfer(address(this), amount);
  }
}

I have confirmed contract type ERC20 is valid.

0

ERC20 requires two transactions for transferring:

  1. account approves an allowance for Contract A
  2. account then calls Contract A where Contract A calls transferFrom with an amount within the allowance.

The issue with your code is that you are using transfer rather than transferFrom and you need to set an allowance.

If you are creating ERC20 tokens you may want to look at the OpenZeppelin Contracts implementation to see if this meets your needs. See the documentation for details: https://docs.openzeppelin.com/contracts/2.x/tokens#ERC20

Alternatively you could look at creating ERC777 tokens (no need to do approve and transferFrom in two separate transactions). See the documentation for details: https://docs.openzeppelin.com/contracts/2.x/tokens#ERC777

If you have questions on using OpenZeppelin you can ask in the Community Forum: https://forum.openzeppelin.com/

Disclosure: I am the Community Manager at OpenZeppelin

  • Thanks for replying! I'd like to ask one more question. How can I do those two actions from my front-end thru web3.js? are there any examples?? – kohshiba Sep 4 at 6:01
  • Your users will have to do two transactions. First approve an allowance on the token contract and then call the deposit function. – abcoathup Sep 4 at 9:39

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.