thirdweb-dev / engine

The most powerful backend engine for web3 apps.
https://thirdweb.com/engine
Apache License 2.0
152 stars 59 forks source link

Simulate Tx Bug #660

Closed dcapitator closed 1 month ago

dcapitator commented 1 month ago

I am trying to simulate a transaction from sendTransaction api .

The Tx data :

{
  "toAddress": "0x3bFA4769FB09eefC5a80d6E87c3B9C650f7Ae48E",
  "data": "0x04e45aaf000000000000000000000000fff9976782d46cc05630d1f6ebab18b2324d6b140000000000000000000000001c7d4b196cb0c7b01d743fbc6116a902379c72380000000000000000000000000000000000000000000000000000000000000bb800000000000000000000000080fbf2648f1ea0c0f7c2cdb6145a4ac87de8157500000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000",
  "value": "0x00",
  "txOverrides": {
    "gas": "50000"
  }
}

The transaction is a swap transaction which requires more gas then 50000. The simulateTx returned true but the Tx failed: https://sepolia.etherscan.io/tx/0x239ba1b499be9ce1feb5e945e4e3a9b53a6db017f140140fc72ace87abb8159c

I also tried to change the approval data selector and it still returns true as long as the bytes count is correct! 0x095ea7b30000000000000000000000003bfa4769fb09eefc5a80d6e87c3b9c650f7ae48e00000000000000000000000000000000000004ee2d6d415b85acef8100000000

If we try to simulate Tx using swapRouter.exactInputSingle.staticCall(params, { gasLimit: 50000, }) then it reverts correctly with low gas.

Also the simulate-transaction api is not working properly and altering data wont affect its return ( true most of times )

I guess its a high priority to fix it as it makes all Tx simulation incorrect!

I raised gas and the same Tx data succeeded now: https://sepolia.etherscan.io/tx/0x85c1ea447a46572b5266e0644e4ea00a3294d68065d92a4b6e1d9e03df82da82

@farhanW3 @furqanrydhan

dcapitator commented 1 month ago

I am not able to filter transactions by status in get-all-transactions api . default cannot be changed

dcapitator commented 1 month ago

Aren't we supposed to get errorMessage when onchainStatus is reverted?

dcapitator commented 1 month ago

@warengonzaga

arcoraven commented 1 month ago

Hi @dcapitator,

In your first transaction if you open "More Details" you can see the transaction reached the 50,000 gas limit in your txOverrides:

image

Is there a reason you want to override the gas? This is an advanced feature to limit the amount of gas your transaction call can make. If you omit it, Engine will estimate it properly for you.

The reason the second transaction worked is because you raised the gas limit, and the transaction ended up needing 138,843 gas to complete successfully.

Simulating a transaction is intended for testing how a transaction's inputs would be handled by a smart contract. For example, it will fail if you try to mint an NFT from a contract that is out of supply. It unfortunately does not handle gas-related issues like the gas limit being too low or the backend wallet being out of funds.

Our advice is to not set gas unless you are certain how much gas the contract call will consume, and to set the gas to higher than that to prevent the transaction from being reverted if it runs out of gas to complete.

I am not able to filter transactions by status in get-all-transactions api . default cannot be changed

If you are on a v2.0.x version of Engine, you should be able to filter the results with a query param: /transaction/get-all?status=mined

Aren't we supposed to get errorMessage when onchainStatus is reverted? If a transaction is reverted, Engine still considers it as "mined" because the transaction was confirmed onchain. Unfortunately when a transaction is reverted, there is not an clear errorMessage. We'll consider setting the errorMessage to a generic error when onchainStatus reverted.

dcapitator commented 1 month ago

Hi @dcapitator,

In your first transaction if you open "More Details" you can see the transaction reached the 50,000 gas limit in your txOverrides: image

Is there a reason you want to override the gas? This is an advanced feature to limit the amount of gas your transaction call can make. If you omit it, Engine will estimate it properly for you.

The reason the second transaction worked is because you raised the gas limit, and the transaction ended up needing 138,843 gas to complete successfully.

Simulating a transaction is intended for testing how a transaction's inputs would be handled by a smart contract. For example, it will fail if you try to mint an NFT from a contract that is out of supply. It unfortunately does not handle gas-related issues like the gas limit being too low or the backend wallet being out of funds.

Our advice is to not set gas unless you are certain how much gas the contract call will consume, and to set the gas to higher than that to prevent the transaction from being reverted if it runs out of gas to complete.

I am not able to filter transactions by status in get-all-transactions api . default cannot be changed

If you are on a v2.0.x version of Engine, you should be able to filter the results with a query param: /transaction/get-all?status=mined

Aren't we supposed to get errorMessage when onchainStatus is reverted? If a transaction is reverted, Engine still considers it as "mined" because the transaction was confirmed onchain. Unfortunately when a transaction is reverted, there is not an clear errorMessage. We'll consider setting the errorMessage to a generic error when onchainStatus reverted.

Regarding the results filter , I was pointing to the dashboard docs "status" dropdown only has "queued".

I changed the gas to check if simulate Tx would detect low gas and revert. About the gas, ok now its clear that engine handles that properly !

Now i see the simulate-transaction api or simulate param is working properly for swap data but not sure why return always true for WETH9 approval data !

Swap:

{
  "toAddress": "0x3bFA4769FB09eefC5a80d6E87c3B9C650f7Ae48E",
  "value": "0",
  "data": "0x04e45aaf000000000000000000000000fff9976782d46cc05630d1f6ebab18b2324d6b140000000000000000000000001c7d4b196cb0c7b01d743fbc6116a902379c72380000000000000000000000000000000000000000000000000000000000000bb800000000000000000000000080fbf2648f1ea0c0f7c2cdb6145a4ac87de8157500000000000000000000000000000000000000000000000000005af3107a400000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000"
}

Approval:

{
  "toAddress": "0xfff9976782d46cc05630d1f6ebab18b2324d6b14",
  "value": "0x00",
  "data": "0x095ea7b30000000000000000000000003bfa4769fb09eefc5a80d6e87c3b9c650f7ae48e00000000000000000000000000000000000004ee2d6d415b85acef8100000000"
}

If i change the function selector then the simulate returns true Even though WETH9 doesnt have a fallback function.

Thank you