@swapkit/helpers 1.0.0-rc.11 → 1.0.0-rc.111

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 (42) hide show
  1. package/dist/index.js +2900 -0
  2. package/dist/index.js.map +31 -0
  3. package/package.json +26 -37
  4. package/src/helpers/__tests__/asset.test.ts +186 -103
  5. package/src/helpers/__tests__/memo.test.ts +53 -41
  6. package/src/helpers/__tests__/others.test.ts +44 -37
  7. package/src/helpers/__tests__/validators.test.ts +24 -0
  8. package/src/helpers/asset.ts +184 -95
  9. package/src/helpers/derivationPath.ts +53 -0
  10. package/src/helpers/liquidity.ts +50 -43
  11. package/src/helpers/memo.ts +34 -31
  12. package/src/helpers/others.ts +46 -12
  13. package/src/helpers/validators.ts +15 -6
  14. package/src/helpers/web3wallets.ts +200 -0
  15. package/src/index.ts +14 -9
  16. package/src/modules/__tests__/assetValue.test.ts +486 -129
  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 +220 -162
  20. package/src/modules/bigIntArithmetics.ts +214 -165
  21. package/src/modules/requestClient.ts +38 -0
  22. package/src/modules/swapKitError.ts +41 -5
  23. package/src/modules/swapKitNumber.ts +1 -1
  24. package/src/types/abis/erc20.ts +99 -0
  25. package/src/types/abis/mayaEvmVaults.ts +331 -0
  26. package/src/types/abis/tcEthVault.ts +496 -0
  27. package/src/types/chains.ts +226 -0
  28. package/src/types/commonTypes.ts +123 -0
  29. package/src/types/derivationPath.ts +58 -0
  30. package/src/types/errors/apiV1.ts +0 -0
  31. package/src/types/index.ts +12 -0
  32. package/src/types/network.ts +45 -0
  33. package/src/types/quotes.ts +391 -0
  34. package/src/types/radix.ts +14 -0
  35. package/src/types/sdk.ts +126 -0
  36. package/src/types/tokens.ts +30 -0
  37. package/src/types/wallet.ts +72 -0
  38. package/LICENSE +0 -201
  39. package/dist/index.cjs +0 -1
  40. package/dist/index.d.ts +0 -356
  41. package/dist/index.es.js +0 -1071
  42. package/src/helpers/request.ts +0 -16
@@ -1,221 +1,517 @@
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");
44
+
45
+ const radixXWBTC = new AssetValue({
46
+ identifier:
47
+ "RADIX.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
48
+ decimal: 8,
49
+ value: 11112222,
50
+ });
51
+
52
+ expect(radixXWBTC.toString()).toBe(
53
+ "RADIX.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
54
+ );
45
55
  });
46
56
  });
