Skip to content

Commit

Permalink
Fix weth wrapping/unwrapping on other networks (#5911)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobar79 committed Jul 3, 2024
1 parent d905682 commit 7ce9206
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
23 changes: 18 additions & 5 deletions src/__swaps__/utils/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -39,6 +39,7 @@ import {
orderOfMagnitudeWorklet,
isNumberStringWorklet,
} from '../safe-math/SafeMath';
import { ETH_ADDRESS } from '@/references';

// /---- 🎨 Color functions 🎨 ----/ //
//
Expand Down Expand Up @@ -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 = ({
Expand All @@ -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 = ({
Expand Down Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions src/raps/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
4 changes: 1 addition & 3 deletions src/raps/unlockAndCrosschainSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 6 additions & 7 deletions src/raps/unlockAndSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7ce9206

Please sign in to comment.