0

I'm trying to send some ETH to a presale smart contract on Kovan. Allowance has been set with presale address as a spender - with the exact same amount as the tokens allocated in the presale contract. But then I got:

Warning! Error encountered during contract execution [Bad instruction] at tx hash: https://kovan.etherscan.io/tx/0x8d0451f2a9ba2440bf5f2a27d171850437772ccb12afeb8846507c0497e65cc0

From what I've read and learned, there's something wrong during the token transfer. But I can't find which one it is.

Would like to know if there's a way to fix this issue or at least knowing which one caused this, in case I'm facing the same issue in the future. Thanks.

3
  • You can use the service tenderly.co to debug your transactions on live nets. – Mikko Ohtamaa Dec 30 '20 at 15:31
  • @MikkoOhtamaa how about testnet? – NOZR Dec 30 '20 at 15:39
  • Supported Networks: Mainnet, Kovan, Rinkeby, Ropsten, Görli, xDai, POA Network, Matic Mainnet, Matic Testnet v3, Matic Mumbai, Binance Smart Chain – Mikko Ohtamaa Dec 30 '20 at 16:00
0

The error message Bad instruction and the gas consumption equal to gas limit 1,000,000 indicates the problem was caused by an assert or low level error like array out of bound access.

Looking at the receive function none of the transfers should cause Bad Instruction. Failed transfers revert but do not consume all available gas.

After discarding mapping access, and the requires conditions the only suspicious code is getTokensPerEth function

function getTokensPerEth(uint256 amount) internal view returns(uint256) {
    return amount.mul(tokenRatePerEth).div(
        10**(uint256(9).sub(tokenDecimals).add(rateDecimals))
        );
}

The failure is at uint256(9).sub(tokenDecimals). Since tokenDecimals is 18 SafeMath's sub will fail at assert(b <= a) (b = 18, a = 9).

1
  • Such a great answer! Thanks a lot Ismael! – NOZR Dec 30 '20 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.