@rango-dev/widget-embedded 0.36.1-next.12 → 0.36.1-next.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.d.ts.map +1 -1
  2. package/dist/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.d.ts.map +1 -1
  3. package/dist/components/Quotes/Quotes.d.ts.map +1 -1
  4. package/dist/constants/quote.d.ts +8 -0
  5. package/dist/constants/quote.d.ts.map +1 -1
  6. package/dist/containers/QuoteInfo/QuoteInfo.d.ts.map +1 -1
  7. package/dist/hooks/useConfirmSwap/useConfirmSwap.d.ts.map +1 -1
  8. package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts +6 -4
  9. package/dist/hooks/useConfirmSwap/useConfirmSwap.helpers.d.ts.map +1 -1
  10. package/dist/hooks/useSwapInput.d.ts.map +1 -1
  11. package/dist/index.js +2 -2
  12. package/dist/index.js.map +4 -4
  13. package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
  14. package/dist/store/quote.d.ts.map +1 -1
  15. package/dist/types/quote.d.ts +8 -17
  16. package/dist/types/quote.d.ts.map +1 -1
  17. package/dist/utils/quote.d.ts +5 -6
  18. package/dist/utils/quote.d.ts.map +1 -1
  19. package/dist/utils/swap.d.ts +4 -4
  20. package/dist/utils/swap.d.ts.map +1 -1
  21. package/dist/widget-embedded.build.json +1 -1
  22. package/package.json +2 -2
  23. package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +2 -19
  24. package/src/components/QuoteWarningsAndErrors/QuoteWarningsAndErrors.helpers.ts +42 -22
  25. package/src/components/Quotes/Quotes.tsx +2 -3
  26. package/src/constants/quote.ts +23 -0
  27. package/src/containers/QuoteInfo/QuoteInfo.tsx +3 -9
  28. package/src/hooks/useConfirmSwap/useConfirmSwap.helpers.ts +29 -66
  29. package/src/hooks/useConfirmSwap/useConfirmSwap.ts +10 -15
  30. package/src/hooks/useSwapInput.ts +2 -9
  31. package/src/pages/ConfirmSwapPage.tsx +7 -15
  32. package/src/store/quote.ts +3 -13
  33. package/src/types/quote.ts +10 -24
  34. package/src/utils/quote.ts +57 -45
  35. package/src/utils/swap.ts +55 -36
  36. package/dist/constants/warnings.d.ts +0 -3
  37. package/dist/constants/warnings.d.ts.map +0 -1
  38. package/src/constants/warnings.ts +0 -26
@@ -15,7 +15,6 @@ import type {
15
15
  MultiRouteSimulationResult,
16
16
  PreferenceType,
17
17
  RouteTag,
18
- Token,
19
18
  } from 'rango-sdk';
20
19
  import type { PendingSwap } from 'rango-types';
21
20
 
@@ -29,30 +28,19 @@ import { QuoteWarningType } from '../types';
29
28
 
30
29
  import { areEqual } from './common';
31
30
  import { createTokenHash, findBlockchain } from './meta';
31
+ import { numberToString } from './numbers';
32
32
  import {
33
- calcOutputUsdValue,
34
33
  checkSlippageWarnings,
35
34
  getMinRequiredSlippage,
36
35
  getPercentageChange,
37
36
  getTotalFeeInUsd,
37
+ getUsdInputFrom,
38
+ getUsdOutputFrom,
38
39
  hasHighValueLoss,
39
40
  hasProperSlippage,
41
+ isOutputAmountChangedExcessively,
40
42
  } from './swap';
41
43
 
42
- export function getQuoteToTokenUsdPrice(
43
- quote: SelectedQuote | null
44
- ): number | null | undefined {
45
- const swaps = quote?.swaps || [];
46
- return swaps[swaps.length - 1].to.usdPrice;
47
- }
48
-
49
- export function getQuoteFromTokenUsdPrice(
50
- quote: SelectedQuote | null
51
- ): number | null | undefined {
52
- const swaps = quote?.swaps || [];
53
- return swaps[0].from.usdPrice;
54
- }
55
-
56
44
  export function isNumberOfSwapsChanged(
57
45
  quoteA: SelectedQuote,
58
46
  quoteB: SelectedQuote
@@ -195,56 +183,73 @@ export function createRetryQuote(
195
183
  };
196
184
  }
197
185
 
