Gas Estimation Errors on Transfer Token - but tx succeeds

On the following dApp code, where someone can sell a token for eth (using BSC testnet right now cause its faster). When I go to call the function from the dapp, I approve the approval via metamask, second screen I get approve for the transfer, with this: "Transaction Error. Exception thrown in contract code" along with a crazy 0.285BNB suggested gas fee.

However, the function works fine and goes through.


function sellToken(uint256 _amount) external {
            token.transferFrom(msg.sender, address(this), _amount);

React JS:

const sellToken = (amount) => {
     token.methods.approve(spender._address, amount).send({from:         account}).on('transactionHash', (hash) => { 
  spender.methods.sellToken(amount).send({from: account}).on('transactionHash',             (hash) => {

This is just the approve from the dapp -> transfer from the spender smart contract part, now sure why I keep getting this high gas error. Is it perhaps in the way this is being called from react?

That is happening probably because your reactJS code is not waiting for the approval transaction to be mined. The transactionHash event is triggered when the tx is sent. Your code should only try to sell tokens after receiving the receipt

1 Like