I confirmed that approval is accurate, yet I receive the "revert erc20 transfer amount exceeds allowance" error when using the transferFrom()
function. Yet for some reason the internal _transfer()
function works without any issues.
I checked and transferFrom()
is public, not external.
The following code results in an error:
function addLiquidity(uint256 _tokenAmount) external payable onlyOwner {
// Transfer the tokens from the sender to the contract
// Sender should approve the contract spending the tokens
require(allowance(msg.sender, address(this)) >= _tokenAmount);
transferFrom(msg.sender, address(this), _tokenAmount);
}
Where this works:
function addLiquidity(uint256 _tokenAmount) external payable onlyOwner {
// Transfer the tokens from the sender to the contract
// Sender should approve the contract spending the tokens
require(allowance(msg.sender, address(this)) >= _tokenAmount);
_transfer(msg.sender, address(this), _tokenAmount);
}
Note the require
statement before each attempt to transfer that passes and therefore clearly shows the contract is allowed to transfer the tokens.
Why does _transfer
work but transferFrom
does not? Shouldn't transferFrom
work, especially considering the allowance has been correctly set?
allowance
returns a valid amount? Make sure you are checking the allowance from your wallet address to this contract.