@rango-dev/widget-embedded 0.1.10-next.100

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 (204) hide show
  1. package/dist/App.d.ts +9 -0
  2. package/dist/App.d.ts.map +1 -0
  3. package/dist/QueueManager.d.ts +4 -0
  4. package/dist/QueueManager.d.ts.map +1 -0
  5. package/dist/components/AppRouter.d.ts +9 -0
  6. package/dist/components/AppRouter.d.ts.map +1 -0
  7. package/dist/components/AppRoutes.d.ts +8 -0
  8. package/dist/components/AppRoutes.d.ts.map +1 -0
  9. package/dist/components/BottomLogo.d.ts +3 -0
  10. package/dist/components/BottomLogo.d.ts.map +1 -0
  11. package/dist/components/ChangeSlippageButton.d.ts +3 -0
  12. package/dist/components/ChangeSlippageButton.d.ts.map +1 -0
  13. package/dist/components/ConfirmSwapErrors.d.ts +4 -0
  14. package/dist/components/ConfirmSwapErrors.d.ts.map +1 -0
  15. package/dist/components/ConfirmSwapWarnings.d.ts +4 -0
  16. package/dist/components/ConfirmSwapWarnings.d.ts.map +1 -0
  17. package/dist/components/Footer.d.ts +2 -0
  18. package/dist/components/Footer.d.ts.map +1 -0
  19. package/dist/components/Header.d.ts +10 -0
  20. package/dist/components/Header.d.ts.map +1 -0
  21. package/dist/components/HeaderButtons.d.ts +4 -0
  22. package/dist/components/HeaderButtons.d.ts.map +1 -0
  23. package/dist/components/Layout.d.ts +7 -0
  24. package/dist/components/Layout.d.ts.map +1 -0
  25. package/dist/components/RoutesOverview.d.ts +10 -0
  26. package/dist/components/RoutesOverview.d.ts.map +1 -0
  27. package/dist/components/SwitchFromAndTo.d.ts +4 -0
  28. package/dist/components/SwitchFromAndTo.d.ts.map +1 -0
  29. package/dist/components/TokenInfo.d.ts +18 -0
  30. package/dist/components/TokenInfo.d.ts.map +1 -0
  31. package/dist/components/TokenPreview.d.ts +20 -0
  32. package/dist/components/TokenPreview.d.ts.map +1 -0
  33. package/dist/components/UpdateUrl.d.ts +2 -0
  34. package/dist/components/UpdateUrl.d.ts.map +1 -0
  35. package/dist/components/warnings/BalanceWarnings.d.ts +7 -0
  36. package/dist/components/warnings/BalanceWarnings.d.ts.map +1 -0
  37. package/dist/components/warnings/ConfirmSwapExtraMessages.d.ts +7 -0
  38. package/dist/components/warnings/ConfirmSwapExtraMessages.d.ts.map +1 -0
  39. package/dist/components/warnings/HighSlippageWarning.d.ts +7 -0
  40. package/dist/components/warnings/HighSlippageWarning.d.ts.map +1 -0
  41. package/dist/components/warnings/MinRequiredSlippage.d.ts +7 -0
  42. package/dist/components/warnings/MinRequiredSlippage.d.ts.map +1 -0
  43. package/dist/constants/errors.d.ts +4 -0
  44. package/dist/constants/errors.d.ts.map +1 -0
  45. package/dist/constants/navigationRoutes.d.ts +15 -0
  46. package/dist/constants/navigationRoutes.d.ts.map +1 -0
  47. package/dist/constants/numbers.d.ts +3 -0
  48. package/dist/constants/numbers.d.ts.map +1 -0
  49. package/dist/constants/searchParams.d.ts +8 -0
  50. package/dist/constants/searchParams.d.ts.map +1 -0
  51. package/dist/constants/swapSettings.d.ts +6 -0
  52. package/dist/constants/swapSettings.d.ts.map +1 -0
  53. package/dist/globalStyles.d.ts +3 -0
  54. package/dist/globalStyles.d.ts.map +1 -0
  55. package/dist/hooks/useConfirmSwap.d.ts +11 -0
  56. package/dist/hooks/useConfirmSwap.d.ts.map +1 -0
  57. package/dist/hooks/useCopyToClipboard.d.ts +2 -0
  58. package/dist/hooks/useCopyToClipboard.d.ts.map +1 -0
  59. package/dist/hooks/useNavigateBack.d.ts +4 -0
  60. package/dist/hooks/useNavigateBack.d.ts.map +1 -0
  61. package/dist/hooks/usePrevious.d.ts +2 -0
  62. package/dist/hooks/usePrevious.d.ts.map +1 -0
  63. package/dist/hooks/useSelectLanguage.d.ts +5 -0
  64. package/dist/hooks/useSelectLanguage.d.ts.map +1 -0
  65. package/dist/hooks/useTheme.d.ts +8 -0
  66. package/dist/hooks/useTheme.d.ts.map +1 -0
  67. package/dist/i18n.d.ts +3 -0
  68. package/dist/i18n.d.ts.map +1 -0
  69. package/dist/index.d.ts +2 -0
  70. package/dist/index.d.ts.map +1 -0
  71. package/dist/index.js +8 -0
  72. package/dist/lib.d.ts +8 -0
  73. package/dist/lib.d.ts.map +1 -0
  74. package/dist/mockData/pendingSwap.d.ts +2 -0
  75. package/dist/mockData/pendingSwap.d.ts.map +1 -0
  76. package/dist/pages/ConfirmSwapPage.d.ts +3 -0
  77. package/dist/pages/ConfirmSwapPage.d.ts.map +1 -0
  78. package/dist/pages/ConfirmWalletsPage.d.ts +3 -0
  79. package/dist/pages/ConfirmWalletsPage.d.ts.map +1 -0
  80. package/dist/pages/HistoryPage.d.ts +3 -0
  81. package/dist/pages/HistoryPage.d.ts.map +1 -0
  82. package/dist/pages/Home.d.ts +9 -0
  83. package/dist/pages/Home.d.ts.map +1 -0
  84. package/dist/pages/LiquiditySourcesPage.d.ts +8 -0
  85. package/dist/pages/LiquiditySourcesPage.d.ts.map +1 -0
  86. package/dist/pages/SelectChainPage.d.ts +9 -0
  87. package/dist/pages/SelectChainPage.d.ts.map +1 -0
  88. package/dist/pages/SelectTokenPage.d.ts +15 -0
  89. package/dist/pages/SelectTokenPage.d.ts.map +1 -0
  90. package/dist/pages/SettingsPage.d.ts +8 -0
  91. package/dist/pages/SettingsPage.d.ts.map +1 -0
  92. package/dist/pages/SwapDetailsPage.d.ts +3 -0
  93. package/dist/pages/SwapDetailsPage.d.ts.map +1 -0
  94. package/dist/pages/WalletsPage.d.ts +9 -0
  95. package/dist/pages/WalletsPage.d.ts.map +1 -0
  96. package/dist/services/httpService.d.ts +3 -0
  97. package/dist/services/httpService.d.ts.map +1 -0
  98. package/dist/store/bestRoute.d.ts +28 -0
  99. package/dist/store/bestRoute.d.ts.map +1 -0
  100. package/dist/store/meta.d.ts +9 -0
  101. package/dist/store/meta.d.ts.map +1 -0
  102. package/dist/store/selectors.d.ts +11 -0
  103. package/dist/store/selectors.d.ts.map +1 -0
  104. package/dist/store/settings.d.ts +17 -0
  105. package/dist/store/settings.d.ts.map +1 -0
  106. package/dist/store/ui.d.ts +2 -0
  107. package/dist/store/ui.d.ts.map +1 -0
  108. package/dist/store/wallets.d.ts +28 -0
  109. package/dist/store/wallets.d.ts.map +1 -0
  110. package/dist/types/config.d.ts +40 -0
  111. package/dist/types/config.d.ts.map +1 -0
  112. package/dist/types/index.d.ts +4 -0
  113. package/dist/types/index.d.ts.map +1 -0
  114. package/dist/types/routing.d.ts +45 -0
  115. package/dist/types/routing.d.ts.map +1 -0
  116. package/dist/types/swap.d.ts +11 -0
  117. package/dist/types/swap.d.ts.map +1 -0
  118. package/dist/utils/common.d.ts +4 -0
  119. package/dist/utils/common.d.ts.map +1 -0
  120. package/dist/utils/numbers.d.ts +14 -0
  121. package/dist/utils/numbers.d.ts.map +1 -0
  122. package/dist/utils/queue.d.ts +4 -0
  123. package/dist/utils/queue.d.ts.map +1 -0
  124. package/dist/utils/routing.d.ts +18 -0
  125. package/dist/utils/routing.d.ts.map +1 -0
  126. package/dist/utils/swap.d.ts +45 -0
  127. package/dist/utils/swap.d.ts.map +1 -0
  128. package/dist/utils/wallets.d.ts +29 -0
  129. package/dist/utils/wallets.d.ts.map +1 -0
  130. package/dist/widget-embedded.cjs.development.js +3101 -0
  131. package/dist/widget-embedded.cjs.development.js.map +1 -0
  132. package/dist/widget-embedded.cjs.production.min.js +3101 -0
  133. package/dist/widget-embedded.cjs.production.min.js.map +1 -0
  134. package/dist/widget-embedded.esm.js +3092 -0
  135. package/dist/widget-embedded.esm.js.map +1 -0
  136. package/package.json +55 -0
  137. package/readme.md +2 -0
  138. package/src/App.tsx +91 -0
  139. package/src/QueueManager.tsx +94 -0
  140. package/src/app.css +27 -0
  141. package/src/components/AppRouter.tsx +60 -0
  142. package/src/components/AppRoutes.tsx +105 -0
  143. package/src/components/BottomLogo.tsx +47 -0
  144. package/src/components/ChangeSlippageButton.tsx +19 -0
  145. package/src/components/ConfirmSwapErrors.tsx +38 -0
  146. package/src/components/ConfirmSwapWarnings.tsx +35 -0
  147. package/src/components/Footer.tsx +8 -0
  148. package/src/components/Header.tsx +37 -0
  149. package/src/components/HeaderButtons.tsx +47 -0
  150. package/src/components/Layout.tsx +96 -0
  151. package/src/components/RoutesOverview.tsx +60 -0
  152. package/src/components/SwitchFromAndTo.tsx +30 -0
  153. package/src/components/TokenInfo.tsx +311 -0
  154. package/src/components/TokenPreview.tsx +175 -0
  155. package/src/components/UpdateUrl.tsx +162 -0
  156. package/src/components/warnings/BalanceWarnings.tsx +39 -0
  157. package/src/components/warnings/ConfirmSwapExtraMessages.tsx +20 -0
  158. package/src/components/warnings/HighSlippageWarning.tsx +18 -0
  159. package/src/components/warnings/MinRequiredSlippage.tsx +18 -0
  160. package/src/constants/errors.ts +3 -0
  161. package/src/constants/navigationRoutes.ts +14 -0
  162. package/src/constants/numbers.ts +3 -0
  163. package/src/constants/searchParams.ts +7 -0
  164. package/src/constants/swapSettings.ts +9 -0
  165. package/src/globalStyles.ts +26 -0
  166. package/src/hooks/useConfirmSwap.ts +260 -0
  167. package/src/hooks/useCopyToClipboard.ts +23 -0
  168. package/src/hooks/useNavigateBack.ts +40 -0
  169. package/src/hooks/usePrevious.ts +13 -0
  170. package/src/hooks/useSelectLanguage.ts +15 -0
  171. package/src/hooks/useTheme.ts +127 -0
  172. package/src/i18n.js +30 -0
  173. package/src/index.tsx +14 -0
  174. package/src/languages/en.json +14 -0
  175. package/src/languages/tr.json +11 -0
  176. package/src/lib.tsx +119 -0
  177. package/src/mockData/pendingSwap.ts +607 -0
  178. package/src/pages/ConfirmSwapPage.tsx +72 -0
  179. package/src/pages/ConfirmWalletsPage.tsx +127 -0
  180. package/src/pages/HistoryPage.tsx +29 -0
  181. package/src/pages/Home.tsx +228 -0
  182. package/src/pages/LiquiditySourcesPage.tsx +64 -0
  183. package/src/pages/SelectChainPage.tsx +42 -0
  184. package/src/pages/SelectTokenPage.tsx +64 -0
  185. package/src/pages/SettingsPage.tsx +98 -0
  186. package/src/pages/SwapDetailsPage.tsx +38 -0
  187. package/src/pages/WalletsPage.tsx +119 -0
  188. package/src/services/httpService.ts +5 -0
  189. package/src/store/bestRoute.ts +245 -0
  190. package/src/store/meta.ts +35 -0
  191. package/src/store/selectors.ts +19 -0
  192. package/src/store/settings.ts +84 -0
  193. package/src/store/ui.ts +21 -0
  194. package/src/store/wallets.ts +190 -0
  195. package/src/types/config.ts +44 -0
  196. package/src/types/index.ts +3 -0
  197. package/src/types/routing.ts +60 -0
  198. package/src/types/swap.ts +13 -0
  199. package/src/utils/common.ts +35 -0
  200. package/src/utils/numbers.ts +133 -0
  201. package/src/utils/queue.ts +24 -0
  202. package/src/utils/routing.ts +150 -0
  203. package/src/utils/swap.ts +552 -0
  204. package/src/utils/wallets.ts +481 -0
