-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathtokenConfigTests.ts
More file actions
562 lines (485 loc) · 20 KB
/
tokenConfigTests.ts
File metadata and controls
562 lines (485 loc) · 20 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
import 'should';
import {
coins,
CoinFamily,
Networks,
AccountCoin,
CoinFeature,
UnderlyingAsset,
BaseUnit,
KeyCurve,
CoinMap,
} from '../../src';
import {
getFormattedEthLikeTokenConfig,
getEthLikeTokens,
getFormattedTokens,
EthLikeTokenConfig,
} 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]))[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]))[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]))[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]))[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]))[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]))[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]));
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]));
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();
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]));
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 enabled chains', function () {
const result = getEthLikeTokens('Mainnet');
result.should.be.an.Object();
// The function filters by enabledChains which currently includes 'ip' and 'hypeevm'
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');
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');
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');
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');
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');
// 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 only include tokens from enabled chains', function () {
const mainnetResult = getEthLikeTokens('Mainnet');
const testnetResult = getEthLikeTokens('Testnet');
// Current implementation enables 'ip' and 'hypeevm' chains
const enabledChains = ['ip', 'hypeevm'];
Object.keys(mainnetResult).forEach((family) => {
enabledChains.should.containEql(family);
});
Object.keys(testnetResult).forEach((family) => {
enabledChains.should.containEql(family);
});
});
it('should return empty tokens array for chains without tokens', function () {
const result = getEthLikeTokens('Mainnet');
// 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');
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');
});
}
});
it('should return tokens with correct structure', function () {
const mainnetResult = getEthLikeTokens('Mainnet');
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');
const testnetResult = getEthLikeTokens('Testnet');
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');
const testnetResult = getEthLikeTokens('Testnet');
// 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);
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);
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);
});
});
});
});