1

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,

  1. Tom send 10 BUSD to my contract X.
  2. Contract X send TOKEN A to Tom.

In Solidity(based on remix)

  1. Tom approve 10 BUSD ⇨ contractX.approve(Tom's Address, 10)
  2. 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.

2 Answers 2

1

I have finally successed.

I stopeed to use remix. I just used from web3.

haha,,,

IERC20.approve(spender, amount) thinks the owner is also the contract

thanks to this post

0

You are correct. The correct process is first Tom should approve BUSD to contract x.

Tom approve 10 BUSD ⇨ BUSD.approve(ContractX, 10)

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.