@rango-dev/widget-embedded 0.12.1-next.30 → 0.12.1-next.31

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 (69) hide show
  1. package/dist/Widget.d.ts.map +1 -1
  2. package/dist/components/AppRouter.d.ts +2 -0
  3. package/dist/components/AppRouter.d.ts.map +1 -1
  4. package/dist/components/AppRoutes.d.ts.map +1 -1
  5. package/dist/components/BlockchainsSection/BlockchainsSection.d.ts.map +1 -1
  6. package/dist/components/TokenList/TokenList.d.ts.map +1 -1
  7. package/dist/components/TokenList/TokenList.helpers.d.ts.map +1 -1
  8. package/dist/components/TokenList/TokenList.types.d.ts +0 -1
  9. package/dist/components/TokenList/TokenList.types.d.ts.map +1 -1
  10. package/dist/components/UpdateUrl.d.ts +6 -1
  11. package/dist/components/UpdateUrl.d.ts.map +1 -1
  12. package/dist/constants.d.ts +2 -0
  13. package/dist/constants.d.ts.map +1 -0
  14. package/dist/hooks/usePrepareBlockchainList.d.ts +18 -0
  15. package/dist/hooks/usePrepareBlockchainList.d.ts.map +1 -0
  16. package/dist/hooks/useSyncStoresWithConfig.d.ts +3 -0
  17. package/dist/hooks/useSyncStoresWithConfig.d.ts.map +1 -0
  18. package/dist/hooks/useWalletProviders.d.ts +3 -3
  19. package/dist/hooks/useWalletProviders.d.ts.map +1 -1
  20. package/dist/index.js +1 -1
  21. package/dist/index.js.map +4 -4
  22. package/dist/pages/SelectBlockchainPage.d.ts +1 -2
  23. package/dist/pages/SelectBlockchainPage.d.ts.map +1 -1
  24. package/dist/pages/SelectSwapItemsPage.d.ts +1 -5
  25. package/dist/pages/SelectSwapItemsPage.d.ts.map +1 -1
  26. package/dist/services/httpService.d.ts +0 -1
  27. package/dist/services/httpService.d.ts.map +1 -1
  28. package/dist/store/app.d.ts +26 -0
  29. package/dist/store/app.d.ts.map +1 -0
  30. package/dist/store/bestRoute.d.ts +4 -8
  31. package/dist/store/bestRoute.d.ts.map +1 -1
  32. package/dist/store/meta.d.ts +0 -2
  33. package/dist/store/meta.d.ts.map +1 -1
  34. package/dist/store/slices/config.d.ts +8 -0
  35. package/dist/store/slices/config.d.ts.map +1 -0
  36. package/dist/store/slices/data.d.ts +26 -0
  37. package/dist/store/slices/data.d.ts.map +1 -0
  38. package/dist/store/wallets.d.ts.map +1 -1
  39. package/dist/utils/configs.d.ts +3 -0
  40. package/dist/utils/configs.d.ts.map +1 -1
  41. package/dist/utils/wallets.d.ts +1 -1
  42. package/dist/utils/wallets.d.ts.map +1 -1
  43. package/package.json +2 -2
  44. package/src/Widget.tsx +11 -62
  45. package/src/components/AppRouter.tsx +3 -1
  46. package/src/components/AppRoutes.tsx +4 -28
  47. package/src/components/BlockchainsSection/BlockchainsSection.tsx +19 -41
  48. package/src/components/TokenList/TokenList.helpers.ts +6 -8
  49. package/src/components/TokenList/TokenList.tsx +24 -20
  50. package/src/components/TokenList/TokenList.types.ts +0 -1
  51. package/src/components/UpdateUrl.tsx +7 -1
  52. package/src/constants.ts +1 -0
  53. package/src/hooks/usePrepareBlockchainList.ts +122 -0
  54. package/src/hooks/useSyncStoresWithConfig.ts +102 -0
  55. package/src/hooks/useWalletProviders.ts +7 -7
  56. package/src/pages/SelectBlockchainPage.tsx +18 -23
  57. package/src/pages/SelectSwapItemsPage.tsx +52 -90
  58. package/src/services/httpService.ts +4 -5
  59. package/src/store/app.ts +15 -0
  60. package/src/store/bestRoute.ts +45 -71
  61. package/src/store/meta.ts +11 -29
  62. package/src/store/slices/config.ts +26 -0
  63. package/src/store/slices/data.ts +203 -0
  64. package/src/store/wallets.ts +0 -27
  65. package/src/utils/configs.ts +5 -2
  66. package/src/utils/wallets.ts +38 -18
  67. package/dist/components/BlockchainsSection/BlockchainSection.helpers.d.ts +0 -3
  68. package/dist/components/BlockchainsSection/BlockchainSection.helpers.d.ts.map +0 -1
  69. package/src/components/BlockchainsSection/BlockchainSection.helpers.ts +0 -20
