1

I am getting this error: Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? execution reverted: ERC20: insufficient allowance { "originalError": { "code": 3, "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001d45524332303a20696e73756666696369656e7420616c6c6f77616e6365000000", "message": "execution reverted: ERC20: insufficient allowance" } }

This is my code:

address private sender; 

address private thiscontract = address(this);

function approveSmartcontract(IERC20 _token, address _sender, uint tokens)internal returns(bool)  {
    return _token.approve(thiscontract,tokens);
}
function transferfunds(address tokenadd,uint _tokens) public{
    sender = msg.sender;
    IERC20 token = IERC20(tokenadd);
    if (approveSmartcontract(token,sender,_tokens) == true ){
        token.transferFrom(sender,thiscontract,_tokens);
    }
}

}

why am I getting this error can someone pls provide the right code below that would work? please

2 Answers 2

3

The same question. The same answer man. When you are calling approveSmartcontract Then the msg.sender is the contract itself. it means when this function calls the approve function in the IERC20 contract then the msg.sender is this contract's address which is caling the function. Now the approve function works in a way that the parameter you pass gets the allowance to spend tokens on behalf of the msg.snder. This way when you call approveSmartcontract then the msg.sender and the parameter both are same i.e. the contract's adddress. And the transferfunds function is trying to send tokens from your wallet address to the contract and again the msg.sender is the contract's address when the transferFrom function is called in the ERC20 contract. And because your contract address is not approved to spend tokens on behalf of the wallet address, you are getting this error. To perform this asked you have to directly call the ERC20 approve contract either form the etherscan or you can write a small script to talk with the contract.

3
  • Can you give me a sample code or an example of how i can approve the smart contract to transfer funds from users wallet to the smart contract i created Commented Nov 7, 2022 at 13:11
  • 1
    there are two ways. 1. first remove the approving function from this contract. Now you have to first deploy the receiver smart contract, and then take the address of this receiver contract and manually approve it from the token contract if the token contract is available on the ether scan. 2. write a javascript script using ethers js and hardhat to simultaneously call both the approve function and then transfer function, this is only possible if you know how to use ethers and hardhat. Commented Nov 7, 2022 at 15:42
  • 1
    The main problem is approving the contract address. You can do that by either way I said above but not from the same contract like you have tried. Commented Nov 7, 2022 at 15:44
0

You need to approve the allowance of the token before you transfer the token to your smart contract you should call token.approve(sender, _tokens);

This will set allowance of the of the tokens for the msg.sender.

3
  • 1
    This won't work. As doing so will allow the sender to spend tokens owned by the contract address. because approve function takes the msg.sender and the function argument, and make the argument address eligible to spend tokens owned by the msg.sender. But he is trying to send tokens "from the sender address to the contract" which is not allowed. He has to call the approve function directly from the ERC20 token contract Commented Nov 7, 2022 at 5:26
  • he is making IERC20 token = IERC20(tokenadd); which is an interface of the ERC20 token. So when he calls token.approve(sender, _tokens); he is calling the approve function of ERC20 token right? Commented Nov 7, 2022 at 5:40
  • 1
    Yes right. but he is trying to send tokens from the sender to the contract According to your function token.approve(sender, _tokens); The sender will get access to spend tokens which are owned by the contract. Your function is correct if he sends token from the contract to the sender. But he is trying just opposite of this. Commented Nov 7, 2022 at 12:48

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.