transferFrom() always reverts with "revert ERC20: transfer amount exceeds allowance"

:computer: Environment
Truffle, fresh project with an ERC20 constructor

:memo:Details
Hey,
I’m trying to use the approve(), then transferFrom() methods from OpenZeppelin library, to transfer ERC20 from an account to another through my contract. approve() seems fine, allowance() returns the expected value, but after that transferFrom() with the same addresses reverts with transfer amount exceeds allowance.

I approved the contract address to use tokens of account A, and transfer them to account B. It does not happen.
What am I doing wrong?

truffle(development)> token.address
'0x28ddbD790dFD1931cB4C00016d95FE011e486589'
truffle(development)> token.balanceOf('0x0bce99B3309ec259394Ba0CF59C1E57d79e4ADFC')
BN {
  negative: 0,
  words: [ 1000, <1 empty item> ],
  length: 1,
  red: null
}
truffle(development)> token.approve('0x28ddbD790dFD1931cB4C00016d95FE011e486589', 500)
{
  tx: '0x18080fc9a1e14c5d2f5b058bf3c29aef87fde94c31850b39a8218345400a3bbe',
  receipt: {
    transactionHash: '0x18080fc9a1e14c5d2f5b058bf3c29aef87fde94c31850b39a8218345400a3bbe',
    transactionIndex: 0,
    blockHash: '0xde963841341695ff3ebc961647ce12be0e0a06ca567a096c754ba022369bf57a',
    blockNumber: 40,
    from: '0x0bce99b3309ec259394ba0cf59c1e57d79e4adfc',`
    `to: '0x28ddbd790dfd1931cb4c00016d95fe011e486589',
    gasUsed: 44137,
    cumulativeGasUsed: 44137,
    contractAddress: null,
    logs: [ [Object] ],
    status: true,
    logsBloom: '0x00000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000004000000000000020000000000000000000000000000000408080000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000001000000000000000000000000000002000',
    rawLogs: [ [Object] ]
  },
  logs: [
    {
      logIndex: 0,
      transactionIndex: 0,
      transactionHash: '0x18080fc9a1e14c5d2f5b058bf3c29aef87fde94c31850b39a8218345400a3bbe',
      blockHash: '0xde963841341695ff3ebc961647ce12be0e0a06ca567a096c754ba022369bf57a',
      blockNumber: 40,
      address: '0x28ddbD790dFD1931cB4C00016d95FE011e486589',
      type: 'mined',
      id: 'log_3de8224c',
      event: 'Approval',
      args: [Result]
    }
  ]
}
truffle(development)> token.allowance('0x0bce99B3309ec259394Ba0CF59C1E57d79e4ADFC', '0x28ddbD790dFD1931cB4C00016d95FE011e486589')
BN {
  negative: 0,
  words: [ 500, <1 empty item> ],
  length: 1,
  red: null
}
truffle(development)> token.transferFrom('0x0bce99B3309ec259394Ba0CF59C1E57d79e4ADFC', '0x28ddbD790dFD1931cB4C00016d95FE011e486589', 500)
Uncaught:
Error: Returned error: VM Exception while processing transaction: revert ERC20: transfer amount exceeds allowance -- Reason given: ERC20: transfer amount exceeds allowance.

Thank you!

1 Like

Hello @kaszasneni!

I think the issue here is that transferFrom expects to be called by the beneficiary of the allowance, in this case the token contract itself (0x28ddbD790dFD1931cB4C00016d95FE011e486589), but you’re calling it from the console’s account (0x0bce99B3309ec259394Ba0CF59C1E57d79e4ADFC), that is the holder of the funds but has no allowance.

Refer to the official documentation for more details.

2 Likes

Hey @martriay!

You saved my life, I guessed something similar, but I didn’t find out how to call transferFrom() with the contract itself. You led me, the missing thing was a this before the method call in the Solidity code. Rookie mistake.

Thank you for your rapid response!

2 Likes