@rango-dev/widget-embedded 0.1.10-next.92 → 0.1.10-next.94

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 (80) hide show
  1. package/dist/QueueManager.d.ts.map +1 -1
  2. package/dist/components/AppRoutes.d.ts +5 -1
  3. package/dist/components/AppRoutes.d.ts.map +1 -1
  4. package/dist/components/ConfirmSwapErrors.d.ts +3 -2
  5. package/dist/components/ConfirmSwapErrors.d.ts.map +1 -1
  6. package/dist/components/ConfirmSwapWarnings.d.ts +3 -2
  7. package/dist/components/ConfirmSwapWarnings.d.ts.map +1 -1
  8. package/dist/components/Layout.d.ts +1 -1
  9. package/dist/components/Layout.d.ts.map +1 -1
  10. package/dist/components/SwitchFromAndTo.d.ts +1 -1
  11. package/dist/components/SwitchFromAndTo.d.ts.map +1 -1
  12. package/dist/components/UpdateUrl.d.ts +1 -1
  13. package/dist/components/UpdateUrl.d.ts.map +1 -1
  14. package/dist/components/warnings/BalanceWarnings.d.ts.map +1 -1
  15. package/dist/components/warnings/ConfirmSwapExtraMessages.d.ts.map +1 -1
  16. package/dist/hooks/useConfirmSwap.d.ts +11 -0
  17. package/dist/hooks/useConfirmSwap.d.ts.map +1 -0
  18. package/dist/hooks/usePrevious.d.ts +1 -1
  19. package/dist/hooks/usePrevious.d.ts.map +1 -1
  20. package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
  21. package/dist/pages/Home.d.ts.map +1 -1
  22. package/dist/pages/SelectChainPage.d.ts.map +1 -1
  23. package/dist/pages/SelectTokenPage.d.ts.map +1 -1
  24. package/dist/pages/SettingsPage.d.ts.map +1 -1
  25. package/dist/pages/WalletsPage.d.ts.map +1 -1
  26. package/dist/store/bestRoute.d.ts +25 -0
  27. package/dist/store/bestRoute.d.ts.map +1 -1
  28. package/dist/store/settings.d.ts +15 -0
  29. package/dist/store/settings.d.ts.map +1 -1
  30. package/dist/store/wallets.d.ts.map +1 -1
  31. package/dist/types/index.d.ts +1 -0
  32. package/dist/types/index.d.ts.map +1 -1
  33. package/dist/{store/confirmSwap.d.ts → types/routing.d.ts} +14 -5
  34. package/dist/types/routing.d.ts.map +1 -0
  35. package/dist/utils/numbers.d.ts +1 -1
  36. package/dist/utils/numbers.d.ts.map +1 -1
  37. package/dist/utils/routing.d.ts +11 -5
  38. package/dist/utils/routing.d.ts.map +1 -1
  39. package/dist/utils/swap.d.ts +4 -3
  40. package/dist/utils/swap.d.ts.map +1 -1
  41. package/dist/utils/wallets.d.ts +7 -2
  42. package/dist/utils/wallets.d.ts.map +1 -1
  43. package/dist/widget-embedded.cjs.development.js +581 -492
  44. package/dist/widget-embedded.cjs.development.js.map +1 -1
  45. package/dist/widget-embedded.cjs.production.min.js +581 -492
  46. package/dist/widget-embedded.cjs.production.min.js.map +1 -1
  47. package/dist/widget-embedded.esm.js +582 -493
  48. package/dist/widget-embedded.esm.js.map +1 -1
  49. package/package.json +1 -1
  50. package/src/QueueManager.tsx +1 -0
  51. package/src/components/AppRoutes.tsx +54 -21
  52. package/src/components/ChangeSlippageButton.tsx +1 -1
  53. package/src/components/ConfirmSwapErrors.tsx +1 -1
  54. package/src/components/ConfirmSwapWarnings.tsx +1 -4
  55. package/src/components/Header.tsx +1 -1
  56. package/src/components/Layout.tsx +10 -11
  57. package/src/components/UpdateUrl.tsx +13 -22
  58. package/src/components/warnings/BalanceWarnings.tsx +27 -4
  59. package/src/components/warnings/ConfirmSwapExtraMessages.tsx +7 -1
  60. package/src/components/warnings/HighSlippageWarning.tsx +1 -1
  61. package/src/components/warnings/MinRequiredSlippage.tsx +2 -2
  62. package/src/hooks/useConfirmSwap.ts +260 -0
  63. package/src/pages/ConfirmSwapPage.tsx +17 -8
  64. package/src/pages/ConfirmWalletsPage.tsx +1 -1
  65. package/src/pages/Home.tsx +0 -1
  66. package/src/pages/SelectChainPage.tsx +2 -7
  67. package/src/pages/SelectTokenPage.tsx +22 -57
  68. package/src/pages/SettingsPage.tsx +8 -9
  69. package/src/pages/WalletsPage.tsx +16 -6
  70. package/src/store/bestRoute.ts +82 -37
  71. package/src/store/settings.ts +2 -2
  72. package/src/store/wallets.ts +26 -0
  73. package/src/types/index.ts +1 -0
  74. package/src/types/routing.ts +60 -0
  75. package/src/utils/numbers.ts +38 -17
  76. package/src/utils/routing.ts +115 -24
  77. package/src/utils/swap.ts +19 -11
  78. package/src/utils/wallets.ts +117 -73
  79. package/dist/store/confirmSwap.d.ts.map +0 -1
  80. package/src/store/confirmSwap.ts +0 -342
