diff --git a/src/ui/views/Swap/Component/QuoteItem.tsx b/src/ui/views/Swap/Component/QuoteItem.tsx index 147915ac7fd..3be7b4be678 100644 --- a/src/ui/views/Swap/Component/QuoteItem.tsx +++ b/src/ui/views/Swap/Component/QuoteItem.tsx @@ -1,4 +1,4 @@ -import { formatAmount, formatUsdValue } from '@/ui/utils'; +import { formatAmount, formatUsdValue, isSameAddress } from '@/ui/utils'; import { CHAINS_ENUM } from '@debank/common'; import { TokenItem } from '@rabby-wallet/rabby-api/dist/types'; import { QuoteResult } from '@rabby-wallet/rabby-swap/dist/quote'; @@ -176,13 +176,16 @@ export const DexQuoteItem = ( let disable = false; let receivedTokenUsd: React.ReactNode = null; let diffUsd: React.ReactNode = null; - + const balanceChangeReceiveTokenAmount = preExecResult + ? preExecResult.swapPreExecTx.balance_change.receive_token_list.find( + (item) => isSameAddress(item.id, receiveToken.id) + )?.amount || '0' + : '0'; const actualReceiveAmount = inSufficient ? new BigNumber(quote?.toTokenAmount || 0) .div(10 ** (quote?.toTokenDecimals || receiveToken.decimals)) .toString() - : preExecResult?.swapPreExecTx.balance_change.receive_token_list[0] - ?.amount; + : balanceChangeReceiveTokenAmount; if (actualReceiveAmount || dexId === 'WrapToken') { const receiveAmount = actualReceiveAmount || (dexId === 'WrapToken' ? payAmount : 0); @@ -273,6 +276,7 @@ export const DexQuoteItem = ( disable, receivedTokenUsd, diffUsd, + receiveToken, ]; }, [ quote?.toTokenAmount, @@ -313,6 +317,10 @@ export const DexQuoteItem = ( return; } if (disabled) return; + const actualReceiveAmount = + preExecResult?.swapPreExecTx.balance_change.receive_token_list.find( + (item) => isSameAddress(item.id, receiveToken.id) + )?.amount || 0; setActiveProvider?.({ manualClick: true, name: dexId, @@ -323,9 +331,7 @@ export const DexQuoteItem = ( error: !preExecResult, halfBetterRate: halfBetterRateString, quoteWarning: undefined, - actualReceiveAmount: - preExecResult?.swapPreExecTx.balance_change.receive_token_list[0] - ?.amount || '', + actualReceiveAmount, gasUsd: preExecResult?.gasUsd, preExecResult: preExecResult, }); @@ -339,6 +345,7 @@ export const DexQuoteItem = ( quote, preExecResult, gasFeeTooHight, + receiveToken, ]); const isWrapToken = useMemo( diff --git a/src/ui/views/Swap/Component/Quotes.tsx b/src/ui/views/Swap/Component/Quotes.tsx index 0f3dfde4b8d..6d6690b93c6 100644 --- a/src/ui/views/Swap/Component/Quotes.tsx +++ b/src/ui/views/Swap/Component/Quotes.tsx @@ -12,6 +12,7 @@ import { getTokenSymbol } from '@/ui/utils/token'; import { ReactComponent as RcIconHiddenArrow } from '@/ui/assets/swap/hidden-quote-arrow.svg'; import clsx from 'clsx'; import { useRabbySelector } from '@/ui/store'; +import { isSameAddress } from '@/ui/utils'; interface QuotesProps extends Omit< @@ -43,7 +44,7 @@ export const Quotes = ({ () => [ ...(list?.sort((a, b) => { const getNumber = (quote: typeof a) => { - const price = other.receiveToken.price ? other.receiveToken.price : 1; + const price = other.receiveToken.price ? other.receiveToken.price : 0; if (inSufficient) { return new BigNumber(quote.data?.toTokenAmount || 0) .div( @@ -55,35 +56,31 @@ export const Quotes = ({ if (!quote.preExecResult) { return new BigNumber(Number.MIN_SAFE_INTEGER); } - + const receiveTokenAmount = + quote?.preExecResult.swapPreExecTx.balance_change.receive_token_list.find( + (item) => isSameAddress(item.id, other.receiveToken.id) + )?.amount || 0; if (sortIncludeGasFee) { - return new BigNumber( - quote?.preExecResult.swapPreExecTx.balance_change - .receive_token_list?.[0]?.amount || 0 - ) + return new BigNumber(receiveTokenAmount) .times(price) .minus(quote?.preExecResult?.gasUsdValue || 0); } - return new BigNumber( - quote?.preExecResult.swapPreExecTx.balance_change - .receive_token_list?.[0]?.amount || 0 - ).times(price); + return new BigNumber(receiveTokenAmount).times(price); }; return getNumber(b).minus(getNumber(a)).toNumber(); }) || []), ], - [ - inSufficient, - list, - other.receiveToken.decimals, - other?.receiveToken?.price, - sortIncludeGasFee, - ] + [inSufficient, list, other.receiveToken, sortIncludeGasFee] ); const [bestQuoteAmount, bestQuoteGasUsd] = useMemo(() => { const bestQuote = sortedList?.[0]; + const receiveTokenAmount = bestQuote?.preExecResult + ? bestQuote.preExecResult.swapPreExecTx.balance_change.receive_token_list.find( + (item) => isSameAddress(item.id, other.receiveToken.id) + )?.amount || 0 + : 0; return [ inSufficient @@ -95,11 +92,10 @@ export const Quotes = ({ 1) ) .toString(10) - : bestQuote?.preExecResult?.swapPreExecTx.balance_change - .receive_token_list[0]?.amount, + : receiveTokenAmount, bestQuote?.isDex ? bestQuote.preExecResult?.gasUsdValue || '0' : '0', ]; - }, [inSufficient, other?.receiveToken?.decimals, sortedList]); + }, [inSufficient, other?.receiveToken, sortedList]); const fetchedList = useMemo(() => list?.map((e) => e.name) || [], [list]); const [hiddenError, setHiddenError] = useState(true); @@ -120,8 +116,9 @@ export const Quotes = ({ name={dex?.name} isBestQuote bestQuoteAmount={`${ - dex?.preExecResult?.swapPreExecTx.balance_change - .receive_token_list[0]?.amount || '0' + dex?.preExecResult?.swapPreExecTx.balance_change.receive_token_list.find( + (token) => isSameAddress(token.id, other.receiveToken.id) + )?.amount || '0' }`} bestQuoteGasUsd={bestQuoteGasUsd} isLoading={dex.loading} diff --git a/src/ui/views/Swap/hooks/quote.tsx b/src/ui/views/Swap/hooks/quote.tsx index 2b41f6316cc..08ddb89b90a 100644 --- a/src/ui/views/Swap/hooks/quote.tsx +++ b/src/ui/views/Swap/hooks/quote.tsx @@ -535,34 +535,6 @@ interface getPreExecResultParams quote: QuoteResult; } -export const halfBetterRate = ( - full: ExplainTxResponse, - half: ExplainTxResponse -) => { - if ( - full.balance_change.success && - half.balance_change.success && - half.balance_change.receive_token_list[0]?.amount && - full.balance_change.receive_token_list[0]?.amount - ) { - const halfReceive = new BigNumber( - half.balance_change.receive_token_list[0].amount - ); - - const fullREceive = new BigNumber( - full.balance_change.receive_token_list[0]?.amount - ); - const diff = new BigNumber(halfReceive).times(2).minus(fullREceive); - - return diff.gt(0) - ? new BigNumber(diff.div(fullREceive).toPrecision(1)) - .times(100) - .toString(10) - : null; - } - return null; -}; - export type QuotePreExecResultInfo = { shouldApproveToken: boolean; shouldTwoStepApprove: boolean; diff --git a/src/ui/views/Swap/hooks/token.tsx b/src/ui/views/Swap/hooks/token.tsx index 785d3c0f8c2..e155ba07e4b 100644 --- a/src/ui/views/Swap/hooks/token.tsx +++ b/src/ui/views/Swap/hooks/token.tsx @@ -437,20 +437,18 @@ export const useTokenPair = (userAddress: string) => { if (!quote.preExecResult || !quote.preExecResult.isSdkPass) { return new BigNumber(Number.MIN_SAFE_INTEGER); } + const balanceChangeReceiveTokenAmount = + quote?.preExecResult.swapPreExecTx.balance_change.receive_token_list.find( + (token) => isSameAddress(token.id, receiveToken.id) + )?.amount || 0; if (sortIncludeGasFee) { - return new BigNumber( - quote?.preExecResult.swapPreExecTx.balance_change - .receive_token_list?.[0]?.amount || 0 - ) + return new BigNumber(balanceChangeReceiveTokenAmount) .times(price) .minus(quote?.preExecResult?.gasUsdValue || 0); } - return new BigNumber( - quote?.preExecResult.swapPreExecTx.balance_change - .receive_token_list?.[0]?.amount || 0 - ).times(price); + return new BigNumber(balanceChangeReceiveTokenAmount).times(price); }; return getNumber(b).minus(getNumber(a)).toNumber(); }) || []), @@ -478,8 +476,9 @@ export const useTokenPair = (userAddress: string) => { halfBetterRate: '', quoteWarning: undefined, actualReceiveAmount: - preExecResult?.swapPreExecTx.balance_change - .receive_token_list[0]?.amount || '', + preExecResult?.swapPreExecTx.balance_change.receive_token_list.find( + (token) => isSameAddress(token.id, receiveToken.id) + )?.amount || '', gasUsd: preExecResult?.gasUsd, } );