@@ -0,0 +1,203 @@
1
+ // We keep all the received data from server in this slice
2
+
3
+ import type { ConfigSlice } from './config';
4
+ import type { BlockchainMeta, SwapperMeta, Token } from 'rango-sdk';
5
+ import type { StateCreator } from 'zustand';
6
+
7
+ import { httpService as sdk } from '../../services/httpService';
8
+ import { containsText } from '../../utils/common';
9
+ import { tokensAreEqual } from '../../utils/wallets';
10
+
11
+ type BlockchainOptions = {
12
+ type?: 'source' | 'destination';
13
+ };
14
+
15
+ type TokenOptions = {
16
+ type?: 'source' | 'destination';
17
+ // filter by an specific blockchain
18
+ blockchain?: string;
19
+ searchFor?: string;
20
+ };
21
+
22
+ export interface DataSlice {
23
+ _blockchains: BlockchainMeta[];
24
+ _tokens: Token[];
25
+ _popularTokens: Token[];
26
+ _swappers: SwapperMeta[];
27
+
28
+ blockchains: (options?: BlockchainOptions) => BlockchainMeta[];
29
+ tokens: (options?: TokenOptions) => Token[];
30
+ isTokenPinned: (token: Token) => boolean;
31
+
32
+ fetch: () => Promise<void>;
33
+ }
34
+
35
+ export const createDataSlice: StateCreator<
36
+ DataSlice & ConfigSlice,
37
+ [],
38
+ [],
39
+ DataSlice
40
+ > = (set, get) => ({
41
+ // State
42
+ _blockchains: [],
43
+ _tokens: [],
44
+ _popularTokens: [],
45
+ _swappers: [],
46
+
47
+ // Selectors
48
+ blockchains: (options) => {
49
+ const blockchainsFromState = get()._blockchains;
50
+
51
+ if (!options || !options?.type) {
52
+ return blockchainsFromState;
53
+ }
54
+
55
+ const config = get().config;
56
+ const supportedBlockchainsFromConfig =
57
+ (options.type === 'source'
58
+ ? config.from?.blockchains
59
+ : config.to?.blockchains) ?? [];
60
+
61
+ const list = blockchainsFromState.filter((blockchain) => {
62
+ if (
63
+ supportedBlockchainsFromConfig.length > 0 &&
64
+ !supportedBlockchainsFromConfig.includes(blockchain.name)
65
+ ) {
66
+ return false;
67
+ }
68
+
69
+ return true;
70
+ });
71
+
72
+ return list;
73
+ },
74
+ tokens: (options) => {
75
+ const tokensFromState = get()._tokens;
76
+
77
+ if (!options || !options?.type) {
78
+ return tokensFromState;
79
+ }
80
+
81
+ const config = get().config;
82
+ const supportedTokensFromConfig =
83
+ (options.type === 'source' ? config.from?.tokens : config.to?.tokens) ??
84
+ [];
85
+ const blockchains = get().blockchains({
86
+ type: options.type,
87
+ });
88
+
89
+ const list = tokensFromState
90
+ .filter((token) => {
91
+ // If there is a list of tokens in config, we only keep them.
92
+ if (
93
+ supportedTokensFromConfig.length > 0 &&
94
+ !supportedTokensFromConfig.some((asset) => {
95
+ return tokensAreEqual(asset, token);
96
+ })
97
+ ) {
98
+ return false;
99
+ }
100
+
101
+ // If a specific blockchain has passed, we only keep that blockchain's tokens.
102
+ if (!!options.blockchain && token.blockchain !== options.blockchain) {
103
+ return false;
104
+ }
105
+
106
+ // Check only available blockchains
107
+ if (
108
+ !blockchains.some((blockchain) => {
109
+ return blockchain.name === token.blockchain;
110
+ })
111
+ ) {
112
+ return false;
113
+ }
114
+
115
+ // Search functionality
116
+ if (options.searchFor) {
117
+ if (
118
+ containsText(token.symbol, options.searchFor) ||
119
+ containsText(token.address || '', options.searchFor) ||
120
+ containsText(token.name || '', options.searchFor)
121
+ ) {
122
+ return true;
123
+ }
124
+
125
+ return false;
126
+ }
127
+
128
+ return true;
129
+ })
130
+ .sort((a, b) => {
131
+ // Check pinned tokens
132
+ if (get().isTokenPinned(a)) {
133
+ return -1;
134
+ }
135
+ if (get().isTokenPinned(b)) {
136
+ return 1;
137
+ }
138
+
139
+ // Check secondary coins
140
+ if (a.isSecondaryCoin) {
141
+ return 1;
142
+ }
143
+ if (b.isSecondaryCoin) {
144
+ return -1;
145
+ }
146
+
147
+ // Check it has an address or not.
148
+ if (!a.address && b.address) {
149
+ return -1;
150
+ }
151
+ if (a.address && !b.address) {
152
+ return 1;
153
+ }
154
+
155
+ return 0;
156
+ });
157
+
158
+ return list;
159
+ },
160
+ isTokenPinned: (token) => {
161
+ const pinned = !!get().config.pinnedTokens?.some((pinnedToken) =>
162
+ tokensAreEqual(pinnedToken, token)
163
+ );
164
+ return pinned;
165
+ },
166
+
167
+ // Actions
168
+ fetch: async () => {
169
+ const response = await sdk().getAllMetadata();
170
+
171
+ const blockchains: BlockchainMeta[] = [];
172
+ const tokens: Token[] = [];
173
+ const popularTokens: Token[] = response.popularTokens;
174
+ const swappers: SwapperMeta[] = response.swappers;
175
+
176
+ const blockchainsWithAtLeastOneToken = new Set();
177
+
178
+ response.tokens.forEach((token) => {
179
+ blockchainsWithAtLeastOneToken.add(token.blockchain);
180
+
181
+ tokens.push(token);
182
+ });
183
+
184
+ response.blockchains.forEach((blockchain) => {
185
+ if (
186
+ blockchain.enabled &&
187
+ blockchainsWithAtLeastOneToken.has(blockchain.name)
188
+ ) {
189
+ blockchains.push(blockchain);
190
+ }
191
+ });
192
+
193
+ // Sort
194
+ blockchains.sort((a, b) => a.sort - b.sort);
195
+
196
+ set({
197
+ _blockchains: blockchains,
198
+ _tokens: tokens,
199
+ _popularTokens: popularTokens,
200
+ _swappers: swappers,
201
+ });
202
+ },
203
+ });
@@ -3,18 +3,14 @@ import type { WalletType } from '@rango-dev/wallets-shared';
3
3
 