47
57
 
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');
58
+ describe("toUrl", () => {
59
+ test("returns asset compliance with url", () => {
60
+ const fakeAvaxUSDCAsset = new AssetValue({
61
+ decimal: 6,
62
+ value: 1234567890,
63
+ chain: Chain.Avalanche,
64
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
65
+ });
66
+ expect(fakeAvaxUSDCAsset.toUrl()).toBe(
67
+ "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
68
+ );
69
+
70
+ const thor = AssetValue.fromChainOrSignature("ETH.THOR");
71
+ expect(thor.toUrl()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
72
+
73
+ const ethSynth = new AssetValue({
74
+ chain: Chain.THORChain,
75
+ symbol: "ETH/ETH",
76
+ decimal: 8,
77
+ value: 1234567890,
78
+ });
79
+ expect(ethSynth.toUrl()).toBe("THOR.ETH.ETH");
80
+
81
+ const ethThorSynth = new AssetValue({
82
+ chain: Chain.THORChain,
83
+ symbol: "ETH/THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
84
+ decimal: 8,
85
+ value: 1234567890,
86
+ });
87
+ expect(ethThorSynth.toUrl()).toBe("THOR.ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
88
+ });
89
+ });
90
+
91
+ describe("eq", () => {
92
+ test("checks if assets are same chain and symbol", () => {
93
+ const firstThor = AssetValue.fromChainOrSignature("ETH.THOR");
94
+ const secondThor = AssetValue.fromChainOrSignature("ETH.THOR");
95
+ const vThor = AssetValue.fromChainOrSignature("ETH.vTHOR");
53
96
  const firstUsdc = new AssetValue({
54
97
  chain: Chain.Avalanche,
55
- symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
98
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
56
99
  decimal: 6,
57
100
  value: 1234567890,
58
101
  });
59
102
  const secondUsdc = new AssetValue({
60
103
  chain: Chain.Avalanche,
61
- symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
104
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
62
105
  decimal: 6,
63
106
  value: 1234,
64
107
  });
65
108
 
109
+ expect(firstThor.eqAsset(firstThor)).toBe(true);
110
+ expect(firstThor.eqAsset(secondThor)).toBe(true);
111
+ expect(firstThor.eqAsset(vThor)).toBe(false);
112
+ expect(firstThor.eqAsset(firstUsdc)).toBe(false);
113
+ expect(firstThor.eqAsset(secondUsdc)).toBe(false);
114
+
115
+ expect(firstUsdc.eqAsset(firstThor)).toBe(false);
116
+ expect(firstUsdc.eqAsset(secondThor)).toBe(false);
117
+ expect(firstUsdc.eqAsset(vThor)).toBe(false);
118
+ expect(firstUsdc.eqAsset(firstUsdc)).toBe(true);
119
+ expect(firstUsdc.eqAsset(secondUsdc)).toBe(true);
120
+ });
121
+
122
+ test("check if assets have same value, even if not same asset", () => {
123
+ const firstThor = AssetValue.fromChainOrSignature("ETH.THOR", "20");
124
+ const secondThor = AssetValue.fromChainOrSignature("ETH.THOR", "35");
125
+ const thirdThor = AssetValue.fromChainOrSignature("ETH.THOR", "35");
126
+ const vThor = AssetValue.fromChainOrSignature("ETH.vTHOR", "20");
127
+
128
+ expect(firstThor.eqValue(firstThor)).toBe(true);
129
+ expect(firstThor.eqValue(secondThor)).toBe(false);
130
+ expect(secondThor.eqValue(thirdThor)).toBe(true);
131
+ expect(firstThor.eqValue(vThor)).toBe(true);
132
+ });
133
+
134
+ test("check if assets have identical asset and value", () => {
135
+ const firstThor = AssetValue.fromChainOrSignature("ETH.THOR", "20");
136
+ const secondThor = AssetValue.fromChainOrSignature("ETH.THOR", "35");
137
+ const thirdThor = AssetValue.fromChainOrSignature("ETH.THOR", "35");
138
+ const vThor = AssetValue.fromChainOrSignature("ETH.vTHOR", "20");
139
+
66
140
  expect(firstThor.eq(firstThor)).toBe(true);
67
- expect(firstThor.eq(secondThor)).toBe(true);
141
+ expect(firstThor.eq(secondThor)).toBe(false);
142
+ expect(secondThor.eq(thirdThor)).toBe(true);
68
143
  expect(firstThor.eq(vThor)).toBe(false);
69
- expect(firstThor.eq(firstUsdc)).toBe(false);
70
- expect(firstThor.eq(secondUsdc)).toBe(false);
71
-
72
- expect(firstUsdc.eq(firstThor)).toBe(false);
73
- expect(firstUsdc.eq(secondThor)).toBe(false);
74
- expect(firstUsdc.eq(vThor)).toBe(false);
75
- expect(firstUsdc.eq(firstUsdc)).toBe(true);
76
- expect(firstUsdc.eq(secondUsdc)).toBe(true);
77
144
  });
78
145
  });
79
146
 
80
- describe('from bigint', () => {
81
- test('returns asset value with correct decimal', async () => {
147
+ describe("from bigint", () => {
148
+ test("returns asset value with correct decimal", async () => {
82
149
  const avaxUSDCAsset = await AssetValue.fromIdentifier(
83
150
  `${Chain.Avalanche}.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e`,
84
151
  1234567800n,
85
152
  );
86
- expect(avaxUSDCAsset.getValue('string')).toBe('1234.5678');
153
+ expect(avaxUSDCAsset.getValue("string")).toBe("1234.5678");
87
154
  });
88
155
  });
89
156
 
90
- describe('toString', () => {
91
- test('returns asset value string/identifier', async () => {
157
+ describe("toString", () => {
158
+ test("returns asset value string/identifier", async () => {
92
159
  const avaxUSDCAsset = new AssetValue({
93
160
  decimal: 6,
94
161
  value: 1234567890,
95
162
  chain: Chain.Avalanche,
96
- symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
163
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
97
164
  });
98
- expect(avaxUSDCAsset.toString()).toBe('AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e');
165
+ expect(avaxUSDCAsset.toString()).toBe("AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e");
99
166
 
100
- const thor = AssetValue.fromChainOrSignature('ETH.THOR');
101
- expect(thor.toString()).toBe('ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044');
167
+ const thor = AssetValue.fromChainOrSignature("ETH.THOR");
168
+ expect(thor.toString()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
102
169
 
103
- const ethSynth = await AssetValue.fromIdentifier('ETH/ETH');
104
- expect(ethSynth.toString()).toBe('THOR.ETH/ETH');
170
+ const ethSynth = await AssetValue.fromIdentifier("ETH/ETH");
171
+ expect(ethSynth.toString()).toBe("ETH/ETH");
105
172
  });
106
173
  });
107
174
 
108
- describe('fromIdentifier', () => {
109
- test('creates AssetValue from string', async () => {
175
+ describe("fromIdentifier", () => {
176
+ test("creates AssetValue from string", async () => {
110
177
  const avaxUSDCAsset = await AssetValue.fromIdentifier(
111
- 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
178
+ "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
112
179
  );
113
180
 
114
181
  expect(avaxUSDCAsset).toEqual(
115
182
  expect.objectContaining({
116
- address: '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
183
+ address: "0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
117
184
  chain: Chain.Avalanche,
118
185
  decimal: 6,
119
186
  isGasAsset: false,
120
187
  isSynthetic: false,
121
- symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
122
- ticker: 'USDC',
188
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
189
+ ticker: "USDC",
190
+ }),
191
+ );
192
+ });
193
+
194
+ test("creates AssetValue from string with multiple dashes", async () => {
195
+ const ethPendleLptAsset = await AssetValue.fromIdentifier("ETH.PENDLE-LPT-0x1234");
196
+
197
+ expect(ethPendleLptAsset).toEqual(
198
+ expect.objectContaining({
199
+ address: "0x1234",
200
+ chain: Chain.Ethereum,
201
+ decimal: 18,
202
+ isGasAsset: false,
203
+ isSynthetic: false,
204
+ symbol: "PENDLE-LPT-0x1234",
205
+ ticker: "PENDLE-LPT",
123
206
  }),
124
207
  );
125
208
  });
126
209
  });
127
210
 
128
- describe('fromString', () => {
129
- test('creates AssetValue from string', () => {
130
- test('creates AssetValue from string', async () => {
131
- const fakeAvaxAssetString = 'AVAX.ASDF-1234';
132
- const fakeAvaxAsset = await AssetValue.fromString(fakeAvaxAssetString);
211
+ describe("fromString", () => {
212
+ test("creates AssetValue from string", async () => {
213
+ const fakeAvaxAssetString = "AVAX.ASDF-1234";
214
+ const fakeAvaxAsset = await AssetValue.fromString(fakeAvaxAssetString);
133
215
 
134
- expect(fakeAvaxAsset).toEqual(
135
- expect.objectContaining({
136
- address: '1234',
137
- chain: Chain.Avalanche,
138
- decimal: 10,
139
- isGasAsset: false,
140
- isSynthetic: false,
141
- symbol: 'ASDF-1234',
142
- ticker: 'ASDF',
143
- }),
144
- );
145
- });
216
+ expect(fakeAvaxAsset).toEqual(
217
+ expect.objectContaining({
218
+ address: "1234",
219
+ chain: Chain.Avalanche,
220
+ decimal: 18,
221
+ isGasAsset: false,
222
+ isSynthetic: false,
223
+ symbol: "ASDF-1234",
224
+ ticker: "ASDF",
225
+ }),
226
+ );
227
+ });
228
+
229
+ test("creates AssetValue from string with multiple dashes", async () => {
230
+ const fakeAvaxAssetString = "AVAX.ASDF-LP-1234";
231
+ const fakeAvaxAsset = await AssetValue.fromString(fakeAvaxAssetString);
232
+
233
+ expect(fakeAvaxAsset).toEqual(
234
+ expect.objectContaining({
235
+ address: "1234",
236
+ chain: Chain.Avalanche,
237
+ decimal: 18,
238
+ isGasAsset: false,
239
+ isSynthetic: false,
240
+ symbol: "ASDF-LP-1234",
241
+ ticker: "ASDF-LP",
242
+ }),
243
+ );
244
+ });
245
+
246
+ test("creates AssetValue with _ symbol", async () => {
247
+ const radixXWBTC = await AssetValue.fromString(
248
+ "XRD.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
249
+ );
250
+
251
+ expect(radixXWBTC).toEqual(
252
+ expect.objectContaining({
253
+ address: "resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
254
+ chain: Chain.Radix,
255
+ decimal: 8,
256
+ isGasAsset: false,
257
+ isSynthetic: false,
258
+ symbol: "XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
259
+ ticker: "XWBTC",
260
+ }),
261
+ );
262
+ });
263
+ });
264
+
265
+ describe("fromStringWithBase", () => {
266
+ test("creates AssetValue from string with base", async () => {
267
+ const fakeAvaxAssetString = "AVAX.ASDF-1234";
268
+ const fakeAvaxAsset = await AssetValue.fromStringWithBase(fakeAvaxAssetString, 1, 8);
269
+
270
+ expect(fakeAvaxAsset).toEqual(
271
+ expect.objectContaining({
272
+ address: "1234",
273
+ chain: Chain.Avalanche,
274
+ decimal: 18,
275
+ isGasAsset: false,
276
+ isSynthetic: false,
277
+ symbol: "ASDF-1234",
278
+ ticker: "ASDF",
279
+ }),
280
+ );
281
+ expect(fakeAvaxAsset.getValue("string")).toBe("100000000");
282
+ expect(fakeAvaxAsset.getBaseValue("string")).toBe("100000000000000000000000000");
283
+ });
284
+ });
285
+
286
+ describe("fromUrl", () => {
287
+ test("creates AssetValue from url like format", async () => {
288
+ const synthETHString = "THOR.ETH.ETH";
289
+ const ethString = "ETH.ETH";
290
+ const thorString = "ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044";
291
+ const synthThorString = "THOR.ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044";
292
+ const synthDashesString = "THOR.ETH.PENDLE-LPT-0x1234";
293
+
294
+ const synthETH = await AssetValue.fromUrl(synthETHString);
295
+ const eth = await AssetValue.fromUrl(ethString);
296
+ const thor = await AssetValue.fromUrl(thorString);
297
+ const synthThor = await AssetValue.fromUrl(synthThorString);
298
+ const synthDashes = await AssetValue.fromUrl(synthDashesString);
299
+
300
+ expect(synthETH.toString()).toBe("ETH/ETH");
301
+ expect(eth.toString()).toBe("ETH.ETH");
302
+ expect(thor.toString()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
303
+ expect(synthThor.toString()).toBe("ETH/THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
304
+ expect(synthDashes.toString()).toBe("ETH/PENDLE-LPT-0x1234");
146
305
  });
147
306
  });
148
307
 
149
- describe('fromIdentifierSync', () => {
150
- test('(same as fromIdentifier) - creates AssetValue from string via `@swapkit/tokens` lists', async () => {
308
+ describe("fromIdentifierSync", () => {
309
+ test("(same as fromIdentifier) - creates AssetValue from string via `@swapkit/tokens` lists", async () => {
151
310
  await AssetValue.loadStaticAssets();
152
311
  const thor = AssetValue.fromIdentifierSync(
153
- 'ARB.USDT-0XFD086BC7CD5C481DCC9C85EBE478A1C0B69FCBB9',
312
+ "ARB.USDT-0XFD086BC7CD5C481DCC9C85EBE478A1C0B69FCBB9",
154
313
  );
155
314
 
156
315
  expect(thor).toBeDefined();
157
316
  expect(thor).toEqual(
158
317
  expect.objectContaining({
159
- address: '0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9',
318
+ address: "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
160
319
  chain: Chain.Arbitrum,
161
320
  decimal: 6,
162
321
  isGasAsset: false,
163
322
  isSynthetic: false,
164
- symbol: 'USDT-0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9',
165
- ticker: 'USDT',
323
+ symbol: "USDT-0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
324
+ ticker: "USDT",
166
325
  }),
167
326
  );
168
327
  });
169
328
  });
170
329
 
171
- describe('fromStringSync', () => {
172
- test('creates AssetValue from string via `@swapkit/tokens` lists', async () => {
330
+ describe("fromStringSync", () => {
331
+ test("creates AssetValue from string via `@swapkit/tokens` lists", async () => {
173
332
  await AssetValue.loadStaticAssets();
174
- const thor = AssetValue.fromStringSync('ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044');
333
+ const thor = AssetValue.fromStringSync("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
175
334
 
176
335
  expect(thor).toBeDefined();
177
336
  expect(thor).toEqual(
178
337
  expect.objectContaining({
179
- address: '0xa5f2211b9b8170f694421f2046281775e8468044',
338
+ address: "0xa5f2211b9b8170f694421f2046281775e8468044",
339
+ chain: Chain.Ethereum,
340
+ decimal: 18,
341
+ isGasAsset: false,
342
+ isSynthetic: false,
343
+ symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
344
+ ticker: "THOR",
345
+ }),
346
+ );
347
+
348
+ const usdc = AssetValue.fromStringSync("ETH.USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48");
349
+ expect(usdc).toBeDefined();
350
+ expect(usdc).toEqual(
351
+ expect.objectContaining({
352
+ address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
180
353
  chain: Chain.Ethereum,
354
+ decimal: 6,
355
+ isGasAsset: false,
356
+ isSynthetic: false,
357
+ symbol: "USDC-0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
358
+ ticker: "USDC",
359
+ }),
360
+ );
361
+ });
362
+
363
+ test("returns safe decimals if string is not in `@swapkit/tokens` lists", async () => {
364
+ await AssetValue.loadStaticAssets();
365
+ const fakeAvaxUSDCAssetString = "AVAX.USDC-1234";
366
+ const fakeAvaxUSDCAsset = AssetValue.fromStringSync(fakeAvaxUSDCAssetString);
367
+
368
+ expect(fakeAvaxUSDCAsset).toBeDefined();
369
+ expect(fakeAvaxUSDCAsset).toEqual(
370
+ expect.objectContaining({
371
+ address: "1234",
372
+ chain: Chain.Avalanche,
181
373
  decimal: 18,
182
374
  isGasAsset: false,
183
375
  isSynthetic: false,
184
- symbol: 'THOR-0xa5f2211b9b8170f694421f2046281775e8468044',
185
- ticker: 'THOR',
376
+ symbol: "USDC-1234",
377
+ ticker: "USDC",
186
378
  }),
187
379
  );
188
380
  });
189
381
 
190
- test('returns undefined if string is not in `@swapkit/tokens` lists', async () => {
382
+ test("returns safe decimals if string is not in `@swapkit/tokens` lists with multiple dashes", async () => {
191
383
  await AssetValue.loadStaticAssets();
192
- const fakeAvaxUSDCAssetString = 'AVAX.USDC-1234';
384
+ const fakeAvaxUSDCAssetString = "AVAX.USDC-LPT-1234";
193
385
  const fakeAvaxUSDCAsset = AssetValue.fromStringSync(fakeAvaxUSDCAssetString);
194
386
 
195
- expect(fakeAvaxUSDCAsset).toBeUndefined();
387
+ expect(fakeAvaxUSDCAsset).toBeDefined();
388
+ expect(fakeAvaxUSDCAsset).toEqual(
389
+ expect.objectContaining({
390
+ address: "1234",
391
+ chain: Chain.Avalanche,
392
+ decimal: 18,
393
+ isGasAsset: false,
394
+ isSynthetic: false,
395
+ symbol: "USDC-LPT-1234",
396
+ ticker: "USDC-LPT",
397
+ }),
398
+ );
399
+ });
400
+
401
+ test("returns proper avax string with address from `@swapkit/tokens` lists", async () => {
402
+ await AssetValue.loadStaticAssets();
403
+ const avaxBTCb = "AVAX.BTC.b-0x152b9d0fdc40c096757f570a51e494bd4b943e50";
404
+ const AvaxBTCb = AssetValue.fromStringSync(avaxBTCb);
405
+
406
+ expect(AvaxBTCb).toBeDefined();
407
+ expect(AvaxBTCb).toEqual(
408
+ expect.objectContaining({
409
+ address: "0x152b9d0fdc40c096757f570a51e494bd4b943e50",
410
+ chain: Chain.Avalanche,
411
+ decimal: 8,
412
+ isGasAsset: false,
413
+ isSynthetic: false,
414
+ symbol: "BTC.b-0x152b9d0fdc40c096757f570a51e494bd4b943e50",
415
+ ticker: "BTC.b",
416
+ }),
417
+ );
196
418
  });
197
419
  });
198
420
 
199
- describe('fromChainOrSignature', () => {
200
- test('creates AssetValue from common asset string or chain', () => {
201
- const customBaseAsset = [Chain.Cosmos, Chain.BinanceSmartChain, Chain.THORChain, Chain.Maya];
202
- Object.values(Chain)
203
- .filter((c) => !customBaseAsset.includes(c))
204
- .forEach((chain) => {
205
- const asset = AssetValue.fromChainOrSignature(chain);
206
- expect(asset).toEqual(
207
- expect.objectContaining({
208
- address: undefined,
209
- chain,
210
- decimal: BaseDecimal[chain],
211
- isGasAsset: ![Chain.Arbitrum, Chain.Optimism].includes(chain),
212
- isSynthetic: false,
213
- symbol: chain,
214
- ticker: chain,
215
- type: 'Native',
216
- }),
217
- );
218
- });
421
+ describe("fromStringWithBaseSync", () => {
422
+ test("creates AssetValue from string with base decimals via `@swapkit/tokens` lists", async () => {
423
+ await AssetValue.loadStaticAssets();
424
+ const btc = AssetValue.fromStringWithBaseSync("BTC.BTC", 5200000000000, 8);
425
+
426
+ expect(btc).toBeDefined();
427
+ expect(btc).toEqual(
428
+ expect.objectContaining({
429
+ chain: Chain.Bitcoin,
430
+ decimal: 8,
431
+ isGasAsset: true,
432
+ isSynthetic: false,
433
+ symbol: "BTC",
434
+ ticker: "BTC",
435
+ }),
436
+ );
437
+
438
+ expect(btc.getValue("string")).toBe("52000");
439
+ expect(btc.getBaseValue("string")).toBe("5200000000000");
440
+ });
441
+
442
+ test("returns safe decimals if string is not in `@swapkit/tokens` lists", async () => {
443
+ await AssetValue.loadStaticAssets();
444
+ const fakeAvaxUSDCAssetString = "AVAX.USDC-1234";
445
+ const fakeAvaxUSDCAsset = AssetValue.fromStringWithBaseSync(fakeAvaxUSDCAssetString, 1, 8);
446
+
447
+ expect(fakeAvaxUSDCAsset).toBeDefined();
448
+ expect(fakeAvaxUSDCAsset).toEqual(
449
+ expect.objectContaining({
450
+ address: "1234",
451
+ chain: Chain.Avalanche,
452
+ decimal: 18,
453
+ isGasAsset: false,
454
+ isSynthetic: false,
455
+ symbol: "USDC-1234",
456
+ ticker: "USDC",
457
+ }),
458
+ );
459
+
460
+ expect(fakeAvaxUSDCAsset.getValue("string")).toBe("0.00000001");
461
+ expect(fakeAvaxUSDCAsset.getBaseValue("string")).toBe("10000000000");
462
+ });
463
+
464
+ test("returns proper avax string with address from `@swapkit/tokens` lists", async () => {
465
+ await AssetValue.loadStaticAssets();
466
+ const avaxUSDC = "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e";
467
+ const AvaxUSDC = AssetValue.fromStringWithBaseSync(avaxUSDC, 100000000, 8);
468
+
469
+ expect(AvaxUSDC).toBeDefined();
470
+ expect(AvaxUSDC).toEqual(
471
+ expect.objectContaining({
472
+ address: "0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
473
+ chain: Chain.Avalanche,
474
+ decimal: 6,
475
+ isGasAsset: false,
476
+ isSynthetic: false,
477
+ symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
478
+ ticker: "USDC",
479
+ }),
480
+ );
481
+
482
+ expect(AvaxUSDC.getValue("string")).toBe("1");
483
+ expect(AvaxUSDC.getBaseValue("string")).toBe("1000000");
484
+ });
485
+ });
486
+
487
+ describe("fromChainOrSignature", () => {
488
+ test("creates AssetValue from common asset string or chain", () => {
489
+ const customBaseAsset = [
490
+ Chain.Cosmos,
491
+ Chain.BinanceSmartChain,
492
+ Chain.THORChain,
493
+ Chain.Maya,
494
+ Chain.Arbitrum,
495
+ Chain.Optimism,
496
+ Chain.Radix,
497
+ ];
498
+ const filteredChains = Object.values(Chain).filter((c) => !customBaseAsset.includes(c));
499
+
500
+ for (const chain of filteredChains) {
501
+ const asset = AssetValue.fromChainOrSignature(chain);
502
+ expect(asset).toEqual(
503
+ expect.objectContaining({
504
+ address: undefined,
505
+ chain,
506
+ decimal: BaseDecimal[chain],
507
+ isGasAsset: true,
508
+ isSynthetic: false,
509
+ symbol: chain,
510
+ ticker: chain,
511
+ type: "Native",
512
+ }),
513
+ );
514
+ }
219
515
 
220
516
  const cosmosAsset = AssetValue.fromChainOrSignature(Chain.Cosmos);
221
517
  expect(cosmosAsset).toEqual(
@@ -225,9 +521,9 @@ describe('AssetValue', () => {
225
521
  decimal: BaseDecimal.GAIA,
226
522
  isGasAsset: true,
227
523
  isSynthetic: false,
228
- symbol: 'ATOM',
229
- ticker: 'ATOM',
230
- type: 'Native',
524
+ symbol: "ATOM",
525
+ ticker: "ATOM",
526
+ type: "Native",
231
527
  }),
232
528
  );
233
529
 
@@ -239,9 +535,9 @@ describe('AssetValue', () => {
239
535
  decimal: BaseDecimal.BSC,
240
536
  isGasAsset: true,
241
537
  isSynthetic: false,
242
- symbol: 'BNB',
243
- ticker: 'BNB',
244
- type: 'Native',
538
+ symbol: "BNB",
539
+ ticker: "BNB",
540
+ type: "Native",
245
541
  }),
246
542
  );
247
543
 
@@ -253,9 +549,9 @@ describe('AssetValue', () => {
253
549
  decimal: BaseDecimal.THOR,
254
550
  isGasAsset: true,
255
551
  isSynthetic: false,
256
- symbol: 'RUNE',
257
- ticker: 'RUNE',
258
- type: 'Native',
552
+ symbol: "RUNE",
553
+ ticker: "RUNE",
554
+ type: "Native",
259
555
  }),
260
556
  );
261
557
 
@@ -267,45 +563,106 @@ describe('AssetValue', () => {
267
563
  decimal: BaseDecimal.MAYA,
268
564
  isGasAsset: true,
269
565
  isSynthetic: false,
270
- symbol: 'CACAO',
271
- ticker: 'CACAO',
272
- type: 'Native',
566
+ symbol: "CACAO",
567
+ ticker: "CACAO",
568
+ type: "Native",
273
569
  }),
274
570
  );
275
571
 
276
- const thor = AssetValue.fromChainOrSignature('ETH.THOR');
572
+ const thor = AssetValue.fromChainOrSignature("ETH.THOR");
277
573
  expect(thor).toEqual(
278
574
  expect.objectContaining({
279
- address: '0xa5f2211b9b8170f694421f2046281775e8468044',
575
+ address: "0xa5f2211b9b8170f694421f2046281775e8468044",
280
576
  chain: Chain.Ethereum,
281
577
  decimal: 18,
282
578
  isGasAsset: false,
283
579
  isSynthetic: false,
284
- symbol: 'THOR-0xa5f2211b9b8170f694421f2046281775e8468044',
285
- ticker: 'THOR',
580
+ symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
581
+ ticker: "THOR",
286
582
  }),
287
583
  );
288
584
 
289
- const vthor = AssetValue.fromChainOrSignature('ETH.vTHOR');
585
+ const vthor = AssetValue.fromChainOrSignature("ETH.vTHOR");
290
586
  expect(vthor).toEqual(
291
587
  expect.objectContaining({
292
- address: '0x815c23eca83261b6ec689b60cc4a58b54bc24d8d',
588
+ address: "0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
293
589
  chain: Chain.Ethereum,
294
590
  decimal: 18,
295
591
  isGasAsset: false,
296
592
  isSynthetic: false,
297
- symbol: 'vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d',
298
- ticker: 'vTHOR',
593
+ symbol: "vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
594
+ ticker: "vTHOR",
595
+ }),
596
+ );
597
+
598
+ const arbAsset = AssetValue.fromChainOrSignature(Chain.Arbitrum);
599
+ expect(arbAsset).toEqual(
600
+ expect.objectContaining({
601
+ address: undefined,
602
+ chain: Chain.Arbitrum,
603
+ decimal: BaseDecimal.ARB,
604
+ isGasAsset: true,
605
+ isSynthetic: false,
606
+ symbol: "ETH",
607
+ ticker: "ETH",
608
+ type: "Native",
609
+ }),
610
+ );
611
+
612
+ const opAsset = AssetValue.fromChainOrSignature(Chain.Optimism);
613
+ expect(opAsset).toEqual(
614
+ expect.objectContaining({
615
+ address: undefined,
616
+ chain: Chain.Optimism,
617
+ decimal: BaseDecimal.OP,
618
+ isGasAsset: true,
619
+ isSynthetic: false,
620
+ symbol: "ETH",
621
+ ticker: "ETH",
622
+ type: "Native",
623
+ }),
624
+ );
625
+
626
+ const xrdAsset = AssetValue.fromChainOrSignature(Chain.Radix);
627
+ expect(xrdAsset).toEqual(
628
+ expect.objectContaining({
629
+ address: undefined,
630
+ chain: Chain.Radix,
631
+ decimal: BaseDecimal.XRD,
632
+ isGasAsset: true,
633
+ isSynthetic: false,
634
+ symbol: "XRD",
635
+ ticker: "XRD",
636
+ type: "Native",
299
637
  }),
300
638
  );
301
639
  });
302
640
  });
303
641
 
304
- describe('loadStaticAssets', () => {
305
- test('loads static assets from `@swapkit/tokens` lists', async () => {
642
+ describe("loadStaticAssets", () => {
643
+ test("loads static assets from `@swapkit/tokens` lists", async () => {
306
644
  // Dummy test - think of sth more meaningful
307
645
  const { ok } = await AssetValue.loadStaticAssets();
308
646
  expect(ok).toBe(true);
309
647
  });
310
648
  });
311
649
  });
650
+
651
+ describe("getMinAmountByChain", () => {
652
+ test("returns min amount for chain", () => {
653
+ expect(getMinAmountByChain(Chain.THORChain).getValue("string")).toBe("0");
654
+ expect(getMinAmountByChain(Chain.Maya).getValue("string")).toBe("0");
655
+ expect(getMinAmountByChain(Chain.Cosmos).getValue("string")).toBe("0.000001");
656
+
657
+ expect(getMinAmountByChain(Chain.Bitcoin).getValue("string")).toBe("0.00010001");
658
+ expect(getMinAmountByChain(Chain.Litecoin).getValue("string")).toBe("0.00010001");
659
+ expect(getMinAmountByChain(Chain.BitcoinCash).getValue("string")).toBe("0.00010001");
660
+ expect(getMinAmountByChain(Chain.Dogecoin).getValue("string")).toBe("1.00000001");
661
+
662
+ expect(getMinAmountByChain(Chain.BinanceSmartChain).getValue("string")).toBe("0.00000001");
663
+ expect(getMinAmountByChain(Chain.Ethereum).getValue("string")).toBe("0.00000001");
664
+ expect(getMinAmountByChain(Chain.Avalanche).getValue("string")).toBe("0.00000001");
665
+ expect(getMinAmountByChain(Chain.Arbitrum).getValue("string")).toBe("0.00000001");
666
+ expect(getMinAmountByChain(Chain.Optimism).getValue("string")).toBe("0.00000001");
667
+ });
668
+ });