I've tried almost every tutorial and help available online trying to accept BUSD or any ERC20 token in my solidity smart contract. I'm deploying the contract on Binance Testnet and I have 20 BUSD in testnet. When I call GetUserTokenBalance() it shows me my BUSD balance. but when I call Approvetokens(), even after a successful transaction the GetAllowance() shows 0. Hence, when I call the AcceptPayment() I get the below error. This forum is my last HOPE.
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? Internal JSON-RPC error. { "code": 3, "message": "execution reverted: insufficient funds", "data": "0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000012696e73756666696369656e742066756e64730000000000000000000000000000" }
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract ECommerce {
IERC20 token;
address private owner;
constructor() public {
token = IERC20(0xeD24FC36d5Ee211Ea25A80239Fb8C4Cfd80f12Ee);
// this token address is BUSD deployed on Binance testnet
// You can use any other ERC20 token smart contarct address here
owner = msg.sender;
}
modifier OnlyOwner() {
require(msg.sender == owner);
_;
}
function GetUserTokenBalance() public view returns(uint256){
return token.balanceOf(msg.sender);// balancdOf function is already declared in ERC20 token function
}
function Approvetokens(uint256 _tokenamount) public returns(bool){
token.approve(address(this), _tokenamount);
return true;
}
function GetAllowance() public view returns(uint256){
return token.allowance(msg.sender, address(this));
}
function AcceptPayment(uint256 _tokenamount) public returns(bool) {
require(_tokenamount > GetAllowance(), "Please approve tokens before transferring");
token.transfer(address(this), _tokenamount);
return true;
}
function GetContractTokenBalance() public OnlyOwner view returns(uint256){
return token.balanceOf(address(this));
}
}