198
- export function generateQuoteWarnings(
199
- quote: SelectedQuote,
200
- params: {
201
- fromToken: Token;
202
- toToken: Token;
203
- findToken: FindToken;
204
- userSlippage: number;
205
- }
206
- ): QuoteWarning | null {
207
- const { fromToken, toToken, findToken, userSlippage } = params;
208
- const inputUsdValue = calcOutputUsdValue(
209
- quote.requestAmount,
210
- getQuoteFromTokenUsdPrice(quote) || fromToken?.usdPrice
211
- );
212
- const outputUsdValue = calcOutputUsdValue(
213
- quote.outputAmount,
214
- getQuoteToTokenUsdPrice(quote) || toToken?.usdPrice
215
- );
216
-
217
- if (!!quote && inputUsdValue && outputUsdValue) {
186
+ export function generateQuoteWarnings(params: {
187
+ previousQuote?: SelectedQuote;
188
+ currentQuote: SelectedQuote;
189
+ findToken: FindToken;
190
+ userSlippage: number;
191
+ }): QuoteWarning | null {
192
+ const { previousQuote, currentQuote, findToken, userSlippage } = params;
193
+ const usdInput = getUsdInputFrom(currentQuote);
194
+ const usdOutput = getUsdOutputFrom(currentQuote);
195
+
196
+ if (!!currentQuote && usdInput && usdOutput) {
218
197
  const priceImpact = getPriceImpact(
219
- inputUsdValue.toString(),
220
- outputUsdValue.toString()
198
+ usdInput.toString(),
199
+ usdOutput.toString()
221
200
  );
222
201
  const highValueLoss =
223
- !!priceImpact && hasHighValueLoss(inputUsdValue, priceImpact);
202
+ !!priceImpact && hasHighValueLoss(usdInput, priceImpact);
224
203
 
225
204
  if (highValueLoss) {
226
- const totalFee = getTotalFeeInUsd(quote?.swaps, findToken);
205
+ const totalFee = getTotalFeeInUsd(currentQuote?.swaps, findToken);
227
206
  const warningLevel = getPriceImpactLevel(priceImpact);
228
207
  return {
229
208
  type: QuoteWarningType.HIGH_VALUE_LOSS,
230
- inputUsdValue,
231
- outputUsdValue,
209
+ inputUsdValue: usdInput,
210
+ outputUsdValue: usdOutput,
232
211
  priceImpact,
233
212
  totalFee,
234
213
  warningLevel,
235
214
  };
236
215
  }
237
- } else if (quote && (!inputUsdValue || !outputUsdValue)) {
216
+ }
217
+
218
+ if (
219
+ previousQuote &&
220
+ isOutputAmountChangedExcessively(previousQuote, currentQuote)
221
+ ) {
222
+ return {
223
+ type: QuoteWarningType.EXCESSIVE_OUTPUT_AMOUNT_CHANGE,
224
+ usdValueChange: numberToString(
225
+ getUsdOutputFrom(currentQuote)
226
+ ?.minus(getUsdOutputFrom(previousQuote) ?? 0)
227
+ .toString() ?? '0',
228
+ null,
229
+ 2
230
+ ),
231
+ percentageChange: numberToString(
232
+ getPriceImpact(
233
+ getUsdOutputFrom(previousQuote) ?? '1',
234
+ getUsdOutputFrom(currentQuote) ?? '1'
235
+ ),
236
+ null,
237
+ 2
238
+ ),
239
+ };
240
+ }
241
+
242
+ if (currentQuote && (!usdInput || !usdOutput)) {
238
243
  return { type: QuoteWarningType.UNKNOWN_PRICE };
239
244
  }
240
245
 
241
- const minRequiredSlippage = getMinRequiredSlippage(quote.swaps);
246
+ const minRequiredSlippage = getMinRequiredSlippage(currentQuote.swaps);
242
247
  const highSlippage = userSlippage > HIGH_SLIPPAGE;
243
248
 
244
249
  if (!hasProperSlippage(params.userSlippage.toString(), minRequiredSlippage)) {
245
250
  return {
246
251
  type: QuoteWarningType.INSUFFICIENT_SLIPPAGE,
247
- recommendedSlippages: checkSlippageWarnings(quote, userSlippage),
252
+ recommendedSlippages: checkSlippageWarnings(currentQuote, userSlippage),
248
253
  minRequiredSlippage: minRequiredSlippage,
249
254
  };
250
255
  } else if (
@@ -260,6 +265,13 @@ export function generateQuoteWarnings(
260
265
  return null;
261
266
  }
262
267
 
268
+ export function isQuoteWarningConfirmationRequired(warning: QuoteWarning) {
269
+ const WARNINGS_NOT_REQUIRING_CONFIRMATION = [
270
+ QuoteWarningType.EXCESSIVE_OUTPUT_AMOUNT_CHANGE,
271
+ ];
272
+ return !WARNINGS_NOT_REQUIRING_CONFIRMATION.includes(warning.type);
273
+ }
274
+
263
275
  export function getPriceImpact(
264
276
  inputUsdValue: BigNumber | string | null,
265
277
  outputUsdValue: BigNumber | string | null
package/src/utils/swap.ts CHANGED
@@ -1,5 +1,3 @@
1
- /* eslint-disable @typescript-eslint/no-magic-numbers */
2
- import type { FeesGroup, NameOfFees } from '../constants/quote';
3
1
  import type { FetchStatus, FindToken } from '../store/slices/data';
4
2
  import type { ConnectedWallet } from '../store/slices/wallets';
5
3
  import type {
@@ -34,6 +32,14 @@ import { PendingSwapNetworkStatus } from 'rango-types';
34
32
  import { errorMessages } from '../constants/errors';
35
33
  import { swapButtonTitles } from '../constants/messages';
36
34
  import { ZERO } from '../constants/numbers';
35
+ import {
36
+ type FeesGroup,
37
+ HIGH_FEE_THRESHOLD_USD,
38
+ HIGH_VALUE_LOSS_CRITERIA,
39
+ type NameOfFees,
40
+ OUTPUT_CHANGE_WARNING_CRITERIA,
41
+ PERCENT_MULTIPLIER,
42
+ } from '../constants/quote';
37
43
  import {
38
44
  BALANCE_MAX_DECIMALS,
39
45
  BALANCE_MIN_DECIMALS,
@@ -43,7 +49,7 @@ import {
43
49
 
44
50
  import { getBlockchainShortNameFor, isValidTokenAddress } from './meta';
45
51
  import { numberToString } from './numbers';
46
- import { getPriceImpact, getRequiredBalanceOfWallet } from './quote';
52
+ import { getRequiredBalanceOfWallet } from './quote';
47
53
  import { getQuoteWallets } from './wallets';
48
54
 
49
55
  export function getOutputRatio(
@@ -61,7 +67,7 @@ export function getOutputRatio(
61
67
  return outputUsdValue
62
68
  .div(inputUsdValue)
63
69
  .minus(1)
64
- .multipliedBy(100)
70
+ .multipliedBy(PERCENT_MULTIPLIER)
65
71
  .toNumber();
66
72
  }
67
73
 
@@ -69,12 +75,16 @@ export function hasHighValueLoss(
69
75
  inputUsdValue: BigNumber | null,
70
76
  priceImpact: number
71
77
  ): boolean {
72
- return (
73
- ((parseInt(priceImpact.toFixed(2) || '0') <= -10 &&
74
- inputUsdValue?.gte(new BigNumber(400))) ||
75
- (parseInt(priceImpact.toFixed(2) || '0') <= -5 &&
76
- inputUsdValue?.gte(new BigNumber(1000)))) ??
77
- false
78
+ if (!inputUsdValue) {
79
+ return false;
80
+ }
81
+
82
+ const formattedPriceImpact = parseInt(priceImpact.toFixed(2) || '0');
83
+
84
+ return HIGH_VALUE_LOSS_CRITERIA.some(
85
+ ({ threshold, minInput }) =>
86
+ formattedPriceImpact <= threshold &&
87
+ inputUsdValue.gte(new BigNumber(minInput))
78
88
  );
79
89
  }
80
90
 
@@ -328,7 +338,7 @@ export function hasHighFee(totalFeeInUsd: BigNumber | null) {
328
338
  if (!totalFeeInUsd) {
329
339
  return false;
330
340
  }
331
- return !totalFeeInUsd.lt(new BigNumber(30));
341
+ return !totalFeeInUsd.lt(new BigNumber(HIGH_FEE_THRESHOLD_USD));
332
342
  }
333
343
 
334
344
  export function hasSlippageError(
@@ -480,8 +490,7 @@ export function createQuoteRequestBody(params: {
480
490
  addresses: [wallet.address],
481
491
  });
482
492
  });
483
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
484
- // @ts-ignore
493
+
485
494
  const requestBody: BestRouteRequest = {
486
495
  amount: inputAmount.toString(),
487
496
  affiliateRef: affiliateRef ?? undefined,
@@ -535,33 +544,52 @@ export function getWalletsForNewSwap(selectedWallets: Wallet[]) {
535
544
  return wallets;
536
545
  }
537
546
 
538
- export function getQuoteOutputAmount(quote: SelectedQuote) {
539
- return quote?.outputAmount || null;
547
+ export function getUsdInputFrom(quote: SelectedQuote): BigNumber | undefined {
548
+ const inputAmount = quote.requestAmount;
549
+ const inputTokenUsdPrice = quote.swaps[0].from.usdPrice;
550
+ if (!inputAmount || !inputTokenUsdPrice) {
551
+ return;
552
+ }
553
+ return new BigNumber(inputAmount).multipliedBy(inputTokenUsdPrice);
554
+ }
555
+
556
+ export function getUsdOutputFrom(quote: SelectedQuote): BigNumber | undefined {
557
+ const outputAmount = quote?.outputAmount || null;
558
+ const outputTokenUsdPrice = quote.swaps[quote.swaps.length - 1].to.usdPrice;
559
+ if (!outputAmount || !outputTokenUsdPrice) {
560
+ return;
561
+ }
562
+ return new BigNumber(outputAmount).multipliedBy(outputTokenUsdPrice);
540
563
  }
541
564
 
542
565
  export function getPercentageChange(input: string, output: string) {
543
566
  return new BigNumber(output)
544
567
  .div(new BigNumber(input))
545
568
  .minus(1)
546
- .multipliedBy(100)
569
+ .multipliedBy(PERCENT_MULTIPLIER)
547
570
  .toNumber();
548
571
  }
549
572
 
550
- export function isOutputAmountChangedALot(
551
- oldQuote: SelectedQuote,
552
- newQuote: SelectedQuote
573
+ export function isOutputAmountChangedExcessively(
574
+ previousQuote: SelectedQuote,
575
+ currentQuote: SelectedQuote
553
576
  ) {
554
- const oldOutputAmount = getQuoteOutputAmount(oldQuote);
555
- const newOutputAmount = getQuoteOutputAmount(newQuote);
556
- if (!oldOutputAmount || !newOutputAmount) {
577
+ const usdInput = getUsdInputFrom(previousQuote);
578
+ const previousUsdOutput = getUsdOutputFrom(previousQuote);
579
+ const currentUsdOutput = getUsdOutputFrom(currentQuote);
580
+ if (!usdInput || !previousUsdOutput || !currentUsdOutput) {
557
581
  return false;
558
582
  }
559
- const percentageChange = getPriceImpact(oldOutputAmount, newOutputAmount);
560
- if (!percentageChange) {
561
- return true;
562
- }
563
583
 
564
- return percentageChange <= -1;
584
+ const percentageChange = getPercentageChange(
585
+ previousUsdOutput.toString(),
586
+ currentUsdOutput.toString()
587
+ );
588
+
589
+ return OUTPUT_CHANGE_WARNING_CRITERIA.some(
590
+ ({ threshold, minInput }) =>
591
+ percentageChange <= threshold && usdInput.isGreaterThanOrEqualTo(minInput)
592
+ );
565
593
  }
566
594
 
567
595
  export function generateBalanceWarnings(
@@ -623,15 +651,6 @@ export function generateBalanceWarnings(
623
651
  });
624
652
  }
625
653
 
626
- export function calcOutputUsdValue(
627
- outputAmount?: string,
628
- tokenPrice?: number | null
629
- ) {
630
- const amount = !!outputAmount ? new BigNumber(outputAmount) : ZERO;
631
-
632
- return amount.multipliedBy(tokenPrice || 0);
633
- }
634
-
635
654
  export function isNetworkStatusInWarningState(
636
655
  pendingSwapStep: PendingSwapStep | null
637
656
  ): boolean {
@@ -1,3 +0,0 @@
1
- import type { QuoteUpdateWarning } from '../types';
2
- export declare function getQuoteUpdateWarningMessage(warning: QuoteUpdateWarning): string;
3
- //# sourceMappingURL=warnings.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"warnings.d.ts","sourceRoot":"","sources":["../../src/constants/warnings.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAMnD,wBAAgB,4BAA4B,CAAC,OAAO,EAAE,kBAAkB,UAmBvE"}
@@ -1,26 +0,0 @@
1
- import type { QuoteUpdateWarning } from '../types';
2
-
3
- import { i18n } from '@lingui/core';
4
-
5
- import { QuoteUpdateType } from '../types';
6
-
7
- export function getQuoteUpdateWarningMessage(warning: QuoteUpdateWarning) {
8
- switch (warning.type) {
9
- case QuoteUpdateType.QUOTE_UPDATED:
10
- return i18n.t('Route has been updated.');
11
- case QuoteUpdateType.QUOTE_AND_OUTPUT_AMOUNT_UPDATED:
12
- return i18n.t(
13
- 'Output amount changed to {newOutputAmount} ({percentageChange}% change).',
14
- {
15
- newOutputAmount: warning.newOutputAmount,
16
- percentageChange: warning.percentageChange,
17
- }
18
- );
19
- case QuoteUpdateType.QUOTE_SWAPPERS_UPDATED:
20
- return i18n.t('Route swappers has been updated.');
21
- case QuoteUpdateType.QUOTE_COINS_UPDATED:
22
- return i18n.t('Route internal coins has been updated.');
23
- default:
24
- return '';
25
- }
26
- }