4
4
  import { create } from 'zustand';
5
5
  import { subscribeWithSelector } from 'zustand/middleware';
6
- import { shallow } from 'zustand/shallow';
7
6
 
8
7
  import { httpService } from '../services/httpService';
9
8
  import {
10
- getTokensWithBalance,
11
9
  isAccountAndWalletMatched,
12
10
  makeBalanceFor,
13
11
  resetConnectedWalletState,
14
- sortTokens,
15
12
  } from '../utils/wallets';
16
13
 
17
- import { useBestRouteStore } from './bestRoute';
18
14
  import { useMetaStore } from './meta';
19
15
  import createSelectors from './selectors';
20
16
 
@@ -212,28 +208,5 @@ export const useWalletsStore = createSelectors(
212
208
  )
213
209
  );
214
210
 
215
- useWalletsStore.subscribe(
216
- (state) => state.connectedWallets,
217
- (connectedWallets) => {
218
- useBestRouteStore.setState(({ sourceTokens, destinationTokens }) => {
219
- const sourceTokensWithBalance = getTokensWithBalance(
220
- sourceTokens,
221
- connectedWallets
222
- );
223
- const destinationTokensWithBalance = getTokensWithBalance(
224
- destinationTokens,
225
- connectedWallets
226
- );
227
- return {
228
- sourceTokens: sortTokens(sourceTokensWithBalance),
229
- destinationTokens: sortTokens(destinationTokensWithBalance),
230
- };
231
- });
232
- },
233
- {
234
- equalityFn: shallow,
235
- }
236
- );
237
-
238
211
  export const fetchingBalanceSelector = (state: WalletsStore) =>
239
212
  !!state.connectedWallets.find((wallet) => wallet.loading);
@@ -1,9 +1,9 @@
1
+ import { RANGO_PUBLIC_API_KEY } from '../constants';
2
+
1
3
  export interface Configs {
2
4
  API_KEY: string;
3
5
  }
4
6
 
5
- const RANGO_PUBLIC_API_KEY = 'c6381a79-2817-4602-83bf-6a641a409e32';
6
-
7
7
  let configs: Configs = {
8
8
  API_KEY: RANGO_PUBLIC_API_KEY,
9
9
  };
@@ -18,6 +18,9 @@ export function setConfig(name: keyof Configs, value: any) {
18
18
  return value;
19
19
  }
20
20
 
