From e9d57053873a3c7aabddda1d02aed73bd749be66 Mon Sep 17 00:00:00 2001 From: Jin Date: Tue, 12 Nov 2024 15:43:28 -0600 Subject: [PATCH] Remove permit support and modify node ack logic (#1764) --- src/core/raps/actions/swap.test.ts | 1 - src/core/raps/actions/swap.ts | 7 ++----- src/core/raps/execute.ts | 23 ++++++++++++++++------- src/core/raps/unlockAndCrosschainSwap.ts | 1 - src/core/raps/unlockAndSwap.ts | 1 - src/core/raps/utils.ts | 17 +---------------- 6 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/core/raps/actions/swap.test.ts b/src/core/raps/actions/swap.test.ts index 99be6ddb87..b6b312e205 100644 --- a/src/core/raps/actions/swap.test.ts +++ b/src/core/raps/actions/swap.test.ts @@ -56,7 +56,6 @@ test('[rap/swap] :: should execute swap', async () => { }, quote: quote as Quote, wallet, - permit: false, }); expect(swapTx?.hash).toBeDefined(); diff --git a/src/core/raps/actions/swap.ts b/src/core/raps/actions/swap.ts index fcd31a6c0d..fbb5d5573f 100644 --- a/src/core/raps/actions/swap.ts +++ b/src/core/raps/actions/swap.ts @@ -220,7 +220,6 @@ export const executeSwap = async ({ quote, gasParams, wallet, - permit = false, }: { chainId: ChainId; gasLimit: string; @@ -228,7 +227,6 @@ export const executeSwap = async ({ nonce?: number; quote: Quote; wallet: Signer; - permit: boolean; }): Promise => { if (!wallet || !quote) return null; @@ -260,7 +258,7 @@ export const executeSwap = async ({ quote, transactionParams, wallet, - permit, + false, chainId as unknown as SwapChainId, REFERRER, ); @@ -276,7 +274,7 @@ export const swap = async ({ }: ActionProps<'swap'>): Promise => { const { selectedGas, gasFeeParamsBySpeed } = gasStore.getState(); - const { quote, permit, chainId, requiresApprove } = parameters; + const { quote, chainId, requiresApprove } = parameters; let gasParams = selectedGas.transactionGasParams; // if swap isn't the last action, use fast gas or custom (whatever is faster) @@ -312,7 +310,6 @@ export const swap = async ({ chainId, gasLimit, nonce, - permit: !!permit, quote, wallet, }; diff --git a/src/core/raps/execute.ts b/src/core/raps/execute.ts index d0187805d4..5d6926572a 100644 --- a/src/core/raps/execute.ts +++ b/src/core/raps/execute.ts @@ -3,6 +3,7 @@ /* eslint-disable no-promise-executor-return */ import { Signer } from '@ethersproject/abstract-signer'; +import { ChainId } from '~/core/types/chains'; import { RainbowError, logger } from '~/logger'; import { claim, swap, unlock } from './actions'; @@ -111,9 +112,12 @@ function getRapFullName(actions: RapAction[]) { const delay = (ms: number) => new Promise((res) => setTimeout(res, ms)); +const NODE_ACK_MAX_TRIES = 10; + const waitForNodeAck = async ( hash: string, provider: Signer['provider'], + tries = 0, ): Promise => { return new Promise(async (resolve) => { const tx = await provider?.getTransaction(hash); @@ -125,8 +129,10 @@ const waitForNodeAck = async ( resolve(); } else { // Wait for 1 second and try again - await delay(1000); - return waitForNodeAck(hash, provider); + if (tries < NODE_ACK_MAX_TRIES) { + await delay(1000); + return waitForNodeAck(hash, provider, tries + 1); + } } }); }; @@ -163,13 +169,16 @@ export const walletExecuteRap = async ( const { baseNonce, errorMessage: error, - hash, + hash: firstHash, } = await executeAction(actionParams); + const shouldWaitForNodeAck = parameters.chainId !== ChainId.mainnet; + if (typeof baseNonce === 'number') { - actions.length > 1 && - hash && - (await waitForNodeAck(hash, wallet.provider)); + let latestHash = firstHash; for (let index = 1; index < actions.length; index++) { + latestHash && + shouldWaitForNodeAck && + (await waitForNodeAck(latestHash, wallet.provider)); const action = actions[index]; const actionParams = { action, @@ -181,7 +190,7 @@ export const walletExecuteRap = async ( flashbots: parameters?.flashbots, }; const { hash } = await executeAction(actionParams); - hash && (await waitForNodeAck(hash, wallet.provider)); + latestHash = hash; if (index === actions.length - 1) { txHash = hash; } diff --git a/src/core/raps/unlockAndCrosschainSwap.ts b/src/core/raps/unlockAndCrosschainSwap.ts index 49dfb28a24..08c7e22458 100644 --- a/src/core/raps/unlockAndCrosschainSwap.ts +++ b/src/core/raps/unlockAndCrosschainSwap.ts @@ -112,7 +112,6 @@ export const createUnlockAndCrosschainSwapRap = async ( // create a swap rap const swap = createNewAction('crosschainSwap', { chainId, - permit: swapAssetNeedsUnlocking, requiresApprove: swapAssetNeedsUnlocking, quote, meta: swapParameters.meta, diff --git a/src/core/raps/unlockAndSwap.ts b/src/core/raps/unlockAndSwap.ts index 6660a1e21f..ff3e7fff08 100644 --- a/src/core/raps/unlockAndSwap.ts +++ b/src/core/raps/unlockAndSwap.ts @@ -125,7 +125,6 @@ export const createUnlockAndSwapRap = async ( const swap = createNewAction('swap', { chainId, sellAmount, - permit: swapAssetNeedsUnlocking, requiresApprove: swapAssetNeedsUnlocking, quote, meta: swapParameters.meta, diff --git a/src/core/raps/utils.ts b/src/core/raps/utils.ts index a31799ffd1..f877b594b2 100644 --- a/src/core/raps/utils.ts +++ b/src/core/raps/utils.ts @@ -3,7 +3,6 @@ import { MaxUint256 } from '@ethersproject/constants'; import { Contract, PopulatedTransaction } from '@ethersproject/contracts'; import { StaticJsonRpcProvider } from '@ethersproject/providers'; import { - ALLOWS_PERMIT, CrosschainQuote, Quote, getQuoteExecutionDetails, @@ -207,22 +206,8 @@ export const getDefaultGasLimitForTrade = ( quote: Quote, chainId: ChainId, ): string => { - const allowsPermit = - chainId === mainnet.id && - ALLOWS_PERMIT[quote?.sellTokenAddress?.toLowerCase()]; - - let defaultGasLimit = quote?.defaultGasLimit; - - if (allowsPermit) { - defaultGasLimit = Math.max( - Number(defaultGasLimit), - Number( - multiply(getChainGasUnits(chainId).basic.swapPermit, EXTRA_GAS_PADDING), - ), - ).toString(); - } return ( - defaultGasLimit || + quote?.defaultGasLimit || multiply(getChainGasUnits(chainId).basic.swap, EXTRA_GAS_PADDING) ); };