@swapkit/helpers 1.0.0-rc.7 → 1.0.0-rc.9
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/dist/index.cjs +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.es.js +711 -381
- package/package.json +8 -5
- package/src/helpers/asset.ts +7 -13
- package/src/helpers/others.ts +0 -48
- package/src/helpers/request.ts +16 -0
- package/src/index.ts +1 -0
- package/src/modules/__tests__/assetValue.test.ts +6 -7
- package/src/modules/__tests__/swapKitNumber.test.ts +47 -44
- package/src/modules/bigIntArithmetics.ts +6 -4
package/package.json
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "swapkit-oss-team",
|
|
3
3
|
"description": "SwapKit Lib swapkit-helpers",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"ky": "1.1.2"
|
|
6
|
+
},
|
|
4
7
|
"devDependencies": {
|
|
5
8
|
"@vitest/coverage-istanbul": "0.34.4",
|
|
6
9
|
"vite": "4.4.9",
|
|
7
10
|
"vitest": "0.34.4",
|
|
8
11
|
"@internal/config": "0.0.0-internal.0",
|
|
9
|
-
"@swapkit/tokens": "1.0.0-rc.
|
|
10
|
-
"@swapkit/types": "1.0.0-rc.
|
|
12
|
+
"@swapkit/tokens": "1.0.0-rc.6",
|
|
13
|
+
"@swapkit/types": "1.0.0-rc.6"
|
|
11
14
|
},
|
|
12
15
|
"eslintConfig": {
|
|
13
16
|
"extends": "../../../internal/eslint-config"
|
|
14
17
|
},
|
|
15
18
|
"peerDependencies": {
|
|
16
|
-
"@swapkit/tokens": "1.0.0-rc.
|
|
17
|
-
"@swapkit/types": "1.0.0-rc.
|
|
19
|
+
"@swapkit/tokens": "1.0.0-rc.6",
|
|
20
|
+
"@swapkit/types": "1.0.0-rc.6"
|
|
18
21
|
},
|
|
19
22
|
"exports": {
|
|
20
23
|
".": {
|
|
@@ -39,7 +42,7 @@
|
|
|
39
42
|
"repository": "https://github.com/thorswap/SwapKit.git",
|
|
40
43
|
"type": "module",
|
|
41
44
|
"types": "./dist/index.d.ts",
|
|
42
|
-
"version": "1.0.0-rc.
|
|
45
|
+
"version": "1.0.0-rc.9",
|
|
43
46
|
"scripts": {
|
|
44
47
|
"build": "vite build",
|
|
45
48
|
"clean": "rm -rf dist vite.config.ts.* .turbo node_modules",
|
package/src/helpers/asset.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import type { EVMChain } from '@swapkit/types';
|
|
2
2
|
import { BaseDecimal, Chain, ChainToRPC, FeeOption } from '@swapkit/types';
|
|
3
3
|
|
|
4
|
-
import type
|
|
5
|
-
|
|
6
|
-
import { postRequest } from './others.ts';
|
|
4
|
+
import { type AssetValue, RequestClient } from '../index.ts';
|
|
7
5
|
|
|
8
6
|
const getDecimalMethodHex = '0x313ce567';
|
|
9
7
|
|
|
@@ -11,19 +9,15 @@ export type CommonAssetString = 'MAYA.MAYA' | 'ETH.THOR' | 'ETH.vTHOR' | Chain;
|
|
|
11
9
|
|
|
12
10
|
const getContractDecimals = async ({ chain, to }: { chain: EVMChain; to: string }) => {
|
|
13
11
|
try {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
JSON.stringify({
|
|
17
|
-
method: 'eth_call',
|
|
18
|
-
params: [{ to: to.toLowerCase(), data: getDecimalMethodHex }, 'latest'],
|
|
12
|
+
const { result } = await RequestClient.post<{ result: string }>(ChainToRPC[chain], {
|
|
13
|
+
headers: { accept: '*/*', 'cache-control': 'no-cache' },
|
|
14
|
+
body: JSON.stringify({
|
|
19
15
|
id: 44,
|
|
20
16
|
jsonrpc: '2.0',
|
|
17
|
+
method: 'eth_call',
|
|
18
|
+
params: [{ to: to.toLowerCase(), data: getDecimalMethodHex }, 'latest'],
|
|
21
19
|
}),
|
|
22
|
-
|
|
23
|
-
true,
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
const { result } = JSON.parse(response) as { result: string };
|
|
20
|
+
});
|
|
27
21
|
|
|
28
22
|
return parseInt(BigInt(result).toString());
|
|
29
23
|
} catch (error) {
|
package/src/helpers/others.ts
CHANGED
|
@@ -18,51 +18,3 @@ export const derivationPathToString = ([network, chainId, account, change, index
|
|
|
18
18
|
|
|
19
19
|
return `${network}'/${chainId}'/${account}'/${change}${shortPath ? '' : `/${index}`}`;
|
|
20
20
|
};
|
|
21
|
-
|
|
22
|
-
export const getRequest = async <T>(
|
|
23
|
-
url: string,
|
|
24
|
-
params?: { [key in string]?: any },
|
|
25
|
-
): Promise<T> => {
|
|
26
|
-
try {
|
|
27
|
-
const queryParams = Object.entries(params || {}).reduce(
|
|
28
|
-
(acc, [key, value]) => {
|
|
29
|
-
if (value) {
|
|
30
|
-
acc[key] = value;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return acc;
|
|
34
|
-
},
|
|
35
|
-
{} as { [key in string]: any },
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
const response = await fetch(
|
|
39
|
-
`${url}${params ? `?${new URLSearchParams(queryParams).toString()}` : ''}`,
|
|
40
|
-
{ method: 'GET', mode: 'cors', credentials: 'omit', referrer: 'https://sk.thorswap.net' },
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
return response.json();
|
|
44
|
-
} catch (error) {
|
|
45
|
-
console.error(error);
|
|
46
|
-
return {} as T;
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const postRequest = async <T>(
|
|
51
|
-
url: string,
|
|
52
|
-
body: string,
|
|
53
|
-
headers?: Record<string, string>,
|
|
54
|
-
parseAsString = false,
|
|
55
|
-
): Promise<T> => {
|
|
56
|
-
try {
|
|
57
|
-
const response = await fetch(`${url}`, {
|
|
58
|
-
body,
|
|
59
|
-
headers,
|
|
60
|
-
method: 'POST',
|
|
61
|
-
referrer: 'https://sk.thorswap.net',
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
return parseAsString ? response.text() : response.json();
|
|
65
|
-
} catch (error) {
|
|
66
|
-
return {} as T;
|
|
67
|
-
}
|
|
68
|
-
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Options } from 'ky';
|
|
2
|
+
import ky from 'ky';
|
|
3
|
+
|
|
4
|
+
const kyClient = ky.create({
|
|
5
|
+
headers: {
|
|
6
|
+
referrer: 'https://sk.thorswap.net',
|
|
7
|
+
referer: 'https://sk.thorswap.net',
|
|
8
|
+
'Content-Type': 'application/json',
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const RequestClient = {
|
|
13
|
+
get: <T>(url: string | URL | Request, options?: Options) => kyClient.get(url, options).json<T>(),
|
|
14
|
+
post: <T>(url: string | URL | Request, options?: Options) =>
|
|
15
|
+
kyClient.post(url, options).json<T>(),
|
|
16
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -79,15 +79,13 @@ describe('AssetValue', () => {
|
|
|
79
79
|
|
|
80
80
|
describe('toString', () => {
|
|
81
81
|
test('returns asset value string/identifier', async () => {
|
|
82
|
-
const
|
|
82
|
+
const avaxUSDCAsset = new AssetValue({
|
|
83
83
|
decimal: 6,
|
|
84
84
|
value: 1234567890,
|
|
85
85
|
chain: Chain.Avalanche,
|
|
86
86
|
symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
87
87
|
});
|
|
88
|
-
expect(
|
|
89
|
-
'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
90
|
-
);
|
|
88
|
+
expect(avaxUSDCAsset.toString()).toBe('AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e');
|
|
91
89
|
|
|
92
90
|
const thor = AssetValue.fromChainOrSignature('ETH.THOR');
|
|
93
91
|
expect(thor.toString()).toBe('ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044');
|
|
@@ -99,10 +97,11 @@ describe('AssetValue', () => {
|
|
|
99
97
|
|
|
100
98
|
describe('fromIdentifier', () => {
|
|
101
99
|
test('creates AssetValue from string', async () => {
|
|
102
|
-
const
|
|
103
|
-
|
|
100
|
+
const avaxUSDCAsset = await AssetValue.fromIdentifier(
|
|
101
|
+
'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
102
|
+
);
|
|
104
103
|
|
|
105
|
-
expect(
|
|
104
|
+
expect(avaxUSDCAsset).toEqual(
|
|
106
105
|
expect.objectContaining({
|
|
107
106
|
address: '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
108
107
|
chain: Chain.Avalanche,
|
|
@@ -8,37 +8,40 @@ describe('SwapKitNumber', () => {
|
|
|
8
8
|
const skNumber1 = new SwapKitNumber(1);
|
|
9
9
|
expect(skNumber1.getValue('string')).toBe('1');
|
|
10
10
|
expect(skNumber1.getValue('number')).toBe(1);
|
|
11
|
-
expect(skNumber1.
|
|
11
|
+
expect(skNumber1.getBaseValue('bigint')).toBe(100000000n);
|
|
12
12
|
|
|
13
13
|
const skNumber2 = new SwapKitNumber('1');
|
|
14
14
|
expect(skNumber2.getValue('string')).toBe('1');
|
|
15
|
-
expect(skNumber2.
|
|
15
|
+
expect(skNumber2.getBaseValue('bigint')).toBe(100000000n);
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* because by default we have 8 decimals - it will be rounded to 0 on base value
|
|
19
|
+
*/
|
|
17
20
|
const skNumber3 = new SwapKitNumber('0.0000000001');
|
|
18
21
|
expect(skNumber3.getValue('string')).toBe('0.0000000001');
|
|
19
|
-
expect(skNumber3.
|
|
22
|
+
expect(skNumber3.getBaseValue('bigint')).toBe(0n);
|
|
20
23
|
|
|
21
|
-
const skNumber4 = new SwapKitNumber(0.
|
|
22
|
-
expect(skNumber4.getValue('string')).toBe('0.
|
|
23
|
-
expect(skNumber4.
|
|
24
|
+
const skNumber4 = new SwapKitNumber({ value: '0.0000000001', decimal: 10 });
|
|
25
|
+
expect(skNumber4.getValue('string')).toBe('0.0000000001');
|
|
26
|
+
expect(skNumber4.getBaseValue('bigint')).toBe(1n);
|
|
24
27
|
|
|
25
|
-
const
|
|
26
|
-
expect(
|
|
27
|
-
expect(
|
|
28
|
+
const skNumber5 = new SwapKitNumber({ value: 0.1005, decimal: 3 });
|
|
29
|
+
expect(skNumber5.getValue('string')).toBe('0.101');
|
|
30
|
+
expect(skNumber5.getBaseValue('bigint')).toBe(100n);
|
|
28
31
|
|
|
29
|
-
const
|
|
30
|
-
expect(
|
|
31
|
-
expect(
|
|
32
|
-
expect(
|
|
33
|
-
expect(
|
|
34
|
-
expect(
|
|
32
|
+
const skNumber6 = new SwapKitNumber({ value: -0.1005, decimal: 3 });
|
|
33
|
+
expect(skNumber6.getValue('string')).toBe('-0.101');
|
|
34
|
+
expect(skNumber6.getBaseValue('bigint')).toBe(-100n);
|
|
35
|
+
expect(skNumber6.decimal).toBe(3);
|
|
36
|
+
expect(skNumber6.getValue('number')).toBe(-0.101);
|
|
37
|
+
expect(skNumber6.decimalMultiplier).toBe(100000000n);
|
|
35
38
|
});
|
|
36
39
|
|
|
37
40
|
test('creates SwapKitInstance from BigInt: (12.345678901234, decimals: 12)', () => {
|
|
38
41
|
const skNumber = SwapKitNumber.fromBigInt(12345678901234n, 12);
|
|
39
42
|
|
|
40
43
|
expect(skNumber.getValue('string')).toBe('12.345678901234');
|
|
41
|
-
expect(skNumber.
|
|
44
|
+
expect(skNumber.getBaseValue('bigint')).toBe(12345678901234n);
|
|
42
45
|
});
|
|
43
46
|
});
|
|
44
47
|
|
|
@@ -46,7 +49,7 @@ describe('SwapKitNumber', () => {
|
|
|
46
49
|
test('shifts up and bumps number', () => {
|
|
47
50
|
const skNumber = new SwapKitNumber(1);
|
|
48
51
|
expect(skNumber.getValue('string')).toBe('1');
|
|
49
|
-
expect(skNumber.
|
|
52
|
+
expect(skNumber.getBaseValue('bigint')).toBe(100000000n);
|
|
50
53
|
|
|
51
54
|
const shiftedSkNumber = SwapKitNumber.shiftDecimals({
|
|
52
55
|
value: skNumber,
|
|
@@ -55,13 +58,13 @@ describe('SwapKitNumber', () => {
|
|
|
55
58
|
});
|
|
56
59
|
|
|
57
60
|
expect(shiftedSkNumber.getValue('string')).toBe('100');
|
|
58
|
-
expect(shiftedSkNumber.
|
|
61
|
+
expect(shiftedSkNumber.getBaseValue('bigint')).toBe(10000000000n);
|
|
59
62
|
});
|
|
60
63
|
|
|
61
64
|
test('shifts down and rounds down number', () => {
|
|
62
65
|
const skNumber = new SwapKitNumber(2.12345678);
|
|
63
66
|
expect(skNumber.getValue('string')).toBe('2.12345678');
|
|
64
|
-
expect(skNumber.
|
|
67
|
+
expect(skNumber.getBaseValue('bigint')).toBe(212345678n);
|
|
65
68
|
|
|
66
69
|
const shiftedSkNumber = SwapKitNumber.shiftDecimals({
|
|
67
70
|
value: skNumber,
|
|
@@ -70,7 +73,7 @@ describe('SwapKitNumber', () => {
|
|
|
70
73
|
});
|
|
71
74
|
|
|
72
75
|
expect(shiftedSkNumber.getValue('string')).toBe('2.123456');
|
|
73
|
-
expect(shiftedSkNumber.
|
|
76
|
+
expect(shiftedSkNumber.getBaseValue('bigint')).toBe(2123456n);
|
|
74
77
|
});
|
|
75
78
|
});
|
|
76
79
|
|
|
@@ -102,7 +105,7 @@ describe('SwapKitNumber', () => {
|
|
|
102
105
|
describe('bigint', () => {
|
|
103
106
|
test('returns bigint value', () => {
|
|
104
107
|
const skNumber = new SwapKitNumber(1);
|
|
105
|
-
expect(skNumber.
|
|
108
|
+
expect(skNumber.getBaseValue('bigint')).toBe(100000000n);
|
|
106
109
|
});
|
|
107
110
|
});
|
|
108
111
|
});
|
|
@@ -158,7 +161,7 @@ describe('SwapKitNumber', () => {
|
|
|
158
161
|
const result = skNumber1.add(skNumber2, skNumber3);
|
|
159
162
|
|
|
160
163
|
expect(result.getValue('string')).toBe('15.5');
|
|
161
|
-
expect(result.
|
|
164
|
+
expect(result.getBaseValue('bigint')).toBe(1550000000n);
|
|
162
165
|
});
|
|
163
166
|
|
|
164
167
|
test('adds different type numbers correctly', () => {
|
|
@@ -166,14 +169,14 @@ describe('SwapKitNumber', () => {
|
|
|
166
169
|
const result = skNumber1.add(6, '0.5');
|
|
167
170
|
|
|
168
171
|
expect(result.getValue('string')).toBe('16.5');
|
|
169
|
-
expect(result.
|
|
172
|
+
expect(result.getBaseValue('bigint')).toBe(1650000000n);
|
|
170
173
|
});
|
|
171
174
|
|
|
172
175
|
test('adds large decimal numbers correctly', () => {
|
|
173
176
|
const skNumber1 = new SwapKitNumber(0.0000000001);
|
|
174
177
|
const result = skNumber1.add(6.000000000001, '0.0000000000000005');
|
|
175
178
|
expect(result.getValue('string')).toBe('6.0000000001010005');
|
|
176
|
-
expect(result.
|
|
179
|
+
expect(result.getBaseValue('bigint')).toBe(600000000n);
|
|
177
180
|
});
|
|
178
181
|
});
|
|
179
182
|
|
|
@@ -185,7 +188,7 @@ describe('SwapKitNumber', () => {
|
|
|
185
188
|
const result = skNumber1.sub(skNumber2, skNumber3);
|
|
186
189
|
|
|
187
190
|
expect(result.getValue('string')).toBe('4.5');
|
|
188
|
-
expect(result.
|
|
191
|
+
expect(result.getBaseValue('bigint')).toBe(450000000n);
|
|
189
192
|
});
|
|
190
193
|
|
|
191
194
|
test('subtracts different type numbers correctly', () => {
|
|
@@ -193,7 +196,7 @@ describe('SwapKitNumber', () => {
|
|
|
193
196
|
const result = skNumber1.sub(6, '0.5');
|
|
194
197
|
|
|
195
198
|
expect(result.getValue('string')).toBe('3.5');
|
|
196
|
-
expect(result.
|
|
199
|
+
expect(result.getBaseValue('bigint')).toBe(350000000n);
|
|
197
200
|
});
|
|
198
201
|
|
|
199
202
|
test('can process negative results', () => {
|
|
@@ -203,8 +206,8 @@ describe('SwapKitNumber', () => {
|
|
|
203
206
|
|
|
204
207
|
expect(result0.getValue('string')).toBe('0');
|
|
205
208
|
expect(resultMinus.getValue('string')).toBe('-10');
|
|
206
|
-
expect(result0.
|
|
207
|
-
expect(resultMinus.
|
|
209
|
+
expect(result0.getBaseValue('bigint')).toBe(0n);
|
|
210
|
+
expect(resultMinus.getBaseValue('bigint')).toBe(-1000000000n);
|
|
208
211
|
});
|
|
209
212
|
});
|
|
210
213
|
|
|
@@ -216,7 +219,7 @@ describe('SwapKitNumber', () => {
|
|
|
216
219
|
const result = skNumber1.mul(skNumber2, skNumber3);
|
|
217
220
|
|
|
218
221
|
expect(result.getValue('string')).toBe('25');
|
|
219
|
-
expect(result.
|
|
222
|
+
expect(result.getBaseValue('bigint')).toBe(2500000000n);
|
|
220
223
|
});
|
|
221
224
|
|
|
222
225
|
test('multiplies different type numbers correctly', () => {
|
|
@@ -224,7 +227,7 @@ describe('SwapKitNumber', () => {
|
|
|
224
227
|
const result = skNumber1.mul(6, '0.5');
|
|
225
228
|
|
|
226
229
|
expect(result.getValue('string')).toBe('30');
|
|
227
|
-
expect(result.
|
|
230
|
+
expect(result.getBaseValue('bigint')).toBe(3000000000n);
|
|
228
231
|
});
|
|
229
232
|
|
|
230
233
|
test('multiplies numbers correctly if decimals of SKN is lower than number multiplied with', () => {
|
|
@@ -232,19 +235,19 @@ describe('SwapKitNumber', () => {
|
|
|
232
235
|
const result = skNumber1.mul('0.00001');
|
|
233
236
|
|
|
234
237
|
expect(result.getValue('string')).toBe('10');
|
|
235
|
-
expect(result.
|
|
238
|
+
expect(result.getBaseValue('bigint')).toBe(100000n);
|
|
236
239
|
});
|
|
237
240
|
|
|
238
241
|
test('should correctly round the result of multiplication', () => {
|
|
239
|
-
const skNumber1 = new SwapKitNumber({ decimal:
|
|
242
|
+
const skNumber1 = new SwapKitNumber({ decimal: 3, value: 1.23 });
|
|
240
243
|
const skNumber2 = new SwapKitNumber({ decimal: 4, value: 4.56 });
|
|
241
244
|
|
|
242
245
|
const result = skNumber1.mul(skNumber2);
|
|
243
246
|
|
|
244
247
|
// The exact result of 1.23 * 4.56 is 5.6088
|
|
245
248
|
// If we round it to 2 decimal places, we should get 5.61
|
|
246
|
-
expect(result.getValue('string')).toBe('5.
|
|
247
|
-
expect(result.
|
|
249
|
+
expect(result.getValue('string')).toBe('5.609');
|
|
250
|
+
expect(result.getBaseValue('bigint')).toBe(5608n);
|
|
248
251
|
|
|
249
252
|
const skNumber3 = new SwapKitNumber({ decimal: 2, value: 1.23 });
|
|
250
253
|
const skNumber4 = new SwapKitNumber(-1.234567891);
|
|
@@ -254,7 +257,7 @@ describe('SwapKitNumber', () => {
|
|
|
254
257
|
// The exact result of 1.23 * -1.234567891 is -1,518518505
|
|
255
258
|
// If we round it to 2 decimal places, we should get 5.61
|
|
256
259
|
expect(result2.getValue('string')).toBe('-1.52');
|
|
257
|
-
expect(result2.
|
|
260
|
+
expect(result2.getBaseValue('bigint')).toBe(-151n);
|
|
258
261
|
});
|
|
259
262
|
});
|
|
260
263
|
|
|
@@ -266,13 +269,13 @@ describe('SwapKitNumber', () => {
|
|
|
266
269
|
const result = skNumber1.div(skNumber2, skNumber3);
|
|
267
270
|
|
|
268
271
|
expect(result.getValue('string')).toBe('4');
|
|
269
|
-
expect(result.
|
|
272
|
+
expect(result.getBaseValue('bigint')).toBe(400000000n);
|
|
270
273
|
|
|
271
274
|
const skNumber4 = new SwapKitNumber(10.12);
|
|
272
275
|
const result2 = skNumber4.div(0.0001);
|
|
273
276
|
|
|
274
277
|
expect(result2.getValue('string')).toBe('101200');
|
|
275
|
-
expect(result2.
|
|
278
|
+
expect(result2.getBaseValue('bigint')).toBe(10120000000000n);
|
|
276
279
|
});
|
|
277
280
|
|
|
278
281
|
test('divides different type numbers correctly', () => {
|
|
@@ -280,7 +283,7 @@ describe('SwapKitNumber', () => {
|
|
|
280
283
|
const result = skNumber1.div(5, '0.5');
|
|
281
284
|
|
|
282
285
|
expect(result.getValue('string')).toBe('8');
|
|
283
|
-
expect(result.
|
|
286
|
+
expect(result.getBaseValue('bigint')).toBe(800000000n);
|
|
284
287
|
});
|
|
285
288
|
|
|
286
289
|
test('divides different type numbers correctly when decimal is set', () => {
|
|
@@ -288,7 +291,7 @@ describe('SwapKitNumber', () => {
|
|
|
288
291
|
const result = skNumber1.div(0.001);
|
|
289
292
|
|
|
290
293
|
expect(result.getValue('string')).toBe('1200');
|
|
291
|
-
expect(result.
|
|
294
|
+
expect(result.getBaseValue('bigint')).toBe(120000n);
|
|
292
295
|
});
|
|
293
296
|
|
|
294
297
|
test('divides smaller number by larger number', () => {
|
|
@@ -296,7 +299,7 @@ describe('SwapKitNumber', () => {
|
|
|
296
299
|
const result = skNumber1.div(2);
|
|
297
300
|
|
|
298
301
|
expect(result.getValue('string')).toBe('0.5');
|
|
299
|
-
expect(result.
|
|
302
|
+
expect(result.getBaseValue('bigint')).toBe(50000000n);
|
|
300
303
|
});
|
|
301
304
|
|
|
302
305
|
test('divides a number with 18 decimals by a negative number with less decimals', () => {
|
|
@@ -307,7 +310,7 @@ describe('SwapKitNumber', () => {
|
|
|
307
310
|
|
|
308
311
|
// The exact result of 1.000000000000000010 / -2 is -0.500000000000000005
|
|
309
312
|
expect(result.getValue('string')).toBe('-0.500000000000000005');
|
|
310
|
-
expect(result.
|
|
313
|
+
expect(result.getBaseValue('bigint')).toBe(-500000000000000005n);
|
|
311
314
|
});
|
|
312
315
|
|
|
313
316
|
test('divides a number with 2 decimals by a negative number with more decimals', () => {
|
|
@@ -318,7 +321,7 @@ describe('SwapKitNumber', () => {
|
|
|
318
321
|
|
|
319
322
|
// The exact result of 2 / -0.000005 is -400000
|
|
320
323
|
expect(result.getValue('string')).toBe('-400000');
|
|
321
|
-
expect(result.
|
|
324
|
+
expect(result.getBaseValue('bigint')).toBe(-40000000n);
|
|
322
325
|
});
|
|
323
326
|
});
|
|
324
327
|
|
|
@@ -329,7 +332,7 @@ describe('SwapKitNumber', () => {
|
|
|
329
332
|
|
|
330
333
|
const result = skNumber1.mul(skNumber2);
|
|
331
334
|
expect(result.getValue('string')).toBe('987654321000000987654321000');
|
|
332
|
-
expect(result.
|
|
335
|
+
expect(result.getBaseValue('bigint')).toBe(987654321000000987654321000000000000000000000n);
|
|
333
336
|
});
|
|
334
337
|
|
|
335
338
|
test('divide huge numbers', () => {
|
|
@@ -338,7 +341,7 @@ describe('SwapKitNumber', () => {
|
|
|
338
341
|
|
|
339
342
|
const result = skNumber1.div(skNumber2);
|
|
340
343
|
expect(result.getValue('string')).toBe('1012.4999999873447625');
|
|
341
|
-
expect(result.
|
|
344
|
+
expect(result.getBaseValue('bigint')).toBe(1012499999987344762500n);
|
|
342
345
|
});
|
|
343
346
|
});
|
|
344
347
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { BaseDecimal } from '@swapkit/types';
|
|
2
|
+
|
|
1
3
|
import type { SwapKitNumber } from './swapKitNumber.ts';
|
|
2
4
|
|
|
3
5
|
type NumberPrimitivesType = {
|
|
@@ -141,7 +143,7 @@ export class BigIntArithmetics {
|
|
|
141
143
|
return this.bigIntValue === this.getBigIntValue(value);
|
|
142
144
|
}
|
|
143
145
|
|
|
144
|
-
getValue<T extends 'number' | 'string'
|
|
146
|
+
getValue<T extends 'number' | 'string'>(type: T): NumberPrimitivesType[T] {
|
|
145
147
|
const value = this.formatBigIntToSafeValue(
|
|
146
148
|
this.bigIntValue,
|
|
147
149
|
this.decimal || decimalFromMultiplier(this.decimalMultiplier),
|
|
@@ -156,12 +158,12 @@ export class BigIntArithmetics {
|
|
|
156
158
|
return value;
|
|
157
159
|
default:
|
|
158
160
|
// @ts-expect-error False positive
|
|
159
|
-
return this.bigIntValue;
|
|
161
|
+
return (this.bigIntValue * BigInt(this.decimal || 8n)) / this.decimalMultiplier;
|
|
160
162
|
}
|
|
161
163
|
}
|
|
162
164
|
|
|
163
165
|
getBaseValue<T extends 'number' | 'string' | 'bigint'>(type: T): NumberPrimitivesType[T] {
|
|
164
|
-
const divisor = this.decimalMultiplier / toMultiplier(this.decimal ||
|
|
166
|
+
const divisor = this.decimalMultiplier / toMultiplier(this.decimal || BaseDecimal.THOR);
|
|
165
167
|
const baseValue = this.bigIntValue / divisor;
|
|
166
168
|
|
|
167
169
|
switch (type) {
|
|
@@ -173,7 +175,7 @@ export class BigIntArithmetics {
|
|
|
173
175
|
return baseValue.toString();
|
|
174
176
|
default:
|
|
175
177
|
// @ts-expect-error False positive
|
|
176
|
-
return
|
|
178
|
+
return baseValue;
|
|
177
179
|
}
|
|
178
180
|
}
|
|
179
181
|
|