From 5d27d77514dc5780e749b324bf299ff5a22c139c Mon Sep 17 00:00:00 2001 From: brdy <41711440+BrodyHughes@users.noreply.github.com> Date: Fri, 25 Oct 2024 22:30:00 -0500 Subject: [PATCH] remaining of swaps sdk migrations, fix wrap/unwrap (#1746) Co-authored-by: Daniel Sinclair <4412473+DanielSinclair@users.noreply.github.com> --- src/core/raps/actions/swap.ts | 11 ++--- src/core/raps/unlockAndCrosschainSwap.ts | 54 ++++----------------- src/core/raps/unlockAndSwap.ts | 60 ++++-------------------- 3 files changed, 25 insertions(+), 100 deletions(-) diff --git a/src/core/raps/actions/swap.ts b/src/core/raps/actions/swap.ts index d208bb48ce..fcd31a6c0d 100644 --- a/src/core/raps/actions/swap.ts +++ b/src/core/raps/actions/swap.ts @@ -6,6 +6,7 @@ import { ETH_ADDRESS as ETH_ADDRESS_AGGREGATORS, Quote, ChainId as SwapChainId, + SwapType, WRAPPED_ASSET, fillQuote, getQuoteExecutionDetails, @@ -22,7 +23,6 @@ import { ChainId } from '~/core/types/chains'; import { NewTransaction, TxHash } from '~/core/types/transactions'; import { add } from '~/core/utils/numbers'; import { isLowerCaseMatch } from '~/core/utils/strings'; -import { isUnwrapEth, isWrapEth } from '~/core/utils/swaps'; import { addNewTransaction } from '~/core/utils/transactions'; import { getProvider } from '~/core/wagmi/clientToProvider'; import { TransactionSimulationResponse } from '~/entries/popup/pages/messages/useSimulateTransaction'; @@ -232,23 +232,22 @@ export const executeSwap = async ({ }): Promise => { if (!wallet || !quote) return null; - const { sellTokenAddress, buyTokenAddress } = quote; const transactionParams = { gasLimit: toHex(gasLimit) || undefined, nonce: nonce ? toHex(`${nonce}`) : undefined, ...gasParams, }; - // Wrap Eth - if (isWrapEth({ buyTokenAddress, sellTokenAddress, chainId })) { + // Wrap native + if (quote.swapType === SwapType.wrap) { return wrapNativeAsset( quote.buyAmount, wallet, chainId as unknown as SwapChainId, transactionParams, ); - // Unwrap Weth - } else if (isUnwrapEth({ buyTokenAddress, sellTokenAddress, chainId })) { + // Unwrap native + } else if (quote.swapType === SwapType.unwrap) { return unwrapNativeAsset( quote.sellAmount, wallet, diff --git a/src/core/raps/unlockAndCrosschainSwap.ts b/src/core/raps/unlockAndCrosschainSwap.ts index d044bc78c3..49dfb28a24 100644 --- a/src/core/raps/unlockAndCrosschainSwap.ts +++ b/src/core/raps/unlockAndCrosschainSwap.ts @@ -1,16 +1,6 @@ -import { - ALLOWS_PERMIT, - ChainId, - ETH_ADDRESS as ETH_ADDRESS_AGGREGATOR, - PermitSupportedTokenList, - WRAPPED_ASSET, -} from '@rainbow-me/swaps'; import { Address } from 'viem'; -import { ETH_ADDRESS } from '../references'; -import { isNativeAsset } from '../utils/chains'; import { add } from '../utils/numbers'; -import { isLowerCaseMatch } from '../utils/strings'; import { assetNeedsUnlocking, estimateApprove } from './actions'; import { estimateCrosschainSwapGasLimit } from './actions/crosschainSwap'; @@ -29,29 +19,20 @@ export const estimateUnlockAndCrosschainSwap = async ( const { from: accountAddress, sellTokenAddress, - buyTokenAddress, allowanceTarget, + allowanceNeeded, } = quote as { from: Address; sellTokenAddress: Address; buyTokenAddress: Address; allowanceTarget: Address; + allowanceNeeded: boolean; }; - const isNativeAssetUnwrapping = - (isLowerCaseMatch(sellTokenAddress, WRAPPED_ASSET?.[chainId]) && - isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS)) || - isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS_AGGREGATOR); - let gasLimits: (string | number)[] = []; let swapAssetNeedsUnlocking = false; - // Aggregators represent native asset as 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE - const nativeAsset = - isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) || - isNativeAsset(assetToSell.address, chainId); - - if (!isNativeAssetUnwrapping && !nativeAsset) { + if (allowanceNeeded) { swapAssetNeedsUnlocking = await assetNeedsUnlocking({ owner: accountAddress, amount: sellAmount, @@ -95,28 +76,19 @@ export const createUnlockAndCrosschainSwapRap = async ( const { from: accountAddress, - sellTokenAddress, - buyTokenAddress, allowanceTarget, + allowanceNeeded, } = quote as { from: Address; sellTokenAddress: Address; buyTokenAddress: Address; allowanceTarget: Address; + allowanceNeeded: boolean; }; - const isNativeAssetUnwrapping = - isLowerCaseMatch(sellTokenAddress, WRAPPED_ASSET[`${chainId}`]) && - isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS); - - // Aggregators represent native asset as 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE - const nativeAsset = - isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) || - assetToSell?.isNativeAsset; - let swapAssetNeedsUnlocking = false; - if (!isNativeAssetUnwrapping && !nativeAsset) { + if (allowanceNeeded) { swapAssetNeedsUnlocking = await assetNeedsUnlocking({ owner: accountAddress, amount: sellAmount, @@ -125,14 +97,8 @@ export const createUnlockAndCrosschainSwapRap = async ( chainId, }); } - const allowsPermit = - !nativeAsset && - chainId === ChainId.mainnet && - ALLOWS_PERMIT[ - assetToSell.address?.toLowerCase() as keyof PermitSupportedTokenList - ]; - - if (swapAssetNeedsUnlocking && !allowsPermit) { + + if (swapAssetNeedsUnlocking) { const unlock = createNewAction('unlock', { fromAddress: accountAddress, amount: sellAmount, @@ -146,8 +112,8 @@ export const createUnlockAndCrosschainSwapRap = async ( // create a swap rap const swap = createNewAction('crosschainSwap', { chainId, - permit: swapAssetNeedsUnlocking && allowsPermit, - requiresApprove: swapAssetNeedsUnlocking && !allowsPermit, + permit: swapAssetNeedsUnlocking, + requiresApprove: swapAssetNeedsUnlocking, quote, meta: swapParameters.meta, assetToSell, diff --git a/src/core/raps/unlockAndSwap.ts b/src/core/raps/unlockAndSwap.ts index 88b0ce48da..6660a1e21f 100644 --- a/src/core/raps/unlockAndSwap.ts +++ b/src/core/raps/unlockAndSwap.ts @@ -1,18 +1,7 @@ -import { - ALLOWS_PERMIT, - ETH_ADDRESS as ETH_ADDRESS_AGGREGATOR, - PermitSupportedTokenList, - WRAPPED_ASSET, - getRainbowRouterContractAddress, -} from '@rainbow-me/swaps'; +import { getRainbowRouterContractAddress } from '@rainbow-me/swaps'; import { Address } from 'viem'; -import { ETH_ADDRESS } from '../references'; -import { ChainId } from '../types/chains'; -import { isNativeAsset } from '../utils/chains'; import { add } from '../utils/numbers'; -import { isLowerCaseMatch } from '../utils/strings'; -import { isUnwrapEth } from '../utils/swaps'; import { assetNeedsUnlocking, @@ -35,26 +24,18 @@ export const estimateUnlockAndSwap = async ( const { from: accountAddress, sellTokenAddress, - buyTokenAddress, + allowanceNeeded, } = quote as { from: Address; sellTokenAddress: Address; buyTokenAddress: Address; + allowanceNeeded: boolean; }; - const isNativeAssetUnwrapping = - isLowerCaseMatch(sellTokenAddress, WRAPPED_ASSET?.[chainId]) && - (isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS?.[chainId]) || - isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS_AGGREGATOR?.[chainId])); - let gasLimits: (string | number)[] = []; let swapAssetNeedsUnlocking = false; - const nativeAsset = - isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) || - isNativeAsset(sellTokenAddress, chainId); - - if (!isNativeAssetUnwrapping && !nativeAsset) { + if (allowanceNeeded) { swapAssetNeedsUnlocking = await assetNeedsUnlocking({ owner: accountAddress, amount: sellAmount, @@ -110,30 +91,16 @@ export const createUnlockAndSwapRap = async ( const { sellAmount, quote, chainId, assetToSell, assetToBuy } = swapParameters; - const { - from: accountAddress, - sellTokenAddress, - buyTokenAddress, - } = quote as { + const { from: accountAddress, allowanceNeeded } = quote as { from: Address; sellTokenAddress: Address; buyTokenAddress: Address; + allowanceNeeded: boolean; }; - const isNativeAssetUnwrapping = isUnwrapEth({ - buyTokenAddress, - chainId, - sellTokenAddress, - }); - - // Aggregators represent native asset as 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE - const nativeAsset = - isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) || - isNativeAsset(sellTokenAddress, chainId); - let swapAssetNeedsUnlocking = false; - if (!isNativeAssetUnwrapping && !nativeAsset) { + if (allowanceNeeded) { swapAssetNeedsUnlocking = await assetNeedsUnlocking({ owner: accountAddress, amount: sellAmount as string, @@ -143,14 +110,7 @@ export const createUnlockAndSwapRap = async ( }); } - const allowsPermit = - !nativeAsset && - chainId === ChainId.mainnet && - ALLOWS_PERMIT[ - assetToSell.address?.toLowerCase() as keyof PermitSupportedTokenList - ]; - - if (swapAssetNeedsUnlocking && !allowsPermit) { + if (swapAssetNeedsUnlocking) { const unlock = createNewAction('unlock', { fromAddress: accountAddress, amount: sellAmount, @@ -165,8 +125,8 @@ export const createUnlockAndSwapRap = async ( const swap = createNewAction('swap', { chainId, sellAmount, - permit: swapAssetNeedsUnlocking && allowsPermit, - requiresApprove: swapAssetNeedsUnlocking && !allowsPermit, + permit: swapAssetNeedsUnlocking, + requiresApprove: swapAssetNeedsUnlocking, quote, meta: swapParameters.meta, assetToSell,