@swapkit/helpers 1.0.0-rc.10 → 1.0.0-rc.12
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 -8
- package/dist/index.es.js +224 -210
- package/package.json +1 -1
- package/src/modules/__tests__/assetValue.test.ts +10 -0
- package/src/modules/__tests__/swapKitNumber.test.ts +31 -0
- package/src/modules/assetValue.ts +25 -13
- package/src/modules/bigIntArithmetics.ts +16 -2
- package/src/modules/swapKitNumber.ts +8 -1
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.12",
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "vite build",
|
|
48
48
|
"clean": "rm -rf dist vite.config.ts.* .turbo node_modules",
|
|
@@ -77,6 +77,16 @@ describe('AssetValue', () => {
|
|
|
77
77
|
});
|
|
78
78
|
});
|
|
79
79
|
|
|
80
|
+
describe('from bigint', () => {
|
|
81
|
+
test('returns asset value with correct decimal', async () => {
|
|
82
|
+
const avaxUSDCAsset = await AssetValue.fromIdentifier(
|
|
83
|
+
`${Chain.Avalanche}.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e`,
|
|
84
|
+
1234567800n,
|
|
85
|
+
);
|
|
86
|
+
expect(avaxUSDCAsset.getValue('string')).toBe('1234.5678');
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
80
90
|
describe('toString', () => {
|
|
81
91
|
test('returns asset value string/identifier', async () => {
|
|
82
92
|
const avaxUSDCAsset = new AssetValue({
|
|
@@ -153,6 +153,37 @@ describe('SwapKitNumber', () => {
|
|
|
153
153
|
});
|
|
154
154
|
});
|
|
155
155
|
|
|
156
|
+
describe('toAbbreviation', () => {
|
|
157
|
+
test('returns abbreviation with up to 3 integer digits', () => {
|
|
158
|
+
const skNumber = new SwapKitNumber(1234.5678);
|
|
159
|
+
expect(skNumber.toAbbreviation()).toBe('1.23K');
|
|
160
|
+
|
|
161
|
+
const skNumber2 = new SwapKitNumber(1234567.5678);
|
|
162
|
+
expect(skNumber2.toAbbreviation()).toBe('1.23M');
|
|
163
|
+
|
|
164
|
+
const skNumber3 = new SwapKitNumber(1234567890.5678);
|
|
165
|
+
expect(skNumber3.toAbbreviation()).toBe('1.23B');
|
|
166
|
+
|
|
167
|
+
const skNumber4 = new SwapKitNumber(1234567890123.5678);
|
|
168
|
+
expect(skNumber4.toAbbreviation()).toBe('1.23T');
|
|
169
|
+
|
|
170
|
+
const skNumber5 = new SwapKitNumber(1234567890123456.5678);
|
|
171
|
+
expect(skNumber5.toAbbreviation()).toBe('1.23Q');
|
|
172
|
+
|
|
173
|
+
const skNumber6 = new SwapKitNumber(1234567890123456789.5678);
|
|
174
|
+
expect(skNumber6.toAbbreviation()).toBe('1.23Qi');
|
|
175
|
+
|
|
176
|
+
const skNumber7 = new SwapKitNumber(1234567890123456789012.5678);
|
|
177
|
+
expect(skNumber7.toAbbreviation()).toBe('1.23S');
|
|
178
|
+
|
|
179
|
+
const skNumber8 = new SwapKitNumber(1234.5678);
|
|
180
|
+
expect(skNumber8.toAbbreviation(0)).toBe('1K');
|
|
181
|
+
|
|
182
|
+
const skNumber9 = new SwapKitNumber(1234.5678);
|
|
183
|
+
expect(skNumber9.toAbbreviation(1)).toBe('1.2K');
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
156
187
|
describe('add', () => {
|
|
157
188
|
test('adds same type numbers correctly', () => {
|
|
158
189
|
const skNumber1 = new SwapKitNumber(10);
|
|
@@ -17,9 +17,15 @@ import type { CommonAssetString } from '../helpers/asset.ts';
|
|
|
17
17
|
import { getAssetType, getCommonAssetInfo, getDecimal, isGasAsset } from '../helpers/asset.ts';
|
|
18
18
|
import { validateIdentifier } from '../helpers/validators.ts';
|
|
19
19
|
|
|
20
|
-
import {
|
|
20
|
+
import type { NumberPrimitives } from './bigIntArithmetics.ts';
|
|
21
|
+
import { BigIntArithmetics, formatBigIntToSafeValue } from './bigIntArithmetics.ts';
|
|
21
22
|
import type { SwapKitValueType } from './swapKitNumber.ts';
|
|
22
23
|
|
|
24
|
+
const safeValue = (value: NumberPrimitives, decimal: number) =>
|
|
25
|
+
typeof value === 'bigint'
|
|
26
|
+
? formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal })
|
|
27
|
+
: value;
|
|
28
|
+
|
|
23
29
|
type AssetValueParams = { decimal: number; value: SwapKitValueType } & (
|
|
24
30
|
| { chain: Chain; symbol: string }
|
|
25
31
|
| { identifier: string }
|
|
@@ -51,11 +57,13 @@ const getStaticToken = (identifier: TokenNames) => {
|
|
|
51
57
|
return tokenInfo || { decimal: BaseDecimal.THOR, identifier: '' };
|
|
52
58
|
};
|
|
53
59
|
|
|
54
|
-
const createAssetValue = async (assetString: string, value:
|
|
60
|
+
const createAssetValue = async (assetString: string, value: NumberPrimitives = 0) => {
|
|
55
61
|
validateIdentifier(assetString);
|
|
56
62
|
|
|
57
63
|
const decimal = await getDecimal(getAssetInfo(assetString));
|
|
58
|
-
|
|
64
|
+
const parsedValue = safeValue(value, decimal);
|
|
65
|
+
|
|
66
|
+
return new AssetValue({ decimal, value: parsedValue, identifier: assetString });
|
|
59
67
|
};
|
|
60
68
|
|
|
61
69
|
export class AssetValue extends BigIntArithmetics {
|
|
@@ -105,47 +113,51 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
105
113
|
return this.chain === chain && this.symbol === symbol;
|
|
106
114
|
}
|
|
107
115
|
|
|
108
|
-
static async fromString(assetString: string, value:
|
|
116
|
+
static async fromString(assetString: string, value: NumberPrimitives = 0) {
|
|
109
117
|
return createAssetValue(assetString, value);
|
|
110
118
|
}
|
|
111
119
|
|
|
112
|
-
static fromStringSync(assetString: string, value:
|
|
120
|
+
static fromStringSync(assetString: string, value: NumberPrimitives = 0) {
|
|
113
121
|
const { decimal, identifier: tokenIdentifier } = getStaticToken(
|
|
114
122
|
assetString as unknown as TokenNames,
|
|
115
123
|
);
|
|
116
124
|
|
|
125
|
+
const parsedValue = safeValue(value, decimal);
|
|
126
|
+
|
|
117
127
|
return tokenIdentifier
|
|
118
|
-
? new AssetValue({ decimal, identifier: tokenIdentifier, value })
|
|
128
|
+
? new AssetValue({ decimal, identifier: tokenIdentifier, value: parsedValue })
|
|
119
129
|
: undefined;
|
|
120
130
|
}
|
|
121
131
|
|
|
122
132
|
static async fromIdentifier(
|
|
123
133
|
assetString: `${Chain}.${string}` | `${Chain}/${string}` | `${Chain}.${string}-${string}`,
|
|
124
|
-
value:
|
|
134
|
+
value: NumberPrimitives = 0,
|
|
125
135
|
) {
|
|
126
136
|
return createAssetValue(assetString, value);
|
|
127
137
|
}
|
|
128
138
|
|
|
129
|
-
static fromIdentifierSync(identifier: TokenNames, value:
|
|
139
|
+
static fromIdentifierSync(identifier: TokenNames, value: NumberPrimitives = 0) {
|
|
130
140
|
const { decimal, identifier: tokenIdentifier } = getStaticToken(identifier);
|
|
141
|
+
const parsedValue = safeValue(value, decimal);
|
|
131
142
|
|
|
132
|
-
return new AssetValue({ decimal, identifier: tokenIdentifier, value });
|
|
143
|
+
return new AssetValue({ decimal, identifier: tokenIdentifier, value: parsedValue });
|
|
133
144
|
}
|
|
134
145
|
|
|
135
|
-
static fromChainOrSignature(assetString: CommonAssetString, value:
|
|
146
|
+
static fromChainOrSignature(assetString: CommonAssetString, value: NumberPrimitives = 0) {
|
|
136
147
|
const { decimal, identifier } = getCommonAssetInfo(assetString);
|
|
148
|
+
const parsedValue = safeValue(value, decimal);
|
|
137
149
|
|
|
138
|
-
return new AssetValue({ value, decimal, identifier });
|
|
150
|
+
return new AssetValue({ value: parsedValue, decimal, identifier });
|
|
139
151
|
}
|
|
140
152
|
|
|
141
|
-
static async fromTCQuote(identifier: TCTokenNames, value:
|
|
153
|
+
static async fromTCQuote(identifier: TCTokenNames, value: NumberPrimitives = 0) {
|
|
142
154
|
const decimal = await getDecimal(getAssetInfo(identifier));
|
|
143
155
|
const shiftedValue = this.shiftDecimals({ value, from: BaseDecimal.THOR, to: decimal });
|
|
144
156
|
|
|
145
157
|
return new AssetValue({ value: shiftedValue, identifier, decimal });
|
|
146
158
|
}
|
|
147
159
|
|
|
148
|
-
static fromTCQuoteStatic(identifier: TCTokenNames, value:
|
|
160
|
+
static fromTCQuoteStatic(identifier: TCTokenNames, value: NumberPrimitives = 0) {
|
|
149
161
|
const tokenInfo = getStaticToken(identifier);
|
|
150
162
|
const shiftedValue = this.shiftDecimals({
|
|
151
163
|
value,
|
|
@@ -7,7 +7,7 @@ type NumberPrimitivesType = {
|
|
|
7
7
|
number: number;
|
|
8
8
|
string: string;
|
|
9
9
|
};
|
|
10
|
-
type NumberPrimitives = bigint | number | string;
|
|
10
|
+
export type NumberPrimitives = bigint | number | string;
|
|
11
11
|
type InitialisationValueType = NumberPrimitives | BigIntArithmetics | SwapKitNumber;
|
|
12
12
|
|
|
13
13
|
type SKBigIntParams = InitialisationValueType | { decimal?: number; value: number | string };
|
|
@@ -75,7 +75,7 @@ export class BigIntArithmetics {
|
|
|
75
75
|
from: number;
|
|
76
76
|
to: number;
|
|
77
77
|
}) {
|
|
78
|
-
return
|
|
78
|
+
return this.fromBigInt(
|
|
79
79
|
(new BigIntArithmetics(value).bigIntValue * toMultiplier(to)) / toMultiplier(from),
|
|
80
80
|
to,
|
|
81
81
|
);
|
|
@@ -270,6 +270,20 @@ export class BigIntArithmetics {
|
|
|
270
270
|
)}`;
|
|
271
271
|
}
|
|
272
272
|
|
|
273
|
+
toAbbreviation(digits = 2) {
|
|
274
|
+
const value = this.getValue('number');
|
|
275
|
+
const abbreviations = ['', 'K', 'M', 'B', 'T', 'Q', 'Qi', 'S'];
|
|
276
|
+
const tier = Math.floor(Math.log10(Math.abs(value)) / 3);
|
|
277
|
+
const suffix = abbreviations[tier];
|
|
278
|
+
|
|
279
|
+
if (!suffix) return value;
|
|
280
|
+
|
|
281
|
+
const scale = 10 ** (tier * 3);
|
|
282
|
+
const scaled = value / scale;
|
|
283
|
+
|
|
284
|
+
return `${scaled.toFixed(digits)}${suffix}`;
|
|
285
|
+
}
|
|
286
|
+
|
|
273
287
|
#arithmetics(method: 'add' | 'sub' | 'mul' | 'div', ...args: InitialisationValueType[]): this {
|
|
274
288
|
const precisionDecimal = this.#retrievePrecisionDecimal(this, ...args);
|
|
275
289
|
const precisionDecimalMultiplier = toMultiplier(precisionDecimal);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BigIntArithmetics } from './bigIntArithmetics.ts';
|
|
1
|
+
import { BigIntArithmetics, formatBigIntToSafeValue } from './bigIntArithmetics.ts';
|
|
2
2
|
|
|
3
3
|
export type SwapKitValueType = BigIntArithmetics | string | number;
|
|
4
4
|
|
|
@@ -6,4 +6,11 @@ export class SwapKitNumber extends BigIntArithmetics {
|
|
|
6
6
|
eq(value: SwapKitValueType) {
|
|
7
7
|
return this.eqValue(value);
|
|
8
8
|
}
|
|
9
|
+
|
|
10
|
+
static fromBigInt(value: bigint, decimal?: number) {
|
|
11
|
+
return new SwapKitNumber({
|
|
12
|
+
decimal,
|
|
13
|
+
value: formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal }),
|
|
14
|
+
});
|
|
15
|
+
}
|
|
9
16
|
}
|