From 7ce920647242d56cec3f39e1e3837bf828f4e6f8 Mon Sep 17 00:00:00 2001 From: Bruno Barbieri <1247834+brunobar79@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:28:19 -0400 Subject: [PATCH] Fix weth wrapping/unwrapping on other networks (#5911) --- src/__swaps__/utils/swaps.ts | 23 ++++++++++++++++++----- src/raps/actions/swap.ts | 4 ++-- src/raps/unlockAndCrosschainSwap.ts | 4 +--- src/raps/unlockAndSwap.ts | 13 ++++++------- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/__swaps__/utils/swaps.ts b/src/__swaps__/utils/swaps.ts index 2f9e540bff7..819abf81c3c 100644 --- a/src/__swaps__/utils/swaps.ts +++ b/src/__swaps__/utils/swaps.ts @@ -20,7 +20,7 @@ import { RainbowConfig } from '@/model/remoteConfig'; import { userAssetsStore } from '@/state/assets/userAssets'; import { colors } from '@/styles'; import { BigNumberish } from '@ethersproject/bignumber'; -import { CrosschainQuote, ETH_ADDRESS, Quote, QuoteParams, SwapType, WRAPPED_ASSET } from '@rainbow-me/swaps'; +import { CrosschainQuote, ETH_ADDRESS as ETH_ADDRESS_AGGREGATOR, Quote, QuoteParams, SwapType, WRAPPED_ASSET } from '@rainbow-me/swaps'; import { swapsStore } from '../../state/swaps/swapsStore'; import { AddressOrEth, ExtendedAnimatedAssetWithColors, ParsedSearchAsset } from '../types/assets'; import { inputKeys } from '../types/swap'; @@ -39,6 +39,7 @@ import { orderOfMagnitudeWorklet, isNumberStringWorklet, } from '../safe-math/SafeMath'; +import { ETH_ADDRESS } from '@/references'; // /---- 🎨 Color functions 🎨 ----/ // // @@ -596,7 +597,13 @@ export const isUnwrapEthWorklet = ({ buyTokenAddress: string; }) => { 'worklet'; - return isLowerCaseMatchWorklet(sellTokenAddress, WRAPPED_ASSET[chainId]) && isLowerCaseMatchWorklet(buyTokenAddress, ETH_ADDRESS); + if (chainId === ChainId.mainnet) { + return isLowerCaseMatchWorklet(sellTokenAddress, WRAPPED_ASSET[chainId]) && isLowerCaseMatchWorklet(buyTokenAddress, ETH_ADDRESS); + } else { + return ( + isLowerCaseMatchWorklet(sellTokenAddress, WRAPPED_ASSET[chainId]) && isLowerCaseMatchWorklet(buyTokenAddress, ETH_ADDRESS_AGGREGATOR) + ); + } }; export const isWrapEthWorklet = ({ @@ -609,7 +616,13 @@ export const isWrapEthWorklet = ({ buyTokenAddress: string; }) => { 'worklet'; - return isLowerCaseMatchWorklet(sellTokenAddress, ETH_ADDRESS) && isLowerCaseMatchWorklet(buyTokenAddress, WRAPPED_ASSET[chainId]); + if (chainId === ChainId.mainnet) { + return isLowerCaseMatchWorklet(sellTokenAddress, ETH_ADDRESS) && isLowerCaseMatchWorklet(buyTokenAddress, WRAPPED_ASSET[chainId]); + } else { + return ( + isLowerCaseMatchWorklet(sellTokenAddress, ETH_ADDRESS_AGGREGATOR) && isLowerCaseMatchWorklet(buyTokenAddress, WRAPPED_ASSET[chainId]) + ); + } }; export const priceForAsset = ({ @@ -718,8 +731,8 @@ export const buildQuoteParams = ({ source: source === 'auto' ? undefined : source, chainId: inputAsset.chainId, fromAddress: currentAddress, - sellTokenAddress: inputAsset.isNativeAsset ? ETH_ADDRESS : inputAsset.address, - buyTokenAddress: outputAsset.isNativeAsset ? ETH_ADDRESS : outputAsset.address, + sellTokenAddress: inputAsset.isNativeAsset ? ETH_ADDRESS_AGGREGATOR : inputAsset.address, + buyTokenAddress: outputAsset.isNativeAsset ? ETH_ADDRESS_AGGREGATOR : outputAsset.address, // TODO: Handle native input cases below sellAmount: lastTypedInput === 'inputAmount' || lastTypedInput === 'inputNativeValue' diff --git a/src/raps/actions/swap.ts b/src/raps/actions/swap.ts index 49582bdd3e6..47fb9a098cd 100644 --- a/src/raps/actions/swap.ts +++ b/src/raps/actions/swap.ts @@ -79,14 +79,14 @@ export const estimateSwapGasLimit = async ({ const gasLimit = await estimateGasWithPadding( { from: quote.from, - value: isWrapNativeAsset ? quote.buyAmount : '0', + value: isWrapNativeAsset ? quote.buyAmount.toString() : '0', }, getWrappedAssetMethod( isWrapNativeAsset ? 'deposit' : 'withdraw', provider as StaticJsonRpcProvider, chainId as unknown as SwapChainId ), - null, + isWrapNativeAsset ? [] : [quote.buyAmount.toString()], provider, WRAP_GAS_PADDING ); diff --git a/src/raps/unlockAndCrosschainSwap.ts b/src/raps/unlockAndCrosschainSwap.ts index 6346f7238a0..0a41ef235a8 100644 --- a/src/raps/unlockAndCrosschainSwap.ts +++ b/src/raps/unlockAndCrosschainSwap.ts @@ -97,9 +97,7 @@ export const createUnlockAndCrosschainSwapRap = async (swapParameters: RapSwapAc }; const isNativeAssetUnwrapping = - isLowerCaseMatch(sellTokenAddress, WRAPPED_ASSET[`${chainId}`]) && - isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS) && - chainId === ChainId.mainnet; + isLowerCaseMatch(sellTokenAddress, WRAPPED_ASSET[`${chainId}`]) && isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS); // Aggregators represent native asset as 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE const nativeAsset = isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) || assetToSell?.isNativeAsset; diff --git a/src/raps/unlockAndSwap.ts b/src/raps/unlockAndSwap.ts index 970029d0b4a..94788237308 100644 --- a/src/raps/unlockAndSwap.ts +++ b/src/raps/unlockAndSwap.ts @@ -13,7 +13,7 @@ import { add } from '@/helpers/utilities'; import { ethereumUtils, isLowerCaseMatch } from '@/utils'; import { ETH_ADDRESS } from '../references'; -import { isWrapNative } from '@/handlers/swap'; +import { isUnwrapNative } from '@/handlers/swap'; import { assetNeedsUnlocking, estimateApprove, estimateSwapGasLimit } from './actions'; import { estimateUnlockAndSwapFromMetadata } from './actions/swap'; import { createNewAction, createNewRap } from './common'; @@ -108,12 +108,11 @@ export const createUnlockAndSwapRap = async (swapParameters: RapSwapActionParame buyTokenAddress: Address; }; - const isNativeAssetUnwrapping = - isWrapNative({ - chainId, - sellTokenAddress, - buyTokenAddress, - }) && chainId === ChainId.mainnet; + const isNativeAssetUnwrapping = isUnwrapNative({ + chainId, + sellTokenAddress, + buyTokenAddress, + }); // TODO: MARK - replace this when we migrate from network => chainId const network = ethereumUtils.getNetworkFromChainId(chainId);