@rango-dev/widget-embedded 0.1.10-next.76 → 0.1.10-next.80

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.
@@ -1,13 +1,24 @@
1
1
  import {
2
+ getCosmosExperimentalChainInfo,
2
3
  isEvmAddress,
4
+ KEPLR_COMPATIBLE_WALLETS,
3
5
  Network,
4
6
  WalletInfo,
5
7
  WalletState,
6
8
  WalletType,
9
+ isCosmosBlockchain,
7
10
  } from '@rango-dev/wallets-shared';
8
11
 
9
- import { WalletInfo as ModalWalletInfo, WalletState as WalletStatus } from '@rango-dev/ui';
10
- import { BestRouteResponse, BlockchainMeta, Token, WalletDetail } from 'rango-sdk';
12
+ import {
13
+ WalletInfo as ModalWalletInfo,
14
+ WalletState as WalletStatus,
15
+ } from '@rango-dev/ui';
16
+ import {
17
+ BestRouteResponse,
18
+ BlockchainMeta,
19
+ Token,
20
+ WalletDetail,
21
+ } from 'rango-sdk';
11
22
  import { readAccountAddress } from '@rango-dev/wallets-core';
12
23
  import { SelectableWallet } from '../pages/ConfirmWalletsPage';
13
24
  import { Account, Balance, TokenBalance } from '../store/wallets';
@@ -29,18 +40,22 @@ export function getStateWallet(state: WalletState): WalletStatus {
29
40
  }
30
41
  }
31
42
 
32
- export const excludedWallets = [
33
- WalletType.UNKNOWN,
34
- WalletType.TERRA_STATION,
35
- WalletType.LEAP,
36
- ];
43
+ export const excludedWallets = [
44
+ WalletType.UNKNOWN,
45
+ WalletType.TERRA_STATION,
46
+ WalletType.LEAP,
47
+ ];
37
48
 
38
49
  export function getlistWallet(
39
50
  getState: (type: WalletType) => WalletState,
40
51
  getWalletInfo: (type: WalletType) => WalletInfo,
41
- list: WalletType[],
52
+ list: WalletType[]
42
53
  ): ModalWalletInfo[] {
43
- const excludedWallets = [WalletType.UNKNOWN, WalletType.TERRA_STATION, WalletType.LEAP];
54
+ const excludedWallets = [
55
+ WalletType.UNKNOWN,
56
+ WalletType.TERRA_STATION,
57
+ WalletType.LEAP,
58
+ ];
44
59
 
45
60
  return list
46
61
  .filter((wallet) => !excludedWallets.includes(wallet))
@@ -57,11 +72,13 @@ export function getlistWallet(
57
72
  });
58
73
  }
59
74
 
