From 63ffd190a4a44b9d1cc53f40e992d31bb9705047 Mon Sep 17 00:00:00 2001 From: gregs Date: Thu, 15 Aug 2024 15:37:17 -0300 Subject: [PATCH] update swap sdk v23 (#1662) --- package.json | 4 +- src/core/raps/actions/claimBridge.ts | 4 +- src/core/raps/actions/crosschainSwap.test.ts | 1 + src/core/raps/actions/swap.test.ts | 1 + src/core/raps/unlockAndCrosschainSwap.test.ts | 15 ++++++- src/core/raps/unlockAndSwap.test.ts | 5 +++ .../popup/hooks/swap/useSwapNativeAmounts.tsx | 42 +++++++------------ src/entries/popup/hooks/swap/useSwapQuote.ts | 5 ++- yarn.lock | 8 ++-- 9 files changed, 49 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index e68a1d9327..54a0211d92 100644 --- a/package.json +++ b/package.json @@ -105,7 +105,7 @@ "@radix-ui/react-tabs": "1.0.4", "@radix-ui/react-tooltip": "1.0.3", "@rainbow-me/provider": "0.1.1", - "@rainbow-me/swaps": "0.21.0", + "@rainbow-me/swaps": "0.23.0", "@rudderstack/analytics-js-service-worker": "3.0.6", "@scure/bip39": "1.2.1", "@sentry/browser": "8.15.0", @@ -328,4 +328,4 @@ "wagmi>@wagmi/connectors>@metamask/sdk>eciesjs>secp256k1": false } } -} \ No newline at end of file +} diff --git a/src/core/raps/actions/claimBridge.ts b/src/core/raps/actions/claimBridge.ts index 554221ab42..b72d6cca81 100644 --- a/src/core/raps/actions/claimBridge.ts +++ b/src/core/raps/actions/claimBridge.ts @@ -9,7 +9,7 @@ import { Address } from 'viem'; import { optimism } from 'viem/chains'; import { REFERRER_CLAIM } from '~/core/references'; -import { gasStore } from '~/core/state'; +import { currentCurrencyStore, gasStore } from '~/core/state'; import { TransactionGasParams } from '~/core/types/gas'; import { NewTransaction, TxHash } from '~/core/types/transactions'; import { calculateL1FeeOptimism } from '~/core/utils/gas'; @@ -55,6 +55,7 @@ export async function claimBridge({ sellAmount: sellAmount, slippage: 2, swapType: SwapType.crossChain, + currency: currentCurrencyStore.getState().currentCurrency, }); // if we don't get a quote or there's an error we can't continue @@ -122,6 +123,7 @@ export async function claimBridge({ sellAmount: maxBridgeableAmount, slippage: 2, swapType: SwapType.crossChain, + currency: currentCurrencyStore.getState().currentCurrency, }); if (!newQuote || (newQuote as QuoteError)?.error) { diff --git a/src/core/raps/actions/crosschainSwap.test.ts b/src/core/raps/actions/crosschainSwap.test.ts index b4d3534b20..e453d58e4f 100644 --- a/src/core/raps/actions/crosschainSwap.test.ts +++ b/src/core/raps/actions/crosschainSwap.test.ts @@ -41,6 +41,7 @@ beforeAll(async () => { destReceiver: TEST_ADDRESS_3, swapType: SwapType.crossChain, toChainId: ChainId.arbitrum, + currency: 'USD', }); }, 20000); diff --git a/src/core/raps/actions/swap.test.ts b/src/core/raps/actions/swap.test.ts index 4bd7ea9840..b304c19739 100644 --- a/src/core/raps/actions/swap.test.ts +++ b/src/core/raps/actions/swap.test.ts @@ -32,6 +32,7 @@ beforeAll(async () => { destReceiver: TEST_ADDRESS_2, swapType: SwapType.normal, toChainId: 1, + currency: 'USD', }); }, 10000); diff --git a/src/core/raps/unlockAndCrosschainSwap.test.ts b/src/core/raps/unlockAndCrosschainSwap.test.ts index f7a0c5d7f0..5c12e4ee13 100644 --- a/src/core/raps/unlockAndCrosschainSwap.test.ts +++ b/src/core/raps/unlockAndCrosschainSwap.test.ts @@ -1,4 +1,9 @@ -import { CrosschainQuote, Quote, QuoteError } from '@rainbow-me/swaps'; +import { + CrosschainQuote, + Quote, + QuoteError, + TokenAsset, +} from '@rainbow-me/swaps'; import { mainnet } from 'viem/chains'; import { beforeAll, expect, test } from 'vitest'; @@ -24,6 +29,7 @@ const needsUnlockQuote: Quote | QuoteError | null = { chainId: 1, buyAmount: '22815411', buyAmountDisplay: '22815411', + buyAmountDisplayMinimum: '22815411', buyAmountInEth: '7674057708816777', buyAmountMinusFees: '7674057708816777', buyTokenAddress: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', @@ -48,12 +54,16 @@ const needsUnlockQuote: Quote | QuoteError | null = { to: '0xdef1c0ded9bec7f1a1670819833240f027b25eff', tradeAmountUSD: 21.84463710898238, value: '0', + buyTokenAsset: {} as TokenAsset, // not used in this test + feeTokenAsset: {} as TokenAsset, // not used in this test + sellTokenAsset: {} as TokenAsset, // not used in this test }; const doesntNeedUnlockQuote: Quote | QuoteError | null = { chainId: 1, buyAmount: '2934529154', buyAmountDisplay: '2934529154', + buyAmountDisplayMinimum: '2934529154', buyAmountInEth: '988585673036047522', buyAmountMinusFees: '988585673036047522', buyTokenAddress: '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', @@ -82,6 +92,9 @@ const doesntNeedUnlockQuote: Quote | QuoteError | null = { to: TEST_ADDRESS_2, tradeAmountUSD: 2963.84, value: '0x0de0b6b3a7640000', + buyTokenAsset: {} as TokenAsset, // not used in this test + feeTokenAsset: {} as TokenAsset, // not used in this test + sellTokenAsset: {} as TokenAsset, // not used in this test }; beforeAll(async () => { diff --git a/src/core/raps/unlockAndSwap.test.ts b/src/core/raps/unlockAndSwap.test.ts index bac2d3b133..352b3f4847 100644 --- a/src/core/raps/unlockAndSwap.test.ts +++ b/src/core/raps/unlockAndSwap.test.ts @@ -72,6 +72,7 @@ beforeAll(async () => { destReceiver: TEST_ADDRESS_2, swapType: SwapType.normal, toChainId: 1, + currency: 'USD', }); ethToEnsQuote = await getQuote({ chainId: 1, @@ -83,6 +84,7 @@ beforeAll(async () => { destReceiver: TEST_ADDRESS_2, swapType: SwapType.normal, toChainId: 1, + currency: 'USD', }); needsUnlockQuote = await getQuote({ chainId: 1, @@ -94,6 +96,7 @@ beforeAll(async () => { destReceiver: TEST_ADDRESS_2, swapType: SwapType.normal, toChainId: 1, + currency: 'USD', }); wrapEthQuote = await getQuote({ chainId: 1, @@ -105,6 +108,7 @@ beforeAll(async () => { destReceiver: TEST_ADDRESS_2, swapType: SwapType.normal, toChainId: 1, + currency: 'USD', }); unwrapEthQuote = await getQuote({ chainId: 1, @@ -116,6 +120,7 @@ beforeAll(async () => { destReceiver: TEST_ADDRESS_2, swapType: SwapType.normal, toChainId: 1, + currency: 'USD', }); }, 10000); diff --git a/src/entries/popup/hooks/swap/useSwapNativeAmounts.tsx b/src/entries/popup/hooks/swap/useSwapNativeAmounts.tsx index d874962395..f8a920f657 100644 --- a/src/entries/popup/hooks/swap/useSwapNativeAmounts.tsx +++ b/src/entries/popup/hooks/swap/useSwapNativeAmounts.tsx @@ -4,15 +4,12 @@ import { useMemo } from 'react'; import { supportedCurrencies } from '~/core/references'; import { useCurrentCurrencyStore } from '~/core/state'; import { ParsedSearchAsset } from '~/core/types/assets'; -import { ChainId } from '~/core/types/chains'; import { convertAmountAndPriceToNativeDisplay, convertRawAmountToNativeDisplay, handleSignificantDecimalsAsNumber, } from '~/core/utils/numbers'; -import { useNativeAssetForNetwork } from '../useNativeAssetForNetwork'; - export const useSwapNativeAmounts = ({ assetToSell, assetToSellValue, @@ -29,24 +26,17 @@ export const useSwapNativeAmounts = ({ isWrapOrUnwrapEth: boolean; }) => { const { currentCurrency } = useCurrentCurrencyStore(); - const sellNativeAsset = useNativeAssetForNetwork({ - chainId: assetToSell?.chainId || ChainId.mainnet, - }); - - const buyNativeAsset = useNativeAssetForNetwork({ - chainId: assetToBuy?.chainId || ChainId.mainnet, - }); const assetToSellNativeDisplay = useMemo(() => { let nativeDisplay = null; if (isWrapOrUnwrapEth) { nativeDisplay = - !quote?.sellAmount || !assetToSell?.price?.value + !quote?.sellAmount || !quote.sellTokenAsset.price.value ? null : convertRawAmountToNativeDisplay( quote?.sellAmount?.toString(), - assetToSell?.decimals || 18, - assetToSell?.price?.value, + quote.sellTokenAsset.decimals || 18, + quote.sellTokenAsset.price.value, currentCurrency, ); } else if (assetToSell?.native?.price?.amount && assetToSellValue) { @@ -57,12 +47,12 @@ export const useSwapNativeAmounts = ({ ); } else { nativeDisplay = - !quote?.sellAmountInEth || !sellNativeAsset?.price?.value + !quote?.sellAmountInEth || !quote.sellTokenAsset.price.value ? null : convertRawAmountToNativeDisplay( quote?.sellAmountInEth.toString(), - sellNativeAsset?.decimals || 18, - sellNativeAsset?.price?.value, + quote.sellTokenAsset.decimals || 18, + quote.sellTokenAsset.price?.value, currentCurrency, ); } @@ -79,14 +69,12 @@ export const useSwapNativeAmounts = ({ }, [ isWrapOrUnwrapEth, assetToSell?.native?.price?.amount, - assetToSell?.price?.value, - assetToSell?.decimals, + assetToSellValue, + currentCurrency, quote?.sellAmount, + quote?.sellTokenAsset.price.value, + quote?.sellTokenAsset.decimals, quote?.sellAmountInEth, - currentCurrency, - assetToSellValue, - sellNativeAsset?.price?.value, - sellNativeAsset?.decimals, ]); const assetToBuyNativeDisplay = useMemo(() => { @@ -109,12 +97,12 @@ export const useSwapNativeAmounts = ({ ); } else { nativeDisplay = - !quote?.buyAmountInEth || !buyNativeAsset?.price?.value + !quote?.buyAmountInEth || !quote.buyTokenAsset.price?.value ? null : convertRawAmountToNativeDisplay( quote?.buyAmountInEth.toString(), - buyNativeAsset?.decimals || 18, - buyNativeAsset?.price?.value, + quote.buyTokenAsset.decimals || 18, + quote.buyTokenAsset.price?.value, currentCurrency, ); } @@ -136,8 +124,8 @@ export const useSwapNativeAmounts = ({ quote?.buyAmount, quote?.buyAmountInEth, currentCurrency, - buyNativeAsset?.price?.value, - buyNativeAsset?.decimals, + quote?.buyTokenAsset.price?.value, + quote?.buyTokenAsset.decimals, ]); return { diff --git a/src/entries/popup/hooks/swap/useSwapQuote.ts b/src/entries/popup/hooks/swap/useSwapQuote.ts index 72b2d499fd..27aba75225 100644 --- a/src/entries/popup/hooks/swap/useSwapQuote.ts +++ b/src/entries/popup/hooks/swap/useSwapQuote.ts @@ -12,7 +12,7 @@ import { import { useQuery } from '@tanstack/react-query'; import { useMemo } from 'react'; -import { useCurrentAddressStore } from '~/core/state'; +import { useCurrentAddressStore, useCurrentCurrencyStore } from '~/core/state'; import { ParsedAsset, ParsedSearchAsset } from '~/core/types/assets'; import { ChainId } from '~/core/types/chains'; import { convertAmountToRawAmount } from '~/core/utils/numbers'; @@ -46,6 +46,7 @@ export const useSwapQuote = ({ isClaim, }: UseSwapQuotesProps) => { const { currentAddress } = useCurrentAddressStore(); + const currency = useCurrentCurrencyStore((s) => s.currentCurrency); const isCrosschainSwap = useMemo( () => @@ -95,6 +96,7 @@ export const useSwapQuote = ({ swapType: isCrosschainSwap ? SwapType.crossChain : SwapType.normal, toChainId: isCrosschainSwap ? assetToBuy.chainId : assetToSell.chainId, feePercentageBasisPoints: INTERNAL_BUILD || isClaim ? 0 : undefined, + currency, }; }, [ assetToBuy, @@ -107,6 +109,7 @@ export const useSwapQuote = ({ slippage, source, isClaim, + currency, ]); const { data, isLoading, isError, fetchStatus } = useQuery({ diff --git a/yarn.lock b/yarn.lock index aabf88ccef..49cf06ed99 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3932,10 +3932,10 @@ eventemitter3 "5.0.1" viem "1.21.4" -"@rainbow-me/swaps@0.21.0": - version "0.21.0" - resolved "https://registry.yarnpkg.com/@rainbow-me/swaps/-/swaps-0.21.0.tgz#c04e72c9bc8fccaa6865dba2386e78665c551747" - integrity sha512-ULTlMrPmJzsgKXJxDtsGMMPsFMtDG0HvAc2BS7LSfmg8IsBHOavr7fo+7LohV5IUnpCVTDYtsZei/mVBUgxSDg== +"@rainbow-me/swaps@0.23.0": + version "0.23.0" + resolved "https://registry.yarnpkg.com/@rainbow-me/swaps/-/swaps-0.23.0.tgz#888e61a302e0f642e4ac7c5f58e6bd6d90b7c953" + integrity sha512-r6xF2v6bPguz7ytHO/UY+IjvyI8QqbytuXikXT8zCp1TVcXYWESsxJbVsGHd4utJOswSt5M7H1He0wY7shBh6g== dependencies: "@ethereumjs/util" "9.0.0" "@ethersproject/abi" "5.7.0"