Skip to content

Commit

Permalink
Merge branch 'fix/swap-actual-receive-amount' into tmp/20240731
Browse files Browse the repository at this point in the history
  • Loading branch information
vvvvvv1vvvvvv committed Jul 31, 2024
2 parents b577b8c + f52a5ea commit 417abc3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 67 deletions.
21 changes: 14 additions & 7 deletions src/ui/views/Swap/Component/QuoteItem.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -273,6 +276,7 @@ export const DexQuoteItem = (
disable,
receivedTokenUsd,
diffUsd,
receiveToken,
];
}, [
quote?.toTokenAmount,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
});
Expand All @@ -339,6 +345,7 @@ export const DexQuoteItem = (
quote,
preExecResult,
gasFeeTooHight,
receiveToken,
]);

const isWrapToken = useMemo(
Expand Down
41 changes: 19 additions & 22 deletions src/ui/views/Swap/Component/Quotes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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}
Expand Down
28 changes: 0 additions & 28 deletions src/ui/views/Swap/hooks/quote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 9 additions & 10 deletions src/ui/views/Swap/hooks/token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}) || []),
Expand Down Expand Up @@ -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,
}
);
Expand Down

0 comments on commit 417abc3

Please sign in to comment.