-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathtokenConfigTests.ts
More file actions
764 lines (660 loc) · 27.5 KB
/
tokenConfigTests.ts
File metadata and controls
764 lines (660 loc) · 27.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
import 'should';
import {
coins,
CoinFamily,
Networks,
AccountCoin,
CoinFeature,
UnderlyingAsset,
BaseUnit,
KeyCurve,
CoinMap,
} from '../../src';
import {
getFormattedEthLikeTokenConfig,
getEthLikeTokens,
getFormattedTokens,
getFormattedTokensByNetwork,
verifyTokens,
EthLikeTokenConfig,
TokenTypeEnum,
BaseTokenConfig,
BaseContractAddressConfig,
} from '../../src/tokenConfig';
import { EthLikeERC20Token } from '../../src/account';
describe('EthLike Token Config Functions', function () {
describe('getEthLikeTokenConfig', function () {
it('should convert an EthLikeERC20Token to EthLikeTokenConfig for mainnet', function () {
// Create a mock mainnet EthLikeERC20Token
const mockMainnetToken = new EthLikeERC20Token({
id: '12345678-1234-4234-8234-123456789012',
name: 'ip:testtoken',
fullName: 'Test Token',
network: Networks.main.ip,
contractAddress: '0x1234567890123456789012345678901234567890',
decimalPlaces: 18,
asset: UnderlyingAsset.IP,
features: [...AccountCoin.DEFAULT_FEATURES, CoinFeature.EIP1559],
prefix: '',
suffix: 'TESTTOKEN',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: true,
baseUnit: BaseUnit.ETH,
});
const config = getFormattedEthLikeTokenConfig(CoinMap.fromCoins([mockMainnetToken]), TokenTypeEnum.ERC20)[0];
config.should.not.be.undefined();
config.type.should.equal('ip:testtoken');
config.coin.should.equal('ip');
config.network.should.equal('Mainnet');
config.name.should.equal('Test Token');
config.tokenContractAddress.should.equal('0x1234567890123456789012345678901234567890');
config.decimalPlaces.should.equal(18);
});
it('should convert an EthLikeERC20Token to EthLikeTokenConfig for testnet', function () {
// Create a mock testnet EthLikeERC20Token
const mockTestnetToken = new EthLikeERC20Token({
id: '22345678-2234-4234-9234-223456789012',
name: 'tip:testtoken',
fullName: 'Test Token Testnet',
network: Networks.test.ip,
contractAddress: '0xabcdef1234567890abcdef1234567890abcdef12',
decimalPlaces: 6,
asset: UnderlyingAsset.IP,
features: [...AccountCoin.DEFAULT_FEATURES, CoinFeature.EIP1559],
prefix: '',
suffix: 'TESTTOKEN',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: true,
baseUnit: BaseUnit.ETH,
});
const config = getFormattedEthLikeTokenConfig(CoinMap.fromCoins([mockTestnetToken]), TokenTypeEnum.ERC20)[0];
config.should.not.be.undefined();
config.type.should.equal('tip:testtoken');
config.coin.should.equal('tip');
config.network.should.equal('Testnet');
config.name.should.equal('Test Token Testnet');
config.tokenContractAddress.should.equal('0xabcdef1234567890abcdef1234567890abcdef12');
config.decimalPlaces.should.equal(6);
});
it('should lowercase the contract address', function () {
const mockToken = new EthLikeERC20Token({
id: '32345678-3234-4234-a234-323456789012',
name: 'ip:uppercase',
fullName: 'Uppercase Token',
network: Networks.main.ip,
contractAddress: '0xabcdef1234567890abcdef1234567890abcdef12',
decimalPlaces: 18,
asset: UnderlyingAsset.IP,
features: [...AccountCoin.DEFAULT_FEATURES, CoinFeature.EIP1559],
prefix: '',
suffix: 'UPPERCASE',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: true,
baseUnit: BaseUnit.ETH,
});
const config = getFormattedEthLikeTokenConfig(CoinMap.fromCoins([mockToken]), TokenTypeEnum.ERC20)[0];
config.tokenContractAddress.should.equal('0xabcdef1234567890abcdef1234567890abcdef12');
config.tokenContractAddress.should.not.match(/[A-F]/);
});
it('should extract coin name from token name using split', function () {
const mockToken = new EthLikeERC20Token({
id: '42345678-4234-4234-b234-423456789012',
name: 'ip:usdc',
fullName: 'USD Coin',
network: Networks.main.ip,
contractAddress: '0x1234567890123456789012345678901234567890',
decimalPlaces: 6,
asset: UnderlyingAsset.IP,
features: [...AccountCoin.DEFAULT_FEATURES, CoinFeature.EIP1559],
prefix: '',
suffix: 'USDC',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: true,
baseUnit: BaseUnit.ETH,
});
const config = getFormattedEthLikeTokenConfig(CoinMap.fromCoins([mockToken]), TokenTypeEnum.ERC20)[0];
config.coin.should.equal('ip');
config.type.should.equal('ip:usdc');
});
it('should convert an EthLikeERC20Token to EthLikeTokenConfig for hypeevm mainnet', function () {
// Create a mock mainnet EthLikeERC20Token for hypeevm
const mockMainnetToken = new EthLikeERC20Token({
id: 'a1234567-1234-4234-8234-123456789012',
name: 'hypeevm:testtoken',
fullName: 'HypeEVM Test Token',
network: Networks.main.hypeevm,
contractAddress: '0x9876543210987654321098765432109876543210',
decimalPlaces: 18,
asset: UnderlyingAsset.HYPEEVM,
features: [...AccountCoin.DEFAULT_FEATURES, CoinFeature.EIP1559],
prefix: '',
suffix: 'TESTTOKEN',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: true,
baseUnit: BaseUnit.ETH,
});
const config = getFormattedEthLikeTokenConfig(CoinMap.fromCoins([mockMainnetToken]), TokenTypeEnum.ERC20)[0];
config.should.not.be.undefined();
config.type.should.equal('hypeevm:testtoken');
config.coin.should.equal('hypeevm');
config.network.should.equal('Mainnet');
config.name.should.equal('HypeEVM Test Token');
config.tokenContractAddress.should.equal('0x9876543210987654321098765432109876543210');
config.decimalPlaces.should.equal(18);
});
it('should convert an EthLikeERC20Token to EthLikeTokenConfig for thypeevm testnet', function () {
// Create a mock testnet EthLikeERC20Token for thypeevm
const mockTestnetToken = new EthLikeERC20Token({
id: 'b2234567-2234-4234-9234-223456789012',
name: 'thypeevm:testtoken',
fullName: 'HypeEVM Test Token Testnet',
network: Networks.test.hypeevm,
contractAddress: '0xfedcba0987654321fedcba0987654321fedcba09',
decimalPlaces: 18,
asset: UnderlyingAsset.HYPEEVM,
features: [...AccountCoin.DEFAULT_FEATURES, CoinFeature.EIP1559],
prefix: '',
suffix: 'TESTTOKEN',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: true,
baseUnit: BaseUnit.ETH,
});
const config = getFormattedEthLikeTokenConfig(CoinMap.fromCoins([mockTestnetToken]), TokenTypeEnum.ERC20)[0];
config.should.not.be.undefined();
config.type.should.equal('thypeevm:testtoken');
config.coin.should.equal('thypeevm');
config.network.should.equal('Testnet');
config.name.should.equal('HypeEVM Test Token Testnet');
config.tokenContractAddress.should.equal('0xfedcba0987654321fedcba0987654321fedcba09');
config.decimalPlaces.should.equal(18);
});
});
describe('getFormattedEthLikeTokenConfig', function () {
it('should filter only EthLikeERC20Token instances from mixed coin types', function () {
const mockEthLikeToken = new EthLikeERC20Token({
id: '52345678-5234-4234-8234-523456789012',
name: 'ip:token1',
fullName: 'Token 1',
network: Networks.main.ip,
contractAddress: '0x1111111111111111111111111111111111111111',
decimalPlaces: 18,
asset: UnderlyingAsset.IP,
features: [...AccountCoin.DEFAULT_FEATURES, CoinFeature.EIP1559],
prefix: '',
suffix: 'TOKEN1',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: true,
baseUnit: BaseUnit.ETH,
});
const mockAccountCoin = new AccountCoin({
id: '62345678-6234-4234-9234-623456789012',
name: 'btc',
fullName: 'Bitcoin',
network: Networks.main.bitcoin,
decimalPlaces: 8,
asset: UnderlyingAsset.BTC,
features: [...AccountCoin.DEFAULT_FEATURES],
prefix: '',
suffix: 'BTC',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: false,
baseUnit: BaseUnit.BTC,
});
const result = getFormattedEthLikeTokenConfig(
CoinMap.fromCoins([mockEthLikeToken, mockAccountCoin]),
TokenTypeEnum.ERC20
);
result.length.should.equal(1);
result[0].type.should.equal('ip:token1');
});
it('should handle multiple EthLikeERC20Token instances', function () {
const mockToken1 = new EthLikeERC20Token({
id: '72345678-7234-4234-a234-723456789012',
name: 'ip:token1',
fullName: 'Token 1',
network: Networks.main.ip,
contractAddress: '0x1111111111111111111111111111111111111111',
decimalPlaces: 18,
asset: UnderlyingAsset.IP,
features: [...AccountCoin.DEFAULT_FEATURES, CoinFeature.EIP1559],
prefix: '',
suffix: 'TOKEN1',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: true,
baseUnit: BaseUnit.ETH,
});
const mockToken2 = new EthLikeERC20Token({
id: '82345678-8234-4234-b234-823456789012',
name: 'tip:token2',
fullName: 'Token 2',
network: Networks.test.ip,
contractAddress: '0x2222222222222222222222222222222222222222',
decimalPlaces: 6,
asset: UnderlyingAsset.IP,
features: [...AccountCoin.DEFAULT_FEATURES, CoinFeature.EIP1559],
prefix: '',
suffix: 'TOKEN2',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: true,
baseUnit: BaseUnit.ETH,
});
const result = getFormattedEthLikeTokenConfig(CoinMap.fromCoins([mockToken1, mockToken2]), TokenTypeEnum.ERC20);
result.length.should.equal(2);
result[0].type.should.equal('ip:token1');
result[0].network.should.equal('Mainnet');
result[1].type.should.equal('tip:token2');
result[1].network.should.equal('Testnet');
});
it('should use default coins map when no parameter is provided', function () {
const result = getFormattedEthLikeTokenConfig(undefined, TokenTypeEnum.ERC20);
result.should.be.an.Array();
// Check that it filters coins from the default coin map
result.forEach((config: EthLikeTokenConfig) => {
config.should.have.property('type');
config.should.have.property('coin');
config.should.have.property('network');
config.should.have.property('name');
config.should.have.property('tokenContractAddress');
config.should.have.property('decimalPlaces');
});
});
it('should return configs with all required properties', function () {
const mockToken = new EthLikeERC20Token({
id: '92345678-9234-4234-8234-923456789012',
name: 'ip:proptest',
fullName: 'Property Test Token',
network: Networks.main.ip,
contractAddress: '0x3333333333333333333333333333333333333333',
decimalPlaces: 12,
asset: UnderlyingAsset.IP,
features: [...AccountCoin.DEFAULT_FEATURES, CoinFeature.EIP1559],
prefix: '',
suffix: 'PROPTEST',
primaryKeyCurve: KeyCurve.Secp256k1,
isToken: true,
baseUnit: BaseUnit.ETH,
});
const result = getFormattedEthLikeTokenConfig(CoinMap.fromCoins([mockToken]), TokenTypeEnum.ERC20);
result[0].should.have.property('type').which.is.a.String();
result[0].should.have.property('coin').which.is.a.String();
result[0].should.have.property('network').which.is.a.String();
result[0].should.have.property('name').which.is.a.String();
result[0].should.have.property('tokenContractAddress').which.is.a.String();
result[0].should.have.property('decimalPlaces').which.is.a.Number();
});
});
describe('getEthLikeTokens', function () {
it('should return a map with tokens for EVM chains supporting ERC20', function () {
const result = getEthLikeTokens('Mainnet', TokenTypeEnum.ERC20);
result.should.be.an.Object();
// The function includes all EVM chains with SUPPORTS_ERC20 feature
if (result.ip) {
result.ip.should.have.property('tokens');
result.ip.tokens.should.be.an.Array();
}
if (result.hypeevm) {
result.hypeevm.should.have.property('tokens');
result.hypeevm.tokens.should.be.an.Array();
}
});
it('should filter mainnet tokens correctly', function () {
const result = getEthLikeTokens('Mainnet', TokenTypeEnum.ERC20);
Object.values(result).forEach((chainData) => {
chainData.tokens.forEach((token) => {
token.network.should.equal('Mainnet');
});
});
});
it('should filter testnet tokens correctly', function () {
const result = getEthLikeTokens('Testnet', TokenTypeEnum.ERC20);
Object.values(result).forEach((chainData) => {
chainData.tokens.forEach((token) => {
token.network.should.equal('Testnet');
});
});
});
it('should prepend "t" to coin name for testnet tokens', function () {
const result = getEthLikeTokens('Testnet', TokenTypeEnum.ERC20);
if (result.ip && result.ip.tokens.length > 0) {
result.ip.tokens.forEach((token) => {
token.coin.should.equal('tip');
});
}
if (result.hypeevm && result.hypeevm.tokens.length > 0) {
result.hypeevm.tokens.forEach((token) => {
token.coin.should.equal('thypeevm');
});
}
});
it('should not prepend "t" to coin name for mainnet tokens', function () {
const result = getEthLikeTokens('Mainnet', TokenTypeEnum.ERC20);
if (result.ip && result.ip.tokens.length > 0) {
result.ip.tokens.forEach((token) => {
token.coin.should.equal('ip');
});
}
if (result.hypeevm && result.hypeevm.tokens.length > 0) {
result.hypeevm.tokens.forEach((token) => {
token.coin.should.equal('hypeevm');
});
}
});
it('should only include tokens from chains with SUPPORTS_ERC20 feature', function () {
const result = getEthLikeTokens('Mainnet', TokenTypeEnum.ERC20);
// Verify that all included chains are AccountCoins with SUPPORTS_ERC20 feature
Object.keys(result).forEach((family) => {
const coin = coins.get(family as CoinFamily);
if (coin) {
coin.should.be.instanceOf(AccountCoin);
coin.features.should.containEql(CoinFeature.SUPPORTS_ERC20);
}
});
});
it('should return empty tokens array for chains without tokens', function () {
const result = getEthLikeTokens('Mainnet', TokenTypeEnum.ERC20);
// If a chain is in the result but has no tokens, it should have an empty array
Object.values(result).forEach((chainData) => {
chainData.tokens.should.be.an.Array();
});
});
it('should group tokens by their coin family', function () {
const result = getEthLikeTokens('Mainnet', TokenTypeEnum.ERC20);
if (result.ip && result.ip.tokens.length > 0) {
result.ip.tokens.forEach((token) => {
// All tokens in ip group should have coin 'ip'
token.coin.should.equal('ip');
});
}
if (result.hypeevm && result.hypeevm.tokens.length > 0) {
result.hypeevm.tokens.forEach((token) => {
// All tokens in hypeevm group should have coin 'hypeevm'
token.coin.should.equal('hypeevm');
});
}
if (result.plume && result.plume.tokens.length > 0) {
result.plume.tokens.forEach((token) => {
token.coin.should.equal('plume');
});
}
});
it('should return tokens with correct structure', function () {
const mainnetResult = getEthLikeTokens('Mainnet', TokenTypeEnum.ERC20);
Object.values(mainnetResult).forEach((chainData) => {
chainData.should.have.property('tokens');
chainData.tokens.should.be.an.Array();
chainData.tokens.forEach((token) => {
token.should.have.property('type');
token.should.have.property('coin');
token.should.have.property('network');
token.should.have.property('name');
token.should.have.property('tokenContractAddress');
token.should.have.property('decimalPlaces');
});
});
});
it('should handle both Mainnet and Testnet parameters', function () {
const mainnetResult = getEthLikeTokens('Mainnet', TokenTypeEnum.ERC20);
const testnetResult = getEthLikeTokens('Testnet', TokenTypeEnum.ERC20);
mainnetResult.should.be.an.Object();
testnetResult.should.be.an.Object();
// Verify network segregation
Object.values(mainnetResult).forEach((chainData) => {
chainData.tokens.forEach((token) => {
token.network.should.equal('Mainnet');
});
});
Object.values(testnetResult).forEach((chainData) => {
chainData.tokens.forEach((token) => {
token.network.should.equal('Testnet');
});
});
});
it('should not mix mainnet and testnet tokens', function () {
const mainnetResult = getEthLikeTokens('Mainnet', TokenTypeEnum.ERC20);
const testnetResult = getEthLikeTokens('Testnet', TokenTypeEnum.ERC20);
// Get all token types from mainnet
const mainnetTokenTypes = new Set<string>();
Object.values(mainnetResult).forEach((chainData) => {
chainData.tokens.forEach((token) => {
mainnetTokenTypes.add(token.type);
});
});
// Verify testnet tokens are different (should start with 't' prefix typically)
Object.values(testnetResult).forEach((chainData) => {
chainData.tokens.forEach((token) => {
// Testnet token types should not be in mainnet set
if (mainnetTokenTypes.has(token.type)) {
// This would be an error - same token in both networks
throw new Error(`Token ${token.type} appears in both mainnet and testnet`);
}
});
});
});
});
describe('Integration with real coins', function () {
it('should work with EthLikeERC20Token instances from the coins map', function () {
const ethLikeTokens = Array.from(coins).filter((coin) => coin instanceof EthLikeERC20Token);
if (ethLikeTokens.length > 0) {
const configs = getFormattedEthLikeTokenConfig(coins, TokenTypeEnum.ERC20);
configs.length.should.be.greaterThanOrEqual(0);
configs.forEach((config) => {
config.should.have.property('type');
config.should.have.property('coin');
config.should.have.property('network');
config.should.have.property('name');
config.should.have.property('tokenContractAddress');
config.should.have.property('decimalPlaces');
// Verify network is either Mainnet or Testnet
['Mainnet', 'Testnet'].should.containEql(config.network);
// Verify contract address is lowercase
config.tokenContractAddress.should.equal(config.tokenContractAddress.toLowerCase());
});
}
});
it('should correctly identify EthLikeERC20Token instances in coins map', function () {
let ethLikeTokenCount = 0;
coins.forEach((coin) => {
if (coin instanceof EthLikeERC20Token) {
ethLikeTokenCount++;
}
});
const formattedConfigs = getFormattedEthLikeTokenConfig(coins, TokenTypeEnum.ERC20);
formattedConfigs.length.should.equal(ethLikeTokenCount);
});
});
describe('getFormattedTokens', function () {
it('should return bitcoin and testnet properties with the same keys', function () {
const tokens = getFormattedTokens();
tokens.should.have.property('bitcoin');
tokens.should.have.property('testnet');
// Get all keys from bitcoin and testnet
const bitcoinKeys = Object.keys(tokens.bitcoin).sort();
const testnetKeys = Object.keys(tokens.testnet).sort();
// Both should have the same number of keys
bitcoinKeys.length.should.equal(testnetKeys.length);
// Both should have the exact same keys
bitcoinKeys.should.deepEqual(testnetKeys);
// Verify all keys are present in both
bitcoinKeys.forEach((key) => {
tokens.testnet.should.have.property(key);
});
testnetKeys.forEach((key) => {
tokens.bitcoin.should.have.property(key);
});
});
});
describe('getFormattedTokensByNetwork', function () {
it('should return tokens for Mainnet network', function () {
const result = getFormattedTokensByNetwork('Mainnet', coins);
result.should.be.an.Object();
result.should.have.property('eth');
result.eth.should.have.property('tokens');
result.eth.should.have.property('nfts');
// All eth tokens should be Mainnet
result.eth.tokens.forEach((token) => {
token.network.should.equal('Mainnet');
});
});
it('should return tokens for Testnet network', function () {
const result = getFormattedTokensByNetwork('Testnet', coins);
result.should.be.an.Object();
result.should.have.property('eth');
result.eth.should.have.property('tokens');
result.eth.should.have.property('nfts');
// All eth tokens should be Testnet
result.eth.tokens.forEach((token) => {
token.network.should.equal('Testnet');
});
});
it('should return the same chain keys for both networks', function () {
const mainnetResult = getFormattedTokensByNetwork('Mainnet', coins);
const testnetResult = getFormattedTokensByNetwork('Testnet', coins);
const mainnetKeys = Object.keys(mainnetResult).sort();
const testnetKeys = Object.keys(testnetResult).sort();
mainnetKeys.should.deepEqual(testnetKeys);
});
it('should have no duplicate token types within any chain', function () {
const mainnetResult = getFormattedTokensByNetwork('Mainnet', coins);
const testnetResult = getFormattedTokensByNetwork('Testnet', coins);
// Check for duplicates in Mainnet
Object.entries(mainnetResult).forEach(([chain, chainData]) => {
if (chainData.tokens && chainData.tokens.length > 0) {
const tokenTypes = chainData.tokens.map((t) => t.type);
const uniqueTokenTypes = new Set(tokenTypes);
const duplicates = tokenTypes.filter((t, i) => tokenTypes.indexOf(t) !== i);
tokenTypes.length.should.equal(
uniqueTokenTypes.size,
`Mainnet ${chain} has duplicate token types: ${duplicates}`
);
}
});
// Check for duplicates in Testnet
Object.entries(testnetResult).forEach(([chain, chainData]) => {
if (chainData.tokens && chainData.tokens.length > 0) {
const tokenTypes = chainData.tokens.map((t) => t.type);
const uniqueTokenTypes = new Set(tokenTypes);
const duplicates = tokenTypes.filter((t, i) => tokenTypes.indexOf(t) !== i);
tokenTypes.length.should.equal(
uniqueTokenTypes.size,
`Testnet ${chain} has duplicate token types: ${duplicates}`
);
}
});
});
it('should filter tokens correctly by network type', function () {
const mainnetResult = getFormattedTokensByNetwork('Mainnet', coins);
const testnetResult = getFormattedTokensByNetwork('Testnet', coins);
// Verify no testnet tokens in mainnet result
Object.values(mainnetResult).forEach((chainData) => {
if (chainData.tokens && chainData.tokens.length > 0) {
chainData.tokens.forEach((token) => {
if (token && token.network) {
token.network.should.equal('Mainnet');
}
});
}
if ('nfts' in chainData && chainData.nfts && chainData.nfts.length > 0) {
chainData.nfts.forEach((nft) => {
if (nft && nft.network) {
nft.network.should.equal('Mainnet');
}
});
}
});
// Verify no mainnet tokens in testnet result
Object.values(testnetResult).forEach((chainData) => {
if (chainData.tokens && chainData.tokens.length > 0) {
chainData.tokens.forEach((token) => {
if (token && token.network) {
token.network.should.equal('Testnet');
}
});
}
if ('nfts' in chainData && chainData.nfts && chainData.nfts.length > 0) {
chainData.nfts.forEach((nft) => {
if (nft && nft.network) {
nft.network.should.equal('Testnet');
}
});
}
});
});
});
describe('verifyTokens', function () {
it('should return verified tokens record when no duplicates exist', function () {
const mockTokens: BaseTokenConfig[] = [
{ type: 'token1', coin: 'eth', name: 'Token 1', decimalPlaces: 18 },
{ type: 'token2', coin: 'eth', name: 'Token 2', decimalPlaces: 18 },
{ type: 'token3', coin: 'eth', name: 'Token 3', decimalPlaces: 6 },
];
const result = verifyTokens(mockTokens);
result.should.be.an.Object();
result.should.have.property('token1', true);
result.should.have.property('token2', true);
result.should.have.property('token3', true);
});
it('should throw an error when duplicate token types exist', function () {
const mockTokensWithDuplicates: BaseTokenConfig[] = [
{ type: 'token1', coin: 'eth', name: 'Token 1', decimalPlaces: 18 },
{ type: 'token2', coin: 'eth', name: 'Token 2', decimalPlaces: 18 },
{ type: 'token1', coin: 'eth', name: 'Token 1 Duplicate', decimalPlaces: 18 }, // Duplicate
];
(() => {
verifyTokens(mockTokensWithDuplicates);
}).should.throw('token : token1 duplicated.');
});
it('should throw an error when token contract address is not lowercase', function () {
const mockTokensWithUppercaseAddress: BaseContractAddressConfig[] = [
{
type: 'token1',
coin: 'eth',
name: 'Token 1',
decimalPlaces: 18,
network: 'Mainnet',
tokenContractAddress: '0xAbCdEf1234567890AbCdEf1234567890AbCdEf12', // Mixed case
},
];
(() => {
verifyTokens(mockTokensWithUppercaseAddress);
}).should.throw(/token contract: token1 is not all lower case/);
});
it('should pass when token contract address is lowercase', function () {
const mockTokensWithLowercaseAddress: BaseContractAddressConfig[] = [
{
type: 'token1',
coin: 'eth',
name: 'Token 1',
decimalPlaces: 18,
network: 'Mainnet',
tokenContractAddress: '0xabcdef1234567890abcdef1234567890abcdef12', // All lowercase
},
];
const result = verifyTokens(mockTokensWithLowercaseAddress);
result.should.have.property('token1', true);
});
it('should handle empty token array', function () {
const result = verifyTokens([]);
result.should.be.an.Object();
Object.keys(result).length.should.equal(0);
});
it('should verify real tokens from getFormattedTokens have no duplicates', function () {
const formattedTokens = getFormattedTokens();
// Test mainnet eth tokens
(() => {
verifyTokens(formattedTokens.bitcoin.eth.tokens);
}).should.not.throw();
// Test testnet eth tokens
(() => {
verifyTokens(formattedTokens.testnet.eth.tokens);
}).should.not.throw();
// Test mainnet xlm tokens
(() => {
verifyTokens(formattedTokens.bitcoin.xlm.tokens);
}).should.not.throw();
// Test testnet xlm tokens
(() => {
verifyTokens(formattedTokens.testnet.xlm.tokens);
}).should.not.throw();
});
});
});