@@ -1,7 +1,8 @@
1
1
  import { BigNumber } from 'bignumber.js';
2
2
  import { BestRouteResponse } from 'rango-sdk';
3
3
 
4
- export const percentToString = (p: number, fractions = 0): string => (p * 100).toFixed(fractions);
4
+ export const percentToString = (p: number, fractions = 0): string =>
5
+ (p * 100).toFixed(fractions);
5
6
  export const secondsToString = (s: number): string => {
6
7
  const seconds = (s % 60).toString().padStart(2, '0');
7
8
  const minutes = parseInt((s / 60).toString())
@@ -13,7 +14,7 @@ export const secondsToString = (s: number): string => {
13
14
  export const numberToString = (
14
15
  number: BigNumber | string | null,
15
16
  minDecimals: number | null = null,
16
- maxDecimals: number | null = null,
17
+ maxDecimals: number | null = null
17
18
  ): string => {
18
19
  if (number === null) return '';
19
20
  if (number === '') return '';
@@ -30,39 +31,57 @@ export const numberToString = (
30
31
  if (n.gte(10000)) return n.toFormat(0, roundingMode);
31
32
  if (n.gte(1000))
32
33
  return n.toFormat(
33
- Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 1))),
34
- roundingMode,
34
+ Math.min(
35
+ maxI,
36
+ Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 1))
37
+ ),
38
+ roundingMode
35
39
  );
36
40
  if (n.gte(100))
37
41
  return n.toFormat(
38
- Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 1))),
39
- roundingMode,
42
+ Math.min(
43
+ maxI,
44
+ Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 1))
45
+ ),
46
+ roundingMode
40
47
  );
41
48
  if (n.gte(1))
42
49
  return n.toFormat(
43
- Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 2))),
44
- roundingMode,
50
+ Math.min(
51
+ maxI,
52
+ Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 2))
53
+ ),
54
+ roundingMode
45
55
  );
46
56
  if (n.gte(0.01))
47
57
  return n.toFormat(
48
- Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 4))),
49
- roundingMode,
58
+ Math.min(
59
+ maxI,
60
+ Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 4))
61
+ ),
62
+ roundingMode
50
63
  );
51
64
  for (let i = minDecimals || 4; i < 17; i++)
52
65
  if (n.gte(Math.pow(10, -i)))
53
66
  return n.toFormat(
54
- Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, i))),
55
- roundingMode,
67
+ Math.min(
68
+ maxI,
69
+ Math.min(maxDecimals || 100, Math.max(minDecimals || 0, i))
70
+ ),
71
+ roundingMode
56
72
  );
57
73
  if (n.isEqualTo(0)) return '0';
58
74
 
59
75
  return n.toFormat(
60
76
  Math.min(maxI, Math.min(maxDecimals || 100, Math.max(minDecimals || 0, 8))),
61
- roundingMode,
77
+ roundingMode
62
78
  );
