@swapkit/helpers 1.0.0-rc.9 → 1.0.0-rc.91

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 (38) hide show
  1. package/dist/index.js +2099 -0
  2. package/dist/index.js.map +29 -0
  3. package/package.json +24 -37
  4. package/src/helpers/__tests__/asset.test.ts +126 -108
  5. package/src/helpers/__tests__/memo.test.ts +52 -40
  6. package/src/helpers/__tests__/others.test.ts +42 -37
  7. package/src/helpers/__tests__/validators.test.ts +24 -0
  8. package/src/helpers/asset.ts +118 -93
  9. package/src/helpers/derivationPath.ts +52 -0
  10. package/src/helpers/liquidity.ts +50 -43
  11. package/src/helpers/memo.ts +31 -28
  12. package/src/helpers/others.ts +30 -13
  13. package/src/helpers/validators.ts +15 -6
  14. package/src/helpers/web3wallets.ts +178 -0
  15. package/src/index.ts +14 -9
  16. package/src/modules/__tests__/assetValue.test.ts +325 -116
  17. package/src/modules/__tests__/bigIntArithmetics.test.ts +30 -0
  18. package/src/modules/__tests__/swapKitNumber.test.ts +306 -183
  19. package/src/modules/assetValue.ts +216 -152
  20. package/src/modules/bigIntArithmetics.ts +214 -147
  21. package/src/modules/requestClient.ts +29 -0
  22. package/src/modules/swapKitError.ts +32 -5
  23. package/src/modules/swapKitNumber.ts +8 -1
  24. package/src/types/abis/erc20.ts +99 -0
  25. package/src/types/abis/tcEthVault.ts +496 -0
  26. package/src/types/chains.ts +220 -0
  27. package/src/types/commonTypes.ts +119 -0
  28. package/src/types/derivationPath.ts +56 -0
  29. package/src/types/index.ts +9 -0
  30. package/src/types/network.ts +43 -0
  31. package/src/types/sdk.ts +44 -0
  32. package/src/types/tokens.ts +30 -0
  33. package/src/types/wallet.ts +47 -0
  34. package/LICENSE +0 -201
  35. package/dist/index.cjs +0 -1
  36. package/dist/index.d.ts +0 -354
  37. package/dist/index.es.js +0 -1054
  38. package/src/helpers/request.ts +0 -16
@@ -1,64 +1,96 @@
1
- import { BaseDecimal, Chain } from '@swapkit/types';
2
- import { describe, expect, test } from 'vitest';
1
+ import { describe, expect, test } from "bun:test";
3
2
 
4
- import { AssetValue } from '../assetValue.ts';
3
+ import { BaseDecimal, Chain } from "../../types/chains.ts";
4
+ import { AssetValue, getMinAmountByChain } from "../assetValue.ts";
5
5
 
