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