@rango-dev/widget-embedded 0.17.1-next.25 → 0.17.1-next.26

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 (34) hide show
  1. package/dist/components/ConfirmWalletsModal/ConfirmWallets.styles.d.ts +3 -3
  2. package/dist/components/Quote/Quote.styles.d.ts +1 -1
  3. package/dist/components/TokenList/TokenList.d.ts.map +1 -1
  4. package/dist/components/TokenList/TokenList.helpers.d.ts +2 -2
  5. package/dist/components/TokenList/TokenList.helpers.d.ts.map +1 -1
  6. package/dist/components/TokenList/TokenList.types.d.ts +3 -9
  7. package/dist/components/TokenList/TokenList.types.d.ts.map +1 -1
  8. package/dist/components/TokenList/index.d.ts +0 -1
  9. package/dist/components/TokenList/index.d.ts.map +1 -1
  10. package/dist/containers/Wallets/Wallets.d.ts.map +1 -1
  11. package/dist/index.js +1 -1
  12. package/dist/index.js.map +3 -3
  13. package/dist/pages/Home.d.ts.map +1 -1
  14. package/dist/pages/SelectSwapItemsPage.d.ts.map +1 -1
  15. package/dist/store/quote.d.ts +4 -5
  16. package/dist/store/quote.d.ts.map +1 -1
  17. package/dist/store/wallets.d.ts +5 -2
  18. package/dist/store/wallets.d.ts.map +1 -1
  19. package/dist/types/wallets.d.ts +15 -1
  20. package/dist/types/wallets.d.ts.map +1 -1
  21. package/dist/utils/wallets.d.ts +6 -8
  22. package/dist/utils/wallets.d.ts.map +1 -1
  23. package/package.json +3 -3
  24. package/src/components/TokenList/TokenList.helpers.ts +2 -2
  25. package/src/components/TokenList/TokenList.tsx +10 -12
  26. package/src/components/TokenList/TokenList.types.ts +3 -10
  27. package/src/components/TokenList/index.ts +0 -1
  28. package/src/containers/Wallets/Wallets.tsx +2 -3
  29. package/src/pages/Home.tsx +9 -33
  30. package/src/pages/SelectSwapItemsPage.tsx +3 -6
  31. package/src/store/quote.ts +2 -3
  32. package/src/store/wallets.ts +56 -29
  33. package/src/types/wallets.ts +18 -1
  34. package/src/utils/wallets.ts +80 -134
@@ -1,8 +1,8 @@
1
- import type { TokenWithBalance } from '../components/TokenList';
2
1
  import type { ConnectedWallet, TokenBalance } from '../store/wallets';
3
- import type { Wallet } from '../types';
2
+ import type { Balance, TokenHash, TokensBalance, Wallet } from '../types';
4
3
  import type { WalletInfo as ModalWalletInfo } from '@rango-dev/ui';