60
- export function walletAndSupportedChainsNames(supportedChains: BlockchainMeta[]): Network[] | null {
75
+ export function walletAndSupportedChainsNames(
76
+ supportedChains: BlockchainMeta[]
77
+ ): Network[] | null {
61
78
  if (!supportedChains) return null;
62
79
  let walletAndSupportedChainsNames: Network[] = [];
63
80
  walletAndSupportedChainsNames = supportedChains.map(
64
- (blockchainMeta) => blockchainMeta.name as Network,
81
+ (blockchainMeta) => blockchainMeta.name as Network
65
82
  );
66
83
 
67
84
  return walletAndSupportedChainsNames;
@@ -71,7 +88,7 @@ export function prepareAccountsForWalletStore(
71
88
  wallet: WalletType,
72
89
  accounts: string[],
73
90
  evmBasedChains: string[],
74
- supportedChainNames: Network[] | null,
91
+ supportedChainNames: Network[] | null
75
92
  ): Account[] {
76
93
  const result: Account[] = [];
77
94
 
@@ -93,7 +110,8 @@ export function prepareAccountsForWalletStore(
93
110
  const hasLimitation = supportedChains.length > 0;
94
111
  const isSupported = supportedChains.includes(network);
95
112
  const isUnknown = network === Network.Unknown;
96
- const notSupportedNetworkByWallet = hasLimitation && !isSupported && !isUnknown;
113
+ const notSupportedNetworkByWallet =
114
+ hasLimitation && !isSupported && !isUnknown;
97
115
 
98
116
  // Here we check given `network` is not supported by wallet
99
117
  // And also the network is known.
@@ -103,7 +121,8 @@ export function prepareAccountsForWalletStore(
103
121
  // pattern and act on it.
104
122
  // Example: showing our evm compatible netwrok when the uknown network is evem.
105
123
  // Otherwise, we stop executing this function.
106
- const isUknownAndEvmBased = network === Network.Unknown && isEvmAddress(address);
124
+ const isUknownAndEvmBased =
125
+ network === Network.Unknown && isEvmAddress(address);
107
126
  if (isUnknown && !isUknownAndEvmBased) return;
108
127
 
109
128
  const isEvmBasedChain = evmBasedChains.includes(network);
@@ -113,7 +132,7 @@ export function prepareAccountsForWalletStore(
113
132
  // all evm chains are not supported in wallets, so we are adding
114
133
  // only to those that are supported by wallet.
115
134
  const evmChainsSupportedByWallet = supportedChains.filter((chain) =>
116
- evmBasedChains.includes(chain),
135
+ evmBasedChains.includes(chain)
117
136
  );
118
137
 
119
138
  evmChainsSupportedByWallet.forEach((network) => {
@@ -136,9 +155,11 @@ export function getRequiredChains(route: BestRouteResponse | null) {
136
155
  const currentStepFromBlockchain = swap.from.blockchain;
137
156
  const currentStepToBlockchain = swap.to.blockchain;
138
157
  let lastAddedWallet = wallets[wallets.length - 1];
139
- if (currentStepFromBlockchain != lastAddedWallet) wallets.push(currentStepFromBlockchain);
158
+ if (currentStepFromBlockchain != lastAddedWallet)
159
+ wallets.push(currentStepFromBlockchain);
140
160
  lastAddedWallet = wallets[wallets.length - 1];
141
- if (currentStepToBlockchain != lastAddedWallet) wallets.push(currentStepToBlockchain);
161
+ if (currentStepToBlockchain != lastAddedWallet)
162
+ wallets.push(currentStepToBlockchain);
142
163
  });
143
164
  return wallets;
144
165
  }
@@ -150,7 +171,7 @@ export function getSelectableWallets(
150
171
  accounts: Account[],
151
172
  selectedWallets: SelectedWallet[],
152
173
  getWalletInfo: (type: WalletType) => WalletInfo,
153
- requiredChains?: string[],
174
+ requiredChains?: string[]
154
175
  ) {
155
176
  const connectedWallets: SelectableWallet[] = accounts.map((account) => ({
156
177
  address: account.address,
@@ -159,28 +180,40 @@ export function getSelectableWallets(
159
180
  image: getWalletInfo(account.walletType).img,
160
181
  name: getWalletInfo(account.walletType).name,
161
182
  selected: !!selectedWallets.find(
162
- (wallet) => wallet.chain === account.chain && wallet.walletType === account.walletType,
183
+ (wallet) =>
184
+ wallet.chain === account.chain &&
185
+ wallet.walletType === account.walletType
163
186
  ),
164
187
  }));
165
188
 
166
189
  return requiredChains
167
- ? connectedWallets.filter((wallet) => requiredChains.includes(wallet.chain))
190
+ ? connectedWallets.filter(
191
+ (wallet, index, array) =>
192
+ requiredChains.includes(wallet.chain) &&
193
+ array.findIndex((w) => w.address === wallet.address) === index
194
+ )
168
195
  : removeDuplicateWallets(connectedWallets, 'walletType');
169
196
  }
170
197
 
171
- const removeDuplicateWallets = (arr: SelectableWallet[], key: string): SelectableWallet[] => {
172
- return [...new Map(arr.map((item) => [item[key], item])).values()];
198
+ const removeDuplicateWallets = (
199
+ arr: SelectableWallet[],
200
+ key: string
201
+ ): SelectableWallet[] => {
202
+ const map = new Map(arr.map((item) => [item[key], item]));
203
+ return Array.from(map.values());
173
204
  };
174
205
 
175
206
  export function getBalanceFromWallet(
176
207
  balances: Balance[],
177
208
  chain: string,
178
209
  symbol: string,
179
- address: string | null,
210
+ address: string | null
180
211
  ): TokenBalance | null {
181
212
  if (balances.length === 0) return null;
182
213
 
183
- const selectedChainBalances = balances.filter((balance) => balance.chain === chain);
214
+ const selectedChainBalances = balances.filter(
215
+ (balance) => balance.chain === chain
216
+ );
184
217
  if (selectedChainBalances.length === 0) return null;
185
218
 
186
219
  return (
@@ -190,11 +223,15 @@ export function getBalanceFromWallet(
190
223
  a.balances?.find(
191
224
  (bl) =>
192
225
  (address !== null && bl.address === address) ||
193
- (address === null && bl.address === address && bl.symbol === symbol),
194
- ) || null,
226
+ (address === null &&
227
+ bl.address === address &&
228
+ bl.symbol === symbol)
229
+ ) || null
195
230
  )
196
231
  .filter((b) => b !== null)
197
- .sort((a, b) => parseFloat(b?.amount || '0') - parseFloat(a?.amount || '1'))
232
+ .sort(
233
+ (a, b) => parseFloat(b?.amount || '0') - parseFloat(a?.amount || '1')
234
+ )
198
235
  .find(() => true) || null
199
236
  );
200
237
  }
@@ -210,9 +247,14 @@ export function isAccountAndBalanceMatched(account: Account, balance: Balance) {
210
247
  export function makeBalanceFor(
211
248
  account: Account,
212
249
  retrivedBalance: WalletDetail,
213
- tokens: Token[],
250
+ tokens: Token[]
214
251
  ): Balance {
215
- const { address, blockChain: chain, explorerUrl, balances = [] } = retrivedBalance;
252
+ const {
253
+ address,
254
+ blockChain: chain,
255
+ explorerUrl,
256
+ balances = [],
257
+ } = retrivedBalance;
216
258
  return {
217
259
  address,
218
260
  chain,
@@ -233,7 +275,12 @@ export function makeBalanceFor(
233
275
  .toFixed(),
234
276
  logo: '',
235
277
  usdPrice:
236
- getUsdPrice(chain, tokenBalance.asset.symbol, tokenBalance.asset.address, tokens) || null,
278
+ getUsdPrice(
279
+ chain,
280
+ tokenBalance.asset.symbol,
281
+ tokenBalance.asset.address,
282
+ tokens
283
+ ) || null,
237
284
  })) || [],
238
285
  };
239
286
  }
@@ -244,19 +291,28 @@ export function resetBalanceState(balance: Balance): Balance {
244
291
 
245
292
  export const calculateWalletUsdValue = (balance: Balance[]) => {
246
293
  const uniqueAccountAddresses = new Set<string | null>();
247
- const uniqueBalane: Balance[] = balance?.reduce((acc: Balance[], current: Balance) => {
248
- return acc.findIndex((i) => i.address === current.address && i.chain === current.chain) === -1
249
- ? [...acc, current]
250
- : acc;
251
- }, []);
294
+ const uniqueBalane: Balance[] = balance?.reduce(
295
+ (acc: Balance[], current: Balance) => {
296
+ return acc.findIndex(
297
+ (i) => i.address === current.address && i.chain === current.chain
298
+ ) === -1
299
+ ? [...acc, current]
300
+ : acc;
301
+ },
302
+ []
303
+ );
252
304
 
253
305
  const modifiedWalletBlockchains = uniqueBalane?.map((chain) => {
254
- const modifiedWalletBlockchain: Blockchain = { name: chain.chain, accounts: [] };
306
+ const modifiedWalletBlockchain: Blockchain = {
307
+ name: chain.chain,
308
+ accounts: [],
309
+ };
255
310
  if (!uniqueAccountAddresses.has(chain.address)) {
256
311
  uniqueAccountAddresses.add(chain.address);
257
312
  }
258
313
  uniqueAccountAddresses.forEach((accountAddress) => {
259
- if (chain.address === accountAddress) modifiedWalletBlockchain.accounts.push(chain);
314
+ if (chain.address === accountAddress)
315
+ modifiedWalletBlockchain.accounts.push(chain);
260
316
  });
261
317
  return modifiedWalletBlockchain;
262
318
  });
@@ -264,13 +320,12 @@ export const calculateWalletUsdValue = (balance: Balance[]) => {
264
320
  modifiedWalletBlockchains
265
321
  ?.flatMap((b) => b.accounts)
266
322
  ?.flatMap((a) => a?.balances)
267
- ?.map((b) => new BigNumber(b?.amount || ZERO).multipliedBy(b?.usdPrice || 0))
268
- ?.reduce((a, b) => a.plus(b), ZERO) || ZERO,
323
+ ?.map((b) =>
324
+ new BigNumber(b?.amount || ZERO).multipliedBy(b?.usdPrice || 0)
325
+ )
326
+ ?.reduce((a, b) => a.plus(b), ZERO) || ZERO
269
327
  ).toString();
270
328
 
271
- console.log('total');
272
- console.log(total);
273
-
274
329
  return numberWithThousandSeperator(total);
275
330
  };
276
331
 
@@ -283,38 +338,42 @@ function numberWithThousandSeperator(number: string | number): string {
283
338
  export const sortedTokens = (
284
339
  tokens: TokenWithBalance[],
285
340
  position: 'from' | 'to',
286
- balance: Balance[],
341
+ balance: Balance[]
287
342
  ): TokenWithBalance[] => {
288
343
  if (!tokens) return [];
289
344
  const walletSymbols = new Set(
290
345
  (balance || [])
291
346
  .flatMap((a) => a.balances || [])
292
347
  .filter((b) => (new BigNumber(b?.rawAmount) || ZERO).gt(0))
293
- .map((b) => `${b?.chain}.${b?.symbol}.${b?.address}`),
348
+ .map((b) => `${b?.chain}.${b?.symbol}.${b?.address}`)
294
349
  );
295
350
  let sortedList = [
296
- ...tokens.filter((t) => !t.address && walletSymbols.size && position === 'to'),
351
+ ...tokens.filter(
352
+ (t) => !t.address && walletSymbols.size && position === 'to'
353
+ ),
297
354
  ...tokens
298
355
  .filter(
299
356
  (t) =>
300
357
  !(position === 'to' && !t.address) &&
301
- walletSymbols.has(`${t.blockchain}.${t.symbol}.${t.address}`),
358
+ walletSymbols.has(`${t.blockchain}.${t.symbol}.${t.address}`)
302
359
  )
303
360
  .sort((tokenA, tokenB) => compareBalance(tokenA, tokenB, balance)),
304
361
  ...tokens.filter(
305
362
  (t) =>
306
363
  !walletSymbols.has(`${t.blockchain}.${t.symbol}.${t.address}`) &&
307
364
  !t.address &&
308
- (!walletSymbols.size || position === 'from'),
365
+ (!walletSymbols.size || position === 'from')
309
366
  ),
310
367
  ...tokens.filter(
311
368
  (t) =>
312
369
  !walletSymbols.has(`${t.blockchain}.${t.symbol}.${t.address}`) &&
313
370
  t.address &&
314
- !t.isSecondaryCoin,
371
+ !t.isSecondaryCoin
315
372
  ),
316
373
  ...tokens.filter(
317
- (t) => !walletSymbols.has(`${t.blockchain}.${t.symbol}.${t.address}`) && t.isSecondaryCoin,
374
+ (t) =>
375
+ !walletSymbols.has(`${t.blockchain}.${t.symbol}.${t.address}`) &&
376
+ t.isSecondaryCoin
318
377
  ),
319
378
  ];
320
379
 
@@ -324,15 +383,25 @@ export const sortedTokens = (
324
383
  const compareBalance = (
325
384
  tokenA: TokenWithBalance,
326
385
  tokenB: TokenWithBalance,
327
- wallet: Balance[],
386
+ wallet: Balance[]
328
387
  ): number => {
329
388
  if (!tokenA.usdPrice || !tokenB.usdPrice) return 0;
330
389
 
331
390
  const tokenAUsdValue = new BigNumber(
332
- getBalanceFromWallet(wallet, tokenA.blockchain, tokenA.symbol, tokenA.address)?.amount || ZERO,
391
+ getBalanceFromWallet(
392
+ wallet,
393
+ tokenA.blockchain,
394
+ tokenA.symbol,
395
+ tokenA.address
396
+ )?.amount || ZERO
333
397
  ).multipliedBy(tokenA.usdPrice);
334
398
  const tokenBUsdValue = new BigNumber(
335
- getBalanceFromWallet(wallet, tokenB.blockchain, tokenB.symbol, tokenB.address)?.amount || ZERO,
399
+ getBalanceFromWallet(
400
+ wallet,
401
+ tokenB.blockchain,
402
+ tokenB.symbol,
403
+ tokenB.address
404
+ )?.amount || ZERO
336
405
  ).multipliedBy(tokenB.usdPrice);
337
406
  if (tokenAUsdValue.gt(tokenBUsdValue)) return -1;
338
407
  return 1;
@@ -342,13 +411,36 @@ export const getUsdPrice = (
342
411
  blockchain: string,
343
412
  symbol: string,
344
413
  address: string | null,
345
- allTokens: Token[],
414
+ allTokens: Token[]
346
415
  ): number | null => {
347
416
  const token = allTokens?.find(
348
417
  (t) =>
349
418
  t.blockchain === blockchain &&
350
419
  t.symbol?.toUpperCase() === symbol?.toUpperCase() &&
351
- t.address === address,
420
+ t.address === address
352
421
  );
353
422
  return token?.usdPrice || null;
354
- };
423
+ };
424
+ export const isExperimentalChain = (
425
+ blockchains: BlockchainMeta[],
426
+ wallet: string
427
+ ): boolean => {
428
+ const cosmosExperimentalChainInfo = getCosmosExperimentalChainInfo(
429
+ Object.entries(blockchains)
430
+ .map(([, blockchainMeta]) => blockchainMeta)
431
+ .filter(isCosmosBlockchain)
432
+ );
433
+ return cosmosExperimentalChainInfo && !!cosmosExperimentalChainInfo[wallet];
434
+ };
435
+
436
+ export const getKeplrCompatibleConnectedWallets = (
437
+ selectableWallets: SelectableWallet[]
438
+ ): WalletType[] => {
439
+ const connectedWalletTypes = new Set(
440
+ selectableWallets.map((a: any) => a.walletType.toString())
441
+ );
442
+
443
+ return KEPLR_COMPATIBLE_WALLETS.filter((compatibleWallet) =>
444
+ connectedWalletTypes.has(compatibleWallet)
445
+ );
446
+ };