6
- describe('AssetValue', () => {
7
- describe('assetValue', () => {
8
- test('returns asset ticker with value', () => {
6
+ describe("AssetValue", () => {
7
+ describe("assetValue", () => {
8
+ test("returns asset ticker with value", () => {
9
9
  const fakeAvaxUSDCAsset = new AssetValue({
10
10
  decimal: 6,
11
11
  value: 1234567890,
12
12
  chain: Chain.Avalanche,
13
- symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
13
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
14
14
  });
15
- expect(fakeAvaxUSDCAsset.assetValue).toBe('1234567890 USDC');
16
15
  expect(fakeAvaxUSDCAsset.toString()).toBe(
17
- 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
16
+ "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
18
17
  );
19
- expect(fakeAvaxUSDCAsset.toString(true)).toBe('AVAX.USDC');
20
-
21
- const thor = AssetValue.fromChainOrSignature('ETH.THOR');
22
- expect(thor.assetValue).toBe('0 THOR');
23
18
 
24
19
  const ethSynth = new AssetValue({
25
20
  chain: Chain.THORChain,
26
- symbol: 'ETH/ETH',
21
+ symbol: "ETH/ETH",
27
22
  decimal: 8,
28
23
  value: 1234567890,
29
24
  });
30
25
 
31
- expect(ethSynth.assetValue).toBe('1234567890 ETH/ETH');
32
- expect(ethSynth.toString()).toBe('THOR.ETH/ETH');
33
- expect(ethSynth.toString(true)).toBe('ETH/ETH');
34
- expect(ethSynth.mul(21.37).getValue('string')).toBe('26382715809.3');
26
+ expect(ethSynth.toString()).toBe("ETH/ETH");
27
+ expect(ethSynth.mul(21.37).getValue("string")).toBe("26382715809.3");
28
+
29
+ const ethThorSynth = new AssetValue({
30
+ chain: Chain.THORChain,
31
+ symbol: "ETH/THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
32
+ decimal: 8,
33
+ value: 1234567890,
34
+ });
35
+ expect(ethThorSynth.toString()).toBe("ETH/THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
35
36
 
36
37
  const atomDerived = new AssetValue({
37
- identifier: 'THOR.ATOM',
38
+ identifier: "THOR.ATOM",
38
39
  decimal: 6,
39
40
  value: 123456789,
40
41
  });
41
42
 
42
- expect(atomDerived.assetValue).toBe('123456789 ATOM');
43
- expect(atomDerived.toString()).toBe('THOR.ATOM');
44
- expect(atomDerived.toString(true)).toBe('THOR.ATOM');
43
+ expect(atomDerived.toString()).toBe("THOR.ATOM");
45
44
  });
46
45
  });
47
46
 
48
- describe('eq', () => {
49
- test('checks if assets are same chain and symbol', () => {
50
- const firstThor = AssetValue.fromChainOrSignature('ETH.THOR');
51
- const secondThor = AssetValue.fromChainOrSignature('ETH.THOR');
52
- const vThor = AssetValue.fromChainOrSignature('ETH.vTHOR');
47
+ describe("toUrl", () => {
48
+ test("returns asset compliance with url", () => {
49
+ const fakeAvaxUSDCAsset = new AssetValue({
50
+ decimal: 6,
51
+ value: 1234567890,
52
+ chain: Chain.Avalanche,
53
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
54
+ });
55
+ expect(fakeAvaxUSDCAsset.toUrl()).toBe(
56
+ "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
57
+ );
58
+
59
+ const thor = AssetValue.fromChainOrSignature("ETH.THOR");
60
+ expect(thor.toUrl()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
61
+
62
+ const ethSynth = new AssetValue({
63
+ chain: Chain.THORChain,
64
+ symbol: "ETH/ETH",
65
+ decimal: 8,
66
+ value: 1234567890,
67
+ });
68
+ expect(ethSynth.toUrl()).toBe("THOR.ETH.ETH");
69
+
70
+ const ethThorSynth = new AssetValue({
71
+ chain: Chain.THORChain,
72
+ symbol: "ETH/THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
73
+ decimal: 8,
74
+ value: 1234567890,
75
+ });
76
+ expect(ethThorSynth.toUrl()).toBe("THOR.ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
77
+ });
78
+ });
79
+
80
+ describe("eq", () => {
81
+ test("checks if assets are same chain and symbol", () => {
82
+ const firstThor = AssetValue.fromChainOrSignature("ETH.THOR");
83
+ const secondThor = AssetValue.fromChainOrSignature("ETH.THOR");
84
+ const vThor = AssetValue.fromChainOrSignature("ETH.vTHOR");
53
85
  const firstUsdc = new AssetValue({
54
86
  chain: Chain.Avalanche,
55
- symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
87
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
56
88
  decimal: 6,
57
89
  value: 1234567890,
58
90
  });
59
91
  const secondUsdc = new AssetValue({
60
92
  chain: Chain.Avalanche,
61
- symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
93
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
62
94
  decimal: 6,
63
95
  value: 1234,
64
96
  });
@@ -77,135 +109,293 @@ describe('AssetValue', () => {
77
109
  });
78
110
  });
79
111
 
80
- describe('toString', () => {
81
- test('returns asset value string/identifier', async () => {
112
+ describe("from bigint", () => {
113
+ test("returns asset value with correct decimal", async () => {
114
+ const avaxUSDCAsset = await AssetValue.fromIdentifier(
115
+ `${Chain.Avalanche}.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e`,
116
+ 1234567800n,
117
+ );
118
+ expect(avaxUSDCAsset.getValue("string")).toBe("1234.5678");
119
+ });
120
+ });
121
+
122
+ describe("toString", () => {
123
+ test("returns asset value string/identifier", async () => {
82
124
  const avaxUSDCAsset = new AssetValue({
83
125
  decimal: 6,
84
126
  value: 1234567890,
85
127
  chain: Chain.Avalanche,
86
- symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
128
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
87
129
  });
88
- expect(avaxUSDCAsset.toString()).toBe('AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e');
130
+ expect(avaxUSDCAsset.toString()).toBe("AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e");
89
131
 
90
- const thor = AssetValue.fromChainOrSignature('ETH.THOR');
91
- expect(thor.toString()).toBe('ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044');
132
+ const thor = AssetValue.fromChainOrSignature("ETH.THOR");
133
+ expect(thor.toString()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
92
134
 
93
- const ethSynth = await AssetValue.fromIdentifier('ETH/ETH');
94
- expect(ethSynth.toString()).toBe('THOR.ETH/ETH');
135
+ const ethSynth = await AssetValue.fromIdentifier("ETH/ETH");
136
+ expect(ethSynth.toString()).toBe("ETH/ETH");
95
137
  });
96
138
  });
97
139
 
98
- describe('fromIdentifier', () => {
99
- test('creates AssetValue from string', async () => {
140
+ describe("fromIdentifier", () => {
141
+ test("creates AssetValue from string", async () => {
100
142
  const avaxUSDCAsset = await AssetValue.fromIdentifier(
101
- 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
143
+ "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
102
144
  );
103
145
 
104
146
  expect(avaxUSDCAsset).toEqual(
105
147
  expect.objectContaining({
106
- address: '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
148
+ address: "0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
107
149
  chain: Chain.Avalanche,
108
150
  decimal: 6,
109
151
  isGasAsset: false,
110
152
  isSynthetic: false,
111
- symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
112
- ticker: 'USDC',
153
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
154
+ ticker: "USDC",
113
155
  }),
114
156
  );
115
157
  });
116
158
  });
117
159
 
118
- describe('fromString', () => {
119
- test('creates AssetValue from string', () => {
120
- test('creates AssetValue from string', async () => {
121
- const fakeAvaxAssetString = 'AVAX.ASDF-1234';
122
- const fakeAvaxAsset = await AssetValue.fromString(fakeAvaxAssetString);
160
+ describe("fromString", () => {
161
+ test("creates AssetValue from string", async () => {
162
+ const fakeAvaxAssetString = "AVAX.ASDF-1234";
163
+ const fakeAvaxAsset = await AssetValue.fromString(fakeAvaxAssetString);
123
164
 
124
- expect(fakeAvaxAsset).toEqual(
125
- expect.objectContaining({
126
- address: '1234',
127
- chain: Chain.Avalanche,
128
- decimal: 10,
129
- isGasAsset: false,
130
- isSynthetic: false,
131
- symbol: 'ASDF-1234',
132
- ticker: 'ASDF',
133
- }),
134
- );
135
- });
165
+ expect(fakeAvaxAsset).toEqual(
166
+ expect.objectContaining({
167
+ address: "1234",
168
+ chain: Chain.Avalanche,
169
+ decimal: 18,
170
+ isGasAsset: false,
171
+ isSynthetic: false,
172
+ symbol: "ASDF-1234",
173
+ ticker: "ASDF",
174
+ }),
175
+ );
176
+ });
177
+ });
178
+
179
+ describe("fromStringWithBase", () => {
180
+ test("creates AssetValue from string with base", async () => {
181
+ const fakeAvaxAssetString = "AVAX.ASDF-1234";
182
+ const fakeAvaxAsset = await AssetValue.fromStringWithBase(fakeAvaxAssetString, 1, 8);
183
+
184
+ expect(fakeAvaxAsset).toEqual(
185
+ expect.objectContaining({
186
+ address: "1234",
187
+ chain: Chain.Avalanche,
188
+ decimal: 18,
189
+ isGasAsset: false,
190
+ isSynthetic: false,
191
+ symbol: "ASDF-1234",
192
+ ticker: "ASDF",
193
+ }),
194
+ );
195
+ expect(fakeAvaxAsset.getValue("string")).toBe("100000000");
196
+ expect(fakeAvaxAsset.getBaseValue("string")).toBe("100000000000000000000000000");
197
+ });
198
+ });
199
+
200
+ describe("fromUrl", () => {
201
+ test("creates AssetValue from url like format", async () => {
202
+ const synthETHString = "THOR.ETH.ETH";
203
+ const ethString = "ETH.ETH";
204
+ const thorString = "ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044";
205
+ const synthThorString = "THOR.ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044";
206
+
207
+ const synthETH = await AssetValue.fromUrl(synthETHString);
208
+ const eth = await AssetValue.fromUrl(ethString);
209
+ const thor = await AssetValue.fromUrl(thorString);
210
+ const synthThor = await AssetValue.fromUrl(synthThorString);
211
+
212
+ expect(synthETH.toString()).toBe("ETH/ETH");
213
+ expect(eth.toString()).toBe("ETH.ETH");
214
+ expect(thor.toString()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
215
+ expect(synthThor.toString()).toBe("ETH/THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
136
216
  });
137
217
  });
138
218
 
139
- describe('fromIdentifierSync', () => {
140
- test('(same as fromIdentifier) - creates AssetValue from string via `@swapkit/tokens` lists', async () => {
219
+ describe("fromIdentifierSync", () => {
220
+ test("(same as fromIdentifier) - creates AssetValue from string via `@swapkit/tokens` lists", async () => {
141
221
  await AssetValue.loadStaticAssets();
142
222
  const thor = AssetValue.fromIdentifierSync(
143
- 'ARB.USDT-0XFD086BC7CD5C481DCC9C85EBE478A1C0B69FCBB9',
223
+ "ARB.USDT-0XFD086BC7CD5C481DCC9C85EBE478A1C0B69FCBB9",
144
224
  );
145
225
 
146
226
  expect(thor).toBeDefined();
147
227
  expect(thor).toEqual(
148
228
  expect.objectContaining({
149
- address: '0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9',
229
+ address: "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
150
230
  chain: Chain.Arbitrum,
151
231
  decimal: 6,
152
232
  isGasAsset: false,
153
233
  isSynthetic: false,
154
- symbol: 'USDT-0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9',
155
- ticker: 'USDT',
234
+ symbol: "USDT-0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
235
+ ticker: "USDT",
156
236
  }),
157
237
  );
158
238
  });
159
239
  });
160
240
 
161
- describe('fromStringSync', () => {
162
- test('creates AssetValue from string via `@swapkit/tokens` lists', async () => {
241
+ describe("fromStringSync", () => {
242
+ test("creates AssetValue from string via `@swapkit/tokens` lists", async () => {
163
243
  await AssetValue.loadStaticAssets();
164
- const thor = AssetValue.fromStringSync('ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044');
244
+ const thor = AssetValue.fromStringSync("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
165
245
 
166
246
  expect(thor).toBeDefined();
167
247
  expect(thor).toEqual(
168
248
  expect.objectContaining({
169
- address: '0xa5f2211b9b8170f694421f2046281775e8468044',
249
+ address: "0xa5f2211b9b8170f694421f2046281775e8468044",
170
250
  chain: Chain.Ethereum,
171
251
  decimal: 18,
172
252
  isGasAsset: false,
173
253
  isSynthetic: false,
174
- symbol: 'THOR-0xa5f2211b9b8170f694421f2046281775e8468044',
175
- ticker: 'THOR',
254
+ symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
255
+ ticker: "THOR",
256
+ }),
257
+ );
258
+
259
+ const usdc = AssetValue.fromStringSync("ETH.USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48");
260
+ expect(usdc).toBeDefined();
261
+ expect(usdc).toEqual(
262
+ expect.objectContaining({
263
+ address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
264
+ chain: Chain.Ethereum,
265
+ decimal: 6,
266
+ isGasAsset: false,
267
+ isSynthetic: false,
268
+ symbol: "USDC-0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
269
+ ticker: "USDC",
176
270
  }),
177
271
  );
178
272
  });
179
273
 
180
- test('returns undefined if string is not in `@swapkit/tokens` lists', async () => {
274
+ test("returns safe decimals if string is not in `@swapkit/tokens` lists", async () => {
181
275
  await AssetValue.loadStaticAssets();
182
- const fakeAvaxUSDCAssetString = 'AVAX.USDC-1234';
276
+ const fakeAvaxUSDCAssetString = "AVAX.USDC-1234";
183
277
  const fakeAvaxUSDCAsset = AssetValue.fromStringSync(fakeAvaxUSDCAssetString);
184
278
 
185
- expect(fakeAvaxUSDCAsset).toBeUndefined();
279
+ expect(fakeAvaxUSDCAsset).toBeDefined();
280
+ expect(fakeAvaxUSDCAsset).toEqual(
281
+ expect.objectContaining({
282
+ address: "1234",
283
+ chain: Chain.Avalanche,
284
+ decimal: 18,
285
+ isGasAsset: false,
286
+ isSynthetic: false,
287
+ symbol: "USDC-1234",
288
+ ticker: "USDC",
289
+ }),
290
+ );
291
+ });
292
+
293
+ test("returns proper avax string with address from `@swapkit/tokens` lists", async () => {
294
+ await AssetValue.loadStaticAssets();
295
+ const avaxBTCb = "AVAX.BTC.b-0x152b9d0fdc40c096757f570a51e494bd4b943e50";
296
+ const AvaxBTCb = AssetValue.fromStringSync(avaxBTCb);
297
+
298
+ expect(AvaxBTCb).toBeDefined();
299
+ expect(AvaxBTCb).toEqual(
300
+ expect.objectContaining({
301
+ address: "0x152b9d0fdc40c096757f570a51e494bd4b943e50",
302
+ chain: Chain.Avalanche,
303
+ decimal: 8,
304
+ isGasAsset: false,
305
+ isSynthetic: false,
306
+ symbol: "BTC.b-0x152b9d0fdc40c096757f570a51e494bd4b943e50",
307
+ ticker: "BTC.b",
308
+ }),
309
+ );
186
310
  });
187
311
  });
188
312
 
189
- describe('fromChainOrSignature', () => {
190
- test('creates AssetValue from common asset string or chain', () => {
313
+ describe("fromStringWithBaseSync", () => {
314
+ test("creates AssetValue from string with base decimals via `@swapkit/tokens` lists", async () => {
315
+ await AssetValue.loadStaticAssets();
316
+ const btc = AssetValue.fromStringWithBaseSync("BTC.BTC", 5200000000000, 8);
317
+
318
+ expect(btc).toBeDefined();
319
+ expect(btc).toEqual(
320
+ expect.objectContaining({
321
+ chain: Chain.Bitcoin,
322
+ decimal: 8,
323
+ isGasAsset: true,
324
+ isSynthetic: false,
325
+ symbol: "BTC",
326
+ ticker: "BTC",
327
+ }),
328
+ );
329
+
330
+ expect(btc.getValue("string")).toBe("52000");
331
+ expect(btc.getBaseValue("string")).toBe("5200000000000");
332
+ });
333
+
334
+ test("returns safe decimals if string is not in `@swapkit/tokens` lists", async () => {
335
+ await AssetValue.loadStaticAssets();
336
+ const fakeAvaxUSDCAssetString = "AVAX.USDC-1234";
337
+ const fakeAvaxUSDCAsset = AssetValue.fromStringWithBaseSync(fakeAvaxUSDCAssetString, 1, 8);
338
+
339
+ expect(fakeAvaxUSDCAsset).toBeDefined();
340
+ expect(fakeAvaxUSDCAsset).toEqual(
341
+ expect.objectContaining({
342
+ address: "1234",
343
+ chain: Chain.Avalanche,
344
+ decimal: 18,
345
+ isGasAsset: false,
346
+ isSynthetic: false,
347
+ symbol: "USDC-1234",
348
+ ticker: "USDC",
349
+ }),
350
+ );
351
+
352
+ expect(fakeAvaxUSDCAsset.getValue("string")).toBe("0.00000001");
353
+ expect(fakeAvaxUSDCAsset.getBaseValue("string")).toBe("10000000000");
354
+ });
355
+
356
+ test("returns proper avax string with address from `@swapkit/tokens` lists", async () => {
357
+ await AssetValue.loadStaticAssets();
358
+ const avaxUSDC = "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e";
359
+ const AvaxUSDC = AssetValue.fromStringWithBaseSync(avaxUSDC, 100000000, 8);
360
+
361
+ expect(AvaxUSDC).toBeDefined();
362
+ expect(AvaxUSDC).toEqual(
363
+ expect.objectContaining({
364
+ address: "0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
365
+ chain: Chain.Avalanche,
366
+ decimal: 6,
367
+ isGasAsset: false,
368
+ isSynthetic: false,
369
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
370
+ ticker: "USDC",
371
+ }),
372
+ );
373
+
374
+ expect(AvaxUSDC.getValue("string")).toBe("1");
375
+ expect(AvaxUSDC.getBaseValue("string")).toBe("1000000");
376
+ });
377
+ });
378
+
379
+ describe("fromChainOrSignature", () => {
380
+ test("creates AssetValue from common asset string or chain", () => {
191
381
  const customBaseAsset = [Chain.Cosmos, Chain.BinanceSmartChain, Chain.THORChain, Chain.Maya];
192
- Object.values(Chain)
193
- .filter((c) => !customBaseAsset.includes(c))
194
- .forEach((chain) => {
195
- const asset = AssetValue.fromChainOrSignature(chain);
196
- expect(asset).toEqual(
197
- expect.objectContaining({
198
- address: undefined,
199
- chain,
200
- decimal: BaseDecimal[chain],
201
- isGasAsset: ![Chain.Arbitrum, Chain.Optimism].includes(chain),
202
- isSynthetic: false,
203
- symbol: chain,
204
- ticker: chain,
205
- type: 'Native',
206
- }),
207
- );
208
- });
382
+ const filteredChains = Object.values(Chain).filter((c) => !customBaseAsset.includes(c));
383
+
384
+ for (const chain of filteredChains) {
385
+ const asset = AssetValue.fromChainOrSignature(chain);
386
+ expect(asset).toEqual(
387
+ expect.objectContaining({
388
+ address: undefined,
389
+ chain,
390
+ decimal: BaseDecimal[chain],
391
+ isGasAsset: ![Chain.Arbitrum, Chain.Optimism].includes(chain),
392
+ isSynthetic: false,
393
+ symbol: chain,
394
+ ticker: chain,
395
+ type: "Native",
396
+ }),
397
+ );
398
+ }
209
399
 
210
400
  const cosmosAsset = AssetValue.fromChainOrSignature(Chain.Cosmos);
211
401
  expect(cosmosAsset).toEqual(
@@ -215,9 +405,9 @@ describe('AssetValue', () => {
215
405
  decimal: BaseDecimal.GAIA,
216
406
  isGasAsset: true,
217
407
  isSynthetic: false,
218
- symbol: 'ATOM',
219
- ticker: 'ATOM',
220
- type: 'Native',
408
+ symbol: "ATOM",
409
+ ticker: "ATOM",
410
+ type: "Native",
221
411
  }),
222
412
  );
223
413
 
@@ -229,9 +419,9 @@ describe('AssetValue', () => {
229
419
  decimal: BaseDecimal.BSC,
230
420
  isGasAsset: true,
231
421
  isSynthetic: false,
232
- symbol: 'BNB',
233
- ticker: 'BNB',
234
- type: 'Native',
422
+ symbol: "BNB",
423
+ ticker: "BNB",
424
+ type: "Native",
235
425
  }),
236
426
  );
237
427
 
@@ -243,9 +433,9 @@ describe('AssetValue', () => {
243
433
  decimal: BaseDecimal.THOR,
244
434
  isGasAsset: true,
245
435
  isSynthetic: false,
246
- symbol: 'RUNE',
247
- ticker: 'RUNE',
248
- type: 'Native',
436
+ symbol: "RUNE",
437
+ ticker: "RUNE",
438
+ type: "Native",
249
439
  }),
250
440
  );
251
441
 
@@ -257,45 +447,64 @@ describe('AssetValue', () => {
257
447
  decimal: BaseDecimal.MAYA,
258
448
  isGasAsset: true,
259
449
  isSynthetic: false,
260
- symbol: 'CACAO',
261
- ticker: 'CACAO',
262
- type: 'Native',
450
+ symbol: "CACAO",
451
+ ticker: "CACAO",
452
+ type: "Native",
263
453
  }),
264
454
  );
265
455
 
266
- const thor = AssetValue.fromChainOrSignature('ETH.THOR');
456
+ const thor = AssetValue.fromChainOrSignature("ETH.THOR");
267
457
  expect(thor).toEqual(
268
458
  expect.objectContaining({
269
- address: '0xa5f2211b9b8170f694421f2046281775e8468044',
459
+ address: "0xa5f2211b9b8170f694421f2046281775e8468044",
270
460
  chain: Chain.Ethereum,
271
461
  decimal: 18,
272
462
  isGasAsset: false,
273
463
  isSynthetic: false,
274
- symbol: 'THOR-0xa5f2211b9b8170f694421f2046281775e8468044',
275
- ticker: 'THOR',
464
+ symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
465
+ ticker: "THOR",
276
466
  }),
277
467
  );
278
468
 
279
- const vthor = AssetValue.fromChainOrSignature('ETH.vTHOR');
469
+ const vthor = AssetValue.fromChainOrSignature("ETH.vTHOR");
280
470
  expect(vthor).toEqual(
281
471
  expect.objectContaining({
282
- address: '0x815c23eca83261b6ec689b60cc4a58b54bc24d8d',
472
+ address: "0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
283
473
  chain: Chain.Ethereum,
284
474
  decimal: 18,
285
475
  isGasAsset: false,
286
476
  isSynthetic: false,
287
- symbol: 'vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d',
288
- ticker: 'vTHOR',
477
+ symbol: "vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
478
+ ticker: "vTHOR",
289
479
  }),
290
480
  );
291
481
  });
292
482
  });
293
483
 
294
- describe('loadStaticAssets', () => {
295
- test('loads static assets from `@swapkit/tokens` lists', async () => {
484
+ describe("loadStaticAssets", () => {
485
+ test("loads static assets from `@swapkit/tokens` lists", async () => {
296
486
  // Dummy test - think of sth more meaningful
297
487
  const { ok } = await AssetValue.loadStaticAssets();
298
488
  expect(ok).toBe(true);
299
489
  });
300
490
  });
301
491
  });
492
+
493
+ describe("getMinAmountByChain", () => {
494
+ test("returns min amount for chain", () => {
495
+ expect(getMinAmountByChain(Chain.THORChain).getValue("string")).toBe("0");
496
+ expect(getMinAmountByChain(Chain.Maya).getValue("string")).toBe("0");
497
+ expect(getMinAmountByChain(Chain.Cosmos).getValue("string")).toBe("0.000001");
498
+
499
+ expect(getMinAmountByChain(Chain.Bitcoin).getValue("string")).toBe("0.00010001");
500
+ expect(getMinAmountByChain(Chain.Litecoin).getValue("string")).toBe("0.00010001");
501
+ expect(getMinAmountByChain(Chain.BitcoinCash).getValue("string")).toBe("0.00010001");
502
+ expect(getMinAmountByChain(Chain.Dogecoin).getValue("string")).toBe("1.00000001");
503
+
504
+ expect(getMinAmountByChain(Chain.BinanceSmartChain).getValue("string")).toBe("0.00000001");
505
+ expect(getMinAmountByChain(Chain.Ethereum).getValue("string")).toBe("0.00000001");
506
+ expect(getMinAmountByChain(Chain.Avalanche).getValue("string")).toBe("0.00000001");
507
+ expect(getMinAmountByChain(Chain.Arbitrum).getValue("string")).toBe("0.00000001");
508
+ expect(getMinAmountByChain(Chain.Optimism).getValue("string")).toBe("0.00000001");
509
+ });
510
+ });
@@ -0,0 +1,30 @@
1
+ import { describe, expect, test } from "bun:test";
2
+
3
+ import { formatBigIntToSafeValue } from "../bigIntArithmetics.ts";
4
+
5
+ describe("BigIntArithmatics", () => {
6
+ describe("formatBigIntToSafeValue", () => {
7
+ test("parse bigint with decimals to string", () => {
8
+ const safeValue1 = formatBigIntToSafeValue({
9
+ value: BigInt(0),
10
+ decimal: 6,
11
+ bigIntDecimal: 6,
12
+ });
13
+ expect(safeValue1).toBe("0");
14
+
15
+ const safeValue2 = formatBigIntToSafeValue({
16
+ value: BigInt(15),
17
+ decimal: 0,
18
+ bigIntDecimal: 0,
19
+ });
20
+ expect(safeValue2).toBe("15");
21
+
22
+ const safeValue3 = formatBigIntToSafeValue({
23
+ value: BigInt(123456789),
24
+ decimal: 4,
25
+ bigIntDecimal: 4,
26
+ });
27
+ expect(safeValue3).toBe("12345.6789");
28
+ });
29
+ });
30
+ });