sifiorg / sifi

7 stars 1 forks source link

feat(frontend): display gas fee estimate #504

Closed boriskubrik closed 10 months ago

boriskubrik commented 10 months ago

Is there a better way to get the estimate?

image

immutablegoblin commented 10 months ago

It's not very accurate. Typically, it's more accurate when it's an on-chain swap, but not very accurate when it comes to jumps.

Would be interested to know the answer to this as well.

I guess we need to account for the varying gas price across chains in this case?

xykota commented 10 months ago

It's not very accurate. Typically, it's more accurate when it's an on-chain swap, but not very accurate when it comes to jumps.

There are two ways we estimate gas:

a) Calculation. We make a guess by adding up gas guesses for each operation. These guesses come from running tests b) Simulation. We use the eth_estimateGas call, which is accurate. A better name for this would be get gas use from tx simulation

We use a) when producing the quote. The reason for this is that a quote is not tied to a fromAddress, so it's not possible to simulate the transaction using b). Even if the user were to supply a fromAddress when requesting the quote, gas estimation may fail because it simulates the transaction and the user may not have performed the token approval yet.

In other words, we're stuck with a) for producing quotes. It is possible to get really good at a) by using a method like they do in Paraswap's open-source dex-lib. I haven't researched it deeply enough to know how they deal with issues like different tokens (ERC-20) costing very different amounts of gas to move.

When there is a cross-chain transaction with post-jump actions (swap-bridge-swap or bridge-swap) the gas cost for the destination chain must be calculated as well. When this is estimate is too low, the swap on the destination chain fails and the tokens are left in the Sifi contract. A Sifi admin needs to pull these tokens out and deliver them to the user. For this reason, the estimate is set very high.

By improving the math we use for a) we can produce better estimates for same-chain and reduce the enormous buffer we put on top of the cross-chain swaps.