5
4
  import type {
5
+ Asset,
6
6
  Network,
7
7
  WalletInfo,
8
8
  WalletState,
@@ -29,6 +29,12 @@ import BigNumber from 'bignumber.js';
29
29
  import { isCosmosBlockchain } from 'rango-types';
30
30
 
31
31
  import { ZERO } from '../constants/numbers';
32
+ import {
33
+ BALANCE_MAX_DECIMALS,
34
+ BALANCE_MIN_DECIMALS,
35
+ USD_VALUE_MAX_DECIMALS,
36
+ USD_VALUE_MIN_DECIMALS,
37
+ } from '../constants/routing';
32
38
  import { EXCLUDED_WALLETS } from '../constants/wallets';
33
39
 
34
40
  import { numberToString } from './numbers';
@@ -218,43 +224,6 @@ export function getRequiredChains(quote: BestRouteResponse | null) {
218
224
 
219
225
  type Blockchain = { name: string; accounts: ConnectedWallet[] };
220
226
 
221
- export function getBalanceFromWallet(
222
- connectedWallets: ConnectedWallet[],
223
- chain: string,
224
- symbol: string,
225
- address: string | null
226
- ): TokenBalance | null {
227
- if (connectedWallets.length === 0) {
228
- return null;
229
- }
230
-
231
- const selectedChainWallets = connectedWallets.filter(
232
- (wallet) => wallet.chain === chain
233
- );
234
- if (selectedChainWallets.length === 0) {
235
- return null;
236
- }
237
-
238
- return (
239
- selectedChainWallets
240
- .map(
241
- (wallet) =>
242
- wallet.balances?.find(
243
- (balance) =>
244
- (address !== null && balance.address === address) ||
245
- (address === null &&
246
- balance.address === address &&
247
- balance.symbol === symbol)
248
- ) || null
249
- )
250
- .filter((balance) => balance !== null)
251
- .sort(
252
- (a, b) => parseFloat(b?.amount || '0') - parseFloat(a?.amount || '1')
253
- )
254
- .find(() => true) || null
255
- );
256
- }
257
-
258
227
  export function isAccountAndWalletMatched(
259
228
  account: Wallet,
260
229
  connectedWallet: ConnectedWallet
@@ -348,30 +317,6 @@ function numberWithThousandSeparator(number: string | number): string {
348
317
  return parts.join('.');
349
318
  }
350
319
 
351
- export const sortTokens = (tokens: TokenWithBalance[]): TokenWithBalance[] => {
352
- const walletConnected = !!tokens.some((token) => token.balance);
353
- if (!walletConnected) {
354
- return tokens
355
- .filter((token) => !token.address)
356
- .concat(
357
- tokens.filter((token) => token.address && !token.isSecondaryCoin),
358
- tokens.filter((token) => token.isSecondaryCoin)
359
- );
360
- }
361
-
362
- return tokens
363
- .filter((token) => !!token.balance)
364
- .sort(
365
- (tokenA, tokenB) =>
366
- parseFloat(tokenB.balance?.usdValue || '0') -
367
- parseFloat(tokenA.balance?.usdValue || '0')
368
- )
369
- .concat(
370
- tokens.filter((token) => !token.balance && !token.isSecondaryCoin),
371
- tokens.filter((token) => !token.balance && token.isSecondaryCoin)
372
- );
373
- };
374
-
375
320
  export const getUsdPrice = (
376
321
  blockchain: string,
377
322
  symbol: string,
@@ -415,98 +360,58 @@ export const getKeplrCompatibleConnectedWallets = (
415
360
  );
416
361
  };
417
362
 
418
- export function getTokensWithBalance(
419
- tokens: TokenWithBalance[],
420
- connectedWallets: ConnectedWallet[]
421
- ): TokenWithBalance[] {
422
- return tokens.map(({ balance, ...otherProps }) => {
423
- const tokenAmount = numberToString(
424
- new BigNumber(
425
- getBalanceFromWallet(
426
- connectedWallets,
427
- otherProps.blockchain,
428
- otherProps.symbol,
429
- otherProps.address
430
- )?.amount || ZERO
431
- )
432
- );
433
-
434
- let tokenUsdValue = '';
435
- if (otherProps.usdPrice) {
436
- tokenUsdValue = numberToString(
437
- new BigNumber(
438
- getBalanceFromWallet(
439
- connectedWallets,
440
- otherProps.blockchain,
441
- otherProps.symbol,
442
- otherProps.address
443
- )?.amount || ZERO
444
- ).multipliedBy(otherProps.usdPrice)
445
- );
446
- }
363
+ export function formatBalance(balance: Balance | null): Balance | null {
364
+ const formattedBalance: Balance | null = balance
365
+ ? {
366
+ ...balance,
367
+ amount: numberToString(
368
+ balance.amount,
369
+ BALANCE_MIN_DECIMALS,
370
+ BALANCE_MAX_DECIMALS
371
+ ),
372
+ usdValue: numberToString(
373
+ balance.usdValue,
374
+ USD_VALUE_MIN_DECIMALS,
375
+ USD_VALUE_MAX_DECIMALS
376
+ ),
377
+ }
378
+ : null;
447
379
 
448
- return {
449
- ...otherProps,
450
- ...(tokenAmount !== '0' && {
451
- balance: { amount: tokenAmount, usdValue: tokenUsdValue },
452
- }),
453
- };
454
- });
380
+ return formattedBalance;
455
381
  }
456
382
 
457
- export function getTokensBalanceFromWalletAndSort(
458
- tokens: TokenWithBalance[],
459
- connectedWallets: ConnectedWallet[]
383
+ export function sortTokensByBalance(
384
+ tokens: Token[],
385
+ getBalanceFor: (token: Token) => Balance | null
460
386
  ) {
461
- const list =
462
- connectedWallets.length > 0
463
- ? getTokensWithBalance(tokens, connectedWallets)
464
- : tokens;
465
- list.sort((tokenA, tokenB) => {
466
- if (tokenA.balance?.usdValue && tokenB.balance?.usdValue) {
387
+ tokens.sort((token1, token2) => {
388
+ const token1Balance = getBalanceFor(token1);
389
+ const token2Balance = getBalanceFor(token2);
390
+ if (token1Balance?.usdValue && token2Balance?.usdValue) {
467
391
  return (
468
- parseFloat(tokenB.balance.usdValue) -
469
- parseFloat(tokenA.balance.usdValue)
392
+ parseFloat(token2Balance.usdValue) - parseFloat(token1Balance.usdValue)
470
393
  );
471
394
  }
472
395
 
473
- if (!tokenA.balance?.usdValue && tokenB.balance?.usdValue) {
396
+ if (!token1Balance?.usdValue && token2Balance?.usdValue) {
474
397
  return 1;
475
398
  }
476
399
 
477
- if (tokenA.balance?.usdValue && !tokenB.balance?.usdValue) {
400
+ if (token1Balance?.usdValue && !token2Balance?.usdValue) {
478
401
  return -1;
479
402
  }
480
403
 
481
- if (!tokenA.balance?.usdValue && !tokenB.balance?.usdValue) {
404
+ if (!token1Balance?.usdValue && !token2Balance?.usdValue) {
482
405
  return (
483
- parseFloat(tokenB.balance?.amount || '0') -
484
- parseFloat(tokenA.balance?.amount || '0')
406
+ parseFloat(token2Balance?.amount || '0') -
407
+ parseFloat(token1Balance?.amount || '0')
485
408
  );
486
409
  }
487
410
 
488
411
  return 0;
489
412
  });
490
413
 
491
- return list;
492
- }
493
-
494
- export function getSortedTokens(
495
- chain: BlockchainMeta | null,
496
- tokens: Token[],
497
- connectedWallets: ConnectedWallet[],
498
- otherChainTokens: TokenWithBalance[]
499
- ): TokenWithBalance[] {
500
- const fromChainEqualsToToBlockchain =
501
- chain?.name === otherChainTokens[0]?.name;
502
- if (fromChainEqualsToToBlockchain) {
503
- return otherChainTokens;
504
- }
505
-
506
- const filteredTokens = tokens.filter(
507
- (token) => token.blockchain === chain?.name
508
- );
509
- return sortTokens(getTokensWithBalance(filteredTokens, connectedWallets));
414
+ return tokens;
510
415
  }
511
416
 
512
417
  export function tokensAreEqual(
@@ -568,3 +473,44 @@ export function getAddress({
568
473
  connectedWallet.chain === chain
569
474
  )?.address;
570
475
  }
476
+
477
+ export function createTokenHash(token: Asset): TokenHash {
478
+ return `${token.blockchain}-${token.symbol}-${token.address ?? ''}`;
479
+ }
480
+
481
+ export function makeTokensBalance(connectedWallets: ConnectedWallet[]) {
482
+ return connectedWallets
483
+ .flatMap((wallet) => wallet.balances)
484
+ .reduce((balances: TokensBalance, balance) => {
485
+ const currentBalance = {
486
+ amount: balance?.amount ?? '',
487
+ decimals: balance?.decimal ?? 0,
488
+ usdValue: balance?.usdPrice
489
+ ? new BigNumber(balance?.usdPrice ?? ZERO)
490
+ .multipliedBy(balance?.amount)
491
+ .toString()
492
+ : '',
493
+ };
494
+
495
+ const tokenHash = balance
496
+ ? createTokenHash({
497
+ symbol: balance.symbol,
498
+ blockchain: balance.chain,
499
+ address: balance.address,
500
+ })
501
+ : null;
502
+
503
+ const prevBalance = tokenHash ? balances[tokenHash] : null;
504
+
505
+ const shouldUpdateBalance =
506
+ tokenHash &&
507
+ (!prevBalance ||
508
+ (prevBalance && prevBalance.amount < currentBalance.amount));
509
+
510
+ if (shouldUpdateBalance) {
511
+ balances[tokenHash] = currentBalance;
512
+ }
513
+
514
+ return balances;
515
+ }, {});
516
+ }