I am trying to estimateGas for an Ethereum transaction with ethers signer.estimateGas(). This transaction should execute several trades on arbitrary DEXs.

To execute the transaction approvals of ERC-20 tokens are required. However I would like to estimate the gas without executing the approvals first. Is it somehow possible to "simulate" the execution of approvals first and then be able to estimate the gas units for the transaction that requires the approvals?

  • approvals are part of business logic, if there is no approval (say you approved for amount 8888, but the transfer is made for 9999, this won't pass as successful) you can't finish the transaction.
    – Nulik
    Jan 31 at 2:21
  • @Nulik I am aware that the transaction would not pass without the approvals, but this wasn't my questions. I was asking about gas estimation. Jan 31 at 7:56
  • the problem is that to estimate gas you need to complete the call() successfully and if the amount of approvals is incorrect this step won't be completed and you won't be able to pass the next step which is get gas estimate. Gas estimate is given at the end of the transaction, but without approval it can't reach the end. Hope now it is clear like crystal
    – Nulik
    Feb 1 at 1:13
  • @Nulik therefore I asked if it's somehow possible to "simulate" the execution of approvals before doing the gas estimation. One option would be to fork mainnet with hardhat, then execute the approval transactions there and then do the gas estimation there but thats pretty much effort for a gas estimation. Feb 1 at 6:07
  • yeah that would be the only "easy" way since hardhat is friendly with development of this kind of stuff, the other way is that you modify Geth to replace the storage variable where the allowance value is stored. The function is called GetState() github.com/ethereum/go-ethereum/blob/… and you would need to do bytes.Compare() with s.address (to make sure the contract is right) and the key must be the address of the allowance value of that user you are trying to fake the transaction of
    – Nulik
    Feb 1 at 14:04


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.