@rango-dev/widget-embedded 0.39.1-next.0 → 0.39.1-next.10

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.39.1-next.0",
3
+ "version": "0.39.1-next.10",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -16,7 +16,7 @@
16
16
  "ts-check": "tsc --declaration --emitDeclarationOnly -p ./tsconfig.json",
17
17
  "clean": "rimraf dist",
18
18
  "format": "prettier --write '{.,src}/**/*.{ts,tsx}'",
19
- "lint": "eslint \"**/*.{ts,tsx}\" --ignore-path ../../.eslintignore",
19
+ "lint": "eslint \"**/*.{ts,tsx}\"",
20
20
  "test": "vitest",
21
21
  "test:watch": "vitest --watch",
22
22
  "test:coverage": "vitest run --coverage"
@@ -24,24 +24,24 @@
24
24
  "dependencies": {
25
25
  "@lingui/core": "4.2.1",
26
26
  "@lingui/react": "4.2.1",
27
- "@rango-dev/logging-core": "^0.6.0",
28
- "@rango-dev/provider-all": "^0.42.1-next.5",
29
- "@rango-dev/queue-manager-core": "^0.27.0",
30
- "@rango-dev/queue-manager-rango-preset": "^0.42.1-next.0",
31
- "@rango-dev/queue-manager-react": "^0.27.0",
32
- "@rango-dev/signer-solana": "^0.36.0",
33
- "@rango-dev/ui": "^0.43.1-next.2",
34
- "@rango-dev/wallets-core": "^0.40.1-next.9",
35
- "@rango-dev/wallets-react": "^0.27.1-next.6",
36
- "@rango-dev/wallets-shared": "^0.41.1-next.1",
27
+ "@rango-dev/logging-core": "^0.6.1-next.0",
28
+ "@rango-dev/provider-all": "^0.42.1-next.12",
29
+ "@rango-dev/queue-manager-core": "^0.27.1-next.0",
30
+ "@rango-dev/queue-manager-rango-preset": "^0.42.1-next.5",
31
+ "@rango-dev/queue-manager-react": "^0.27.1-next.0",
32
+ "@rango-dev/signer-solana": "^0.36.1-next.1",
33
+ "@rango-dev/ui": "^0.43.1-next.6",
34
+ "@rango-dev/wallets-core": "^0.40.1-next.12",
35
+ "@rango-dev/wallets-react": "^0.27.1-next.13",
36
+ "@rango-dev/wallets-shared": "^0.41.1-next.5",
37
37
  "bignumber.js": "^9.1.1",
38
38
  "copy-to-clipboard": "^3.3.3",
39
39
  "dayjs": "^1.11.7",
40
40
  "ethers": "^6.13.2",
41
41
  "immer": "^9.0.19",
42
42
  "mitt": "^3.0.0",
43
- "rango-sdk": "^0.1.57",
44
- "rango-types": "^0.1.74",
43
+ "rango-sdk": "^0.1.62",
44
+ "rango-types": "^0.1.78",
45
45
  "react-i18next": "^12.2.0",
46
46
  "react-router-dom": "^6.8.0",
47
47
  "values.js": "2.1.1",
@@ -63,7 +63,7 @@ export function WalletList(props: PropTypes) {
63
63
  try {
64
64
  await suggestAndConnect(wallet.walletType, wallet.chain);
65
65
  setAddingExperimentalChainStatus('completed');
66
- } catch (e) {
66
+ } catch {
67
67
  setAddingExperimentalChainStatus('rejected');
68
68
  }
69
69
  };
@@ -77,10 +77,16 @@ function Main(props: PropsWithChildren<PropTypes>) {
77
77
  .filter(isEvmBlockchain)
78
78
  .map((chain) => chain.name);
79
79
 
80
- const onUpdateState: EventHandler = (type, event, value, state, meta) => {
80
+ const onUpdateState: EventHandler = (type, event, value, state, info) => {
81
+ if (event === Events.NAMESPACE_DISCONNECTED) {
82
+ removeBalancesForWallet(type, {
83
+ namespaces: value,
84
+ });
85
+ }
86
+
81
87
  if (event === Events.ACCOUNTS) {
82
88
  const supportedChainNames: Network[] | null =
83
- walletAndSupportedChainsNames(meta.supportedBlockchains);
89
+ walletAndSupportedChainsNames(info.supportedBlockchains);
84
90
 
85
91
  /*
86
92
  * When a wallet is connecting to an evm account, we will consider it as the account exists on other evm-compatible blockchains
@@ -88,48 +94,52 @@ function Main(props: PropsWithChildren<PropTypes>) {
88
94
  *
89
95
  * The logic here is for handling switching account functionality in wallets. when a wallet is switching to another account
90
96
  * we need to clean the balances for old accounts.
97
+ *
98
+ * Note: hub will do the cleanup on namespace diconnected event.
91
99
  */
92
- const evmAccounts: string[] = [];
93
- const nonEvmAccounts: string[] = [];
94
-
95
- value?.forEach((account: string) => {
96
- const { network } = readAccountAddress(account);
97
- if (evmBasedChainNames.includes(network)) {
98
- evmAccounts.push(account);
99
- } else {
100
- nonEvmAccounts.push(account);
101
- }
102
- });
103
-
104
- const previousAccounts = connectedWallets
105
- .filter((wallet) => wallet.walletType === type)
106
- .map((wallet) =>
107
- formatAddressWithNetwork(wallet.address, wallet.chain)
108
- );
109
-
110
- if (previousAccounts.length > 0) {
111
- if (evmAccounts.length > 0) {
112
- // We use same logic for removing as we use for adding.
113
- const data = prepareAccountsForWalletStore(
114
- type,
115
- evmAccounts,
116
- evmBasedChainNames,
117
- supportedChainNames,
118
- meta.isContractWallet
100
+ if (!info.isHub) {
101
+ const evmAccounts: string[] = [];
102
+ const nonEvmAccounts: string[] = [];
103
+
104
+ value?.forEach((account: string) => {
105
+ const { network } = readAccountAddress(account);
106
+ if (evmBasedChainNames.includes(network)) {
107
+ evmAccounts.push(account);
108
+ } else {
109
+ nonEvmAccounts.push(account);
110
+ }
111
+ });
112
+
113
+ const previousAccounts = connectedWallets
114
+ .filter((wallet) => wallet.walletType === type)
115
+ .map((wallet) =>
116
+ formatAddressWithNetwork(wallet.address, wallet.chain)
119
117
  );
120
118
 
121
- removeBalancesForWallet(type, {
122
- chains: data.map((account) => account.chain),
123
- });
124
- }
125
-
126
- if (nonEvmAccounts.length > 0) {
127
- removeBalancesForWallet(type, {
128
- chains: nonEvmAccounts.map((account) => {
129
- const { network } = readAccountAddress(account);
130
- return network;
131
- }),
132
- });
119
+ if (previousAccounts.length > 0) {
120
+ if (evmAccounts.length > 0) {
121
+ // We use same logic for removing as we use for adding.
122
+ const data = prepareAccountsForWalletStore(
123
+ type,
124
+ evmAccounts,
125
+ evmBasedChainNames,
126
+ supportedChainNames,
127
+ info.isContractWallet
128
+ );
129
+
130
+ removeBalancesForWallet(type, {
131
+ chains: data.map((account) => account.chain),
132
+ });
133
+ }
134
+
135
+ if (nonEvmAccounts.length > 0) {
136
+ removeBalancesForWallet(type, {
137
+ chains: nonEvmAccounts.map((account) => {
138
+ const { network } = readAccountAddress(account);
139
+ return network;
140
+ }),
141
+ });
142
+ }
133
143
  }
134
144
  }
135
145
 
@@ -140,10 +150,10 @@ function Main(props: PropsWithChildren<PropTypes>) {
140
150
  value,
141
151
  evmBasedChainNames,
142
152
  supportedChainNames,
143
- meta.isContractWallet
153
+ info.isContractWallet
144
154
  );
145
155
  if (data.length) {
146
- void newWalletConnected(data);
156
+ void newWalletConnected(data, info.namespace);
147
157
  }
148
158
  } else {
149
159
  disconnectWallet(type);
@@ -156,11 +166,15 @@ function Main(props: PropsWithChildren<PropTypes>) {
156
166
  }
157
167
  }
158
168
  }
159
- if (
160
- (event === Events.ACCOUNTS && state.connected) ||
161
- // Hub works differently, and this check should be enough.
162
- (event === Events.ACCOUNTS && meta.isHub)
163
- ) {
169
+
170
+ /*
171
+ * TODO: not sure why we used `Events.Acccounts` for detecting if a provider gets connected or not, if we can use `CONNCECTED` for the legacy
172
+ * we can only rely Events.Connected and remove the legacy conidition
173
+ */
174
+ const isLegacyProviderConnected =
175
+ event === Events.ACCOUNTS && state.connected;
176
+ const isHubPorviderConnected = event === Events.CONNECTED && info.isHub;
177
+ if (isLegacyProviderConnected || isHubPorviderConnected) {
164
178
  const key = `${type}-${state.network}-${value}`;
165
179
 
166
180
  if (!!onConnectWalletHandler.current) {
@@ -181,7 +195,7 @@ function Main(props: PropsWithChildren<PropTypes>) {
181
195
 
182
196
  // propagate updates for Dapps using external wallets
183
197
  if (props.onUpdateState) {
184
- props.onUpdateState(type, event, value, state, meta);
198
+ props.onUpdateState(type, event, value, state, info);
185
199
  }
186
200
  };
187
201
  const isActiveTab = useUiStore.use.isActiveTab();
@@ -15,6 +15,11 @@ export function useAppStore() {
15
15
 
16
16
  useEffect(() => {
17
17
  if (store && !store.persist.hasHydrated()) {
18
+ /*
19
+ * folan
20
+ * asds
21
+ *
22
+ */
18
23
  void store.persist.rehydrate();
19
24
  }
20
25
  }, []);
@@ -1,5 +1,6 @@
1
1
  import type { AppStoreState } from './types';
2
- import type { Token } from 'rango-sdk';
2
+ import type { Namespace } from '@rango-dev/wallets-core/namespaces/common';
3
+ import type { Token, WalletDetail } from 'rango-sdk';
3
4
  import type { StateCreator } from 'zustand';
4
5
 
5
6
  import BigNumber from 'bignumber.js';
@@ -45,6 +46,7 @@ export interface ConnectedWallet extends Wallet {
45
46
  selected: boolean;
46
47
  loading: boolean;
47
48
  error: boolean;
49
+ namespace?: Namespace;
48
50
  }
49
51
 
50
52
  interface DeprecatedTokenBalance {
@@ -63,6 +65,17 @@ export interface DeprecatedWalletDetail extends ConnectedWallet {
63
65
  balances: DeprecatedTokenBalance[] | null;
64
66
  }
65
67
 
68
+ function matchWalletDetailsWithConnectedWallet(
69
+ connectedWallet: ConnectedWallet,
70
+ walletsDetails: WalletDetail[]
71
+ ): WalletDetail | undefined {
72
+ return walletsDetails.find(
73
+ (walletDetails) =>
74
+ connectedWallet.address === walletDetails.address &&
75
+ connectedWallet.chain === walletDetails.blockChain
76
+ );
77
+ }
78
+
66
79
  export interface WalletsSlice {
67
80
  _balances: BalanceState;
68
81
  _aggregatedBalances: AggregatedBalanceState;
@@ -71,21 +84,28 @@ export interface WalletsSlice {
71
84
 
72
85
  setConnectedWalletAsRefetching: (walletType: string) => void;
73
86
  setConnectedWalletHasError: (walletType: string) => void;
74
- setConnectedWalletRetrievedData: (walletType: string) => void;
87
+ setConnectedWalletRetrievedData: (
88
+ walletType: string,
89
+ walletsDetails: WalletDetail[]
90
+ ) => void;
75
91
  removeBalancesForWallet: (
76
92
  walletType: string,
77
93
  options?: {
78
94
  chains?: string[];
95
+ namespaces?: Namespace[];
79
96
  }
80
97
  ) => void;
81
- addConnectedWallet: (accounts: Wallet[]) => void;
98
+ addConnectedWallet: (accounts: Wallet[], namespace?: Namespace) => void;
82
99
  setWalletsAsSelected: (
83
100
  wallets: { walletType: string; chain: string }[]
84
101
  ) => void;
85
102
  /**
86
103
  * Add new accounts to store and fetch balances for them.
87
104
  */
88
- newWalletConnected: (accounts: Wallet[]) => Promise<void>;
105
+ newWalletConnected: (
106
+ accounts: Wallet[],
107
+ namespace?: Namespace
108
+ ) => Promise<void>;
89
109
  /**
90
110
  * Disconnect a wallet and clean up balances after that.
91
111
  */
@@ -136,7 +156,10 @@ export const createWalletsSlice: StateCreator<
136
156
  };
137
157
  });
138
158
  },
139
- setConnectedWalletRetrievedData: (walletType: string) => {
159
+ setConnectedWalletRetrievedData: (
160
+ walletType: string,
161
+ walletsDetails: WalletDetail[]
162
+ ) => {
140
163
  set((state) => {
141
164
  return {
142
165
  fetchingWallets: false,
@@ -146,6 +169,11 @@ export const createWalletsSlice: StateCreator<
146
169
  ...connectedWallet,
147
170
  loading: false,
148
171
  error: false,
172
+ explorerUrl:
173
+ matchWalletDetailsWithConnectedWallet(
174
+ connectedWallet,
175
+ walletsDetails
176
+ )?.explorerUrl || connectedWallet.explorerUrl,
149
177
  };
150
178
  }
151
179
 
@@ -172,7 +200,7 @@ export const createWalletsSlice: StateCreator<
172
200
  };
173
201
  });
174
202
  },
175
- addConnectedWallet: (accounts: Wallet[]) => {
203
+ addConnectedWallet: (accounts, namespace) => {
176
204
  /*
177
205
  * When we are going to add a new account, there are two thing that can be happens:
178
206
  * 1. Wallet hasn't add yet.
@@ -192,33 +220,35 @@ export const createWalletsSlice: StateCreator<
192
220
  );
193
221
 
194
222
  if (walletsNeedToBeAdded.length > 0) {
195
- const newConnectedWallets = walletsNeedToBeAdded.map((account) => {
196
- /*
197
- * When a wallet is connecting, we will check if there is any `selected` wallet before, if not, we will consider this new wallet as connected.
198
- * In this way, when user tries to swap, we selected a wallet by default and don't need to do an extra click in ConfirmWalletModal
199
- */
200
- const shouldMarkWalletAsSelected = !connectedWallets.some(
201
- (connectedWallet) =>
202
- connectedWallet.chain === account.chain &&
203
- connectedWallet.selected &&
204
- /**
205
- * Sometimes, the connect function can be called multiple times for a particular wallet type when using the auto-connect feature.
206
- * This check is there to make sure the chosen wallet doesn't end up unselected.
207
- */
208
- connectedWallet.walletType !== account.walletType
209
- );
210
-
211
- return {
212
- address: account.address,
213
- chain: account.chain,
214
- explorerUrl: null,
215
- walletType: account.walletType,
216
- selected: shouldMarkWalletAsSelected,
217
-
218
- loading: false,
219
- error: false,
220
- };
221
- });
223
+ const newConnectedWallets: ConnectedWallet[] = walletsNeedToBeAdded.map(
224
+ (account) => {
225
+ /*
226
+ * When a wallet is connecting, we will check if there is any `selected` wallet before, if not, we will consider this new wallet as connected.
227
+ * In this way, when user tries to swap, we selected a wallet by default and don't need to do an extra click in ConfirmWalletModal
228
+ */
229
+ const shouldMarkWalletAsSelected = !connectedWallets.some(
230
+ (connectedWallet) =>
231
+ connectedWallet.chain === account.chain &&
232
+ connectedWallet.selected &&
233
+ /**
234
+ * Sometimes, the connect function can be called multiple times for a particular wallet type when using the auto-connect feature.
235
+ * This check is there to make sure the chosen wallet doesn't end up unselected.
236
+ */
237
+ connectedWallet.walletType !== account.walletType
238
+ );
239
+
240
+ return {
241
+ address: account.address,
242
+ chain: account.chain,
243
+ explorerUrl: null,
244
+ walletType: account.walletType,
245
+ selected: shouldMarkWalletAsSelected,
246
+ namespace: namespace,
247
+ loading: false,
248
+ error: false,
249
+ };
250
+ }
251
+ );
222
252
 
223
253
  set((state) => {
224
254
  /*
@@ -273,13 +303,13 @@ export const createWalletsSlice: StateCreator<
273
303
  connectedWallets: nextConnectedWalletsWithUpdatedSelectedStatus,
274
304
  });
275
305
  },
276
- newWalletConnected: async (accounts) => {
306
+ newWalletConnected: async (accounts, namespace) => {
277
307
  eventEmitter.emit(WidgetEvents.WalletEvent, {
278
308
  type: WalletEventTypes.CONNECT,
279
309
  payload: { walletType: accounts[0].walletType, accounts },
280
310
  });
281
311
 
282
- get().addConnectedWallet(accounts);
312
+ get().addConnectedWallet(accounts, namespace);
283
313
 
284
314
  void get().fetchBalances(accounts);
285
315
  },
@@ -310,6 +340,15 @@ export const createWalletsSlice: StateCreator<
310
340
  });
311
341
  }
312
342
 
343
+ if (!!options?.namespaces && options.namespaces.length > 0) {
344
+ walletsNeedsToBeRemoved = walletsNeedsToBeRemoved.filter((wallet) => {
345
+ if (wallet.namespace) {
346
+ return options.namespaces?.includes(wallet.namespace);
347
+ }
348
+ return false;
349
+ });
350
+ }
351
+
313
352
  const nextBalancesState: BalanceState = {};
314
353
  let nextAggregatedBalanceState: AggregatedBalanceState =
315
354
  get()._aggregatedBalances;
@@ -328,18 +367,17 @@ export const createWalletsSlice: StateCreator<
328
367
  }) === key
329
368
  );
330
369
 
331
- if (!shouldBalanceBeRemoved) {
332
- nextBalancesState[key] = currentBalancesState[key];
333
- }
334
-
335
370
  // if a balance should be removed, we need to remove its caches in _aggregatedBalances as wel.
336
371
  if (shouldBalanceBeRemoved) {
337
372
  nextAggregatedBalanceState = removeBalanceFromAggregatedBalance(
338
373
  nextAggregatedBalanceState,
339
374
  key
340
375
  );
376
+ } else {
377
+ nextBalancesState[key] = currentBalancesState[key];
341
378
  }
342
379
  });
380
+
343
381
  set({
344
382
  _balances: nextBalancesState,
345
383
  _aggregatedBalances: nextAggregatedBalanceState,
@@ -415,12 +453,12 @@ export const createWalletsSlice: StateCreator<
415
453
  }));
416
454
  const response = await httpService().getWalletsDetails(addressesToFetch);
417
455
 
418
- const listWalletsWithBalances = response.wallets;
456
+ const walletsDetails = response.wallets;
419
457
 
420
- if (listWalletsWithBalances) {
458
+ if (walletsDetails) {
421
459
  const { retryOnFailedBalances = true } = options || {};
422
460
  if (retryOnFailedBalances) {
423
- const failedWallets: Wallet[] = listWalletsWithBalances
461
+ const failedWallets: Wallet[] = walletsDetails
424
462
  .filter((wallet) => wallet.failed)
425
463
  .map((wallet) => ({
426
464
  chain: wallet.blockChain,
@@ -437,7 +475,7 @@ export const createWalletsSlice: StateCreator<
437
475
  let nextBalances: BalanceState = {};
438
476
  let nextAggregatedBalances: AggregatedBalanceState =
439
477
  get()._aggregatedBalances;
440
- listWalletsWithBalances.forEach((wallet) => {
478
+ walletsDetails.forEach((wallet) => {
441
479
  if (wallet.failed) {
442
480
  return;
443
481
  }
@@ -463,7 +501,7 @@ export const createWalletsSlice: StateCreator<
463
501
  _aggregatedBalances: nextAggregatedBalances,
464
502
  }));
465
503
 
466
- get().setConnectedWalletRetrievedData(walletType);
504
+ get().setConnectedWalletRetrievedData(walletType, walletsDetails);
467
505
  } else {
468
506
  get().setConnectedWalletHasError(walletType);
469
507
  throw new Error(
@@ -529,7 +567,6 @@ export const createWalletsSlice: StateCreator<
529
567
 
530
568
  return balancesForTargetWalletAddress;
531
569
  },
532
-
533
570
  getConnectedWalletsDetails: () => {
534
571
  const connectedWallets = get().connectedWallets;
535
572
  return connectedWallets.map((wallet) => {
@@ -545,28 +582,21 @@ export const createWalletsSlice: StateCreator<
545
582
  if (asset.blockchain === wallet.chain) {
546
583
  const token = get().findToken(asset);
547
584
 
548
- if (!!token) {
549
- const amount = balance.amount
550
- ? new BigNumber(balance.amount).shiftedBy(-balance.decimals)
551
- : ZERO;
552
-
553
- output.push({
554
- chain: wallet.chain,
555
- symbol: token.symbol,
556
- ticker: token.symbol,
557
- address: token.address,
558
- rawAmount: balance.amount,
559
- decimal: balance.decimals,
560
- amount: amount.toString(),
561
- logo: token.image,
562
- usdPrice: token.usdPrice,
563
- });
564
- } else {
565
- console.debug(
566
- "Looking for asset but it couldn't be found in tokens store. May not be provided in meta.'",
567
- asset
568
- );
569
- }
585
+ const amount = balance.amount
586
+ ? new BigNumber(balance.amount).shiftedBy(-balance.decimals)
587
+ : ZERO;
588
+
589
+ output.push({
590
+ chain: wallet.chain,
591
+ symbol: asset.symbol,
592
+ ticker: asset.symbol,
593
+ address: asset.address,
594
+ rawAmount: balance.amount,
595
+ decimal: balance.decimals,
596
+ amount: amount.toString(),
597
+ logo: token?.image || null,
598
+ usdPrice: token?.usdPrice || null,
599
+ });
570
600
  }
571
601
 
572
602
  return output;