63
79
  };
64
80
 
65
- export const convertBigNumberToHex = (value: BigNumber, decimals: number): string => {
81
+ export const convertBigNumberToHex = (
82
+ value: BigNumber,
83
+ decimals: number
84
+ ): string => {
66
85
  return '0x' + value.shiftedBy(decimals).toString(16);
67
86
  };
68
87
 
@@ -100,13 +119,15 @@ export const totalArrivalTime = (data: BestRouteResponse | null) =>
100
119
 
101
120
  export const rawFees = (data: BestRouteResponse): string =>
102
121
  (
103
- data?.result?.swaps?.flatMap((s) => s.fee.map((f) => ({ swapperId: s.swapperId, fee: f }))) ||
104
- []
122
+ data?.result?.swaps?.flatMap((s) =>
123
+ s.fee.map((f) => ({ swapperId: s.swapperId, fee: f }))
124
+ ) || []
105
125
  )
106
126
  .reduce((partialSum, a) => partialSum + parseFloat(a.fee.amount), 0)
107
127
  .toFixed(3);
108
128
 
109
- export const decimalNumber = (number = '0', toFixed: number) => parseFloat(number).toFixed(toFixed);
129
+ export const decimalNumber = (number = '0', toFixed: number) =>
130
+ parseFloat(number).toFixed(toFixed);
110
131
 
111
132
  export const containsText = (text: string, searchText: string) =>
112
133
  text.toLowerCase().indexOf(searchText.toLowerCase()) > -1;
@@ -3,57 +3,148 @@ import {
3
3
  SimulationValidationStatus,
4
4
  } from '@rango-dev/ui/dist/types/swaps';
5
5
  import BigNumber from 'bignumber.js';
6
- import { BestRouteResponse } from 'rango-sdk';
6
+ import { BestRouteResponse, BlockchainMeta, Token } from 'rango-sdk';
7
7
  import { areEqual } from './common';
8
8
  import { SelectedWallet } from './wallets';
9
+ import { BestRouteParams } from '../types';
10
+ import { TokenMeta } from '@rango-dev/ui/dist/types/meta';
11
+ import { numberToString } from './numbers';
12
+ import { getUsdFeeOfStep } from './swap';
13
+ import { BestRouteWithFee } from '@rango-dev/ui';
9
14
 
10
- export const getBestRouteToTokenUsdPrice = (
11
- bestRoute: BestRouteResponse | null,
12
- ): number | null | undefined =>
13
- bestRoute?.result?.swaps[bestRoute?.result?.swaps.length - 1].to.usdPrice;
15
+ export function searchParamsToToken(
16
+ tokens: Token[],
17
+ searchParams: string | null,
18
+ chain: BlockchainMeta | null
19
+ ): Token | null {
20
+ if (!chain) return null;
21
+ return (
22
+ tokens.find((token) => {
23
+ const symbolAndAddress = searchParams?.split('--');
24
+ if (symbolAndAddress?.length === 1)
25
+ return (
26
+ token.symbol === symbolAndAddress[0] &&
27
+ token.address === null &&
28
+ token.blockchain === chain.name
29
+ );
30
+ return (
31
+ token.symbol === symbolAndAddress?.[0] &&
32
+ token.address === symbolAndAddress?.[1] &&
33
+ token.blockchain === chain.name
34
+ );
35
+ }) || null
36
+ );
37
+ }
38
+
39
+ export function getBestRouteToTokenUsdPrice(
40
+ bestRoute: BestRouteResponse | null
41
+ ): number | null | undefined {
42
+ return bestRoute?.result?.swaps[bestRoute?.result?.swaps.length - 1].to
43
+ .usdPrice;
44
+ }
14
45
 
15
- export function isNumberOfSwapsChanged(route1: BestRouteResponse, route2: BestRouteResponse) {
46
+ export function isNumberOfSwapsChanged(
47
+ route1: BestRouteResponse,
48
+ route2: BestRouteResponse
49
+ ) {
16
50
  const route1Swaps = route1.result?.swaps || [];
17
51
  const route2Swaps = route2.result?.swaps || [];
18
52
  return route1Swaps.length !== route2Swaps.length;
19
53
  }
20
54
 
21
- export function isRouteSwappersUpdated(route1: BestRouteResponse, route2: BestRouteResponse) {
22
- const route1Swappers = route1.result?.swaps.map((swap) => swap.swapperId) || [];
23
- const route2Swappers = route2.result?.swaps.map((swap) => swap.swapperId) || [];
55
+ export function isRouteSwappersUpdated(
56
+ route1: BestRouteResponse,
57
+ route2: BestRouteResponse
58
+ ) {
59
+ const route1Swappers =
60
+ route1.result?.swaps.map((swap) => swap.swapperId) || [];
61
+ const route2Swappers =
62
+ route2.result?.swaps.map((swap) => swap.swapperId) || [];
24
63
  return !areEqual(route1Swappers, route2Swappers);
25
64
  }
26
65
 
27
- export function isRouteInternalCoinsUpdated(route1: BestRouteResponse, route2: BestRouteResponse) {
28
- const route1InternalCoins = route1.result?.swaps.map((swap) => swap.to.symbol) || [];
29
- const route2InternalCoins = route2.result?.swaps.map((swap) => swap.to.symbol) || [];
66
+ export function isRouteInternalCoinsUpdated(
67
+ route1: BestRouteResponse,
68
+ route2: BestRouteResponse
69
+ ) {
70
+ const route1InternalCoins =
71
+ route1.result?.swaps.map((swap) => swap.to.symbol) || [];
72
+ const route2InternalCoins =
73
+ route2.result?.swaps.map((swap) => swap.to.symbol) || [];
30
74
  return !areEqual(route1InternalCoins, route2InternalCoins);
31
75
  }
32
76
 
33
- export const isRouteChanged = (route1: BestRouteResponse, route2: BestRouteResponse): boolean => {
77
+ export function isRouteChanged(
78
+ route1: BestRouteResponse,
79
+ route2: BestRouteResponse
80
+ ): boolean {
34
81
  return (
35
82
  isNumberOfSwapsChanged(route1, route2) ||
36
83
  isRouteSwappersUpdated(route1, route2) ||
37
84
  isRouteInternalCoinsUpdated(route1, route2)
38
85
  );
39
- };
86
+ }
40
87
 
41
- export const outToRatioHasWarning = (fromUsdValue: BigNumber | null, outToInRatio: BigNumber | 0) =>
42
- (parseInt(outToInRatio?.toFixed(2) || '0') <= -10 &&
43
- (fromUsdValue === null || fromUsdValue.gte(new BigNumber(200)))) ||
44
- (parseInt(outToInRatio?.toFixed(2) || '0') <= -5 &&
45
- (fromUsdValue === null || fromUsdValue.gte(new BigNumber(1000))));
88
+ export function outToRatioHasWarning(
89
+ fromUsdValue: BigNumber | null,
90
+ outToInRatio: BigNumber | 0
91
+ ) {
92
+ return (
93
+ (parseInt(outToInRatio?.toFixed(2) || '0') <= -10 &&
94
+ (fromUsdValue === null || fromUsdValue.gte(new BigNumber(200)))) ||
95
+ (parseInt(outToInRatio?.toFixed(2) || '0') <= -5 &&
96
+ (fromUsdValue === null || fromUsdValue.gte(new BigNumber(1000))))
97
+ );
98
+ }
46
99
 
47
- export const getRequiredBalanceOfWallet = (
100
+ export function getRequiredBalanceOfWallet(
48
101
  selectedWallet: SelectedWallet,
49
- fee: SimulationValidationStatus[] | null,
50
- ): SimulationAssetAndAmount[] | null => {
102
+ fee: SimulationValidationStatus[] | null
103
+ ): SimulationAssetAndAmount[] | null {
51
104
  if (fee === null) return null;
52
105
  const relatedFeeStatus = fee
53
106
  ?.find((item) => item.blockchain === selectedWallet.chain)
54
107
  ?.wallets.find(
55
- (wallet) => wallet.address?.toLowerCase() === selectedWallet.address.toLowerCase(),
108
+ (wallet) =>
109
+ wallet.address?.toLowerCase() === selectedWallet.address.toLowerCase()
56
110
  );
57
111
  if (!relatedFeeStatus) return null;
58
112
  return relatedFeeStatus.requiredAssets;
59
- };
113
+ }
114
+
115
+ export function isRouteParametersChanged(
116
+ prevParams: BestRouteParams,
117
+ currentParams: BestRouteParams
118
+ ) {
119
+ return (
120
+ prevParams.fromChain?.name !== currentParams.fromChain?.name ||
121
+ prevParams.toChain?.name !== currentParams.toChain?.name ||
122
+ prevParams.fromToken?.symbol !== currentParams.fromToken?.symbol ||
123
+ prevParams.toToken?.symbol !== currentParams.toToken?.symbol ||
124
+ prevParams.fromToken?.blockchain !== currentParams.fromToken?.blockchain ||
125
+ prevParams.toToken?.blockchain !== currentParams.toToken?.blockchain ||
126
+ prevParams.fromToken?.address !== currentParams.fromToken?.address ||
127
+ prevParams.toToken?.address !== currentParams.toToken?.address ||
128
+ prevParams.inputAmount !== currentParams.inputAmount ||
129
+ prevParams.slippage !== currentParams.slippage ||
130
+ prevParams.customSlippage !== currentParams.customSlippage ||
131
+ prevParams.disabledLiquiditySources?.length !==
132
+ currentParams.disabledLiquiditySources?.length
133
+ );
134
+ }
135
+
136
+ export function getBestRouteWithCalculatedFees(
137
+ bestRoute: BestRouteResponse | null,
138
+ tokens: TokenMeta[]
139
+ ): BestRouteWithFee | null {
140
+ if (!bestRoute || !bestRoute.result) return null;
141
+ const swapsWithFee = (bestRoute?.result?.swaps || []).map((swap) => ({
142
+ ...swap,
143
+ feeInUsd: numberToString(getUsdFeeOfStep(swap, tokens), 0, 2),
144
+ }));
145
+
146
+ return {
147
+ ...bestRoute,
148
+ result: { ...bestRoute.result, swaps: swapsWithFee },
149
+ };
150
+ }
package/src/utils/swap.ts CHANGED
@@ -248,10 +248,10 @@ export function calculatePendingSwap(
248
248
  transferTransaction: null,
249
249
  diagnosisUrl: null,
250
250
  internalSteps: null,
251
- fromBlockchainLogo: fromBlockchain.logo,
252
- toBlockchainLogo: toBlockchain.logo,
253
- swapperLogo: swapper?.logo,
254
- swapperType: swapperType,
251
+ fromBlockchainLogo: fromBlockchain?.logo || '',
252
+ toBlockchainLogo: toBlockchain?.logo || '',
253
+ swapperLogo: swapper?.logo || '',
254
+ swapperType: swapperType || '',
255
255
  };
256
256
  }) || [],
257
257
  };
@@ -288,7 +288,10 @@ export const getUsdPrice = (
288
288
  return token?.usdPrice || null;
289
289
  };
290
290
 
291
- function getUsdFeeOfStep(step: SwapResult, allTokens: Token[]): BigNumber {
291
+ export function getUsdFeeOfStep(
292
+ step: SwapResult,
293
+ allTokens: Token[]
294
+ ): BigNumber {
292
295
  let totalFeeInUsd = ZERO;
293
296
  for (let i = 0; i < step.fee.length; i++) {
294
297
  const fee = step.fee[i];
@@ -332,7 +335,9 @@ export function hasSlippageError(
332
335
  return (slippages?.filter((s) => !!s?.error)?.length || 0) > 0;
333
336
  }
334
337
 
335
- export function getMinRequiredSlippage(route: BestRouteResponse) {
338
+ export function getMinRequiredSlippage(
339
+ route: BestRouteResponse
340
+ ): number | null {
336
341
  const slippages = route.result?.swaps.map(
337
342
  (slippage) => slippage.recommendedSlippage
338
343
  );
@@ -350,7 +355,8 @@ export function hasProperSlippage(
350
355
  minRequiredSlippage: number | null
351
356
  ) {
352
357
  if (!minRequiredSlippage) return true;
353
- return parseFloat(userSlippage) > minRequiredSlippage;
358
+ //@ts-ignore
359
+ return parseFloat(userSlippage) >= parseFloat(minRequiredSlippage);
354
360
  }
355
361
 
356
362
  export function hasEnoughBalance(
@@ -465,14 +471,15 @@ export function getWalletsForNewSwap(selectedWallets: SelectedWallet[]) {
465
471
  return wallets;
466
472
  }
467
473
 
468
- export function getRouteOutputAmount(route: BestRouteResponse) {
469
- return route.result?.outputAmount || null;
474
+ export function getRouteOutputAmount(route: BestRouteResponse | null) {
475
+ return route?.result?.outputAmount || null;
470
476
  }
471
477
 
472
478
  export function getPercentageChange(
473
- oldValue: string | number,
474
- newValue: string | number
479
+ oldValue: string | number | null,
480
+ newValue: string | number | null
475
481
  ) {
482
+ if (!oldValue || !newValue) return null;
476
483
  return new BigNumber(newValue)
477
484
  .div(new BigNumber(oldValue))
478
485
  .minus(1)
@@ -490,6 +497,7 @@ export function isOutputAmountChangedALot(
490
497
  oldOutputAmount,
491
498
  newOutputAmount
492
499
  );
500
+ if (!percentageChange) return true;
493
501
 
494
502
  return percentageChange.toNumber() <= -1;
495
503
  }
@@ -3,9 +3,10 @@ import {
3
3
  isEvmAddress,
4
4
  KEPLR_COMPATIBLE_WALLETS,
5
5
  Network,
6
- WalletInfo,
7
6
  WalletState,
8
7
  WalletType,
8
+ detectInstallLink,
9
+ WalletInfo,
9
10
  } from '@rango-dev/wallets-shared';
10
11
 
11
12
  import {
@@ -55,19 +56,25 @@ export function getlistWallet(
55
56
  WalletType.UNKNOWN,
56
57
  WalletType.TERRA_STATION,
57
58
  WalletType.LEAP,
58
- ];
59
+ ];
59
60
 
60
61
  return list
61
62
  .filter((wallet) => !excludedWallets.includes(wallet))
62
63
  .map((type) => {
63
- const { name, img: image, installLink } = getWalletInfo(type);
64
+ const {
65
+ name,
66
+ img: image,
67
+ installLink,
68
+ showOnMobile,
69
+ } = getWalletInfo(type);
64
70
  const state = getStateWallet(getState(type));
65
71
  return {
66
72
  name,
67
73
  image,
68
- installLink,
74
+ installLink: detectInstallLink(installLink),
69
75
  state,
70
76
  type,
77
+ showOnMobile: typeof showOnMobile === 'undefined' ? false : true,
71
78
  };
72
79
  });
73
80
  }
@@ -320,76 +327,28 @@ function numberWithThousandSeperator(number: string | number): string {
320
327
  return parts.join('.');
321
328
  }
322
329
 
323
- export const sortedTokens = (
324
- tokens: TokenWithBalance[],
325
- position: 'from' | 'to',
326
- balance: Balance[]
327
- ): TokenWithBalance[] => {
328
- if (!tokens) return [];
329
- const walletSymbols = new Set(
330
- (balance || [])
331
- .flatMap((a) => a.balances || [])
332
- .filter((b) => (new BigNumber(b?.rawAmount) || ZERO).gt(0))
333
- .map((b) => `${b?.chain}.${b?.symbol}.${b?.address}`)
334
- );
335
- let sortedList = [
336
- ...tokens.filter(
337
- (t) => !t.address && walletSymbols.size && position === 'to'
338
- ),
339
- ...tokens
340
- .filter(
341
- (t) =>
342
- !(position === 'to' && !t.address) &&
343
- walletSymbols.has(`${t.blockchain}.${t.symbol}.${t.address}`)
344
- )
345
- .sort((tokenA, tokenB) => compareBalance(tokenA, tokenB, balance)),
346
- ...tokens.filter(
347
- (t) =>
348
- !walletSymbols.has(`${t.blockchain}.${t.symbol}.${t.address}`) &&
349
- !t.address &&
350
- (!walletSymbols.size || position === 'from')
351
- ),
352
- ...tokens.filter(
353
- (t) =>
354
- !walletSymbols.has(`${t.blockchain}.${t.symbol}.${t.address}`) &&
355
- t.address &&
356
- !t.isSecondaryCoin
357
- ),
358
- ...tokens.filter(
359
- (t) =>
360
- !walletSymbols.has(`${t.blockchain}.${t.symbol}.${t.address}`) &&
361
- t.isSecondaryCoin
362
- ),
363
- ];
364
-
365
- return sortedList;
366
- };
330
+ export const sortTokens = (tokens: TokenWithBalance[]): TokenWithBalance[] => {
331
+ let walletConnected = !!tokens.some((token) => token.balance);
332
+ if (!walletConnected) {
333
+ return tokens
334
+ .filter((token) => !token.address)
335
+ .concat(
336
+ tokens.filter((token) => token.address && !token.isSecondaryCoin),
337
+ tokens.filter((token) => token.isSecondaryCoin)
338
+ );
339
+ }
367
340
 
368
- const compareBalance = (
369
- tokenA: TokenWithBalance,
370
- tokenB: TokenWithBalance,
371
- wallet: Balance[]
372
- ): number => {
373
- if (!tokenA.usdPrice || !tokenB.usdPrice) return 0;
374
-
375
- const tokenAUsdValue = new BigNumber(
376
- getBalanceFromWallet(
377
- wallet,
378
- tokenA.blockchain,
379
- tokenA.symbol,
380
- tokenA.address
381
- )?.amount || ZERO
382
- ).multipliedBy(tokenA.usdPrice);
383
- const tokenBUsdValue = new BigNumber(
384
- getBalanceFromWallet(
385
- wallet,
386
- tokenB.blockchain,
387
- tokenB.symbol,
388
- tokenB.address
389
- )?.amount || ZERO
390
- ).multipliedBy(tokenB.usdPrice);
391
- if (tokenAUsdValue.gt(tokenBUsdValue)) return -1;
392
- return 1;
341
+ return tokens
342
+ .filter((token) => !!token.balance)
343
+ .sort(
344
+ (tokenA, tokenB) =>
345
+ parseFloat(tokenB.balance?.usdValue || '0') -
346
+ parseFloat(tokenA.balance?.usdValue || '0')
347
+ )
348
+ .concat(
349
+ tokens.filter((token) => !token.balance && !token.isSecondaryCoin),
350
+ tokens.filter((token) => !token.balance && token.isSecondaryCoin)
351
+ );
393
352
  };
394
353
 
395
354
  export const getUsdPrice = (
@@ -429,3 +388,88 @@ export const getKeplrCompatibleConnectedWallets = (
429
388
  connectedWalletTypes.has(compatibleWallet)
430
389
  );
431
390
  };
391
+
392
+ export function getTokensWithBalance(
393
+ tokens: TokenWithBalance[],
394
+ balances: Balance[]
395
+ ): TokenWithBalance[] {
396
+ return tokens.map(({ balance, ...otherProps }) => {
397
+ const tokenAmount = numberToString(
398
+ new BigNumber(
399
+ getBalanceFromWallet(
400
+ balances,
401
+ otherProps.blockchain,
402
+ otherProps.symbol,
403
+ otherProps.address
404
+ )?.amount || ZERO
405
+ )
406
+ );
407
+
408
+ let tokenUsdValue = '';
409
+ if (otherProps.usdPrice)
410
+ tokenUsdValue = numberToString(
411
+ new BigNumber(
412
+ getBalanceFromWallet(
413
+ balances,
414
+ otherProps.blockchain,
415
+ otherProps.symbol,
416
+ otherProps.address
417
+ )?.amount || ZERO
418
+ ).multipliedBy(otherProps.usdPrice)
419
+ );
420
+
421
+ return {
422
+ ...otherProps,
423
+ ...(tokenAmount !== '0' && {
424
+ balance: { amount: tokenAmount, usdValue: tokenUsdValue },
425
+ }),
426
+ };
427
+ });
428
+ }
429
+
430
+ export function getSortedTokens(
431
+ chain: BlockchainMeta | null,
432
+ tokens: Token[],
433
+ balances: Balance[],
434
+ otherChainTokens: TokenWithBalance[]
435
+ ): TokenWithBalance[] {
436
+ const fromChainEqueulsToToChain = chain?.name === otherChainTokens[0]?.name;
437
+ if (fromChainEqueulsToToChain) return otherChainTokens;
438
+ const filteredTokens = tokens.filter(
439
+ (token) => token.blockchain === chain?.name
440
+ );
441
+ return sortTokens(getTokensWithBalance(filteredTokens, balances));
442
+ }
443
+
444
+ export function tokensAreEqual(tokenA: Token | null, tokenB: Token | null) {
445
+ return (
446
+ tokenA?.blockchain === tokenB?.blockchain &&
447
+ tokenA?.name === tokenB?.name &&
448
+ tokenA?.address === tokenB?.address
449
+ );
450
+ }
451
+
452
+ export function getDefaultToken(
453
+ sortedTokens: TokenWithBalance[],
454
+ otherToken: TokenWithBalance | null
455
+ ): TokenWithBalance {
456
+ let selectedToken: TokenWithBalance;
457
+ const firstToken = sortedTokens[0];
458
+ const secondToken = sortedTokens[1];
459
+ if (sortedTokens.length === 1) selectedToken = firstToken;
460
+ else if (tokensAreEqual(firstToken, otherToken)) selectedToken = secondToken;
461
+ else selectedToken = firstToken;
462
+ return selectedToken;
463
+ }
464
+
465
+ export function sortWalletsBasedOnState(
466
+ wallets: ModalWalletInfo[]
467
+ ): ModalWalletInfo[] {
468
+ return wallets.sort(
469
+ (a, b) =>
470
+ Number(b.state === WalletStatus.CONNECTED) -
471
+ Number(a.state === WalletStatus.CONNECTED) ||
472
+ Number(b.state === WalletStatus.DISCONNECTED) -
473
+ Number(a.state === WalletStatus.DISCONNECTED)
474
+ );
475
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"confirmSwap.d.ts","sourceRoot":"","sources":["../src/store/confirmSwap.ts"],"names":[],"mappings":"AA+BA,OAAO,EAAE,WAAW,EAAE,MAAM,6CAA6C,CAAC;AAS1E,eAAO,MAAM,mBAAmB,KAS/B,CAAC;AAEF,oBAAY,qBAAqB;IAC/B,QAAQ,IAAA;IACR,kCAAkC,IAAA;IAClC,cAAc,IAAA;IACd,qBAAqB,IAAA;CACtB;AAED,oBAAY,gBAAgB,GACxB;IACE,IAAI,EAAE,qBAAqB,CAAC,cAAc,CAAC;IAC3C,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,GACD;IACE,IAAI,EAAE,qBAAqB,CAAC,qBAAqB,CAAC;IAClD,mBAAmB,EAAE,MAAM,CAAC;CAC7B,GACD;IACE,IAAI,EAAE,OAAO,CACX,qBAAqB,EACnB,qBAAqB,CAAC,cAAc,GACpC,qBAAqB,CAAC,qBAAqB,CAC9C,CAAC;CACH,CAAC;AAEN,oBAAY,mBAAmB,GAC3B;IACE,IAAI,EAAE,uBAAuB,CAAC,+BAA+B,CAAC;IAC9D,eAAe,EAAE,MAAM,CAAC;IACxB,gBAAgB,EAAE,MAAM,CAAC;CAC1B,GACD;IAAE,IAAI,EAAE,uBAAuB,CAAC,oBAAoB,CAAC;IAAC,QAAQ,EAAE,MAAM,EAAE,CAAA;CAAE,GAC1E;IACE,IAAI,EAAE,OAAO,CACX,uBAAuB,EACrB,uBAAuB,CAAC,+BAA+B,GACvD,uBAAuB,CAAC,oBAAoB,CAC/C,CAAC;CACH,CAAC;AAEN,oBAAY,uBAAuB;IACjC,aAAa,IAAA;IACb,sBAAsB,IAAA;IACtB,mBAAmB,IAAA;IACnB,+BAA+B,IAAA;IAC/B,oBAAoB,IAAA;CACrB;AAED,eAAO,MAAM,WAAW,QAAa,OAAO,CAAC,WAAW,GAAG,SAAS,CAmPnE,CAAC"}