Skip to content

Commit bcdc0de

Browse files
committed
chore: move isEstimatedReturnLow logic to util file
1 parent 6c2cc08 commit bcdc0de

File tree

2 files changed

+55
-46
lines changed

2 files changed

+55
-46
lines changed

ui/ducks/swaps/swaps.js

+50
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import {
8686
SWAPS_FETCH_ORDER_CONFLICT,
8787
ALLOWED_SMART_TRANSACTIONS_CHAIN_IDS,
8888
Slippage,
89+
SWAPS_QUOTE_MAX_RETURN_DIFFERENCE_PERCENTAGE,
8990
} from '../../../shared/constants/swaps';
9091
import {
9192
IN_PROGRESS_TRANSACTION_STATUSES,
@@ -100,6 +101,7 @@ import {
100101
import { EtherDenomination } from '../../../shared/constants/common';
101102
import { Numeric } from '../../../shared/modules/Numeric';
102103
import { calculateMaxGasLimit } from '../../../shared/lib/swaps-utils';
104+
import { useTokenFiatAmount } from '../../hooks/useTokenFiatAmount';
103105

104106
const debugLog = createProjectLogger('swaps');
105107

@@ -1435,3 +1437,51 @@ export function cancelSwapsSmartTransaction(uuid) {
14351437
}
14361438
};
14371439
}
1440+
1441+
export const getIsEstimatedReturnLow = ({ usedQuote, rawNetworkFees }) => {
1442+
const sourceTokenAmount = calcTokenAmount(
1443+
usedQuote?.sourceAmount,
1444+
usedQuote?.sourceTokenInfo?.decimals,
1445+
);
1446+
const sourceTokenFiatAmount = useTokenFiatAmount(
1447+
usedQuote?.sourceTokenInfo?.address,
1448+
sourceTokenAmount || 0,
1449+
usedQuote?.sourceTokenInfo?.symbol,
1450+
{
1451+
showFiat: true,
1452+
},
1453+
true,
1454+
null,
1455+
false,
1456+
);
1457+
const destinationTokenAmount = calcTokenAmount(
1458+
usedQuote?.destinationAmount,
1459+
usedQuote?.destinationTokenInfo?.decimals,
1460+
);
1461+
const destinationTokenFiatAmount = useTokenFiatAmount(
1462+
usedQuote?.destinationTokenInfo?.address,
1463+
destinationTokenAmount || 0,
1464+
usedQuote?.destinationTokenInfo?.symbol,
1465+
{
1466+
showFiat: true,
1467+
},
1468+
true,
1469+
null,
1470+
false,
1471+
);
1472+
const adjustedReturnValue =
1473+
destinationTokenFiatAmount && rawNetworkFees
1474+
? new BigNumber(destinationTokenFiatAmount).minus(
1475+
new BigNumber(rawNetworkFees),
1476+
)
1477+
: null;
1478+
const isEstimatedReturnLow =
1479+
sourceTokenFiatAmount && adjustedReturnValue
1480+
? adjustedReturnValue.lt(
1481+
new BigNumber(sourceTokenFiatAmount).times(
1482+
1 - SWAPS_QUOTE_MAX_RETURN_DIFFERENCE_PERCENTAGE,
1483+
),
1484+
)
1485+
: false;
1486+
return isEstimatedReturnLow;
1487+
};

ui/pages/swaps/prepare-swap-page/review-quote.js

+5-46
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
fetchSwapsSmartTransactionFees,
4545
getSmartTransactionFees,
4646
getCurrentSmartTransactionsEnabled,
47+
getIsEstimatedReturnLow,
4748
} from '../../../ducks/swaps/swaps';
4849
import { getCurrentChainId } from '../../../../shared/modules/selectors/networks';
4950
import {
@@ -97,7 +98,6 @@ import {
9798
SLIPPAGE_HIGH_ERROR,
9899
SLIPPAGE_LOW_ERROR,
99100
MAX_ALLOWED_SLIPPAGE,
100-
SWAPS_QUOTE_MAX_RETURN_DIFFERENCE_PERCENTAGE,
101101
} from '../../../../shared/constants/swaps';
102102
import { GasRecommendations } from '../../../../shared/constants/gas';
103103
import CountdownTimer from '../countdown-timer';
@@ -149,7 +149,6 @@ import { getTokenFiatAmount } from '../../../helpers/utils/token-util';
149149
import { toChecksumHexAddress } from '../../../../shared/modules/hexstring-utils';
150150
import { useAsyncResult } from '../../../hooks/useAsyncResult';
151151
import { useGasFeeEstimates } from '../../../hooks/useGasFeeEstimates';
152-
import { useTokenFiatAmount } from '../../../hooks/useTokenFiatAmount';
153152
import ViewQuotePriceDifference from './view-quote-price-difference';
154153
import SlippageNotificationModal from './slippage-notification-modal';
155154

@@ -1128,50 +1127,10 @@ export default function ReviewQuote({
11281127
currentCurrency,
11291128
]);
11301129

1131-
const sourceTokenAmount = calcTokenAmount(
1132-
usedQuote?.sourceAmount,
1133-
usedQuote?.sourceTokenInfo?.decimals,
1134-
);
1135-
const sourceTokenFiatAmount = useTokenFiatAmount(
1136-
usedQuote?.sourceTokenInfo?.address,
1137-
sourceTokenAmount || 0,
1138-
usedQuote?.sourceTokenInfo?.symbol,
1139-
{
1140-
showFiat: true,
1141-
},
1142-
true,
1143-
null,
1144-
false,
1145-
);
1146-
const destinationTokenAmount = calcTokenAmount(
1147-
usedQuote?.destinationAmount,
1148-
usedQuote?.destinationTokenInfo?.decimals,
1149-
);
1150-
const destinationTokenFiatAmount = useTokenFiatAmount(
1151-
usedQuote?.destinationTokenInfo?.address,
1152-
destinationTokenAmount || 0,
1153-
usedQuote?.destinationTokenInfo?.symbol,
1154-
{
1155-
showFiat: true,
1156-
},
1157-
true,
1158-
null,
1159-
false,
1160-
);
1161-
const adjustedReturnValue =
1162-
destinationTokenFiatAmount && rawNetworkFees
1163-
? new BigNumber(destinationTokenFiatAmount).minus(
1164-
new BigNumber(rawNetworkFees),
1165-
)
1166-
: null;
1167-
const isEstimatedReturnLow =
1168-
sourceTokenFiatAmount && adjustedReturnValue
1169-
? adjustedReturnValue.lt(
1170-
new BigNumber(sourceTokenFiatAmount).times(
1171-
1 - SWAPS_QUOTE_MAX_RETURN_DIFFERENCE_PERCENTAGE,
1172-
),
1173-
)
1174-
: false;
1130+
const isEstimatedReturnLow = getIsEstimatedReturnLow({
1131+
usedQuote,
1132+
rawNetworkFees,
1133+
});
11751134
setIsEstimatedReturnLow(isEstimatedReturnLow);
11761135

11771136
return (

0 commit comments

Comments
 (0)