0

This is a part of my payable function that is causing a problem. What happens is we receive Eth and swap it to a specific token. It works great in testnet. But on mainnet the "require" check fails. As balanceAfter is not greater than balanceBefore. In swap, balanceAfter is supposed to increase as it receives the swapped tokens.

uint256 balanceBefore = IERC20(token).balanceOf(wallet);

uint256[] memory swappedAmounts = ISwapRouter(SWAP_ROUTER).swapETHForExactTokens{ value: msg.value }(
  price,
  path,
  wallet,
  block.timestamp
);

uint256 balanceAfter = IERC20(token).balanceOf(wallet);

require(balanceAfter > balanceBefore, 'Swap did not succeed');

But if I remove that "require" check, everything works great both on testnet and mainnet, except that I am not sure if swap went successful, but swap does happen.

So, why it is that it is working on testnet but not on mainnet?

Any way to check the success of swap? Does swap happen asynchronously?

1
  • Do you have a transaction hash? Does the contract have a receive function?
    – Ismael
    Sep 16 at 5:22

Your Answer

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

Browse other questions tagged or ask your own question.