0

I want to execute function Tip and transfer IERC20 Token. However, this message is shown.


Uni::transferFrom: transfer amount exceeds spender allowance { "originalError": { "code": 3, "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003c556e693a3a7472616e7366657246726f6d3a207472616e7366657220616d6f756e742065786365656473207370656e64657220616c6c6f77616e636500000000", "message": "execution reverted: Uni::transferFrom: transfer amount exceeds spender allowance" } }


I think I couldn't approve correctly so this error message has appeared, but I don't know how to fix... Is there are any good ideas? My code is here.

pragma solidity >=0.7.0 <0.9.0;
    
    import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
    import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
    
    contract Tip is Ownable{
        
        address public token = 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984;
    
        function approval (uint amount) public {
            IERC20(token).approve(address(this),amount);
        }
        
    
        function tip (address to, uint amount) payable public {
            address from = msg.sender;
            IERC20(token).transferFrom(from,to,amount);
        }
    }

1 Answer 1

0

Since the tip function is calling transferFrom of token, in order for it to be able to do that the user calling tip has to approve this Tip contract to spend the tokens.

Before calling tip(), you need to call token.approve(_TipContractAddress, amountToSpend);. So the Tip contract can have the token balance owned by the account calling approve spent by it.

Notice this isn't some line of code you can add to the Tip contract. The UI/script you are building has to have the user first do approve on the token, then you can have them call tip() on your contract.

0

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.