@rango-dev/widget-embedded 0.40.2-next.10 → 0.40.2-next.11

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rango-dev/widget-embedded",
3
- "version": "0.40.2-next.10",
3
+ "version": "0.40.2-next.11",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -54,4 +54,4 @@
54
54
  "publishConfig": {
55
55
  "access": "public"
56
56
  }
57
- }
57
+ }
@@ -1,5 +1,6 @@
1
1
  import type { ConfigSlice } from './config';
2
2
  import type { DataSlice } from './data';
3
+ import type { WalletsSlice } from './wallets';
3
4
  import type { TokenData } from '../../components/TokenList/TokenList.types';
4
5
  import type { WidgetConfig } from '../../types';
5
6
  import type { SwapperMeta, Token } from 'rango-sdk';
@@ -53,7 +54,7 @@ export interface SettingsSlice {
53
54
  }
54
55
 
55
56
  export const createSettingsSlice: StateCreator<
56
- SettingsSlice & DataSlice & ConfigSlice,
57
+ SettingsSlice & DataSlice & ConfigSlice & WalletsSlice,
57
58
  [],
58
59
  [],
59
60
  SettingsSlice
@@ -187,10 +188,15 @@ export const createSettingsSlice: StateCreator<
187
188
  }),
188
189
  });
189
190
  },
190
- setCustomToken: (token) =>
191
+ setCustomToken: (token) => {
192
+ void get().fetchCustomTokensBalance({
193
+ tokens: [token],
194
+ connectedWallets: get().connectedWallets,
195
+ });
191
196
  set((state) => ({
192
197
  _customTokens: [token, ...state._customTokens],
193
- })),
198
+ }));
199
+ },
194
200
  deleteCustomToken: (token) =>