21
+ /**
22
+ * Getting new configs from params and reset the value of global `configs` with provided param.
23
+ */
21
24
  export function initConfig(nextConfigs: Configs) {
22
25
  let clonedConfigs;
23
26
  if (typeof structuredClone === 'function') {
@@ -428,7 +428,6 @@ export function getTokensWithBalance(
428
428
  tokens: TokenWithBalance[],
429
429
  connectedWallets: ConnectedWallet[]
430
430
  ): TokenWithBalance[] {
431
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
432
431
  return tokens.map(({ balance, ...otherProps }) => {
433
432
  const tokenAmount = numberToString(
434
433
  new BigNumber(
@@ -464,6 +463,43 @@ export function getTokensWithBalance(
464
463
  });
465
464
  }
466
465
 
466
+ export function getTokensBalanceFromWalletAndSort(
467
+ tokens: TokenWithBalance[],
468
+ connectedWallets: ConnectedWallet[]
469
+ ) {
470
+ const list =
471
+ connectedWallets.length > 0
472
+ ? getTokensWithBalance(tokens, connectedWallets)
473
+ : tokens;
474
+ list.sort((tokenA, tokenB) => {
475
+ if (tokenA.balance?.usdValue && tokenB.balance?.usdValue) {
476
+ return (
477
+ parseFloat(tokenB.balance.usdValue) -
478
+ parseFloat(tokenA.balance.usdValue)
479
+ );
480
+ }
481
+
482
+ if (!tokenA.balance?.usdValue && tokenB.balance?.usdValue) {
483
+ return 1;
484
+ }
485
+
486
+ if (tokenA.balance?.usdValue && !tokenB.balance?.usdValue) {
487
+ return -1;
488
+ }
489
+
490
+ if (!tokenA.balance?.usdValue && !tokenB.balance?.usdValue) {
491
+ return (
492
+ parseFloat(tokenB.balance?.amount || '0') -
493
+ parseFloat(tokenA.balance?.amount || '0')
494
+ );
495
+ }
496
+
497
+ return 0;
498
+ });
499
+
500
+ return list;
501
+ }
502
+
467
503
  export function getSortedTokens(
468
504
  chain: BlockchainMeta | null,
469
505
  tokens: Token[],
@@ -475,6 +511,7 @@ export function getSortedTokens(
475
511
  if (fromChainEqualsToToBlockchain) {
476
512
  return otherChainTokens;
477
513
  }
514
+
478
515
  const filteredTokens = tokens.filter(
479
516
  (token) => token.blockchain === chain?.name
480
517
  );
@@ -492,23 +529,6 @@ export function tokensAreEqual(
492
529
  );
493
530
  }
494
531
 
495
- export function getDefaultToken(
496
- sortedTokens: TokenWithBalance[],
497
- otherToken: TokenWithBalance | null
498
- ): TokenWithBalance {
499
- let selectedToken: TokenWithBalance;
500
- const firstToken = sortedTokens[0];
501
- const secondToken = sortedTokens[1];
502
- if (sortedTokens.length === 1) {
503
- selectedToken = firstToken;
504
- } else if (tokensAreEqual(firstToken, otherToken)) {
505
- selectedToken = secondToken;
506
- } else {
507
- selectedToken = firstToken;
508
- }
509
- return selectedToken;
510
- }
511
-
512
532
  export function sortWalletsBasedOnConnectionState(
513
533
  wallets: ModalWalletInfo[]
514
534
  ): ModalWalletInfo[] {
@@ -1,3 +0,0 @@
1
- import type { BlockchainMeta } from 'rango-sdk';
2
- export declare const sortBlockchains: (blockchains: BlockchainMeta[]) => BlockchainMeta[];
3
- //# sourceMappingURL=BlockchainSection.helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BlockchainSection.helpers.d.ts","sourceRoot":"","sources":["../../../src/components/BlockchainsSection/BlockchainSection.helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhD,eAAO,MAAM,eAAe,gBAAiB,cAAc,EAAE,qBAiB5D,CAAC"}
@@ -1,20 +0,0 @@
1
- import type { BlockchainMeta } from 'rango-sdk';
2
-
3
- export const sortBlockchains = (blockchains: BlockchainMeta[]) => {
4
- const importantBlockchains: BlockchainMeta[] = [];
5
- const otherBlockchains: BlockchainMeta[] = [];
6
-
7
- for (const blockchain of blockchains) {
8
- if (
9
- blockchain.name === 'ETH' ||
10
- blockchain.name === 'COSMOS' ||
11
- blockchain.name === 'OSMOSIS'
12
- ) {
13
- importantBlockchains.push(blockchain);
14
- } else {
15
- otherBlockchains.push(blockchain);
16
- }
17
- }
18
-
19
- return importantBlockchains.concat(otherBlockchains);
20
- };