shapeshift / web

ShapeShift Web
https://app.shapeshift.com
MIT License
171 stars 183 forks source link

Fees estimation for quotes failing EVM simulation #7434

Closed gomesalexandre closed 3 months ago

gomesalexandre commented 3 months ago

Overview

Currently upstream, some swappers do have some magic to do fees estimation for Txs with:

They usually expose this as an optional validate option, see e.g ZRX enforcing validation at trade execution with the priceOrQuote option

https://github.com/shapeshift/web/blob/c3bac3d447f8aff54f9257002aca62aaab9a2153/packages/swapper/src/swappers/ZrxSwapper/endpoints.ts#L45-L47

but ditching validation and letting ZRX do its magic at quote time:

https://github.com/shapeshift/web/blob/c3bac3d447f8aff54f9257002aca62aaab9a2153/packages/swapper/src/swappers/ZrxSwapper/getZrxTradeQuote/getZrxTradeQuote.ts#L74-L75

Arbitrum Bridge is an example of a swapper which does not support such magic, and the WIP Portals swapper will be another one.

We should implement the samg magic as upstream to allow fees estimates in this case.

References and additional details

This could be implemented with something like this (pseudo-ish code):

const getDummyQuoteParams = (assetId: AssetId) => {
  const chainId = fromAccountId(assetId).chainId
  const dummyAccountId = `toAccountId(vitalikAddyToAccountIdParams)
  const isNativeAsset = !isToken(fromAssetId(assetId).assetReference)

  const dummyAssetId = isNativeAsset ? assetId : wethAssetId

  return {accountId: dummyAccountId, assetId: dummyAssetId}
}

i.e the naive implementation is:

Once you get those dummy params, make a second, dummy request upstream in case the first one fails simulation with those params, and voila 🎉 you get a new quote/Tx data can succesfully be simulated!

Obviously, we may want to have an address per chain if vitalik.eth. doesn't work for all of our EVM chains, as well as possibly changing the dummy token per chain, but the high-level idea is here

Acceptance Criteria

Need By Date

No response

Screenshots/Mockups

No response

Estimated effort

No response

gomesalexandre commented 3 months ago

Note - this directly related to Portals, as without implement this, we won't be able to get network fees at quote time if the Tx simulation fails upstream (i.e for the cases mentioned above)

suhailgme commented 3 months ago

@gomesalexandre just came across this. We are implementing improved simulations which will return accurate estimates/gas even if the sender doesn't have the tokens or approvals in place. Should be about 2-3 weeks and will use the same /estimate endpoint with no changes needed.

gomesalexandre commented 3 months ago

That sound sperfect @suhailgme! Let us know when that's implemented, we may even revert this guy to leverage only upstream estimates again.

gomesalexandre commented 3 months ago

Also: closed by https://github.com/shapeshift/web/pull/7439