@@ -0,0 +1,481 @@
1
+ import {
2
+ getCosmosExperimentalChainInfo,
3
+ isEvmAddress,
4
+ KEPLR_COMPATIBLE_WALLETS,
5
+ Network,
6
+ WalletState,
7
+ WalletType,
8
+ detectInstallLink,
9
+ WalletInfo,
10
+ } from '@rango-dev/wallets-shared';
11
+
12
+ import {
13
+ WalletInfo as ModalWalletInfo,
14
+ WalletState as WalletStatus,
15
+ SelectableWallet,
16
+ } from '@rango-dev/ui';
17
+ import {
18
+ BestRouteResponse,
19
+ BlockchainMeta,
20
+ Token,
21
+ WalletDetail,
22
+ } from 'rango-sdk';
23
+ import { isCosmosBlockchain } from 'rango-types';
24
+ import { readAccountAddress } from '@rango-dev/wallets-core';
25
+ import { Account, Balance, TokenBalance } from '../store/wallets';
26
+ import { numberToString } from './numbers';
27
+ import BigNumber from 'bignumber.js';
28
+ import { TokenWithBalance } from '../pages/SelectTokenPage';
29
+ import { ZERO } from '../constants/numbers';
30
+
31
+ export function getStateWallet(state: WalletState): WalletStatus {
32
+ switch (true) {
33
+ case state.connected:
34
+ return WalletStatus.CONNECTED;
35
+ case state.connecting:
36
+ return WalletStatus.CONNECTING;
37
+ case !state.installed:
38
+ return WalletStatus.NOT_INSTALLED;
39
+ default:
40
+ return WalletStatus.DISCONNECTED;
41
+ }
42
+ }
43
+
44
+ export const excludedWallets = [
45
+ WalletType.UNKNOWN,
46
+ WalletType.TERRA_STATION,
47
+ WalletType.LEAP,
48
+ ];
49
+
50
+ export function getlistWallet(
51
+ getState: (type: WalletType) => WalletState,
52
+ getWalletInfo: (type: WalletType) => WalletInfo,
53
+ list: WalletType[]
54
+ ): ModalWalletInfo[] {
55
+ const excludedWallets = [
56
+ WalletType.UNKNOWN,
57
+ WalletType.TERRA_STATION,
58
+ WalletType.LEAP,
59
+ ];
60
+
61
+ return list
62
+ .filter((wallet) => !excludedWallets.includes(wallet))
63
+ .map((type) => {
64
+ const {
65
+ name,
66
+ img: image,
67
+ installLink,
68
+ showOnMobile,
69
+ } = getWalletInfo(type);
70
+ const state = getStateWallet(getState(type));
71
+ return {
72
+ name,
73
+ image,
74
+ installLink: detectInstallLink(installLink),
75
+ state,
76
+ type,
77
+ showOnMobile: typeof showOnMobile === 'undefined' ? false : true,
78
+ };
79
+ });
80
+ }
81
+
82
+ export function walletAndSupportedChainsNames(
83
+ supportedChains: BlockchainMeta[]
84
+ ): Network[] | null {
85
+ if (!supportedChains) return null;
86
+ let walletAndSupportedChainsNames: Network[] = [];
87
+ walletAndSupportedChainsNames = supportedChains.map(
88
+ (blockchainMeta) => blockchainMeta.name as Network
89
+ );
90
+
91
+ return walletAndSupportedChainsNames;
92
+ }
93
+
94
+ export function prepareAccountsForWalletStore(
95
+ wallet: WalletType,
96
+ accounts: string[],
97
+ evmBasedChains: string[],
98
+ supportedChainNames: Network[] | null
99
+ ): Account[] {
100
+ const result: Account[] = [];
101
+
102
+ function addAccount(network: Network, address: string) {
103
+ const newAccount: Account = {
104
+ address,
105
+ chain: network,
106
+ walletType: wallet,
107
+ };
108
+
109
+ result.push(newAccount);
110
+ }
111
+
112
+ const supportedChains = supportedChainNames || [];
113
+
114
+ accounts.forEach((account) => {
115
+ const { address, network } = readAccountAddress(account);
116
+
117
+ const hasLimitation = supportedChains.length > 0;
118
+ const isSupported = supportedChains.includes(network);
119
+ const isUnknown = network === Network.Unknown;
120
+ const notSupportedNetworkByWallet =
121
+ hasLimitation && !isSupported && !isUnknown;
122
+
123
+ // Here we check given `network` is not supported by wallet
124
+ // And also the network is known.
125
+ if (notSupportedNetworkByWallet) return;
126
+
127
+ // In some cases we can handle unknown network by checking its address
128
+ // pattern and act on it.
129
+ // Example: showing our evm compatible netwrok when the uknown network is evem.
130
+ // Otherwise, we stop executing this function.
131
+ const isUknownAndEvmBased =
132
+ network === Network.Unknown && isEvmAddress(address);
133
+ if (isUnknown && !isUknownAndEvmBased) return;
134
+
135
+ const isEvmBasedChain = evmBasedChains.includes(network);
136
+
137
+ // If it's an evm network, we will add the address to all the evm chains.
138
+ if (isEvmBasedChain || isUknownAndEvmBased) {
139
+ // all evm chains are not supported in wallets, so we are adding
140
+ // only to those that are supported by wallet.
141
+ const evmChainsSupportedByWallet = supportedChains.filter((chain) =>
142
+ evmBasedChains.includes(chain)
143
+ );
144
+
145
+ evmChainsSupportedByWallet.forEach((network) => {
146
+ // EVM addresses are not case sensetive.
147
+ // Some wallets like Binance-chain return some letters in uppercase which produces bugs in our wallet state.
148
+ addAccount(network, address.toLowerCase());
149
+ });
150
+ } else {
151
+ addAccount(network, address);
152
+ }
153
+ });
154
+
155
+ return result;
156
+ }
157
+
158
+ export function getRequiredChains(route: BestRouteResponse | null) {
159
+ const wallets: string[] = [];
160
+
161
+ route?.result?.swaps.forEach((swap) => {
162
+ const currentStepFromBlockchain = swap.from.blockchain;
163
+ const currentStepToBlockchain = swap.to.blockchain;
164
+ let lastAddedWallet = wallets[wallets.length - 1];
165
+ if (currentStepFromBlockchain != lastAddedWallet)
166
+ wallets.push(currentStepFromBlockchain);
167
+ lastAddedWallet = wallets[wallets.length - 1];
168
+ if (currentStepToBlockchain != lastAddedWallet)
169
+ wallets.push(currentStepToBlockchain);
170
+ });
171
+ return wallets;
172
+ }
173
+
174
+ export interface SelectedWallet extends Account {}
175
+ type Blockchain = { name: string; accounts: Balance[] };
176
+
177
+ export function getSelectableWallets(
178
+ accounts: Account[],
179
+ selectedWallets: SelectedWallet[],
180
+ getWalletInfo: (type: WalletType) => WalletInfo
181
+ ): SelectableWallet[] {
182
+ const connectedWallets: SelectableWallet[] = accounts.map((account) => ({
183
+ address: account.address,
184
+ walletType: account.walletType,
185
+ chain: account.chain,
186
+ image: getWalletInfo(account.walletType).img,
187
+ name: getWalletInfo(account.walletType).name,
188
+ selected: !!selectedWallets.find(
189
+ (wallet) =>
190
+ wallet.chain === account.chain &&
191
+ wallet.walletType === account.walletType
192
+ ),
193
+ }));
194
+
195
+ return connectedWallets;
196
+ }
197
+
198
+ export function getBalanceFromWallet(
199
+ balances: Balance[],
200
+ chain: string,
201
+ symbol: string,
202
+ address: string | null
203
+ ): TokenBalance | null {
204
+ if (balances.length === 0) return null;
205
+
206
+ const selectedChainBalances = balances.filter(
207
+ (balance) => balance.chain === chain
208
+ );
209
+ if (selectedChainBalances.length === 0) return null;
210
+
211
+ return (
212
+ selectedChainBalances
213
+ .map(
214
+ (a) =>
215
+ a.balances?.find(
216
+ (bl) =>
217
+ (address !== null && bl.address === address) ||
218
+ (address === null &&
219
+ bl.address === address &&
220
+ bl.symbol === symbol)
221
+ ) || null
222
+ )
223
+ .filter((b) => b !== null)
224
+ .sort(
225
+ (a, b) => parseFloat(b?.amount || '0') - parseFloat(a?.amount || '1')
226
+ )
227
+ .find(() => true) || null
228
+ );
229
+ }
230
+
231
+ export function isAccountAndBalanceMatched(account: Account, balance: Balance) {
232
+ return (
233
+ account.address === balance.address &&
234
+ account.chain === balance.chain &&
235
+ account.walletType === balance.walletType
236
+ );
237
+ }
238
+
239
+ export function makeBalanceFor(
240
+ account: Account,
241
+ retrivedBalance: WalletDetail,
242
+ tokens: Token[]
243
+ ): Balance {
244
+ const {
245
+ address,
246
+ blockChain: chain,
247
+ explorerUrl,
248
+ balances = [],
249
+ } = retrivedBalance;
250
+ return {
251
+ address,
252
+ chain,
253
+ loading: false,
254
+ error: false,
255
+ explorerUrl,
256
+ walletType: account.walletType,
257
+ balances:
258
+ balances?.map((tokenBalance) => ({
259
+ chain,
260
+ symbol: tokenBalance.asset.symbol,
261
+ ticker: tokenBalance.asset.symbol,
262
+ address: tokenBalance.asset.address || null,
263
+ rawAmount: tokenBalance.amount.amount,
264
+ decimal: tokenBalance.amount.decimals,
265
+ amount: new BigNumber(tokenBalance.amount.amount)
266
+ .shiftedBy(-tokenBalance.amount.decimals)
267
+ .toFixed(),
268
+ logo: '',
269
+ usdPrice:
270
+ getUsdPrice(
271
+ chain,
272
+ tokenBalance.asset.symbol,
273
+ tokenBalance.asset.address,
274
+ tokens
275
+ ) || null,
276
+ })) || [],
277
+ };
278
+ }
279
+
280
+ export function resetBalanceState(balance: Balance): Balance {
281
+ return { ...balance, loading: false, error: true };
282
+ }
283
+
284
+ export const calculateWalletUsdValue = (balance: Balance[]) => {
285
+ const uniqueAccountAddresses = new Set<string | null>();
286
+ const uniqueBalane: Balance[] = balance?.reduce(
287
+ (acc: Balance[], current: Balance) => {
288
+ return acc.findIndex(
289
+ (i) => i.address === current.address && i.chain === current.chain
290
+ ) === -1
291
+ ? [...acc, current]
292
+ : acc;
293
+ },
294
+ []
295
+ );
296
+
297
+ const modifiedWalletBlockchains = uniqueBalane?.map((chain) => {
298
+ const modifiedWalletBlockchain: Blockchain = {
299
+ name: chain.chain,
300
+ accounts: [],
301
+ };
302
+ if (!uniqueAccountAddresses.has(chain.address)) {
303
+ uniqueAccountAddresses.add(chain.address);
304
+ }
305
+ uniqueAccountAddresses.forEach((accountAddress) => {
306
+ if (chain.address === accountAddress)
307
+ modifiedWalletBlockchain.accounts.push(chain);
308
+ });
309
+ return modifiedWalletBlockchain;
310
+ });
311
+ const total = numberToString(
312
+ modifiedWalletBlockchains
313
+ ?.flatMap((b) => b.accounts)
314
+ ?.flatMap((a) => a?.balances)
315
+ ?.map((b) =>
316
+ new BigNumber(b?.amount || ZERO).multipliedBy(b?.usdPrice || 0)
317
+ )
318
+ ?.reduce((a, b) => a.plus(b), ZERO) || ZERO
319
+ ).toString();
320
+
321
+ return numberWithThousandSeperator(total);
322
+ };
323
+
324
+ function numberWithThousandSeperator(number: string | number): string {
325
+ var parts = number.toString().split('.');
326
+ parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
327
+ return parts.join('.');
328
+ }
329
+
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
+ }
340
+
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
+ );
352
+ };
353
+
354
+ export const getUsdPrice = (
355
+ blockchain: string,
356
+ symbol: string,
357
+ address: string | null,
358
+ allTokens: Token[]
359
+ ): number | null => {
360
+ const token = allTokens?.find(
361
+ (t) =>
362
+ t.blockchain === blockchain &&
363
+ t.symbol?.toUpperCase() === symbol?.toUpperCase() &&
364
+ t.address === address
365
+ );
366
+ return token?.usdPrice || null;
367
+ };
368
+ export const isExperimentalChain = (
369
+ blockchains: BlockchainMeta[],
370
+ wallet: string
371
+ ): boolean => {
372
+ const cosmosExperimentalChainInfo = getCosmosExperimentalChainInfo(
373
+ Object.entries(blockchains)
374
+ .map(([, blockchainMeta]) => blockchainMeta)
375
+ .filter(isCosmosBlockchain)
376
+ );
377
+ return cosmosExperimentalChainInfo && !!cosmosExperimentalChainInfo[wallet];
378
+ };
379
+
380
+ export const getKeplrCompatibleConnectedWallets = (
381
+ selectableWallets: SelectableWallet[]
382
+ ): WalletType[] => {
383
+ const connectedWalletTypes = new Set(
384
+ selectableWallets.map((a: any) => a.walletType.toString())
385
+ );
386
+
387
+ return KEPLR_COMPATIBLE_WALLETS.filter((compatibleWallet) =>
388
+ connectedWalletTypes.has(compatibleWallet)
389
+ );
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(
473
+ b.state === WalletStatus.DISCONNECTED ||
474
+ b.state === WalletStatus.CONNECTING
475
+ ) -
476
+ Number(
477
+ a.state === WalletStatus.DISCONNECTED ||
478
+ a.state === WalletStatus.CONNECTING
479
+ )
480
+ );
481
+ }