@swapkit/helpers 2.7.7 → 2.8.0

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.
@@ -95,8 +95,12 @@ export function getSymmetricPoolShare({
95
95
  assetDepth: string;
96
96
  }>) {
97
97
  return {
98
- assetAmount: toTCSwapKitNumber(assetDepth).mul(liquidityUnits).div(poolUnits),
99
- runeAmount: toTCSwapKitNumber(runeDepth).mul(liquidityUnits).div(poolUnits),
98
+ assetAmount: toTCSwapKitNumber(assetDepth)
99
+ .mul(liquidityUnits)
100
+ .div(poolUnits || "1"),
101
+ runeAmount: toTCSwapKitNumber(runeDepth)
102
+ .mul(liquidityUnits)
103
+ .div(poolUnits || "1"),
100
104
  };
101
105
  }
102
106
 
@@ -13,7 +13,7 @@ describe("AssetValue", () => {
13
13
  symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
14
14
  });
15
15
  expect(fakeAvaxUSDCAsset.toString()).toBe(
16
- "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
16
+ "AVAX.USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
17
17
  );
18
18
 
19
19
  const ethSynth = new AssetValue({
@@ -79,13 +79,15 @@ describe("AssetValue", () => {
79
79
 
80
80
  expect(atomDerived.toString()).toBe("THOR.ATOM");
81
81
 
82
+ const value = 10;
82
83
  const mayaCacao = AssetValue.from({
83
84
  asset: "MAYA.CACAO",
84
- value: 10,
85
+ value,
85
86
  });
86
87
 
87
88
  expect(mayaCacao.toString()).toBe("MAYA.CACAO");
88
- expect(mayaCacao.getBaseValue("string")).toBe("1000000000");
89
+ const expectedValue = value * 10_000_000_000;
90
+ expect(mayaCacao.getBaseValue("string")).toBe(expectedValue.toString());
89
91
 
90
92
  const ethMayaSynth = AssetValue.from({
91
93
  asset: "MAYA.ETH/ETH",
@@ -105,29 +107,38 @@ describe("AssetValue", () => {
105
107
  expect(ethTCSynthFallback.toString({ includeSynthProtocol: true })).toBe("THOR.ETH/ETH");
106
108
  expect(ethTCSynthFallback.getBaseValue("string")).toBe("1000000000");
107
109
 
108
- // TODO:
109
- // const radixXWBTC = new AssetValue({
110
- // identifier:
111
- // "RADIX.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
112
- // decimal: 8,
113
- // value: 11112222,
114
- // });
115
-
116
- // expect(radixXWBTC.toString()).toBe(
117
- // "RADIX.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
118
- // )
110
+ const solFromString = AssetValue.from({ asset: "SOL.SOL" });
111
+ expect(solFromString.toString()).toBe("SOL.SOL");
112
+ });
113
+
114
+ test("regres cases", () => {
115
+ const arbWeth = AssetValue.from({
116
+ asset: "ARB.WETH-0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
117
+ });
118
+ expect(arbWeth.toString()).toBe("ARB.WETH-0x82aF49447D8a07e3bd95BD0d56f35241523fBab1");
119
+
120
+ const baseAssetFromString = AssetValue.from({
121
+ asset: "BASE.USDC-0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
122
+ });
123
+ expect(baseAssetFromString.toString()).toBe(
124
+ "BASE.USDC-0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
125
+ );
126
+
127
+ const avaxSolanaAsset = AssetValue.from({
128
+ asset: "AVAX.SOL-0XFE6B19286885A4F7F55ADAD09C3CD1F906D2478F",
129
+ });
130
+ expect(avaxSolanaAsset.toString()).toBe(
131
+ "AVAX.SOL-0xFE6B19286885a4F7F55AdAD09C3Cd1f906D2478F",
132
+ );
119
133
  });
120
134
  });
121
135
 
122
136
  describe("set", () => {
123
137
  test("get a copy of an assetValue with a new value", () => {
124
- const btc = AssetValue.from({
125
- asset: "BTC.BTC",
126
- });
138
+ const btc = AssetValue.from({ asset: "BTC.BTC" });
127
139
 
128
140
  const btc2 = btc.set(10);
129
141
 
130
- expect(btc2).toBeDefined();
131
142
  expect(btc2).toEqual(
132
143
  expect.objectContaining({
133
144
  chain: Chain.Bitcoin,
@@ -179,11 +190,11 @@ describe("AssetValue", () => {
179
190
  symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
180
191
  });
181
192
  expect(fakeAvaxUSDCAsset.toUrl()).toBe(
182
- "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
193
+ "AVAX.USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
183
194
  );
184
195
 
185
196
  const thor = AssetValue.from({ asset: "ETH.THOR" });
186
- expect(thor.toUrl()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
197
+ expect(thor.toUrl()).toBe("ETH.THOR-0xa5f2211B9b8170F694421f2046281775E8468044");
187
198
 
188
199
  const ethSynth = new AssetValue({
189
200
  chain: Chain.THORChain,
@@ -288,13 +299,21 @@ describe("AssetValue", () => {
288
299
  chain: Chain.Avalanche,
289
300
  symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
290
301
  });
291
- expect(avaxUSDCAsset.toString()).toBe("AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e");
302
+ expect(avaxUSDCAsset.toString()).toBe("AVAX.USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E");
292
303
 
293
304
  const thor = AssetValue.from({ asset: "ETH.THOR" });
294
- expect(thor.toString()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
305
+ expect(thor.toString()).toBe("ETH.THOR-0xa5f2211B9b8170F694421f2046281775E8468044");
295
306
 
296
- const ethSynth = await AssetValue.from({ asset: "ETH/ETH", asyncTokenLookup: true });
307
+ const ethSynth = await AssetValue.from({
308
+ asset: "ETH/ETH",
309
+ asyncTokenLookup: true,
310
+ });
297
311
  expect(ethSynth.toString()).toBe("ETH/ETH");
312
+
313
+ const ethFromChain = await AssetValue.from({
314
+ chain: Chain.Ethereum,
315
+ });
316
+ expect(ethFromChain.toString()).toBe("ETH.ETH");
298
317
  });
299
318
  });
300
319
 
@@ -307,12 +326,12 @@ describe("AssetValue", () => {
307
326
 
308
327
  expect(avaxUSDCAsset).toEqual(
309
328
  expect.objectContaining({
310
- address: "0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
329
+ address: "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
311
330
  chain: Chain.Avalanche,
312
331
  decimal: 6,
313
332
  isGasAsset: false,
314
333
  isSynthetic: false,
315
- symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
334
+ symbol: "USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
316
335
  ticker: "USDC",
317
336
  }),
318
337
  );
@@ -441,7 +460,7 @@ describe("AssetValue", () => {
441
460
 
442
461
  expect(synthETH.toString()).toBe("ETH/ETH");
443
462
  expect(eth.toString()).toBe("ETH.ETH");
444
- expect(thor.toString()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
463
+ expect(thor.toString()).toBe("ETH.THOR-0xa5f2211B9b8170F694421f2046281775E8468044");
445
464
  expect(synthThor.toString()).toBe("ETH/THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
446
465
  expect(synthDashes.toString()).toBe("ETH/PENDLE-LPT-0x1234");
447
466
  });
@@ -457,12 +476,12 @@ describe("AssetValue", () => {
457
476
  expect(thor).toBeDefined();
458
477
  expect(thor).toEqual(
459
478
  expect.objectContaining({
460
- address: "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
479
+ address: "0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9",
461
480
  chain: Chain.Arbitrum,
462
481
  decimal: 6,
463
482
  isGasAsset: false,
464
483
  isSynthetic: false,
465
- symbol: "USDT-0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
484
+ symbol: "USDT-0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9",
466
485
  ticker: "USDT",
467
486
  }),
468
487
  );
@@ -479,12 +498,12 @@ describe("AssetValue", () => {
479
498
  expect(thor).toBeDefined();
480
499
  expect(thor).toEqual(
481
500
  expect.objectContaining({
482
- address: "0xa5f2211b9b8170f694421f2046281775e8468044",
501
+ address: "0xa5f2211B9b8170F694421f2046281775E8468044",
483
502
  chain: Chain.Ethereum,
484
503
  decimal: 18,
485
504
  isGasAsset: false,
486
505
  isSynthetic: false,
487
- symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
506
+ symbol: "THOR-0xa5f2211B9b8170F694421f2046281775E8468044",
488
507
  ticker: "THOR",
489
508
  }),
490
509
  );
@@ -495,12 +514,12 @@ describe("AssetValue", () => {
495
514
  expect(usdc).toBeDefined();
496
515
  expect(usdc).toEqual(
497
516
  expect.objectContaining({
498
- address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
517
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
499
518
  chain: Chain.Ethereum,
500
519
  decimal: 6,
501
520
  isGasAsset: false,
502
521
  isSynthetic: false,
503
- symbol: "USDC-0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
522
+ symbol: "USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
504
523
  ticker: "USDC",
505
524
  }),
506
525
  );
@@ -509,7 +528,9 @@ describe("AssetValue", () => {
509
528
  test("returns safe decimals if string is not in `@swapkit/tokens` lists", async () => {
510
529
  await AssetValue.loadStaticAssets();
511
530
  const fakeAvaxUSDCAssetString = "AVAX.USDC-1234";
512
- const fakeAvaxUSDCAsset = AssetValue.from({ asset: fakeAvaxUSDCAssetString });
531
+ const fakeAvaxUSDCAsset = AssetValue.from({
532
+ asset: fakeAvaxUSDCAssetString,
533
+ });
513
534
 
514
535
  expect(fakeAvaxUSDCAsset).toBeDefined();
515
536
  expect(fakeAvaxUSDCAsset).toEqual(
@@ -528,7 +549,9 @@ describe("AssetValue", () => {
528
549
  test("returns safe decimals if string is not in `@swapkit/tokens` lists with multiple dashes", async () => {
529
550
  await AssetValue.loadStaticAssets();
530
551
  const fakeAvaxUSDCAssetString = "AVAX.USDC-LPT-1234";
531
- const fakeAvaxUSDCAsset2 = AssetValue.from({ asset: fakeAvaxUSDCAssetString });
552
+ const fakeAvaxUSDCAsset2 = AssetValue.from({
553
+ asset: fakeAvaxUSDCAssetString,
554
+ });
532
555
 
533
556
  expect(fakeAvaxUSDCAsset2).toBeDefined();
534
557
  expect(fakeAvaxUSDCAsset2).toEqual(
@@ -552,12 +575,12 @@ describe("AssetValue", () => {
552
575
  expect(AvaxBTCb).toBeDefined();
553
576
  expect(AvaxBTCb).toEqual(
554
577
  expect.objectContaining({
555
- address: "0x152b9d0fdc40c096757f570a51e494bd4b943e50",
578
+ address: "0x152b9d0FdC40C096757F570A51E494bd4b943E50",
556
579
  chain: Chain.Avalanche,
557
580
  decimal: 8,
558
581
  isGasAsset: false,
559
582
  isSynthetic: false,
560
- symbol: "BTC.b-0x152b9d0fdc40c096757f570a51e494bd4b943e50",
583
+ symbol: "BTC.B-0x152b9d0FdC40C096757F570A51E494bd4b943E50",
561
584
  ticker: "BTC.b",
562
585
  }),
563
586
  );
@@ -627,12 +650,12 @@ describe("AssetValue", () => {
627
650
  expect(AvaxUSDC).toBeDefined();
628
651
  expect(AvaxUSDC).toEqual(
629
652
  expect.objectContaining({
630
- address: "0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
653
+ address: "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
631
654
  chain: Chain.Avalanche,
632
655
  decimal: 6,
633
656
  isGasAsset: false,
634
657
  isSynthetic: false,
635
- symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
658
+ symbol: "USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
636
659
  ticker: "USDC",
637
660
  }),
638
661
  );
@@ -703,12 +726,12 @@ describe("AssetValue", () => {
703
726
  const thor = AssetValue.from({ asset: "ETH.THOR" });
704
727
  expect(thor).toEqual(
705
728
  expect.objectContaining({
706
- address: "0xa5f2211b9b8170f694421f2046281775e8468044",
729
+ address: "0xa5f2211B9b8170F694421f2046281775E8468044",
707
730
  chain: Chain.Ethereum,
708
731
  decimal: 18,
709
732
  isGasAsset: false,
710
733
  isSynthetic: false,
711
- symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
734
+ symbol: "THOR-0xa5f2211B9b8170F694421f2046281775E8468044",
712
735
  ticker: "THOR",
713
736
  }),
714
737
  );
@@ -758,12 +781,10 @@ describe("AssetValue", () => {
758
781
  const xrdAsset = AssetValue.from({ chain: Chain.Radix });
759
782
  expect(xrdAsset).toEqual(
760
783
  expect.objectContaining({
761
- address: undefined,
762
784
  chain: Chain.Radix,
763
785
  decimal: BaseDecimal.XRD,
764
786
  isGasAsset: true,
765
787
  isSynthetic: false,
766
- symbol: "XRD",
767
788
  ticker: "XRD",
768
789
  type: "Native",
769
790
  }),
@@ -1,3 +1,4 @@
1
+ import { getAddress } from "ethers";
1
2
  import {
2
3
  type CommonAssetString,
3
4
  CommonAssetStrings,
@@ -8,7 +9,14 @@ import {
8
9
  } from "../helpers/asset";
9
10
  import { warnOnce } from "../helpers/others";
10
11
  import { validateIdentifier } from "../helpers/validators";
11
- import { BaseDecimal, Chain, type ChainId, ChainToChainId } from "../types/chains";
12
+ import {
13
+ BaseDecimal,
14
+ Chain,
15
+ type ChainId,
16
+ ChainToChainId,
17
+ type EVMChain,
18
+ EVMChains,
19
+ } from "../types/chains";
12
20
  import type { TokenNames, TokenTax } from "../types/tokens";
13
21
 
14
22
  import type { NumberPrimitives } from "./bigIntArithmetics";
@@ -379,15 +387,28 @@ function getAssetInfo(identifier: string) {
379
387
 
380
388
  const address = chain === Chain.Solana ? unformattedAddress : unformattedAddress?.toLowerCase();
381
389
 
390
+ let formattedAddress: string | undefined;
391
+
392
+ try {
393
+ formattedAddress =
394
+ address && EVMChains.includes(chain as EVMChain) && getAddress(address)
395
+ ? getAddress(address)
396
+ : address;
397
+ } catch (_error) {
398
+ formattedAddress = address;
399
+ }
400
+
401
+ const formattedSymbol =
402
+ (isSynthOrTradeAsset ? `${synthChain}${assetSeperator}` : "") +
403
+ (formattedAddress ? `${ticker.toUpperCase()}-${formattedAddress ?? ""}` : symbol);
404
+
382
405
  return {
383
- address,
406
+ address: formattedAddress,
384
407
  chain,
385
408
  isGasAsset: isGasAsset({ chain, symbol }),
386
409
  isSynthetic,
387
410
  isTradeAsset,
388
411
  ticker,
389
- symbol:
390
- (isSynthOrTradeAsset ? `${synthChain}${assetSeperator}` : "") +
391
- (address ? `${ticker}-${address ?? ""}` : symbol),
412
+ symbol: formattedSymbol,
392
413
  };
393
414
  }