195
201
  set((state) => ({
196
202
  _customTokens: state._customTokens.filter(
@@ -1,6 +1,6 @@
1
1
  import type { AppStoreState } from './types';
2
2
  import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';
3
- import type { Token, WalletDetail } from 'rango-sdk';
3
+ import type { Asset, Token, WalletDetail } from 'rango-sdk';
4
4
 
5
5
  import BigNumber from 'bignumber.js';
6
6
 
@@ -24,6 +24,7 @@ import {
24
24
  extractAssetFromBalanceKey,
25
25
  removeBalanceFromAggregatedBalance,
26
26
  updateAggregatedBalanceStateForNewAccount,
27
+ updateBalancesWithNewPrices,
27
28
  } from '../utils/wallets';
28
29
 
29
30
  type WalletAddress = string;
@@ -124,6 +125,10 @@ export interface WalletsSlice {
124
125
  accounts: Wallet[],
125
126
  options?: { retryOnFailedBalances?: boolean }
126
127
  ) => Promise<void>;
128
+ fetchCustomTokensBalance: (params: {
129
+ tokens: Asset[];
130
+ connectedWallets: Wallet[];
131
+ }) => Promise<void>;
127
132
  getBalanceFor: (token: Token) => Balance | null;
128
133
  getBalances: () => BalanceState;
129
134
  getBalancesForWalletAddress: (address: string) => BalanceState;
@@ -295,6 +300,98 @@ export const createWalletsSlice = keepLastUpdated<AppStoreState, WalletsSlice>(
295
300
  });
296
301
  }
297
302
  },
303
+ fetchCustomTokensBalance: async (params) => {
304
+ const { tokens, connectedWallets } = params;
305
+
306
+ const tokensByBlockchain = tokens.reduce<{ [key: string]: Asset[] }>(
307
+ (acc, asset) => {
308
+ (acc[asset.blockchain] ||= []).push(asset);
309
+ return acc;
310
+ },
311
+ {}
312
+ );
313
+
314
+ const addedWallets = new Set<string>();
315
+
316
+ const tokensByWalletAddress = connectedWallets.reduce<{
317
+ [key: string]: Asset[];
318
+ }>((acc, wallet) => {
319
+ const key = `${wallet.address}-${wallet.chain}`;
320
+ if (addedWallets.has(key)) {
321
+ return acc;
322
+ }
323
+
324
+ addedWallets.add(key);
325
+ if (tokensByBlockchain[wallet.chain]) {
326
+ if (!acc[wallet.address]) {
327
+ acc[wallet.address] = [];
328
+ }
329
+ acc[wallet.address].push(...tokensByBlockchain[wallet.chain]);
330
+ }
331
+ return acc;
332
+ }, {});
333
+
334
+ Object.entries(tokensByWalletAddress).forEach(
335
+ async ([walletAddress, tokens]) => {
336
+ try {
337
+ const { balances } = await httpService().getMultipleTokenBalance({
338
+ assets: tokens.map(({ symbol, address, blockchain }) => ({
339
+ symbol,
340
+ address,
341
+ blockchain,
342
+ })),
343
+ walletAddress,
344
+ });
345
+
346
+ if (balances) {
347
+ let nextBalances: BalanceState = get()._balances;
348
+ let nextAggregatedBalances: AggregatedBalanceState =
349
+ get()._aggregatedBalances;
350
+
351
+ balances.forEach((balance) => {
352
+ if (parseFloat(balance.amount.amount) === 0) {
353
+ return;
354
+ }
355
+
356
+ const WalletDetail = {
357
+ blockChain: balance.asset.blockchain,
358
+ balances: [balance],
359
+ address: walletAddress,
360
+ };
361
+
362
+ updateBalancesWithNewPrices(WalletDetail, nextBalances, get);
363
+
364
+ const balancesForWallet = createBalanceStateForNewAccount(
365
+ WalletDetail,
366
+ get
367
+ );
368
+
369
+ nextAggregatedBalances =
370
+ updateAggregatedBalanceStateForNewAccount(
371
+ nextAggregatedBalances,
372
+ balancesForWallet
373
+ );
374
+
375
+ nextBalances = {
376
+ ...nextBalances,
377
+ ...balancesForWallet,
378
+ };
379
+ });
380
+
381
+ set((state) => ({
382
+ _balances: {
383
+ ...state._balances,
384
+ ...nextBalances,
385
+ },
386
+ _aggregatedBalances: nextAggregatedBalances,
387
+ }));
388
+ }
389
+ } catch (error) {
390
+ console.error(error);
391
+ }
392
+ }
393
+ );
394
+ },
298
395
  setWalletsAsSelected: (wallets) => {
299
396
  const nextConnectedWalletsWithUpdatedSelectedStatus =
300
397
  get().connectedWallets.map((connectedWallet) => {
@@ -332,6 +429,10 @@ export const createWalletsSlice = keepLastUpdated<AppStoreState, WalletsSlice>(
332
429
  get().addConnectedWallet(accounts, namespace);
333
430
 
334
431
  void get().fetchBalances(accounts);
432
+ void get().fetchCustomTokensBalance({
433
+ tokens: get().customTokens(),
434
+ connectedWallets: accounts,
435
+ });
335
436
  },
336
437
  removeBalancesForWallet: (walletType, options) => {
337
438
  let walletsNeedsToBeRemoved = get().connectedWallets.filter(
@@ -562,7 +663,7 @@ export const createWalletsSlice = keepLastUpdated<AppStoreState, WalletsSlice>(
562
663
  }
563
664
  }
564
665
 
565
- let nextBalances: BalanceState = {};
666
+ let nextBalances: BalanceState = get()._balances;
566
667
  let nextAggregatedBalances: AggregatedBalanceState =
567
668
  get()._aggregatedBalances;
568
669
  walletsDetails.forEach((wallet) => {
@@ -570,6 +671,8 @@ export const createWalletsSlice = keepLastUpdated<AppStoreState, WalletsSlice>(
570
671
  return;
571
672
  }
572
673
 
674
+ updateBalancesWithNewPrices(wallet, nextBalances, get);
675
+
573
676
  // Remove old balances for current wallet and blockchain
574
677
  get().removeBalancesForWallet(walletType, {
575
678
  chains: [wallet.blockChain],
@@ -44,8 +44,36 @@ export function extractAssetFromBalanceKey(key: BalanceKey): Asset {
44
44
  };
45
45
  }
46
46
 
47
+ export function updateBalancesWithNewPrices(
48
+ wallet: Omit<WalletDetail, 'failed' | 'explorerUrl'>,
49
+ balanceState: BalanceState,
50
+ store: () => AppStoreState
51
+ ): BalanceState {
52
+ wallet.balances?.forEach((balance) => {
53
+ const usdPrice =
54
+ balance.price ?? store().findToken(balance.asset)?.usdPrice;
55
+ const balancesToUpdate =
56
+ store()._aggregatedBalances[createAssetKey(balance.asset)];
57
+
58
+ balancesToUpdate?.forEach((balanceKey) => {
59
+ if (balanceState[balanceKey]) {
60
+ balanceState[balanceKey] = {
61
+ ...balanceState[balanceKey],
62
+ usdValue: usdPrice
63
+ ? new BigNumber(usdPrice ?? ZERO)
64
+ .multipliedBy(balanceState[balanceKey].amount)
65
+ .toString()
66
+ : '',
67
+ };
68
+ }
69
+ });
70
+ });
71
+
72
+ return balanceState;
73
+ }
74
+
47
75
  export function createBalanceStateForNewAccount(
48
- account: WalletDetail,
76
+ account: Omit<WalletDetail, 'failed' | 'explorerUrl'>,
49
77
  store: () => AppStoreState
50
78
  ): BalanceState {
51
79
  const state: BalanceState = {};
@@ -55,7 +83,8 @@ export function createBalanceStateForNewAccount(
55
83
  const amount = accountBalance.amount.amount;
56
84
  const decimals = accountBalance.amount.decimals;
57
85
 
58
- const usdPrice = store().findToken(accountBalance.asset)?.usdPrice;
86
+ const usdPrice =
87
+ accountBalance.price ?? store().findToken(accountBalance.asset)?.usdPrice;
59
88
  const usdValue = usdPrice
60
89
  ? new BigNumber(usdPrice ?? ZERO).multipliedBy(amount).toString()
61
90
  : '';