From a9136684e2dc9e3859eb8d39a69d0b017937b835 Mon Sep 17 00:00:00 2001 From: Matthew Wall Date: Tue, 21 May 2024 15:25:57 -0400 Subject: [PATCH] preprocess gasParams for raps --- src/entities/gas.ts | 2 -- src/raps/actions/crosschainSwap.ts | 11 ++++++----- src/raps/actions/swap.ts | 10 +++++----- src/raps/actions/unlock.ts | 10 +++++----- src/raps/execute.ts | 12 ++++++------ src/raps/references.ts | 6 +++--- src/raps/unlockAndCrosschainSwap.ts | 2 +- src/raps/unlockAndSwap.ts | 2 +- src/screens/ExchangeModal.tsx | 3 ++- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/entities/gas.ts b/src/entities/gas.ts index 2f3702b4a42..7b436d4af01 100644 --- a/src/entities/gas.ts +++ b/src/entities/gas.ts @@ -1,5 +1,3 @@ -import { GasFeeLegacyParams } from '@/__swaps__/types/gas'; - type Numberish = number | string; export interface Fee { diff --git a/src/raps/actions/crosschainSwap.ts b/src/raps/actions/crosschainSwap.ts index 440fd2ddf5c..bd559cc6a01 100644 --- a/src/raps/actions/crosschainSwap.ts +++ b/src/raps/actions/crosschainSwap.ts @@ -23,7 +23,6 @@ import { import { ethereumUtils } from '@/utils'; import { TokenColors } from '@/graphql/__generated__/metadata'; import { ParsedAsset } from '@/resources/assets/types'; -import { parseGasParamAmounts } from '@/parsers'; const getCrosschainSwapDefaultGasLimit = (quote: CrosschainQuote) => quote?.routes?.[0]?.userTxs?.[0]?.gasFees?.gasLimit; @@ -104,13 +103,15 @@ export const crosschainSwap = async ({ index, parameters, baseNonce, - selectedGasFee, + gasParams, gasFeeParamsBySpeed, }: ActionProps<'crosschainSwap'>): Promise => { const { quote, chainId, requiresApprove } = parameters; - let gasParams = parseGasParamAmounts(selectedGasFee); + + let gasParamsToUse = gasParams; + // let gasParams = parseGasParamAmounts(selectedGasFee); if (currentRap.actions.length - 1 > index) { - gasParams = overrideWithFastSpeedIfNeeded({ + gasParamsToUse = overrideWithFastSpeedIfNeeded({ gasParams, chainId, gasFeeParamsBySpeed, @@ -139,7 +140,7 @@ export const crosschainSwap = async ({ nonce, quote, wallet, - gasParams, + gasParams: gasParamsToUse, }; let swap; diff --git a/src/raps/actions/swap.ts b/src/raps/actions/swap.ts index 8724ca53605..b87045ed804 100644 --- a/src/raps/actions/swap.ts +++ b/src/raps/actions/swap.ts @@ -45,7 +45,6 @@ import { populateApprove } from './unlock'; import { TokenColors } from '@/graphql/__generated__/metadata'; import { swapMetadataStorage } from '../common'; import { ParsedAsset } from '@/resources/assets/types'; -import { parseGasParamAmounts } from '@/parsers'; const WRAP_GAS_PADDING = 1.002; @@ -239,16 +238,17 @@ export const swap = async ({ index, parameters, baseNonce, - selectedGasFee, + gasParams, gasFeeParamsBySpeed, }: ActionProps<'swap'>): Promise => { - let gasParams = parseGasParamAmounts(selectedGasFee); + let gasParamsToUse = gasParams; + // let gasParams = parseGasParamAmounts(selectedGasFee); const { quote, permit, chainId, requiresApprove } = parameters; // if swap isn't the last action, use fast gas or custom (whatever is faster) if (currentRap.actions.length - 1 > index) { - gasParams = overrideWithFastSpeedIfNeeded({ + gasParamsToUse = overrideWithFastSpeedIfNeeded({ gasParams, chainId, gasFeeParamsBySpeed, @@ -274,7 +274,7 @@ export const swap = async ({ try { const nonce = baseNonce ? baseNonce + index : undefined; const swapParams = { - gasParams, + gasParams: gasParamsToUse, chainId, gasLimit, nonce, diff --git a/src/raps/actions/unlock.ts b/src/raps/actions/unlock.ts index 140d5ca166d..381b26b9ff0 100644 --- a/src/raps/actions/unlock.ts +++ b/src/raps/actions/unlock.ts @@ -22,7 +22,6 @@ import { ethereumUtils } from '@/utils'; import { toHex } from '@/__swaps__/utils/hex'; import { TokenColors } from '@/graphql/__generated__/metadata'; import { ParsedAsset } from '@/resources/assets/types'; -import { parseGasParamAmounts } from '@/parsers'; export const getAssetRawAllowance = async ({ owner, @@ -215,11 +214,13 @@ export const unlock = async ({ index, parameters, wallet, - selectedGasFee, + gasParams, gasFeeParamsBySpeed, }: ActionProps<'unlock'>): Promise => { const { assetToUnlock, contractAddress, chainId } = parameters; + let gasParamsToUse = gasParams; + const { address: assetAddress } = assetToUnlock; if (assetAddress === ETH_ADDRESS) throw new RainbowError('unlock: Native ETH cannot be unlocked'); @@ -239,8 +240,7 @@ export const unlock = async ({ throw e; } - let gasParams = parseGasParamAmounts(selectedGasFee); - gasParams = overrideWithFastSpeedIfNeeded({ + gasParamsToUse = overrideWithFastSpeedIfNeeded({ gasParams, chainId, gasFeeParamsBySpeed, @@ -254,7 +254,7 @@ export const unlock = async ({ tokenAddress: assetAddress, spender: contractAddress, gasLimit, - gasParams, + gasParams: gasParamsToUse, wallet, nonce, chainId, diff --git a/src/raps/execute.ts b/src/raps/execute.ts index 5b86ba03150..24cd7b0aaf5 100644 --- a/src/raps/execute.ts +++ b/src/raps/execute.ts @@ -19,7 +19,7 @@ import { } from './references'; import { createUnlockAndCrosschainSwapRap } from './unlockAndCrosschainSwap'; import { createUnlockAndSwapRap } from './unlockAndSwap'; -import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacySelectedGasFee, SelectedGasFee } from '@/entities'; +import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacyTransactionGasParamAmounts, TransactionGasParamAmounts } from '@/entities'; export function createSwapRapByType(type: T, swapParameters: RapSwapActionParameters) { switch (type) { @@ -54,7 +54,7 @@ export async function executeAction({ baseNonce, rapName, flashbots, - selectedGasFee, + gasParams, gasFeeParamsBySpeed, }: { action: RapAction; @@ -64,7 +64,7 @@ export async function executeAction({ baseNonce?: number; rapName: string; flashbots?: boolean; - selectedGasFee: SelectedGasFee | LegacySelectedGasFee; + gasParams: TransactionGasParamAmounts | LegacyTransactionGasParamAmounts; gasFeeParamsBySpeed: GasFeeParamsBySpeed | LegacyGasFeeParamsBySpeed; }): Promise { const { type, parameters } = action; @@ -75,7 +75,7 @@ export async function executeAction({ index, parameters: { ...parameters, flashbots }, baseNonce, - selectedGasFee, + gasParams, gasFeeParamsBySpeed, }; const { nonce, hash } = (await typeAction(type, actionProps)()) as RapActionResult; @@ -133,7 +133,7 @@ export const walletExecuteRap = async ( baseNonce: nonce, rapName, flashbots: parameters?.flashbots, - selectedGasFee: parameters?.selectedGasFee, + gasParams: parameters?.gasParams, gasFeeParamsBySpeed: parameters?.gasFeeParamsBySpeed, }; @@ -151,7 +151,7 @@ export const walletExecuteRap = async ( baseNonce, rapName, flashbots: parameters?.flashbots, - selectedGasFee: parameters?.selectedGasFee, + gasParams: parameters?.gasParams, gasFeeParamsBySpeed: parameters?.gasFeeParamsBySpeed, }; const { hash } = await executeAction(actionParams); diff --git a/src/raps/references.ts b/src/raps/references.ts index af51692fc42..b8165ee93b9 100644 --- a/src/raps/references.ts +++ b/src/raps/references.ts @@ -3,7 +3,7 @@ import { CrosschainQuote, Quote } from '@rainbow-me/swaps'; import { Address } from 'viem'; import { ParsedAsset } from '@/__swaps__/types/assets'; -import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacySelectedGasFee, SelectedGasFee } from '@/entities'; +import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacyTransactionGasParamAmounts, TransactionGasParamAmounts } from '@/entities'; export enum SwapModalField { input = 'inputAmount', @@ -49,7 +49,7 @@ export interface RapSwapActionParameters { meta?: SwapMetadata; assetToSell: ParsedAsset; assetToBuy: ParsedAsset; - selectedGasFee: SelectedGasFee | LegacySelectedGasFee; + gasParams: TransactionGasParamAmounts | LegacyTransactionGasParamAmounts; gasFeeParamsBySpeed: GasFeeParamsBySpeed | LegacyGasFeeParamsBySpeed; nonce?: number; flashbots?: boolean; @@ -117,7 +117,7 @@ export interface ActionProps { parameters: RapActionParameterMap[T]; wallet: Signer; currentRap: Rap; - selectedGasFee: SelectedGasFee | LegacySelectedGasFee; + gasParams: TransactionGasParamAmounts | LegacyTransactionGasParamAmounts; gasFeeParamsBySpeed: GasFeeParamsBySpeed | LegacyGasFeeParamsBySpeed; } diff --git a/src/raps/unlockAndCrosschainSwap.ts b/src/raps/unlockAndCrosschainSwap.ts index 02118751b4e..4e74263c6d3 100644 --- a/src/raps/unlockAndCrosschainSwap.ts +++ b/src/raps/unlockAndCrosschainSwap.ts @@ -138,7 +138,7 @@ export const createUnlockAndCrosschainSwapRap = async (swapParameters: RapSwapAc assetToSell, sellAmount, assetToBuy, - selectedGasFee: swapParameters.selectedGasFee, + gasParams: swapParameters.gasParams, gasFeeParamsBySpeed: swapParameters.gasFeeParamsBySpeed, } satisfies RapSwapActionParameters<'crosschainSwap'>); actions = actions.concat(swap); diff --git a/src/raps/unlockAndSwap.ts b/src/raps/unlockAndSwap.ts index bd4f6f05832..08b33a4aad2 100644 --- a/src/raps/unlockAndSwap.ts +++ b/src/raps/unlockAndSwap.ts @@ -154,7 +154,7 @@ export const createUnlockAndSwapRap = async (swapParameters: RapSwapActionParame meta: swapParameters.meta, assetToSell, assetToBuy, - selectedGasFee: swapParameters.selectedGasFee, + gasParams: swapParameters.gasParams, gasFeeParamsBySpeed: swapParameters.gasFeeParamsBySpeed, } satisfies RapSwapActionParameters<'swap'>); actions = actions.concat(swap); diff --git a/src/screens/ExchangeModal.tsx b/src/screens/ExchangeModal.tsx index 8c8c0ef09c2..e31fa05212b 100644 --- a/src/screens/ExchangeModal.tsx +++ b/src/screens/ExchangeModal.tsx @@ -77,6 +77,7 @@ import { TokenColors } from '@/graphql/__generated__/metadata'; import { estimateSwapGasLimit } from '@/raps/actions'; import { estimateCrosschainSwapGasLimit } from '@/raps/actions/crosschainSwap'; import { isUnwrapEth, isWrapEth } from '@/__swaps__/utils/swaps'; +import { parseGasParamAmounts } from '@/parsers'; export const DEFAULT_SLIPPAGE_BIPS = { [Network.mainnet]: 100, @@ -487,7 +488,7 @@ export default function ExchangeModal({ fromDiscover, ignoreInitialTypeCheck, te slippage: slippageInBips, route: source, }, - selectedGasFee, + gasParams: parseGasParamAmounts(selectedGasFee), gasFeeParamsBySpeed, });