@swapkit/helpers 1.0.0-rc.36 → 1.0.0-rc.37
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 +10 -3
- package/dist/index.es.js +251 -233
- package/package.json +1 -1
- package/src/modules/__tests__/swapKitNumber.test.ts +21 -0
- package/src/modules/assetValue.ts +26 -16
- package/src/modules/bigIntArithmetics.ts +24 -5
package/package.json
CHANGED
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"repository": "https://github.com/thorswap/SwapKit.git",
|
|
43
43
|
"type": "module",
|
|
44
44
|
"types": "./dist/index.d.ts",
|
|
45
|
-
"version": "1.0.0-rc.
|
|
45
|
+
"version": "1.0.0-rc.37",
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "vite build",
|
|
48
48
|
"clean": "rm -rf dist vite.config.ts.* .turbo node_modules",
|
|
@@ -455,6 +455,14 @@ describe('SwapKitNumber', () => {
|
|
|
455
455
|
expect(skNumber1.gt(skNumber2)).toBe(true);
|
|
456
456
|
expect(skNumber2.gt(skNumber1)).toBe(false);
|
|
457
457
|
});
|
|
458
|
+
|
|
459
|
+
test("different decimals doesn't affect comparison", () => {
|
|
460
|
+
const skNumber1 = new SwapKitNumber({ value: 10, decimal: 18 });
|
|
461
|
+
const skNumber2 = new SwapKitNumber({ value: '50', decimal: 8 });
|
|
462
|
+
|
|
463
|
+
expect(skNumber1.lt(skNumber2)).toBe(true);
|
|
464
|
+
expect(skNumber2.gt(skNumber1)).toBe(true);
|
|
465
|
+
});
|
|
458
466
|
});
|
|
459
467
|
|
|
460
468
|
describe('gte', () => {
|
|
@@ -500,6 +508,19 @@ describe('SwapKitNumber', () => {
|
|
|
500
508
|
});
|
|
501
509
|
});
|
|
502
510
|
|
|
511
|
+
describe('comparison edge cases with decimals', () => {
|
|
512
|
+
test('compare on cut decimals', () => {
|
|
513
|
+
const skNumber1 = new SwapKitNumber({ value: 0.001, decimal: 3 });
|
|
514
|
+
const value = '0.0019';
|
|
515
|
+
|
|
516
|
+
expect(skNumber1.lt(value)).toBe(true);
|
|
517
|
+
expect(skNumber1.gt(value)).toBe(false);
|
|
518
|
+
expect(skNumber1.eq(value)).toBe(false);
|
|
519
|
+
expect(skNumber1.lte(value)).toBe(true);
|
|
520
|
+
expect(skNumber1.gte(value)).toBe(false);
|
|
521
|
+
});
|
|
522
|
+
});
|
|
523
|
+
|
|
503
524
|
describe('Throws', () => {
|
|
504
525
|
test('throws if division by zero', () => {
|
|
505
526
|
const skNumber1 = new SwapKitNumber(10);
|
|
@@ -21,12 +21,14 @@ import type { NumberPrimitives } from './bigIntArithmetics.ts';
|
|
|
21
21
|
import { BigIntArithmetics, formatBigIntToSafeValue } from './bigIntArithmetics.ts';
|
|
22
22
|
import type { SwapKitValueType } from './swapKitNumber.ts';
|
|
23
23
|
|
|
24
|
+
type TokenTax = { buy: number; sell: number };
|
|
25
|
+
|
|
24
26
|
const safeValue = (value: NumberPrimitives, decimal: number) =>
|
|
25
27
|
typeof value === 'bigint'
|
|
26
28
|
? formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal })
|
|
27
29
|
: value;
|
|
28
30
|
|
|
29
|
-
type AssetValueParams = { decimal: number; value: SwapKitValueType } & (
|
|
31
|
+
type AssetValueParams = { decimal: number; value: SwapKitValueType; tax?: TokenTax } & (
|
|
30
32
|
| { chain: Chain; symbol: string }
|
|
31
33
|
| { identifier: string }
|
|
32
34
|
);
|
|
@@ -46,7 +48,9 @@ type TokenNames =
|
|
|
46
48
|
| (typeof WoofiList)['tokens'][number]['identifier']
|
|
47
49
|
| (typeof UniswapList)['tokens'][number]['identifier'];
|
|
48
50
|
|
|
49
|
-
let staticTokensMap:
|
|
51
|
+
let staticTokensMap:
|
|
52
|
+
| Map<TokenNames, { tax?: TokenTax; decimal: number; identifier: string }>
|
|
53
|
+
| undefined;
|
|
50
54
|
|
|
51
55
|
const getStaticToken = (identifier: TokenNames) => {
|
|
52
56
|
if (!staticTokensMap) {
|
|
@@ -67,6 +71,15 @@ const createAssetValue = async (assetString: string, value: NumberPrimitives = 0
|
|
|
67
71
|
};
|
|
68
72
|
|
|
69
73
|
export class AssetValue extends BigIntArithmetics {
|
|
74
|
+
address?: string;
|
|
75
|
+
chain: Chain;
|
|
76
|
+
isGasAsset = false;
|
|
77
|
+
isSynthetic = false;
|
|
78
|
+
symbol: string;
|
|
79
|
+
tax?: TokenTax;
|
|
80
|
+
ticker: string;
|
|
81
|
+
type: ReturnType<typeof getAssetType>;
|
|
82
|
+
|
|
70
83
|
constructor(params: AssetValueParams) {
|
|
71
84
|
const identifier =
|
|
72
85
|
'identifier' in params ? params.identifier : `${params.chain}.${params.symbol}`;
|
|
@@ -78,6 +91,7 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
78
91
|
);
|
|
79
92
|
|
|
80
93
|
const assetInfo = getAssetInfo(identifier);
|
|
94
|
+
|
|
81
95
|
this.type = getAssetType(assetInfo);
|
|
82
96
|
this.chain = assetInfo.chain;
|
|
83
97
|
this.ticker = assetInfo.ticker;
|
|
@@ -85,15 +99,9 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
85
99
|
this.address = assetInfo.address;
|
|
86
100
|
this.isSynthetic = assetInfo.isSynthetic;
|
|
87
101
|
this.isGasAsset = assetInfo.isGasAsset;
|
|
88
|
-
}
|
|
89
102
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
isGasAsset = false;
|
|
93
|
-
chain: Chain;
|
|
94
|
-
symbol: string;
|
|
95
|
-
ticker: string;
|
|
96
|
-
type: ReturnType<typeof getAssetType>;
|
|
103
|
+
this.tax = params.tax;
|
|
104
|
+
}
|
|
97
105
|
|
|
98
106
|
toString(short = false) {
|
|
99
107
|
// THOR.RUNE | ETH/ETH
|
|
@@ -115,16 +123,18 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
115
123
|
|
|
116
124
|
static fromStringSync(assetString: string, value: NumberPrimitives = 0) {
|
|
117
125
|
const { isSynthetic } = getAssetInfo(assetString);
|
|
118
|
-
const {
|
|
119
|
-
|
|
120
|
-
|
|
126
|
+
const {
|
|
127
|
+
tax,
|
|
128
|
+
decimal,
|
|
129
|
+
identifier: tokenIdentifier,
|
|
130
|
+
} = getStaticToken(assetString as unknown as TokenNames);
|
|
121
131
|
|
|
122
132
|
const parsedValue = safeValue(value, decimal);
|
|
123
133
|
|
|
124
134
|
const asset = tokenIdentifier
|
|
125
|
-
? new AssetValue({ decimal, identifier: tokenIdentifier, value: parsedValue })
|
|
135
|
+
? new AssetValue({ tax, decimal, identifier: tokenIdentifier, value: parsedValue })
|
|
126
136
|
: isSynthetic
|
|
127
|
-
? new AssetValue({ decimal: 8, identifier: assetString, value: parsedValue })
|
|
137
|
+
? new AssetValue({ tax, decimal: 8, identifier: assetString, value: parsedValue })
|
|
128
138
|
: undefined;
|
|
129
139
|
|
|
130
140
|
return asset;
|
|
@@ -234,6 +244,6 @@ const getAssetInfo = (identifier: string) => {
|
|
|
234
244
|
symbol:
|
|
235
245
|
(isSynthetic ? `${synthChain}/` : '') +
|
|
236
246
|
(address ? `${ticker}-${address?.toLowerCase() ?? ''}` : symbol),
|
|
237
|
-
ticker
|
|
247
|
+
ticker,
|
|
238
248
|
};
|
|
239
249
|
};
|
|
@@ -112,19 +112,19 @@ export class BigIntArithmetics {
|
|
|
112
112
|
return this.#arithmetics('div', ...args);
|
|
113
113
|
}
|
|
114
114
|
gt(value: InitialisationValueType) {
|
|
115
|
-
return this
|
|
115
|
+
return this.#comparison('gt', value);
|
|
116
116
|
}
|
|
117
117
|
gte(value: InitialisationValueType) {
|
|
118
|
-
return this
|
|
118
|
+
return this.#comparison('gte', value);
|
|
119
119
|
}
|
|
120
120
|
lt(value: InitialisationValueType) {
|
|
121
|
-
return this
|
|
121
|
+
return this.#comparison('lt', value);
|
|
122
122
|
}
|
|
123
123
|
lte(value: InitialisationValueType) {
|
|
124
|
-
return this
|
|
124
|
+
return this.#comparison('lte', value);
|
|
125
125
|
}
|
|
126
126
|
eqValue(value: InitialisationValueType) {
|
|
127
|
-
return this
|
|
127
|
+
return this.#comparison('eqValue', value);
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
// @ts-expect-error False positive
|
|
@@ -341,6 +341,25 @@ export class BigIntArithmetics {
|
|
|
341
341
|
});
|
|
342
342
|
}
|
|
343
343
|
|
|
344
|
+
#comparison(method: 'gt' | 'gte' | 'lt' | 'lte' | 'eqValue', ...args: InitialisationValueType[]) {
|
|
345
|
+
const decimal = this.#retrievePrecisionDecimal(this, ...args);
|
|
346
|
+
const value = this.getBigIntValue(args[0], decimal);
|
|
347
|
+
const compareToValue = this.getBigIntValue(this, decimal);
|
|
348
|
+
|
|
349
|
+
switch (method) {
|
|
350
|
+
case 'gt':
|
|
351
|
+
return compareToValue > value;
|
|
352
|
+
case 'gte':
|
|
353
|
+
return compareToValue >= value;
|
|
354
|
+
case 'lt':
|
|
355
|
+
return compareToValue < value;
|
|
356
|
+
case 'lte':
|
|
357
|
+
return compareToValue <= value;
|
|
358
|
+
case 'eqValue':
|
|
359
|
+
return compareToValue === value;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
344
363
|
#setValue(value: InitialisationValueType) {
|
|
345
364
|
const safeValue = toSafeValue(value) || '0';
|
|
346
365
|
this.bigIntValue = this.#toBigInt(safeValue);
|