I have tried 2 days working on transerFrom. But not working so please help me.
I know we need to approve() first. Approve() and Allowance() seem to be fine. I read this FAQ( https://forum.openzeppelin.com/t/transferfrom-always-reverts-with-revert-erc20-transfer-amount-exceeds-allowance/3993 ) but ... not working. I read this one too. ERC20 - Approve & TransferFrom not working
Here is what I did.
What I want to do is for example,
- Tom send 10 BUSD to my contract X.
- Contract X send TOKEN A to Tom.
In Solidity(based on remix)
- Tom approve 10 BUSD ⇨ contractX.approve(Tom's Address, 10)
- Tom send function buyToken ⇨ contractX.buyTokens(10)
Here is my code
code
function approveBUSD(address _spender, uint256 _value) public returns (bool) {
return tokenB.approve(_spender, _value);
}
function buyTokens(uint256 amount) public payable {
uint256 allowance = tokenB.allowance(address(this), msg.sender);
require(allowance >= amount, "Check the token allowance");
tokenB.transferFrom(msg.sender, address(this),amount);
emit BoughtTokens(msg.sender, amount); // log event onto the blockchain
I created two Solidity Contract. One is for TOKEN A issue. The other is this exchange program.
Token A transfer seems to be okay. But my contract can't accept BUSD. Even the BUSD approve seems fine..
Error message is
Internal JSON-RPC error. { "code": 3, "message": "execution reverted: insufficient allowance", "data":
So buyTokens(0) completely works!....
Please help me. I will give you some tips...
Thank you in advance.