@rango-dev/widget-embedded 0.23.1-next.20 → 0.23.1-next.22

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,4 +1,13 @@
1
+ import type { Token } from 'rango-sdk';
2
+
1
3
  import { WIDGET_UI_ID } from '../constants';
4
+ import {
5
+ MIN_LENGTH_SYMBOL_CONTAINS,
6
+ MIN_LENGTH_SYMBOL_START_WITH,
7
+ MIN_LENGTH_TOKEN_ADDRESS_CONTAINS,
8
+ MIN_LENGTH_TOKEN_NAME_CONTAINS,
9
+ MIN_LENGTH_TOKEN_NAME_EXACT_MATCH,
10
+ } from '../constants/searchFor';
2
11
 
3
12
  export function removeDuplicateFrom<T>(array: T[]): T[] {
4
13
  return Array.from(new Set(array));
@@ -32,8 +41,180 @@ export function containsText(text: string, searchText: string): boolean {
32
41
  return text.toLowerCase().indexOf(searchText.toLowerCase()) > -1;
33
42
  }
34
43
 
44
+ export function exactText(text: string, searchText: string): boolean {
45
+ return text.toLowerCase() === searchText.toLowerCase();
46
+ }
47
+
48
+ export function startWithText(text: string, searchText: string): boolean {
49
+ return text.toLowerCase().startsWith(searchText.toLowerCase());
50
+ }
51
+
35
52
  export const getContainer = () =>
36
53
  document.getElementById(WIDGET_UI_ID.SWAP_BOX_ID) as HTMLElement;
37
54
 
38
55
  export const getExpanded = () =>
39
56
  document.getElementById(WIDGET_UI_ID.EXPANDED_BOX_ID) as HTMLElement;
57
+
58
+ function compareExactMatchText(
59
+ searchFor: string,
60
+ textA: string | null,
61
+ textB: string | null
62
+ ) {
63
+ const exactMatchA = !!textA && exactText(textA, searchFor);
64
+ const exactMatchB = !!textB && exactText(textB, searchFor);
65
+ if (exactMatchA !== exactMatchB) {
66
+ return exactMatchA ? -1 : 1;
67
+ }
68
+
69
+ return 0;
70
+ }
71
+
72
+ function compareContainsText(
73
+ searchFor: string,
74
+ textA: string | null,
75
+ textB: string | null
76
+ ) {
77
+ const textContainsA = !!textA && containsText(textA, searchFor);
78
+ const textContainsB = !!textB && containsText(textB, searchFor);
79
+ if (textContainsA !== textContainsB) {
80
+ return textContainsA ? -1 : 1;
81
+ }
82
+
83
+ // If both texts contains the searched term, The shorter one has priority
84
+ if (textContainsA && textContainsB && textA?.length !== textB?.length) {
85
+ return textA?.length - textB?.length;
86
+ }
87
+
88
+ return 0;
89
+ }
90
+
91
+ function compareStartWithText(
92
+ searchFor: string,
93
+ textA: string | null,
94
+ textB: string | null
95
+ ) {
96
+ const textStartWithA = !!textA && startWithText(textA, searchFor);
97
+ const textStartWithB = !!textB && startWithText(textB, searchFor);
98
+ if (textStartWithA !== textStartWithB) {
99
+ return textStartWithA ? -1 : 1;
100
+ }
101
+
102
+ // If both texts start with the searched term, The shorter one has priority
103
+ if (textStartWithA && textStartWithB && textA?.length !== textB?.length) {
104
+ return textA?.length - textB?.length;
105
+ }
106
+
107
+ return 0;
108
+ }
109
+
110
+ export function compareWithSearchFor(
111
+ tokenA: Token,
112
+ tokenB: Token,
113
+ searchFor?: string
114
+ ): number {
115
+ if (!searchFor) {
116
+ return 0;
117
+ }
118
+
119
+ // 1: Check the order to exact match the symbol
120
+ const symbolExactMatchCompare = compareExactMatchText(
121
+ searchFor,
122
+ tokenA.symbol,
123
+ tokenB.symbol
124
+ );
125
+ if (symbolExactMatchCompare) {
126
+ return symbolExactMatchCompare;
127
+ }
128
+
129
+ // 2: Check the order to exact match the token name
130
+ if (searchFor.length >= MIN_LENGTH_TOKEN_NAME_EXACT_MATCH) {
131
+ const tokenNameExactMatchCompare = compareExactMatchText(
132
+ searchFor,
133
+ tokenA.name,
134
+ tokenB.name
135
+ );
136
+ if (tokenNameExactMatchCompare) {
137
+ return tokenNameExactMatchCompare;
138
+ }
139
+ }
140
+
141
+ // 3: Check symbol start with searchFor
142
+ if (searchFor.length >= MIN_LENGTH_SYMBOL_START_WITH) {
143
+ const symbolStartWithCompare = compareStartWithText(
144
+ searchFor,
145
+ tokenA.symbol,
146
+ tokenB.symbol
147
+ );
148
+
149
+ if (symbolStartWithCompare) {
150
+ return symbolStartWithCompare;
151
+ }
152
+ }
153
+
154
+ // 4: Check symbol Contains searchFor
155
+ if (searchFor.length >= MIN_LENGTH_SYMBOL_CONTAINS) {
156
+ const symbolContainsCompare = compareContainsText(
157
+ searchFor,
158
+ tokenA.symbol,
159
+ tokenB.symbol
160
+ );
161
+
162
+ if (symbolContainsCompare) {
163
+ return symbolContainsCompare;
164
+ }
165
+ }
166
+
167
+ // 5: Check token name start with searchFor
168
+ if (searchFor.length >= MIN_LENGTH_TOKEN_NAME_CONTAINS) {
169
+ const tokenNameStartWithCompare = compareStartWithText(
170
+ searchFor,
171
+ tokenA.name,
172
+ tokenB.name
173
+ );
174
+
175
+ if (tokenNameStartWithCompare) {
176
+ return tokenNameStartWithCompare;
177
+ }
178
+ }
179
+
180
+ // 6: Check token name Contains searchFor
181
+ if (searchFor.length >= MIN_LENGTH_TOKEN_NAME_CONTAINS) {
182
+ const tokenNameContainsCompare = compareContainsText(
183
+ searchFor,
184
+ tokenA.name,
185
+ tokenB.name
186
+ );
187
+
188
+ if (tokenNameContainsCompare !== 0) {
189
+ return tokenNameContainsCompare;
190
+ }
191
+ }
192
+
193
+ // 7: Check address start with searchFor
194
+ if (searchFor.length >= MIN_LENGTH_TOKEN_ADDRESS_CONTAINS) {
195
+ const addressStartWithCompare = compareStartWithText(
196
+ searchFor,
197
+ tokenA.address,
198
+ tokenB.address
199
+ );
200
+
201
+ if (addressStartWithCompare) {
202
+ return addressStartWithCompare;
203
+ }
204
+ }
205
+
206
+ // 8: Check address Contains searchFor
207
+ if (searchFor.length >= MIN_LENGTH_TOKEN_ADDRESS_CONTAINS) {
208
+ const addressContainCompare = compareContainsText(
209
+ searchFor,
210
+ tokenA.address,
211
+ tokenB.address
212
+ );
213
+
214
+ if (addressContainCompare !== 0) {
215
+ return addressContainCompare;
216
+ }
217
+ }
218
+
219
+ return 0;
220
+ }
package/src/utils/meta.ts CHANGED
@@ -31,3 +31,24 @@ export function findToken(t: Asset, tokens: Token[]) {
31
31
  export function findBlockchain(name: string, blockchains: BlockchainMeta[]) {
32
32
  return blockchains.find((blockchain) => blockchain.name === name) ?? null;
33
33
  }
34
+
35
+ export function isTokenNative(
36
+ token: Token,
37
+ blockchain: BlockchainMeta | undefined
38
+ ) {
39
+ if (!blockchain || !token) {
40
+ return false;
41
+ }
42
+
43
+ for (const feeAsset of blockchain.feeAssets) {
44
+ if (
45
+ token?.blockchain === feeAsset?.blockchain &&
46
+ token?.symbol === feeAsset?.symbol &&
47
+ token?.address === feeAsset?.address
48
+ ) {
49
+ return true;
50
+ }
51
+ }
52
+
53
+ return false;
54
+ }
@@ -381,25 +381,18 @@ export function formatBalance(balance: Balance | null): Balance | null {
381
381
  return formattedBalance;
382
382
  }
383
383
 
384
- function sortTokensByBalance(
384
+ export function compareTokenBalance(
385
385
  token1Balance: Balance | null,
386
386
  token2Balance: Balance | null
387
387
  ): number {
388
- if (token1Balance?.usdValue && token2Balance?.usdValue) {
388
+ if (token1Balance?.usdValue || token2Balance?.usdValue) {
389
389
  return (
390
- parseFloat(token2Balance.usdValue) - parseFloat(token1Balance.usdValue)
390
+ parseFloat(token2Balance?.usdValue || '0') -
391
+ parseFloat(token1Balance?.usdValue || '0')
391
392
  );
392
393
  }
393
394
 
394
- if (!token1Balance?.usdValue && token2Balance?.usdValue) {
395
- return 1;
396
- }
397
-
398
- if (token1Balance?.usdValue && !token2Balance?.usdValue) {
399
- return -1;
400
- }
401
-
402
- if (!token1Balance?.usdValue && !token2Balance?.usdValue) {
395
+ if (token1Balance?.amount || token2Balance?.amount) {
403
396
  return (
404
397
  parseFloat(token2Balance?.amount || '0') -
405
398
  parseFloat(token1Balance?.amount || '0')
@@ -409,41 +402,6 @@ function sortTokensByBalance(
409
402
  return 0;
410
403
  }
411
404
 
412
- function sortTokensByPinnedToken(
413
- token1: Token,
414
- token2: Token,
415
- isTokenPinned: (token: Token) => boolean
416
- ): number {
417
- const isToken1Pinned = isTokenPinned(token1);
418
- const isToken2Pinned = isTokenPinned(token2);
419
-
420
- if (isToken1Pinned === isToken2Pinned) {
421
- return 0;
422
- }
423
- if (isToken1Pinned) {
424
- return -1;
425
- }
426
- return 1;
427
- }
428
- export function sortTokens(
429
- tokens: Token[],
430
- getBalanceFor: (token: Token) => Balance | null,
431
- isTokenPinned: (token: Token) => boolean
432
- ) {
433
- tokens.sort((token1, token2) => {
434
- const token1Balance = getBalanceFor(token1);
435
- const token2Balance = getBalanceFor(token2);
436
- // Check pinned tokens
437
- const isTokenPin = sortTokensByPinnedToken(token1, token2, isTokenPinned);
438
-
439
- return isTokenPin !== 0
440
- ? isTokenPin
441
- : sortTokensByBalance(token1Balance, token2Balance);
442
- });
443
-
444
- return tokens;
445
- }
446
-
447
405
  export function areTokensEqual(
448
406
  tokenA: Pick<Token, 'blockchain' | 'symbol' | 'address'> | null,
449
407
  tokenB: Pick<Token, 'blockchain' | 'symbol' | 'address'> | null