From 21ce0d8f0036c4f7ec337aae200855f34e1b96c7 Mon Sep 17 00:00:00 2001 From: gregs Date: Mon, 12 Aug 2024 14:48:16 -0300 Subject: [PATCH 1/5] hm --- package.json | 2 +- .../Swap/hooks/useSwapInputsController.ts | 56 +++++++++---------- yarn.lock | 10 ++-- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index c5676ef3ca8..ebd6edf061c 100644 --- a/package.json +++ b/package.json @@ -101,7 +101,7 @@ "@notifee/react-native": "7.8.2", "@rainbow-me/provider": "0.0.12", "@rainbow-me/react-native-animated-number": "0.0.2", - "@rainbow-me/swaps": "0.22.0", + "@rainbow-me/swaps": "0.23.0", "@react-native-async-storage/async-storage": "1.23.1", "@react-native-camera-roll/camera-roll": "7.7.0", "@react-native-clipboard/clipboard": "1.13.2", diff --git a/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts b/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts index 266caea1063..3c3174cd3f9 100644 --- a/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts +++ b/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts @@ -1,38 +1,38 @@ -import { useCallback } from 'react'; -import { SharedValue, runOnJS, runOnUI, useAnimatedReaction, useDerivedValue, useSharedValue, withSpring } from 'react-native-reanimated'; -import { useDebouncedCallback } from 'use-debounce'; +import { divWorklet, equalWorklet, greaterThanWorklet, isNumberStringWorklet, mulWorklet } from '@/__swaps__/safe-math/SafeMath'; import { SCRUBBER_WIDTH, SLIDER_WIDTH, snappySpringConfig } from '@/__swaps__/screens/Swap/constants'; +import { ExtendedAnimatedAssetWithColors } from '@/__swaps__/types/assets'; +import { ChainId } from '@/__swaps__/types/chains'; import { RequestNewQuoteParams, inputKeys, inputMethods, inputValuesType } from '@/__swaps__/types/swap'; import { valueBasedDecimalFormatter } from '@/__swaps__/utils/decimalFormatter'; -import { addCommasToNumber, buildQuoteParams, clamp, getDefaultSlippageWorklet, trimTrailingZeros } from '@/__swaps__/utils/swaps'; -import { ExtendedAnimatedAssetWithColors } from '@/__swaps__/types/assets'; -import { CrosschainQuote, Quote, QuoteError, SwapType, getCrosschainQuote, getQuote } from '@rainbow-me/swaps'; -import { useAnimatedInterval } from '@/hooks/reanimated/useAnimatedInterval'; -import store from '@/redux/store'; -import { swapsStore } from '@/state/swaps/swapsStore'; +import { getInputValuesForSliderPositionWorklet, updateInputValuesAfterFlip } from '@/__swaps__/utils/flipAssets'; import { convertAmountToNativeDisplayWorklet, convertRawAmountToDecimalFormat, handleSignificantDecimalsWorklet, } from '@/__swaps__/utils/numbers'; -import { NavigationSteps } from './useSwapNavigation'; +import { addCommasToNumber, buildQuoteParams, clamp, getDefaultSlippageWorklet, trimTrailingZeros } from '@/__swaps__/utils/swaps'; +import { analyticsV2 } from '@/analytics'; +import { SPRING_CONFIGS } from '@/components/animations/animationConfigs'; +import { useAccountSettings } from '@/hooks'; +import { useAnimatedInterval } from '@/hooks/reanimated/useAnimatedInterval'; import { RainbowError, logger } from '@/logger'; +import { getRemoteConfig } from '@/model/remoteConfig'; +import { queryClient } from '@/react-query'; +import store from '@/redux/store'; import { EXTERNAL_TOKEN_STALE_TIME, ExternalTokenQueryFunctionResult, externalTokenQueryKey, fetchExternalToken, } from '@/resources/assets/externalAssetsQuery'; -import { ethereumUtils } from '@/utils'; -import { queryClient } from '@/react-query'; -import { useAccountSettings } from '@/hooks'; -import { analyticsV2 } from '@/analytics'; -import { divWorklet, equalWorklet, greaterThanWorklet, isNumberStringWorklet, mulWorklet } from '@/__swaps__/safe-math/SafeMath'; -import { SPRING_CONFIGS } from '@/components/animations/animationConfigs'; import { triggerHapticFeedback } from '@/screens/points/constants'; -import { getInputValuesForSliderPositionWorklet, updateInputValuesAfterFlip } from '@/__swaps__/utils/flipAssets'; -import { getRemoteConfig } from '@/model/remoteConfig'; -import { ChainId } from '@/__swaps__/types/chains'; +import { swapsStore } from '@/state/swaps/swapsStore'; +import { ethereumUtils } from '@/utils'; +import { CrosschainQuote, Quote, QuoteError, SwapType, getCrosschainQuote, getQuote } from '@rainbow-me/swaps'; +import { useCallback } from 'react'; +import { SharedValue, runOnJS, runOnUI, useAnimatedReaction, useDerivedValue, useSharedValue, withSpring } from 'react-native-reanimated'; +import { useDebouncedCallback } from 'use-debounce'; +import { NavigationSteps } from './useSwapNavigation'; const REMOTE_CONFIG = getRemoteConfig(); @@ -421,6 +421,7 @@ export function useSwapInputsController({ lastTypedInput: lastTypedInputParam, outputAmount, outputAsset: internalSelectedOutputAsset.value, + currency: store.getState().settings.nativeCurrency, }); logger.debug(`[useSwapInputsController]: quote params`, { @@ -433,19 +434,14 @@ export function useSwapInputsController({ } try { - const [quoteResponse, fetchedPrices] = await Promise.all([ - params.swapType === SwapType.crossChain ? getCrosschainQuote(params) : getQuote(params), - fetchAssetPrices({ - inputAsset: internalSelectedInputAsset.value, - outputAsset: internalSelectedOutputAsset.value, - }), - ]); + const quoteResponse = await (params.swapType === SwapType.crossChain ? getCrosschainQuote(params) : getQuote(params)); + if (!quoteResponse || 'error' in quoteResponse) throw ''; const quotedInputAmount = lastTypedInputParam === 'outputAmount' ? Number( convertRawAmountToDecimalFormat( - (quoteResponse as Quote)?.sellAmount?.toString(), + quoteResponse.sellAmount.toString(), internalSelectedInputAsset.value?.networks[internalSelectedInputAsset.value.chainId]?.decimals || internalSelectedInputAsset.value?.decimals || 18 @@ -457,7 +453,7 @@ export function useSwapInputsController({ lastTypedInputParam === 'inputAmount' ? Number( convertRawAmountToDecimalFormat( - (quoteResponse as Quote)?.buyAmountMinusFees?.toString(), + quoteResponse.buyAmountMinusFees.toString(), internalSelectedOutputAsset.value?.networks[internalSelectedOutputAsset.value.chainId]?.decimals || internalSelectedOutputAsset.value?.decimals || 18 @@ -475,7 +471,7 @@ export function useSwapInputsController({ setQuote({ data: quoteResponse, inputAmount: quotedInputAmount, - inputPrice: fetchedPrices.inputPrice, + inputPrice: quoteResponse.sellTokenAsset.price.value, originalQuoteParams: { assetToBuyUniqueId: originalOutputAssetUniqueId, assetToSellUniqueId: originalInputAssetUniqueId, @@ -484,7 +480,7 @@ export function useSwapInputsController({ outputAmount: outputAmount, }, outputAmount: quotedOutputAmount, - outputPrice: fetchedPrices.outputPrice, + outputPrice: quoteResponse.buyTokenAsset.price.value, quoteFetchingInterval, }); })(); diff --git a/yarn.lock b/yarn.lock index d39d92e225c..2a2f3946569 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4299,9 +4299,9 @@ __metadata: languageName: node linkType: hard -"@rainbow-me/swaps@npm:0.22.0": - version: 0.22.0 - resolution: "@rainbow-me/swaps@npm:0.22.0" +"@rainbow-me/swaps@npm:0.23.0": + version: 0.23.0 + resolution: "@rainbow-me/swaps@npm:0.23.0" dependencies: "@ethereumjs/util": "npm:9.0.0" "@ethersproject/abi": "npm:5.7.0" @@ -4316,7 +4316,7 @@ __metadata: "@ethersproject/transactions": "npm:5.7.0" "@ethersproject/wallet": "npm:5.7.0" "@metamask/eth-sig-util": "npm:7.0.0" - checksum: 10c0/5a466d3d03c5e2f80970750a111f5531ade3a1d85c76d3863e538869adebeaf7c4ce6a7c892f340c603ce5f980f742b3bfc2865fc60cf1fe062072aaa2a710e9 + checksum: 10c0/3029ca9ed16a45af1961d2fdc79c545ae20a55212b38c8d3daba707617f8630f5328bc52e0e4732d97bffdbda62408093b3c871ef696322f1853d71231561a0f languageName: node linkType: hard @@ -7811,7 +7811,7 @@ __metadata: "@notifee/react-native": "npm:7.8.2" "@rainbow-me/provider": "npm:0.0.12" "@rainbow-me/react-native-animated-number": "npm:0.0.2" - "@rainbow-me/swaps": "npm:0.22.0" + "@rainbow-me/swaps": "npm:0.23.0" "@react-native-async-storage/async-storage": "npm:1.23.1" "@react-native-camera-roll/camera-roll": "npm:7.7.0" "@react-native-clipboard/clipboard": "npm:1.13.2" From e04062b96d5daf99f50b75f3c08f55eed6e7ebbf Mon Sep 17 00:00:00 2001 From: Ben Goldberg Date: Tue, 13 Aug 2024 13:41:50 -0700 Subject: [PATCH 2/5] done --- .../screens/Swap/components/ReviewPanel.tsx | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/__swaps__/screens/Swap/components/ReviewPanel.tsx b/src/__swaps__/screens/Swap/components/ReviewPanel.tsx index 1988c2fa001..a1e2b68e8e8 100644 --- a/src/__swaps__/screens/Swap/components/ReviewPanel.tsx +++ b/src/__swaps__/screens/Swap/components/ReviewPanel.tsx @@ -6,6 +6,7 @@ import { ChainId, ChainNameDisplay } from '@/__swaps__/types/chains'; import { chainNameFromChainId } from '@/__swaps__/utils/chains'; import { useEstimatedTime } from '@/__swaps__/utils/meteorology'; import { + convertAmountToBalanceDisplay, convertRawAmountToBalance, convertRawAmountToBalanceWorklet, convertRawAmountToNativeDisplay, @@ -91,14 +92,12 @@ const RainbowFee = () => { decimals: 18, }).amount; - const feeInEth = convertRawAmountToNativeDisplay( - quote.feeInEth.toString(), - nativeAsset?.value?.decimals || 18, - nativeAsset?.value?.price?.value || '0', - nativeCurrency - ).display; + const { display: feeDisplay } = convertRawAmountToBalance(quote.fee.toString(), { + decimals: quote.feeTokenAsset.decimals, + symbol: quote.feeTokenAsset.symbol, + }); - rainbowFee.value = [feeInEth, `${handleSignificantDecimals(multiply(feePercentage, 100), 2)}%`]; + rainbowFee.value = [feeDisplay, `${handleSignificantDecimals(multiply(feePercentage, 100), 2)}%`]; }, [nativeAsset?.value?.decimals, nativeAsset?.value?.price?.value, nativeCurrency, rainbowFee] ); @@ -471,7 +470,7 @@ export function ReviewPanel() { - + From f6d0bd411358a680c956421bdd39e8085a8bf2a9 Mon Sep 17 00:00:00 2001 From: Ben Goldberg Date: Tue, 13 Aug 2024 13:57:48 -0700 Subject: [PATCH 3/5] idk how this got deleted --- src/__swaps__/screens/Swap/components/ReviewPanel.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__swaps__/screens/Swap/components/ReviewPanel.tsx b/src/__swaps__/screens/Swap/components/ReviewPanel.tsx index a1e2b68e8e8..7f09f8d9c2e 100644 --- a/src/__swaps__/screens/Swap/components/ReviewPanel.tsx +++ b/src/__swaps__/screens/Swap/components/ReviewPanel.tsx @@ -470,7 +470,7 @@ export function ReviewPanel() { - + From ac84c83211f930e645c3bb22a47f4dbae52e41fc Mon Sep 17 00:00:00 2001 From: Ben Goldberg Date: Tue, 13 Aug 2024 13:58:15 -0700 Subject: [PATCH 4/5] cleanup --- src/__swaps__/screens/Swap/components/ReviewPanel.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/__swaps__/screens/Swap/components/ReviewPanel.tsx b/src/__swaps__/screens/Swap/components/ReviewPanel.tsx index 7f09f8d9c2e..ae3d9243882 100644 --- a/src/__swaps__/screens/Swap/components/ReviewPanel.tsx +++ b/src/__swaps__/screens/Swap/components/ReviewPanel.tsx @@ -6,10 +6,8 @@ import { ChainId, ChainNameDisplay } from '@/__swaps__/types/chains'; import { chainNameFromChainId } from '@/__swaps__/utils/chains'; import { useEstimatedTime } from '@/__swaps__/utils/meteorology'; import { - convertAmountToBalanceDisplay, convertRawAmountToBalance, convertRawAmountToBalanceWorklet, - convertRawAmountToNativeDisplay, handleSignificantDecimals, multiply, } from '@/__swaps__/utils/numbers'; From cf93350c0a440ace7c2709ea4cb385ed8d936da7 Mon Sep 17 00:00:00 2001 From: Ben Goldberg Date: Wed, 14 Aug 2024 12:48:46 -0700 Subject: [PATCH 5/5] fix bad rebase --- src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts b/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts index 3c3174cd3f9..03214c85123 100644 --- a/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts +++ b/src/__swaps__/screens/Swap/hooks/useSwapInputsController.ts @@ -421,7 +421,6 @@ export function useSwapInputsController({ lastTypedInput: lastTypedInputParam, outputAmount, outputAsset: internalSelectedOutputAsset.value, - currency: store.getState().settings.nativeCurrency, }); logger.debug(`[useSwapInputsController]: quote params`, {