@swapkit/helpers 1.0.0-rc.8 → 1.0.0-rc.80
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.
- package/LICENSE +1 -1
- package/dist/index.cjs +2 -1
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +143 -89
- package/dist/index.es.js +578 -446
- package/dist/index.es.js.map +1 -0
- package/package.json +19 -19
- package/src/helpers/__tests__/asset.test.ts +64 -62
- package/src/helpers/__tests__/memo.test.ts +52 -40
- package/src/helpers/__tests__/others.test.ts +57 -30
- package/src/helpers/asset.ts +122 -96
- package/src/helpers/liquidity.ts +50 -42
- package/src/helpers/memo.ts +30 -28
- package/src/helpers/others.ts +30 -56
- package/src/helpers/validators.ts +7 -6
- package/src/index.ts +10 -8
- package/src/modules/__tests__/assetValue.test.ts +329 -121
- package/src/modules/__tests__/bigIntArithmetics.test.ts +30 -0
- package/src/modules/__tests__/swapKitNumber.test.ts +306 -183
- package/src/modules/assetValue.ts +214 -152
- package/src/modules/bigIntArithmetics.ts +214 -146
- package/src/modules/swapKitError.ts +30 -5
- package/src/modules/swapKitNumber.ts +8 -1
- package/src/types.ts +30 -0
|
@@ -1,64 +1,96 @@
|
|
|
1
|
-
import { BaseDecimal, Chain } from
|
|
2
|
-
import { describe, expect, test } from
|
|
1
|
+
import { BaseDecimal, Chain } from "@swapkit/types";
|
|
2
|
+
import { describe, expect, test } from "vitest";
|
|
3
3
|
|
|
4
|
-
import { AssetValue } from
|
|
4
|
+
import { AssetValue, getMinAmountByChain } from "../assetValue.ts";
|
|
5
5
|
|
|
6
|
-
describe(
|
|
7
|
-
describe(
|
|
8
|
-
test(
|
|
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:
|
|
13
|
+
symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
|
|
14
14
|
});
|
|
15
|
-
expect(fakeAvaxUSDCAsset.assetValue).toBe('1234567890 USDC');
|
|
16
15
|
expect(fakeAvaxUSDCAsset.toString()).toBe(
|
|
17
|
-
|
|
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:
|
|
21
|
+
symbol: "ETH/ETH",
|
|
27
22
|
decimal: 8,
|
|
28
23
|
value: 1234567890,
|
|
29
24
|
});
|
|
30
25
|
|
|
31
|
-
expect(ethSynth.
|
|
32
|
-
expect(ethSynth.
|
|
33
|
-
|
|
34
|
-
|
|
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:
|
|
38
|
+
identifier: "THOR.ATOM",
|
|
38
39
|
decimal: 6,
|
|
39
40
|
value: 123456789,
|
|
40
41
|
});
|
|
41
42
|
|
|
42
|
-
expect(atomDerived.
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
expect(atomDerived.toString()).toBe("THOR.ATOM");
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
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");
|
|
45
77
|
});
|
|
46
78
|
});
|
|
47
79
|
|
|
48
|
-
describe(
|
|
49
|
-
test(
|
|
50
|
-
const firstThor = AssetValue.fromChainOrSignature(
|
|
51
|
-
const secondThor = AssetValue.fromChainOrSignature(
|
|
52
|
-
const vThor = AssetValue.fromChainOrSignature(
|
|
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:
|
|
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:
|
|
93
|
+
symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
|
|
62
94
|
decimal: 6,
|
|
63
95
|
value: 1234,
|
|
64
96
|
});
|
|
@@ -77,136 +109,293 @@ describe('AssetValue', () => {
|
|
|
77
109
|
});
|
|
78
110
|
});
|
|
79
111
|
|
|
80
|
-
describe(
|
|
81
|
-
test(
|
|
82
|
-
const
|
|
112
|
+
describe("from bigint", () => {
|
|
113
|
+
test.todo("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 () => {
|
|
124
|
+
const avaxUSDCAsset = new AssetValue({
|
|
83
125
|
decimal: 6,
|
|
84
126
|
value: 1234567890,
|
|
85
127
|
chain: Chain.Avalanche,
|
|
86
|
-
symbol:
|
|
128
|
+
symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
|
|
87
129
|
});
|
|
88
|
-
expect(
|
|
89
|
-
'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
90
|
-
);
|
|
130
|
+
expect(avaxUSDCAsset.toString()).toBe("AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e");
|
|
91
131
|
|
|
92
|
-
const thor = AssetValue.fromChainOrSignature(
|
|
93
|
-
expect(thor.toString()).toBe(
|
|
132
|
+
const thor = AssetValue.fromChainOrSignature("ETH.THOR");
|
|
133
|
+
expect(thor.toString()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
|
|
94
134
|
|
|
95
|
-
const ethSynth = await AssetValue.fromIdentifier(
|
|
96
|
-
expect(ethSynth.toString()).toBe(
|
|
135
|
+
const ethSynth = await AssetValue.fromIdentifier("ETH/ETH");
|
|
136
|
+
expect(ethSynth.toString()).toBe("ETH/ETH");
|
|
97
137
|
});
|
|
98
138
|
});
|
|
99
139
|
|
|
100
|
-
describe(
|
|
101
|
-
test(
|
|
102
|
-
const
|
|
103
|
-
|
|
140
|
+
describe("fromIdentifier", () => {
|
|
141
|
+
test.skip("creates AssetValue from string", async () => {
|
|
142
|
+
const avaxUSDCAsset = await AssetValue.fromIdentifier(
|
|
143
|
+
"AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
|
|
144
|
+
);
|
|
104
145
|
|
|
105
|
-
expect(
|
|
146
|
+
expect(avaxUSDCAsset).toEqual(
|
|
106
147
|
expect.objectContaining({
|
|
107
|
-
address:
|
|
148
|
+
address: "0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
|
|
108
149
|
chain: Chain.Avalanche,
|
|
109
150
|
decimal: 6,
|
|
110
151
|
isGasAsset: false,
|
|
111
152
|
isSynthetic: false,
|
|
112
|
-
symbol:
|
|
113
|
-
ticker:
|
|
153
|
+
symbol: "USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",
|
|
154
|
+
ticker: "USDC",
|
|
114
155
|
}),
|
|
115
156
|
);
|
|
116
157
|
});
|
|
117
158
|
});
|
|
118
159
|
|
|
119
|
-
describe(
|
|
120
|
-
test(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
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);
|
|
124
164
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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");
|
|
137
197
|
});
|
|
138
198
|
});
|
|
139
199
|
|
|
140
|
-
describe(
|
|
141
|
-
test(
|
|
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");
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe("fromIdentifierSync", () => {
|
|
220
|
+
test("(same as fromIdentifier) - creates AssetValue from string via `@swapkit/tokens` lists", async () => {
|
|
142
221
|
await AssetValue.loadStaticAssets();
|
|
143
222
|
const thor = AssetValue.fromIdentifierSync(
|
|
144
|
-
|
|
223
|
+
"ARB.USDT-0XFD086BC7CD5C481DCC9C85EBE478A1C0B69FCBB9",
|
|
145
224
|
);
|
|
146
225
|
|
|
147
226
|
expect(thor).toBeDefined();
|
|
148
227
|
expect(thor).toEqual(
|
|
149
228
|
expect.objectContaining({
|
|
150
|
-
address:
|
|
229
|
+
address: "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
|
|
151
230
|
chain: Chain.Arbitrum,
|
|
152
231
|
decimal: 6,
|
|
153
232
|
isGasAsset: false,
|
|
154
233
|
isSynthetic: false,
|
|
155
|
-
symbol:
|
|
156
|
-
ticker:
|
|
234
|
+
symbol: "USDT-0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
|
|
235
|
+
ticker: "USDT",
|
|
157
236
|
}),
|
|
158
237
|
);
|
|
159
238
|
});
|
|
160
239
|
});
|
|
161
240
|
|
|
162
|
-
describe(
|
|
163
|
-
test(
|
|
241
|
+
describe("fromStringSync", () => {
|
|
242
|
+
test("creates AssetValue from string via `@swapkit/tokens` lists", async () => {
|
|
164
243
|
await AssetValue.loadStaticAssets();
|
|
165
|
-
const thor = AssetValue.fromStringSync(
|
|
244
|
+
const thor = AssetValue.fromStringSync("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044");
|
|
166
245
|
|
|
167
246
|
expect(thor).toBeDefined();
|
|
168
247
|
expect(thor).toEqual(
|
|
169
248
|
expect.objectContaining({
|
|
170
|
-
address:
|
|
249
|
+
address: "0xa5f2211b9b8170f694421f2046281775e8468044",
|
|
171
250
|
chain: Chain.Ethereum,
|
|
172
251
|
decimal: 18,
|
|
173
252
|
isGasAsset: false,
|
|
174
253
|
isSynthetic: false,
|
|
175
|
-
symbol:
|
|
176
|
-
ticker:
|
|
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",
|
|
177
270
|
}),
|
|
178
271
|
);
|
|
179
272
|
});
|
|
180
273
|
|
|
181
|
-
test(
|
|
274
|
+
test("returns safe decimals if string is not in `@swapkit/tokens` lists", async () => {
|
|
182
275
|
await AssetValue.loadStaticAssets();
|
|
183
|
-
const fakeAvaxUSDCAssetString =
|
|
276
|
+
const fakeAvaxUSDCAssetString = "AVAX.USDC-1234";
|
|
184
277
|
const fakeAvaxUSDCAsset = AssetValue.fromStringSync(fakeAvaxUSDCAssetString);
|
|
185
278
|
|
|
186
|
-
expect(fakeAvaxUSDCAsset).
|
|
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
|
+
);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
|
|
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");
|
|
187
376
|
});
|
|
188
377
|
});
|
|
189
378
|
|
|
190
|
-
describe(
|
|
191
|
-
test(
|
|
379
|
+
describe("fromChainOrSignature", () => {
|
|
380
|
+
test("creates AssetValue from common asset string or chain", () => {
|
|
192
381
|
const customBaseAsset = [Chain.Cosmos, Chain.BinanceSmartChain, Chain.THORChain, Chain.Maya];
|
|
193
|
-
Object.values(Chain)
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
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
|
+
}
|
|
210
399
|
|
|
211
400
|
const cosmosAsset = AssetValue.fromChainOrSignature(Chain.Cosmos);
|
|
212
401
|
expect(cosmosAsset).toEqual(
|
|
@@ -216,9 +405,9 @@ describe('AssetValue', () => {
|
|
|
216
405
|
decimal: BaseDecimal.GAIA,
|
|
217
406
|
isGasAsset: true,
|
|
218
407
|
isSynthetic: false,
|
|
219
|
-
symbol:
|
|
220
|
-
ticker:
|
|
221
|
-
type:
|
|
408
|
+
symbol: "ATOM",
|
|
409
|
+
ticker: "ATOM",
|
|
410
|
+
type: "Native",
|
|
222
411
|
}),
|
|
223
412
|
);
|
|
224
413
|
|
|
@@ -230,9 +419,9 @@ describe('AssetValue', () => {
|
|
|
230
419
|
decimal: BaseDecimal.BSC,
|
|
231
420
|
isGasAsset: true,
|
|
232
421
|
isSynthetic: false,
|
|
233
|
-
symbol:
|
|
234
|
-
ticker:
|
|
235
|
-
type:
|
|
422
|
+
symbol: "BNB",
|
|
423
|
+
ticker: "BNB",
|
|
424
|
+
type: "Native",
|
|
236
425
|
}),
|
|
237
426
|
);
|
|
238
427
|
|
|
@@ -244,9 +433,9 @@ describe('AssetValue', () => {
|
|
|
244
433
|
decimal: BaseDecimal.THOR,
|
|
245
434
|
isGasAsset: true,
|
|
246
435
|
isSynthetic: false,
|
|
247
|
-
symbol:
|
|
248
|
-
ticker:
|
|
249
|
-
type:
|
|
436
|
+
symbol: "RUNE",
|
|
437
|
+
ticker: "RUNE",
|
|
438
|
+
type: "Native",
|
|
250
439
|
}),
|
|
251
440
|
);
|
|
252
441
|
|
|
@@ -258,45 +447,64 @@ describe('AssetValue', () => {
|
|
|
258
447
|
decimal: BaseDecimal.MAYA,
|
|
259
448
|
isGasAsset: true,
|
|
260
449
|
isSynthetic: false,
|
|
261
|
-
symbol:
|
|
262
|
-
ticker:
|
|
263
|
-
type:
|
|
450
|
+
symbol: "CACAO",
|
|
451
|
+
ticker: "CACAO",
|
|
452
|
+
type: "Native",
|
|
264
453
|
}),
|
|
265
454
|
);
|
|
266
455
|
|
|
267
|
-
const thor = AssetValue.fromChainOrSignature(
|
|
456
|
+
const thor = AssetValue.fromChainOrSignature("ETH.THOR");
|
|
268
457
|
expect(thor).toEqual(
|
|
269
458
|
expect.objectContaining({
|
|
270
|
-
address:
|
|
459
|
+
address: "0xa5f2211b9b8170f694421f2046281775e8468044",
|
|
271
460
|
chain: Chain.Ethereum,
|
|
272
461
|
decimal: 18,
|
|
273
462
|
isGasAsset: false,
|
|
274
463
|
isSynthetic: false,
|
|
275
|
-
symbol:
|
|
276
|
-
ticker:
|
|
464
|
+
symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044",
|
|
465
|
+
ticker: "THOR",
|
|
277
466
|
}),
|
|
278
467
|
);
|
|
279
468
|
|
|
280
|
-
const vthor = AssetValue.fromChainOrSignature(
|
|
469
|
+
const vthor = AssetValue.fromChainOrSignature("ETH.vTHOR");
|
|
281
470
|
expect(vthor).toEqual(
|
|
282
471
|
expect.objectContaining({
|
|
283
|
-
address:
|
|
472
|
+
address: "0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
|
|
284
473
|
chain: Chain.Ethereum,
|
|
285
474
|
decimal: 18,
|
|
286
475
|
isGasAsset: false,
|
|
287
476
|
isSynthetic: false,
|
|
288
|
-
symbol:
|
|
289
|
-
ticker:
|
|
477
|
+
symbol: "vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d",
|
|
478
|
+
ticker: "vTHOR",
|
|
290
479
|
}),
|
|
291
480
|
);
|
|
292
481
|
});
|
|
293
482
|
});
|
|
294
483
|
|
|
295
|
-
describe(
|
|
296
|
-
test(
|
|
484
|
+
describe("loadStaticAssets", () => {
|
|
485
|
+
test("loads static assets from `@swapkit/tokens` lists", async () => {
|
|
297
486
|
// Dummy test - think of sth more meaningful
|
|
298
487
|
const { ok } = await AssetValue.loadStaticAssets();
|
|
299
488
|
expect(ok).toBe(true);
|
|
300
489
|
});
|
|
301
490
|
});
|
|
302
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 "vitest";
|
|
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
|
+
});
|