1

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?

1
  • Are you certain the allowance returns a valid amount? Make sure you are checking the allowance from your wallet address to this contract. Apr 3, 2023 at 6:31

1 Answer 1

0

I really don't think that the allowance are trully set. Because _transer doesn't require the allowanceanywhere. Can you share the _transferFrom?

1
  • The entire core ERC20 code is imported from OpenZeppelin with import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; (using HardHat). I did not change _transfer, transferFrom, or transferFrom -- so the code is all available to be reviewed in their repositories. The only push-back I would have about assuming that allowance hasn't been set is that require(allowance(msg.sender, address(this)) >= _tokenAmount); statement right before the transfer -- in both versions. This should mean allowance really has been set, or the operation would revert. Apr 6, 2023 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.