@swapkit/helpers 1.0.0-rc.9 → 1.0.0-rc.90
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.js +2065 -0
- package/dist/index.js.map +28 -0
- package/package.json +24 -37
- package/src/helpers/__tests__/asset.test.ts +126 -108
- package/src/helpers/__tests__/memo.test.ts +52 -40
- package/src/helpers/__tests__/others.test.ts +42 -37
- package/src/helpers/__tests__/validators.test.ts +24 -0
- package/src/helpers/asset.ts +119 -87
- package/src/helpers/liquidity.ts +50 -43
- package/src/helpers/memo.ts +31 -28
- package/src/helpers/others.ts +29 -12
- package/src/helpers/validators.ts +15 -6
- package/src/helpers/web3wallets.ts +178 -0
- package/src/index.ts +13 -9
- package/src/modules/__tests__/assetValue.test.ts +325 -116
- package/src/modules/__tests__/bigIntArithmetics.test.ts +30 -0
- package/src/modules/__tests__/swapKitNumber.test.ts +306 -183
- package/src/modules/assetValue.ts +216 -152
- package/src/modules/bigIntArithmetics.ts +214 -147
- package/src/modules/requestClient.ts +29 -0
- package/src/modules/swapKitError.ts +32 -5
- package/src/modules/swapKitNumber.ts +8 -1
- package/src/types/abis/erc20.ts +99 -0
- package/src/types/abis/tcEthVault.ts +496 -0
- package/src/types/chains.ts +220 -0
- package/src/types/commonTypes.ts +119 -0
- package/src/types/derivationPath.ts +56 -0
- package/src/types/index.ts +10 -0
- package/src/types/network.ts +43 -0
- package/src/types/sdk.ts +11 -0
- package/src/types/tokens.ts +30 -0
- package/src/types/transactions.ts +45 -0
- package/src/types/wallet.ts +39 -0
- package/LICENSE +0 -201
- package/dist/index.cjs +0 -1
- package/dist/index.d.ts +0 -354
- package/dist/index.es.js +0 -1054
- package/src/helpers/request.ts +0 -16
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import { BaseDecimal } from
|
|
2
|
-
|
|
3
|
-
import type { SwapKitNumber } from './swapKitNumber.ts';
|
|
1
|
+
import { BaseDecimal } from "../types/chains.ts";
|
|
2
|
+
import type { SwapKitNumber } from "./swapKitNumber.ts";
|
|
4
3
|
|
|
5
4
|
type NumberPrimitivesType = {
|
|
6
5
|
bigint: bigint;
|
|
7
6
|
number: number;
|
|
8
7
|
string: string;
|
|
9
8
|
};
|
|
10
|
-
type NumberPrimitives = bigint | number | string;
|
|
9
|
+
export type NumberPrimitives = bigint | number | string;
|
|
11
10
|
type InitialisationValueType = NumberPrimitives | BigIntArithmetics | SwapKitNumber;
|
|
12
11
|
|
|
13
12
|
type SKBigIntParams = InitialisationValueType | { decimal?: number; value: number | string };
|
|
13
|
+
type AllowedNumberTypes = "bigint" | "number" | "string";
|
|
14
14
|
|
|
15
15
|
const DEFAULT_DECIMAL = 8;
|
|
16
16
|
const toMultiplier = (decimal: number) => 10n ** BigInt(decimal);
|
|
17
|
-
const decimalFromMultiplier = (multiplier: bigint) =>
|
|
17
|
+
const decimalFromMultiplier = (multiplier: bigint) =>
|
|
18
|
+
Math.log10(Number.parseFloat(multiplier.toString()));
|
|
18
19
|
|
|
19
20
|
export function formatBigIntToSafeValue({
|
|
20
21
|
value,
|
|
@@ -25,38 +26,39 @@ export function formatBigIntToSafeValue({
|
|
|
25
26
|
bigIntDecimal?: number;
|
|
26
27
|
decimal?: number;
|
|
27
28
|
}) {
|
|
29
|
+
if (decimal === 0) return value.toString();
|
|
28
30
|
const isNegative = value < 0n;
|
|
29
31
|
let valueString = value.toString().substring(isNegative ? 1 : 0);
|
|
30
32
|
|
|
31
33
|
const padLength = decimal - (valueString.length - 1);
|
|
32
34
|
|
|
33
35
|
if (padLength > 0) {
|
|
34
|
-
valueString =
|
|
36
|
+
valueString = "0".repeat(padLength) + valueString;
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
const decimalIndex = valueString.length - decimal;
|
|
38
40
|
let decimalString = valueString.slice(-decimal);
|
|
39
41
|
|
|
40
42
|
// Check if we need to round up
|
|
41
|
-
if (parseInt(decimalString[bigIntDecimal]) >= 5) {
|
|
43
|
+
if (Number.parseInt(decimalString[bigIntDecimal] || "0") >= 5) {
|
|
42
44
|
// Increment the last decimal place and slice off the rest
|
|
43
45
|
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(
|
|
44
|
-
parseInt(decimalString[bigIntDecimal - 1]) + 1
|
|
46
|
+
Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1
|
|
45
47
|
).toString()}`;
|
|
46
48
|
} else {
|
|
47
49
|
// Just slice off the extra digits
|
|
48
50
|
decimalString = decimalString.substring(0, bigIntDecimal);
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
return `${isNegative ?
|
|
53
|
+
return `${isNegative ? "-" : ""}${valueString.slice(0, decimalIndex)}.${decimalString}`.replace(
|
|
52
54
|
/\.?0*$/,
|
|
53
|
-
|
|
55
|
+
"",
|
|
54
56
|
);
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
export class BigIntArithmetics {
|
|
58
60
|
decimalMultiplier: bigint = 10n ** 8n;
|
|
59
|
-
bigIntValue
|
|
61
|
+
bigIntValue = 0n;
|
|
60
62
|
decimal?: number;
|
|
61
63
|
|
|
62
64
|
static fromBigInt(value: bigint, decimal?: number) {
|
|
@@ -71,124 +73,191 @@ export class BigIntArithmetics {
|
|
|
71
73
|
from,
|
|
72
74
|
to,
|
|
73
75
|
}: {
|
|
74
|
-
value:
|
|
76
|
+
value: InstanceType<typeof SwapKitNumber>;
|
|
75
77
|
from: number;
|
|
76
78
|
to: number;
|
|
77
79
|
}) {
|
|
78
80
|
return BigIntArithmetics.fromBigInt(
|
|
79
|
-
(
|
|
81
|
+
(value.getBaseValue("bigint") * toMultiplier(to)) / toMultiplier(from),
|
|
80
82
|
to,
|
|
81
83
|
);
|
|
82
84
|
}
|
|
83
85
|
|
|
84
86
|
constructor(params: SKBigIntParams) {
|
|
85
87
|
const value = getStringValue(params);
|
|
86
|
-
|
|
88
|
+
const isComplex = typeof params === "object";
|
|
89
|
+
this.decimal = isComplex ? params.decimal : undefined;
|
|
87
90
|
|
|
88
91
|
// use the multiplier to keep track of decimal point - defaults to 8 if lower than 8
|
|
89
|
-
this.decimalMultiplier =
|
|
90
|
-
|
|
91
|
-
|
|
92
|
+
this.decimalMultiplier =
|
|
93
|
+
isComplex && "decimalMultiplier" in params
|
|
94
|
+
? params.decimalMultiplier
|
|
95
|
+
: toMultiplier(Math.max(getFloatDecimals(toSafeValue(value)), this.decimal || 0));
|
|
92
96
|
this.#setValue(value);
|
|
93
97
|
}
|
|
94
98
|
|
|
95
|
-
|
|
96
|
-
* @deprecated Use `getBaseValue('string')` instead
|
|
97
|
-
*/
|
|
98
|
-
get baseValue() {
|
|
99
|
-
return this.getBaseValue('string') as string;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* @deprecated Use `getBaseValue('number')` instead
|
|
103
|
-
*/
|
|
104
|
-
get baseValueNumber() {
|
|
105
|
-
return this.getBaseValue('number') as number;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* @deprecated Use `getBaseValue('bigint')` instead
|
|
109
|
-
*/
|
|
110
|
-
get baseValueBigInt() {
|
|
111
|
-
return this.getBaseValue('bigint') as bigint;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
set(value: SKBigIntParams) {
|
|
99
|
+
set(value: SKBigIntParams): this {
|
|
115
100
|
// @ts-expect-error False positive
|
|
116
101
|
return new this.constructor({ decimal: this.decimal, value, identifier: this.toString() });
|
|
117
102
|
}
|
|
118
103
|
add(...args: InitialisationValueType[]) {
|
|
119
|
-
return this.#arithmetics(
|
|
104
|
+
return this.#arithmetics("add", ...args);
|
|
120
105
|
}
|
|
121
106
|
sub(...args: InitialisationValueType[]) {
|
|
122
|
-
return this.#arithmetics(
|
|
107
|
+
return this.#arithmetics("sub", ...args);
|
|
123
108
|
}
|
|
124
109
|
mul(...args: InitialisationValueType[]) {
|
|
125
|
-
return this.#arithmetics(
|
|
110
|
+
return this.#arithmetics("mul", ...args);
|
|
126
111
|
}
|
|
127
112
|
div(...args: InitialisationValueType[]) {
|
|
128
|
-
return this.#arithmetics(
|
|
113
|
+
return this.#arithmetics("div", ...args);
|
|
129
114
|
}
|
|
130
115
|
gt(value: InitialisationValueType) {
|
|
131
|
-
return this
|
|
116
|
+
return this.#comparison("gt", value);
|
|
132
117
|
}
|
|
133
118
|
gte(value: InitialisationValueType) {
|
|
134
|
-
return this
|
|
119
|
+
return this.#comparison("gte", value);
|
|
135
120
|
}
|
|
136
121
|
lt(value: InitialisationValueType) {
|
|
137
|
-
return this
|
|
122
|
+
return this.#comparison("lt", value);
|
|
138
123
|
}
|
|
139
124
|
lte(value: InitialisationValueType) {
|
|
140
|
-
return this
|
|
125
|
+
return this.#comparison("lte", value);
|
|
141
126
|
}
|
|
142
127
|
eqValue(value: InitialisationValueType) {
|
|
143
|
-
return this
|
|
128
|
+
return this.#comparison("eqValue", value);
|
|
144
129
|
}
|
|
145
130
|
|
|
146
|
-
|
|
131
|
+
// @ts-expect-error False positive
|
|
132
|
+
getValue<T extends AllowedNumberTypes>(type: T): NumberPrimitivesType[T] {
|
|
147
133
|
const value = this.formatBigIntToSafeValue(
|
|
148
134
|
this.bigIntValue,
|
|
149
135
|
this.decimal || decimalFromMultiplier(this.decimalMultiplier),
|
|
150
136
|
);
|
|
151
137
|
|
|
152
138
|
switch (type) {
|
|
153
|
-
case
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
return
|
|
159
|
-
|
|
160
|
-
// @ts-expect-error False positive
|
|
161
|
-
return (this.bigIntValue * BigInt(this.decimal || 8n)) / this.decimalMultiplier;
|
|
139
|
+
case "number":
|
|
140
|
+
return Number(value) as NumberPrimitivesType[T];
|
|
141
|
+
case "string":
|
|
142
|
+
return value as NumberPrimitivesType[T];
|
|
143
|
+
case "bigint":
|
|
144
|
+
return ((this.bigIntValue * 10n ** BigInt(this.decimal || 8n)) /
|
|
145
|
+
this.decimalMultiplier) as NumberPrimitivesType[T];
|
|
162
146
|
}
|
|
163
147
|
}
|
|
164
148
|
|
|
165
|
-
|
|
149
|
+
// @ts-expect-error
|
|
150
|
+
getBaseValue<T extends AllowedNumberTypes>(type: T): NumberPrimitivesType[T] {
|
|
166
151
|
const divisor = this.decimalMultiplier / toMultiplier(this.decimal || BaseDecimal.THOR);
|
|
167
152
|
const baseValue = this.bigIntValue / divisor;
|
|
168
153
|
|
|
169
154
|
switch (type) {
|
|
170
|
-
case
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
return baseValue
|
|
176
|
-
default:
|
|
177
|
-
// @ts-expect-error False positive
|
|
178
|
-
return baseValue;
|
|
155
|
+
case "number":
|
|
156
|
+
return Number(baseValue) as NumberPrimitivesType[T];
|
|
157
|
+
case "string":
|
|
158
|
+
return baseValue.toString() as NumberPrimitivesType[T];
|
|
159
|
+
case "bigint":
|
|
160
|
+
return baseValue as NumberPrimitivesType[T];
|
|
179
161
|
}
|
|
180
162
|
}
|
|
181
163
|
|
|
182
164
|
getBigIntValue(value: InitialisationValueType, decimal?: number) {
|
|
183
|
-
if (!decimal && typeof value ===
|
|
165
|
+
if (!decimal && typeof value === "object") return value.bigIntValue;
|
|
184
166
|
|
|
185
167
|
const stringValue = getStringValue(value);
|
|
186
|
-
const safeValue =
|
|
168
|
+
const safeValue = toSafeValue(stringValue);
|
|
187
169
|
|
|
188
|
-
if (safeValue ===
|
|
170
|
+
if (safeValue === "0" || safeValue === "undefined") return 0n;
|
|
189
171
|
return this.#toBigInt(safeValue, decimal);
|
|
190
172
|
}
|
|
191
173
|
|
|
174
|
+
toSignificant(significantDigits = 6) {
|
|
175
|
+
const [int, dec] = this.getValue("string").split(".");
|
|
176
|
+
const integer = int || "";
|
|
177
|
+
const decimal = dec || "";
|
|
178
|
+
const valueLength = Number.parseInt(integer) ? integer.length + decimal.length : decimal.length;
|
|
179
|
+
|
|
180
|
+
if (valueLength <= significantDigits) {
|
|
181
|
+
return this.getValue("string");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (integer.length >= significantDigits) {
|
|
185
|
+
return integer.slice(0, significantDigits).padEnd(integer.length, "0");
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (Number.parseInt(integer)) {
|
|
189
|
+
return `${integer}.${decimal.slice(0, significantDigits - integer.length)}`.padEnd(
|
|
190
|
+
significantDigits - integer.length,
|
|
191
|
+
"0",
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const trimmedDecimal = Number.parseInt(decimal);
|
|
196
|
+
const slicedDecimal = `${trimmedDecimal}`.slice(0, significantDigits);
|
|
197
|
+
|
|
198
|
+
return `0.${slicedDecimal.padStart(
|
|
199
|
+
decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length,
|
|
200
|
+
"0",
|
|
201
|
+
)}`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
toFixed(fixedDigits = 6) {
|
|
205
|
+
const [int, dec] = this.getValue("string").split(".");
|
|
206
|
+
const integer = int || "";
|
|
207
|
+
const decimal = dec || "";
|
|
208
|
+
|
|
209
|
+
if (Number.parseInt(integer)) {
|
|
210
|
+
return `${integer}.${decimal.slice(0, fixedDigits)}`.padEnd(fixedDigits, "0");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const trimmedDecimal = Number.parseInt(decimal);
|
|
214
|
+
const slicedDecimal = `${trimmedDecimal}`.slice(0, fixedDigits);
|
|
215
|
+
|
|
216
|
+
return `0.${slicedDecimal.padStart(
|
|
217
|
+
decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length,
|
|
218
|
+
"0",
|
|
219
|
+
)}`;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
toAbbreviation(digits = 2) {
|
|
223
|
+
const value = this.getValue("number");
|
|
224
|
+
const abbreviations = ["", "K", "M", "B", "T", "Q", "Qi", "S"];
|
|
225
|
+
const tier = Math.floor(Math.log10(Math.abs(value)) / 3);
|
|
226
|
+
const suffix = abbreviations[tier];
|
|
227
|
+
|
|
228
|
+
if (!suffix) return this.getValue("string");
|
|
229
|
+
|
|
230
|
+
const scale = 10 ** (tier * 3);
|
|
231
|
+
const scaled = value / scale;
|
|
232
|
+
|
|
233
|
+
return `${scaled.toFixed(digits)}${suffix}`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
toCurrency(
|
|
237
|
+
currency = "$",
|
|
238
|
+
{
|
|
239
|
+
currencyPosition = "start",
|
|
240
|
+
decimal = 2,
|
|
241
|
+
decimalSeparator = ".",
|
|
242
|
+
thousandSeparator = ",",
|
|
243
|
+
} = {},
|
|
244
|
+
) {
|
|
245
|
+
const value = this.getValue("number");
|
|
246
|
+
const [int = "", dec = ""] = value.toFixed(6).split(".");
|
|
247
|
+
const integer = int.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);
|
|
248
|
+
|
|
249
|
+
const parsedValue =
|
|
250
|
+
int || dec
|
|
251
|
+
? int === "0"
|
|
252
|
+
? `${Number.parseFloat(`0.${dec}`)}`.replace(".", decimalSeparator)
|
|
253
|
+
: `${integer}${Number.parseInt(dec) ? `${decimalSeparator}${dec.slice(0, decimal)}` : ""}`
|
|
254
|
+
: "0.00";
|
|
255
|
+
|
|
256
|
+
return `${currencyPosition === "start" ? currency : ""}${parsedValue}${
|
|
257
|
+
currencyPosition === "end" ? currency : ""
|
|
258
|
+
}`;
|
|
259
|
+
}
|
|
260
|
+
|
|
192
261
|
formatBigIntToSafeValue(value: bigint, decimal?: number) {
|
|
193
262
|
const bigIntDecimal = decimal || this.decimal || DEFAULT_DECIMAL;
|
|
194
263
|
const decimalToUseForConversion = Math.max(
|
|
@@ -200,70 +269,41 @@ export class BigIntArithmetics {
|
|
|
200
269
|
const valueString = value.toString().substring(isNegative ? 1 : 0);
|
|
201
270
|
const padLength = decimalToUseForConversion - (valueString.length - 1);
|
|
202
271
|
|
|
203
|
-
const parsedValueString = padLength > 0 ?
|
|
272
|
+
const parsedValueString = padLength > 0 ? "0".repeat(padLength) + valueString : valueString;
|
|
204
273
|
|
|
205
274
|
const decimalIndex = parsedValueString.length - decimalToUseForConversion;
|
|
206
275
|
let decimalString = parsedValueString.slice(-decimalToUseForConversion);
|
|
207
276
|
|
|
208
277
|
// Check if we need to round up
|
|
209
|
-
if (parseInt(decimalString[bigIntDecimal]) >= 5) {
|
|
278
|
+
if (Number.parseInt(decimalString[bigIntDecimal] || "0") >= 5) {
|
|
210
279
|
// Increment the last decimal place and slice off the rest
|
|
211
280
|
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(
|
|
212
|
-
parseInt(decimalString[bigIntDecimal - 1]) + 1
|
|
281
|
+
Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1
|
|
213
282
|
).toString()}`;
|
|
214
283
|
} else {
|
|
215
284
|
// Just slice off the extra digits
|
|
216
285
|
decimalString = decimalString.substring(0, bigIntDecimal);
|
|
217
286
|
}
|
|
218
287
|
|
|
219
|
-
return `${isNegative ?
|
|
288
|
+
return `${isNegative ? "-" : ""}${parsedValueString.slice(
|
|
220
289
|
0,
|
|
221
290
|
decimalIndex,
|
|
222
|
-
)}.${decimalString}`.replace(/\.?0*$/,
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
toSignificant(significantDigits: number = 6) {
|
|
226
|
-
const [int, dec] = this.getValue('string').split('.');
|
|
227
|
-
const integer = int || '';
|
|
228
|
-
const decimal = dec || '';
|
|
229
|
-
const valueLength = parseInt(integer) ? integer.length + decimal.length : decimal.length;
|
|
230
|
-
|
|
231
|
-
if (valueLength <= significantDigits) {
|
|
232
|
-
return this.getValue('string');
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
if (integer.length >= significantDigits) {
|
|
236
|
-
return integer.slice(0, significantDigits).padEnd(integer.length, '0');
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (parseInt(integer)) {
|
|
240
|
-
return `${integer}.${decimal.slice(0, significantDigits - integer.length)}`.padEnd(
|
|
241
|
-
valueLength - significantDigits,
|
|
242
|
-
'0',
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
const trimmedDecimal = parseInt(decimal);
|
|
247
|
-
const slicedDecimal = `${trimmedDecimal}`.slice(0, significantDigits);
|
|
248
|
-
|
|
249
|
-
return `0.${slicedDecimal.padStart(
|
|
250
|
-
decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length,
|
|
251
|
-
'0',
|
|
252
|
-
)}`;
|
|
291
|
+
)}.${decimalString}`.replace(/\.?0*$/, "");
|
|
253
292
|
}
|
|
254
293
|
|
|
255
|
-
#arithmetics(method:
|
|
294
|
+
#arithmetics(method: "add" | "sub" | "mul" | "div", ...args: InitialisationValueType[]): this {
|
|
256
295
|
const precisionDecimal = this.#retrievePrecisionDecimal(this, ...args);
|
|
257
|
-
const
|
|
296
|
+
const decimal = Math.max(precisionDecimal, decimalFromMultiplier(this.decimalMultiplier));
|
|
297
|
+
const precisionDecimalMultiplier = toMultiplier(decimal);
|
|
258
298
|
|
|
259
299
|
const result = args.reduce(
|
|
260
300
|
(acc: bigint, arg) => {
|
|
261
|
-
const value = this.getBigIntValue(arg,
|
|
301
|
+
const value = this.getBigIntValue(arg, decimal);
|
|
262
302
|
|
|
263
303
|
switch (method) {
|
|
264
|
-
case
|
|
304
|
+
case "add":
|
|
265
305
|
return acc + value;
|
|
266
|
-
case
|
|
306
|
+
case "sub":
|
|
267
307
|
return acc - value;
|
|
268
308
|
/**
|
|
269
309
|
* Multiplication & division would end up with wrong result if we don't adjust the value
|
|
@@ -273,10 +313,10 @@ export class BigIntArithmetics {
|
|
|
273
313
|
* 200000000n * 200000000n = 40000000000000000n / 100000000n (decimals) => 400000000n
|
|
274
314
|
* (200000000n * 100000000n (decimals)) / 200000000n => 100000000n
|
|
275
315
|
*/
|
|
276
|
-
case
|
|
316
|
+
case "mul":
|
|
277
317
|
return (acc * value) / precisionDecimalMultiplier;
|
|
278
|
-
case
|
|
279
|
-
if (value === 0n) throw new RangeError(
|
|
318
|
+
case "div": {
|
|
319
|
+
if (value === 0n) throw new RangeError("Division by zero");
|
|
280
320
|
return (acc * precisionDecimalMultiplier) / value;
|
|
281
321
|
}
|
|
282
322
|
default:
|
|
@@ -288,65 +328,92 @@ export class BigIntArithmetics {
|
|
|
288
328
|
);
|
|
289
329
|
|
|
290
330
|
const value = formatBigIntToSafeValue({
|
|
291
|
-
bigIntDecimal:
|
|
292
|
-
decimal
|
|
331
|
+
bigIntDecimal: decimal,
|
|
332
|
+
decimal,
|
|
293
333
|
value: result,
|
|
294
334
|
});
|
|
295
335
|
|
|
296
336
|
// @ts-expect-error False positive
|
|
297
|
-
return new this.constructor({
|
|
337
|
+
return new this.constructor({
|
|
338
|
+
decimalMultiplier: toMultiplier(decimal),
|
|
339
|
+
decimal: this.decimal,
|
|
340
|
+
value,
|
|
341
|
+
identifier: this.toString(),
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
#comparison(method: "gt" | "gte" | "lt" | "lte" | "eqValue", ...args: InitialisationValueType[]) {
|
|
346
|
+
const decimal = this.#retrievePrecisionDecimal(this, ...args);
|
|
347
|
+
const value = this.getBigIntValue(args[0] || "0", decimal);
|
|
348
|
+
const compareToValue = this.getBigIntValue(this, decimal);
|
|
349
|
+
|
|
350
|
+
switch (method) {
|
|
351
|
+
case "gt":
|
|
352
|
+
return compareToValue > value;
|
|
353
|
+
case "gte":
|
|
354
|
+
return compareToValue >= value;
|
|
355
|
+
case "lt":
|
|
356
|
+
return compareToValue < value;
|
|
357
|
+
case "lte":
|
|
358
|
+
return compareToValue <= value;
|
|
359
|
+
case "eqValue":
|
|
360
|
+
return compareToValue === value;
|
|
361
|
+
}
|
|
298
362
|
}
|
|
299
363
|
|
|
300
364
|
#setValue(value: InitialisationValueType) {
|
|
301
|
-
const safeValue =
|
|
365
|
+
const safeValue = toSafeValue(value) || "0";
|
|
302
366
|
this.bigIntValue = this.#toBigInt(safeValue);
|
|
303
367
|
}
|
|
304
368
|
|
|
305
369
|
#retrievePrecisionDecimal(...args: InitialisationValueType[]) {
|
|
306
370
|
const decimals = args
|
|
307
|
-
.map((arg) =>
|
|
308
|
-
typeof arg ===
|
|
371
|
+
.map((arg) => {
|
|
372
|
+
const isObject = typeof arg === "object";
|
|
373
|
+
const value = isObject
|
|
309
374
|
? arg.decimal || decimalFromMultiplier(arg.decimalMultiplier)
|
|
310
|
-
:
|
|
311
|
-
|
|
375
|
+
: getFloatDecimals(toSafeValue(arg));
|
|
376
|
+
|
|
377
|
+
return value;
|
|
378
|
+
})
|
|
312
379
|
.filter(Boolean) as number[];
|
|
380
|
+
|
|
313
381
|
return Math.max(...decimals, DEFAULT_DECIMAL);
|
|
314
382
|
}
|
|
315
383
|
|
|
316
384
|
#toBigInt(value: string, decimal?: number) {
|
|
317
385
|
const multiplier = decimal ? toMultiplier(decimal) : this.decimalMultiplier;
|
|
318
386
|
const padDecimal = decimalFromMultiplier(multiplier);
|
|
319
|
-
const [integerPart =
|
|
387
|
+
const [integerPart = "", decimalPart = ""] = value.split(".");
|
|
320
388
|
|
|
321
|
-
return BigInt(`${integerPart}${decimalPart.padEnd(padDecimal,
|
|
389
|
+
return BigInt(`${integerPart}${decimalPart.padEnd(padDecimal, "0")}`);
|
|
322
390
|
}
|
|
391
|
+
}
|
|
323
392
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
useGrouping: false,
|
|
329
|
-
maximumFractionDigits: 20,
|
|
330
|
-
})
|
|
331
|
-
: getStringValue(value);
|
|
332
|
-
|
|
333
|
-
const splitValue = `${parsedValue}`.replaceAll(',', '.').split('.');
|
|
334
|
-
|
|
335
|
-
return splitValue.length > 1
|
|
336
|
-
? `${splitValue.slice(0, -1).join('')}.${splitValue.at(-1)}`
|
|
337
|
-
: splitValue[0];
|
|
338
|
-
}
|
|
393
|
+
const numberFormatter = Intl.NumberFormat("fullwide", {
|
|
394
|
+
useGrouping: false,
|
|
395
|
+
maximumFractionDigits: 20,
|
|
396
|
+
});
|
|
339
397
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
}
|
|
398
|
+
function toSafeValue(value: InitialisationValueType) {
|
|
399
|
+
const parsedValue =
|
|
400
|
+
typeof value === "number" ? numberFormatter.format(value) : getStringValue(value);
|
|
401
|
+
const splitValue = `${parsedValue}`.replaceAll(",", ".").split(".");
|
|
402
|
+
|
|
403
|
+
return splitValue.length > 1
|
|
404
|
+
? `${splitValue.slice(0, -1).join("")}.${splitValue.at(-1)}`
|
|
405
|
+
: splitValue[0] || "0";
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function getFloatDecimals(value: string) {
|
|
409
|
+
const decimals = value.split(".")[1]?.length || 0;
|
|
410
|
+
return Math.max(decimals, DEFAULT_DECIMAL);
|
|
344
411
|
}
|
|
345
412
|
|
|
346
|
-
function getStringValue(
|
|
347
|
-
return typeof
|
|
348
|
-
?
|
|
349
|
-
?
|
|
350
|
-
:
|
|
351
|
-
:
|
|
413
|
+
function getStringValue(param: SKBigIntParams) {
|
|
414
|
+
return typeof param === "object"
|
|
415
|
+
? "getValue" in param
|
|
416
|
+
? param.getValue("string")
|
|
417
|
+
: param.value
|
|
418
|
+
: param;
|
|
352
419
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Options } from "ky";
|
|
2
|
+
import ky from "ky";
|
|
3
|
+
|
|
4
|
+
let kyClient: typeof ky;
|
|
5
|
+
|
|
6
|
+
export const defaultRequestHeaders =
|
|
7
|
+
typeof window !== "undefined"
|
|
8
|
+
? ({} as Record<string, string>)
|
|
9
|
+
: { referrer: "https://sk.thorswap.net", referer: "https://sk.thorswap.net" };
|
|
10
|
+
|
|
11
|
+
export function setRequestClientConfig({ apiKey, ...config }: Options & { apiKey?: string }) {
|
|
12
|
+
kyClient = ky.create({
|
|
13
|
+
...config,
|
|
14
|
+
headers: { ...defaultRequestHeaders, ...config.headers, "x-api-key": apiKey },
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getKyClient() {
|
|
19
|
+
if (kyClient) return kyClient;
|
|
20
|
+
kyClient = ky.create({ headers: defaultRequestHeaders });
|
|
21
|
+
return kyClient;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const RequestClient = {
|
|
25
|
+
get: <T>(url: string | URL | Request, options?: Options) =>
|
|
26
|
+
getKyClient().get(url, options).json<T>(),
|
|
27
|
+
post: <T>(url: string | URL | Request, options?: Options) =>
|
|
28
|
+
getKyClient().post(url, options).json<T>(),
|
|
29
|
+
};
|
|
@@ -7,7 +7,10 @@ const errorMessages = {
|
|
|
7
7
|
core_extend_error: 10003,
|
|
8
8
|
core_inbound_data_not_found: 10004,
|
|
9
9
|
core_approve_asset_address_or_from_not_found: 10005,
|
|
10
|
+
core_plugin_not_found: 10006,
|
|
11
|
+
core_plugin_swap_not_found: 10007,
|
|
10
12
|
core_chain_halted: 10099,
|
|
13
|
+
|
|
11
14
|
/**
|
|
12
15
|
* Core - Wallet Connection
|
|
13
16
|
*/
|
|
@@ -19,6 +22,7 @@ const errorMessages = {
|
|
|
19
22
|
core_wallet_trezor_not_installed: 10106,
|
|
20
23
|
core_wallet_keplr_not_installed: 10107,
|
|
21
24
|
core_wallet_okx_not_installed: 10108,
|
|
25
|
+
core_wallet_keepkey_not_installed: 10109,
|
|
22
26
|
/**
|
|
23
27
|
* Core - Swap
|
|
24
28
|
*/
|
|
@@ -45,12 +49,28 @@ const errorMessages = {
|
|
|
45
49
|
core_transaction_deposit_to_pool_error: 10310,
|
|
46
50
|
core_transaction_deposit_insufficient_funds_error: 10311,
|
|
47
51
|
core_transaction_deposit_gas_error: 10312,
|
|
48
|
-
|
|
52
|
+
core_transaction_invalid_sender_address: 10313,
|
|
53
|
+
core_transaction_deposit_server_error: 10314,
|
|
54
|
+
core_transaction_user_rejected: 10315,
|
|
49
55
|
|
|
50
56
|
/**
|
|
51
57
|
* Wallets
|
|
52
58
|
*/
|
|
53
59
|
wallet_ledger_connection_error: 20001,
|
|
60
|
+
wallet_ledger_connection_claimed: 20002,
|
|
61
|
+
wallet_ledger_get_address_error: 20003,
|
|
62
|
+
wallet_ledger_device_not_found: 20004,
|
|
63
|
+
wallet_ledger_device_locked: 20005,
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Chainflip
|
|
67
|
+
*/
|
|
68
|
+
chainflip_channel_error: 30001,
|
|
69
|
+
chainflip_broker_recipient_error: 30002,
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* THORChain
|
|
73
|
+
*/
|
|
54
74
|
|
|
55
75
|
/**
|
|
56
76
|
* Helpers
|
|
@@ -58,13 +78,20 @@ const errorMessages = {
|
|
|
58
78
|
helpers_number_different_decimals: 99101,
|
|
59
79
|
} as const;
|
|
60
80
|
|
|
61
|
-
export type
|
|
81
|
+
export type ErrorKeys = keyof typeof errorMessages;
|
|
62
82
|
|
|
63
83
|
export class SwapKitError extends Error {
|
|
64
|
-
constructor(errorKey:
|
|
65
|
-
|
|
84
|
+
constructor(errorKey: ErrorKeys, sourceError?: NotWorth) {
|
|
85
|
+
if (sourceError) {
|
|
86
|
+
console.error(sourceError, {
|
|
87
|
+
stack: sourceError?.stack,
|
|
88
|
+
message: sourceError?.message,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
66
91
|
|
|
67
|
-
super(errorKey, {
|
|
92
|
+
super(errorKey, {
|
|
93
|
+
cause: { code: errorMessages[errorKey], message: errorKey },
|
|
94
|
+
});
|
|
68
95
|
Object.setPrototypeOf(this, SwapKitError.prototype);
|
|
69
96
|
}
|
|
70
97
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BigIntArithmetics } from
|
|
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
|
}
|