'ERC20: transfer amount exceeds allowance, even when it is approved
Returned error: VM Exception while processing transaction: revert ERC20: transfer amount exceeds allowance
Code:
ERC20Tokens[tokenTicker].token.approve(ERC20Tokens[tokenTicker].tokenHolder, 10);
emit tokenOwnerBalance(ERC20Tokens[tokenTicker].token.balanceOf(ERC20Tokens[tokenTicker].tokenHolder));
ERC20Tokens[tokenTicker].token.transferFrom(ERC20Tokens[tokenTicker].tokenHolder, address(this), 1);
Logs:
[
{
"from":"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"topic":"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
"event":"Approval",
"args":{
"0":"0x7A2946b37399fa3F1C9EF81c7Bcf94AE1099D18F",
"1":"0x95Ba4cF87D6723ad9C0Db21737D862bE80e93911",
"2":"10",
"owner":"0x7A2946b37399fa3F1C9EF81c7Bcf94AE1099D18F",
"spender":"0x95Ba4cF87D6723ad9C0Db21737D862bE80e93911",
"value":"10"
}
},
{
"from":"0x7A2946b37399fa3F1C9EF81c7Bcf94AE1099D18F",
"topic":"0xcff530ae4ada35c1dd7ac314ef643b9eb7ae40665958ad5899e2fbc18865444a",
"event":"tokenOwnerBalance",
"args":{
"0":"31000000"
}
}
]
Solution 1:[1]
ERC20Tokens[tokenTicker].token.approve(ERC20Tokens[tokenTicker].tokenHolder, 10);
This line approves the tokenHolder to spend your contract's tokens. Not the other way around.
So the transferFrom() fails because your contract is trying to spend the tokenHolder's tokens.
If you want your contract to be able to spend tokenHolder's tokens, the tokenHolder needs to execute the approve() function on the token from their address directly. Not through a contract in between.
Solution 2:[2]
For me, it was a mistake of using transferFrom instead of transfer when transferring tokens from the contract.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Petr Hejda |
| Solution 2 | Tamás Sengel |
