@swapkit/helpers 1.0.0-rc.71 → 1.0.0-rc.73
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.cjs.map +1 -1
- package/dist/index.d.ts +32 -19
- package/dist/index.es.js +376 -361
- package/dist/index.es.js.map +1 -1
- package/package.json +12 -16
- package/src/helpers/__tests__/asset.test.ts +64 -62
- package/src/helpers/__tests__/memo.test.ts +42 -40
- package/src/helpers/__tests__/others.test.ts +31 -31
- package/src/helpers/asset.ts +53 -52
- package/src/helpers/liquidity.ts +9 -9
- package/src/helpers/memo.ts +17 -18
- package/src/helpers/others.ts +3 -3
- package/src/helpers/validators.ts +5 -5
- package/src/index.ts +9 -9
- package/src/modules/__tests__/assetValue.test.ts +187 -173
- package/src/modules/__tests__/bigIntArithmetics.test.ts +8 -8
- package/src/modules/__tests__/swapKitNumber.test.ts +232 -232
- package/src/modules/assetValue.ts +41 -41
- package/src/modules/bigIntArithmetics.ts +86 -86
- package/src/modules/swapKitError.ts +18 -2
- package/src/modules/swapKitNumber.ts +1 -1
- package/src/types.ts +13 -13
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { BaseDecimal, Chain, ChainToChainId } from
|
|
1
|
+
import { BaseDecimal, Chain, ChainToChainId } from "@swapkit/types";
|
|
2
2
|
|
|
3
|
-
import type { CommonAssetString } from
|
|
4
|
-
import { getAssetType, getCommonAssetInfo, getDecimal, isGasAsset } from
|
|
5
|
-
import { validateIdentifier } from
|
|
6
|
-
import type { TokenNames, TokenTax } from
|
|
3
|
+
import type { CommonAssetString } from "../helpers/asset.ts";
|
|
4
|
+
import { getAssetType, getCommonAssetInfo, getDecimal, isGasAsset } from "../helpers/asset.ts";
|
|
5
|
+
import { validateIdentifier } from "../helpers/validators.ts";
|
|
6
|
+
import type { TokenNames, TokenTax } from "../types.ts";
|
|
7
7
|
|
|
8
|
-
import type { NumberPrimitives } from
|
|
9
|
-
import { BigIntArithmetics, formatBigIntToSafeValue } from
|
|
10
|
-
import { SwapKitNumber, type SwapKitValueType } from
|
|
8
|
+
import type { NumberPrimitives } from "./bigIntArithmetics.ts";
|
|
9
|
+
import { BigIntArithmetics, formatBigIntToSafeValue } from "./bigIntArithmetics.ts";
|
|
10
|
+
import { SwapKitNumber, type SwapKitValueType } from "./swapKitNumber.ts";
|
|
11
11
|
|
|
12
12
|
const staticTokensMap = new Map<
|
|
13
13
|
TokenNames,
|
|
@@ -35,7 +35,7 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
35
35
|
| { chain: Chain; symbol: string; identifier?: never }
|
|
36
36
|
| { identifier: string; chain?: never; symbol?: never }
|
|
37
37
|
)) {
|
|
38
|
-
super(typeof value ===
|
|
38
|
+
super(typeof value === "object" ? value : { decimal, value });
|
|
39
39
|
|
|
40
40
|
const assetInfo = getAssetInfo(identifier || `${chain}.${symbol}`);
|
|
41
41
|
|
|
@@ -54,7 +54,7 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
toUrl() {
|
|
57
|
-
return this.isSynthetic ? `${this.chain}.${this.symbol.replace(
|
|
57
|
+
return this.isSynthetic ? `${this.chain}.${this.symbol.replace("/", ".")}` : this.toString();
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
eq({ chain, symbol }: { chain: Chain; symbol: string }) {
|
|
@@ -69,8 +69,8 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
69
69
|
// THOR.ETH.ETH
|
|
70
70
|
// ETH.THOR-0x1234567890
|
|
71
71
|
static fromUrl(urlAsset: string, value: NumberPrimitives = 0) {
|
|
72
|
-
const [chain, ticker, symbol] = urlAsset.split(
|
|
73
|
-
if (!chain
|
|
72
|
+
const [chain, ticker, symbol] = urlAsset.split(".");
|
|
73
|
+
if (!(chain && ticker)) throw new Error("Invalid asset url");
|
|
74
74
|
|
|
75
75
|
const assetString =
|
|
76
76
|
chain === Chain.THORChain && symbol ? `${chain}.${ticker}/${symbol}` : urlAsset;
|
|
@@ -126,7 +126,7 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
126
126
|
value: SwapKitNumber.fromBigInt(BigInt(value)),
|
|
127
127
|
from: 0,
|
|
128
128
|
to: baseDecimal,
|
|
129
|
-
}).getBaseValue(
|
|
129
|
+
}).getBaseValue("string");
|
|
130
130
|
const assetValue = await AssetValue.fromString(assetString, value);
|
|
131
131
|
|
|
132
132
|
return assetValue.set(shiftedAmount);
|
|
@@ -181,20 +181,20 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
181
181
|
|
|
182
182
|
static loadStaticAssets() {
|
|
183
183
|
return new Promise<{ ok: true } | { ok: false; message: string; error: any }>(
|
|
184
|
-
|
|
184
|
+
(resolve, reject) => {
|
|
185
185
|
try {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
}
|
|
186
|
+
import("@swapkit/tokens").then((tokenPackages) => {
|
|
187
|
+
for (const tokenList of Object.values(tokenPackages)) {
|
|
188
|
+
for (const { identifier, chain, ...rest } of tokenList.tokens) {
|
|
189
|
+
staticTokensMap.set(identifier.toUpperCase() as TokenNames, {
|
|
190
|
+
identifier,
|
|
191
|
+
decimal: "decimals" in rest ? rest.decimals : BaseDecimal[chain as Chain],
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
resolve({ ok: true });
|
|
195
197
|
});
|
|
196
|
-
|
|
197
|
-
resolve({ ok: true });
|
|
198
198
|
} catch (error) {
|
|
199
199
|
console.error(error);
|
|
200
200
|
reject({
|
|
@@ -251,11 +251,11 @@ async function createAssetValue(identifier: string, value: NumberPrimitives = 0)
|
|
|
251
251
|
|
|
252
252
|
function createSyntheticAssetValue(identifier: string, value: NumberPrimitives = 0) {
|
|
253
253
|
const [synthChain, symbol] =
|
|
254
|
-
identifier.split(
|
|
255
|
-
? identifier.split(
|
|
256
|
-
: identifier.split(
|
|
254
|
+
identifier.split(".")[0].toUpperCase() === Chain.THORChain
|
|
255
|
+
? identifier.split(".").slice(1).join().split("/")
|
|
256
|
+
: identifier.split("/");
|
|
257
257
|
|
|
258
|
-
if (!synthChain
|
|
258
|
+
if (!(synthChain && symbol)) throw new Error("Invalid asset identifier");
|
|
259
259
|
|
|
260
260
|
return new AssetValue({
|
|
261
261
|
decimal: 8,
|
|
@@ -265,31 +265,31 @@ function createSyntheticAssetValue(identifier: string, value: NumberPrimitives =
|
|
|
265
265
|
}
|
|
266
266
|
|
|
267
267
|
function safeValue(value: NumberPrimitives, decimal: number) {
|
|
268
|
-
return typeof value ===
|
|
268
|
+
return typeof value === "bigint"
|
|
269
269
|
? formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal })
|
|
270
270
|
: value;
|
|
271
271
|
}
|
|
272
272
|
|
|
273
273
|
// TODO refactor & split into smaller functions
|
|
274
274
|
function getAssetInfo(identifier: string) {
|
|
275
|
-
const isSynthetic = identifier.slice(0, 14).includes(
|
|
275
|
+
const isSynthetic = identifier.slice(0, 14).includes("/");
|
|
276
276
|
|
|
277
277
|
const [synthChain, synthSymbol] =
|
|
278
|
-
identifier.split(
|
|
279
|
-
? identifier.split(
|
|
280
|
-
: identifier.split(
|
|
278
|
+
identifier.split(".")[0].toUpperCase() === Chain.THORChain
|
|
279
|
+
? identifier.split(".").slice(1).join().split("/")
|
|
280
|
+
: identifier.split("/");
|
|
281
281
|
|
|
282
|
-
if (isSynthetic && (
|
|
282
|
+
if (isSynthetic && !(synthChain && synthSymbol)) throw new Error("Invalid asset identifier");
|
|
283
283
|
|
|
284
284
|
const adjustedIdentifier =
|
|
285
|
-
identifier.includes(
|
|
285
|
+
identifier.includes(".") && !isSynthetic ? identifier : `${Chain.THORChain}.${synthSymbol}`;
|
|
286
286
|
|
|
287
|
-
const [chain, ...rest] = adjustedIdentifier.split(
|
|
288
|
-
const [ticker, address] = (isSynthetic ? synthSymbol : rest.join(
|
|
287
|
+
const [chain, ...rest] = adjustedIdentifier.split(".") as [Chain, string];
|
|
288
|
+
const [ticker, address] = (isSynthetic ? synthSymbol : rest.join(".")).split("-") as [
|
|
289
289
|
string,
|
|
290
290
|
string?,
|
|
291
291
|
];
|
|
292
|
-
const symbol = isSynthetic ? synthSymbol : rest.join(
|
|
292
|
+
const symbol = isSynthetic ? synthSymbol : rest.join(".");
|
|
293
293
|
|
|
294
294
|
return {
|
|
295
295
|
address: address?.toLowerCase(),
|
|
@@ -297,8 +297,8 @@ function getAssetInfo(identifier: string) {
|
|
|
297
297
|
isGasAsset: isGasAsset({ chain, symbol }),
|
|
298
298
|
isSynthetic,
|
|
299
299
|
symbol:
|
|
300
|
-
(isSynthetic ? `${synthChain}/` :
|
|
301
|
-
(address ? `${ticker}-${address?.toLowerCase() ??
|
|
300
|
+
(isSynthetic ? `${synthChain}/` : "") +
|
|
301
|
+
(address ? `${ticker}-${address?.toLowerCase() ?? ""}` : symbol),
|
|
302
302
|
ticker,
|
|
303
303
|
};
|
|
304
304
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BaseDecimal } from
|
|
1
|
+
import { BaseDecimal } from "@swapkit/types";
|
|
2
2
|
|
|
3
|
-
import type { SwapKitNumber } from
|
|
3
|
+
import type { SwapKitNumber } from "./swapKitNumber.ts";
|
|
4
4
|
|
|
5
5
|
type NumberPrimitivesType = {
|
|
6
6
|
bigint: bigint;
|
|
@@ -11,7 +11,7 @@ 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 };
|
|
14
|
-
type AllowedNumberTypes =
|
|
14
|
+
type AllowedNumberTypes = "bigint" | "number" | "string";
|
|
15
15
|
|
|
16
16
|
const DEFAULT_DECIMAL = 8;
|
|
17
17
|
const toMultiplier = (decimal: number) => 10n ** BigInt(decimal);
|
|
@@ -33,7 +33,7 @@ export function formatBigIntToSafeValue({
|
|
|
33
33
|
const padLength = decimal - (valueString.length - 1);
|
|
34
34
|
|
|
35
35
|
if (padLength > 0) {
|
|
36
|
-
valueString =
|
|
36
|
+
valueString = "0".repeat(padLength) + valueString;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
const decimalIndex = valueString.length - decimal;
|
|
@@ -50,15 +50,15 @@ export function formatBigIntToSafeValue({
|
|
|
50
50
|
decimalString = decimalString.substring(0, bigIntDecimal);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
return `${isNegative ?
|
|
53
|
+
return `${isNegative ? "-" : ""}${valueString.slice(0, decimalIndex)}.${decimalString}`.replace(
|
|
54
54
|
/\.?0*$/,
|
|
55
|
-
|
|
55
|
+
"",
|
|
56
56
|
);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
export class BigIntArithmetics {
|
|
60
60
|
decimalMultiplier: bigint = 10n ** 8n;
|
|
61
|
-
bigIntValue
|
|
61
|
+
bigIntValue = 0n;
|
|
62
62
|
decimal?: number;
|
|
63
63
|
|
|
64
64
|
static fromBigInt(value: bigint, decimal?: number) {
|
|
@@ -77,20 +77,20 @@ export class BigIntArithmetics {
|
|
|
77
77
|
from: number;
|
|
78
78
|
to: number;
|
|
79
79
|
}) {
|
|
80
|
-
return
|
|
81
|
-
(value.getBaseValue(
|
|
80
|
+
return BigIntArithmetics.fromBigInt(
|
|
81
|
+
(value.getBaseValue("bigint") * toMultiplier(to)) / toMultiplier(from),
|
|
82
82
|
to,
|
|
83
83
|
);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
constructor(params: SKBigIntParams) {
|
|
87
87
|
const value = getStringValue(params);
|
|
88
|
-
const isComplex = typeof params ===
|
|
88
|
+
const isComplex = typeof params === "object";
|
|
89
89
|
this.decimal = isComplex ? params.decimal : undefined;
|
|
90
90
|
|
|
91
91
|
// use the multiplier to keep track of decimal point - defaults to 8 if lower than 8
|
|
92
92
|
this.decimalMultiplier =
|
|
93
|
-
isComplex &&
|
|
93
|
+
isComplex && "decimalMultiplier" in params
|
|
94
94
|
? params.decimalMultiplier
|
|
95
95
|
: toMultiplier(Math.max(getFloatDecimals(toSafeValue(value)), this.decimal || 0));
|
|
96
96
|
this.#setValue(value);
|
|
@@ -101,31 +101,31 @@ export class BigIntArithmetics {
|
|
|
101
101
|
return new this.constructor({ decimal: this.decimal, value, identifier: this.toString() });
|
|
102
102
|
}
|
|
103
103
|
add(...args: InitialisationValueType[]) {
|
|
104
|
-
return this.#arithmetics(
|
|
104
|
+
return this.#arithmetics("add", ...args);
|
|
105
105
|
}
|
|
106
106
|
sub(...args: InitialisationValueType[]) {
|
|
107
|
-
return this.#arithmetics(
|
|
107
|
+
return this.#arithmetics("sub", ...args);
|
|
108
108
|
}
|
|
109
109
|
mul(...args: InitialisationValueType[]) {
|
|
110
|
-
return this.#arithmetics(
|
|
110
|
+
return this.#arithmetics("mul", ...args);
|
|
111
111
|
}
|
|
112
112
|
div(...args: InitialisationValueType[]) {
|
|
113
|
-
return this.#arithmetics(
|
|
113
|
+
return this.#arithmetics("div", ...args);
|
|
114
114
|
}
|
|
115
115
|
gt(value: InitialisationValueType) {
|
|
116
|
-
return this.#comparison(
|
|
116
|
+
return this.#comparison("gt", value);
|
|
117
117
|
}
|
|
118
118
|
gte(value: InitialisationValueType) {
|
|
119
|
-
return this.#comparison(
|
|
119
|
+
return this.#comparison("gte", value);
|
|
120
120
|
}
|
|
121
121
|
lt(value: InitialisationValueType) {
|
|
122
|
-
return this.#comparison(
|
|
122
|
+
return this.#comparison("lt", value);
|
|
123
123
|
}
|
|
124
124
|
lte(value: InitialisationValueType) {
|
|
125
|
-
return this.#comparison(
|
|
125
|
+
return this.#comparison("lte", value);
|
|
126
126
|
}
|
|
127
127
|
eqValue(value: InitialisationValueType) {
|
|
128
|
-
return this.#comparison(
|
|
128
|
+
return this.#comparison("eqValue", value);
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
// @ts-expect-error False positive
|
|
@@ -136,11 +136,11 @@ export class BigIntArithmetics {
|
|
|
136
136
|
);
|
|
137
137
|
|
|
138
138
|
switch (type) {
|
|
139
|
-
case
|
|
139
|
+
case "number":
|
|
140
140
|
return Number(value) as NumberPrimitivesType[T];
|
|
141
|
-
case
|
|
141
|
+
case "string":
|
|
142
142
|
return value as NumberPrimitivesType[T];
|
|
143
|
-
case
|
|
143
|
+
case "bigint":
|
|
144
144
|
return ((this.bigIntValue * 10n ** BigInt(this.decimal || 8n)) /
|
|
145
145
|
this.decimalMultiplier) as NumberPrimitivesType[T];
|
|
146
146
|
}
|
|
@@ -152,43 +152,43 @@ export class BigIntArithmetics {
|
|
|
152
152
|
const baseValue = this.bigIntValue / divisor;
|
|
153
153
|
|
|
154
154
|
switch (type) {
|
|
155
|
-
case
|
|
155
|
+
case "number":
|
|
156
156
|
return Number(baseValue) as NumberPrimitivesType[T];
|
|
157
|
-
case
|
|
157
|
+
case "string":
|
|
158
158
|
return baseValue.toString() as NumberPrimitivesType[T];
|
|
159
|
-
case
|
|
159
|
+
case "bigint":
|
|
160
160
|
return baseValue as NumberPrimitivesType[T];
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
getBigIntValue(value: InitialisationValueType, decimal?: number) {
|
|
165
|
-
if (!decimal && typeof value ===
|
|
165
|
+
if (!decimal && typeof value === "object") return value.bigIntValue;
|
|
166
166
|
|
|
167
167
|
const stringValue = getStringValue(value);
|
|
168
168
|
const safeValue = toSafeValue(stringValue);
|
|
169
169
|
|
|
170
|
-
if (safeValue ===
|
|
170
|
+
if (safeValue === "0" || safeValue === "undefined") return 0n;
|
|
171
171
|
return this.#toBigInt(safeValue, decimal);
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
toSignificant(significantDigits
|
|
175
|
-
const [int, dec] = this.getValue(
|
|
176
|
-
const integer = int ||
|
|
177
|
-
const decimal = dec ||
|
|
174
|
+
toSignificant(significantDigits = 6) {
|
|
175
|
+
const [int, dec] = this.getValue("string").split(".");
|
|
176
|
+
const integer = int || "";
|
|
177
|
+
const decimal = dec || "";
|
|
178
178
|
const valueLength = parseInt(integer) ? integer.length + decimal.length : decimal.length;
|
|
179
179
|
|
|
180
180
|
if (valueLength <= significantDigits) {
|
|
181
|
-
return this.getValue(
|
|
181
|
+
return this.getValue("string");
|
|
182
182
|
}
|
|
183
183
|
|
|
184
184
|
if (integer.length >= significantDigits) {
|
|
185
|
-
return integer.slice(0, significantDigits).padEnd(integer.length,
|
|
185
|
+
return integer.slice(0, significantDigits).padEnd(integer.length, "0");
|
|
186
186
|
}
|
|
187
187
|
|
|
188
188
|
if (parseInt(integer)) {
|
|
189
189
|
return `${integer}.${decimal.slice(0, significantDigits - integer.length)}`.padEnd(
|
|
190
190
|
significantDigits - integer.length,
|
|
191
|
-
|
|
191
|
+
"0",
|
|
192
192
|
);
|
|
193
193
|
}
|
|
194
194
|
|
|
@@ -197,17 +197,17 @@ export class BigIntArithmetics {
|
|
|
197
197
|
|
|
198
198
|
return `0.${slicedDecimal.padStart(
|
|
199
199
|
decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length,
|
|
200
|
-
|
|
200
|
+
"0",
|
|
201
201
|
)}`;
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
-
toFixed(fixedDigits
|
|
205
|
-
const [int, dec] = this.getValue(
|
|
206
|
-
const integer = int ||
|
|
207
|
-
const decimal = dec ||
|
|
204
|
+
toFixed(fixedDigits = 6) {
|
|
205
|
+
const [int, dec] = this.getValue("string").split(".");
|
|
206
|
+
const integer = int || "";
|
|
207
|
+
const decimal = dec || "";
|
|
208
208
|
|
|
209
209
|
if (parseInt(integer)) {
|
|
210
|
-
return `${integer}.${decimal.slice(0, fixedDigits)}`.padEnd(fixedDigits,
|
|
210
|
+
return `${integer}.${decimal.slice(0, fixedDigits)}`.padEnd(fixedDigits, "0");
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
const trimmedDecimal = parseInt(decimal);
|
|
@@ -215,17 +215,17 @@ export class BigIntArithmetics {
|
|
|
215
215
|
|
|
216
216
|
return `0.${slicedDecimal.padStart(
|
|
217
217
|
decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length,
|
|
218
|
-
|
|
218
|
+
"0",
|
|
219
219
|
)}`;
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
toAbbreviation(digits = 2) {
|
|
223
|
-
const value = this.getValue(
|
|
224
|
-
const abbreviations = [
|
|
223
|
+
const value = this.getValue("number");
|
|
224
|
+
const abbreviations = ["", "K", "M", "B", "T", "Q", "Qi", "S"];
|
|
225
225
|
const tier = Math.floor(Math.log10(Math.abs(value)) / 3);
|
|
226
226
|
const suffix = abbreviations[tier];
|
|
227
227
|
|
|
228
|
-
if (!suffix) return this.getValue(
|
|
228
|
+
if (!suffix) return this.getValue("string");
|
|
229
229
|
|
|
230
230
|
const scale = 10 ** (tier * 3);
|
|
231
231
|
const scaled = value / scale;
|
|
@@ -234,27 +234,27 @@ export class BigIntArithmetics {
|
|
|
234
234
|
}
|
|
235
235
|
|
|
236
236
|
toCurrency(
|
|
237
|
-
currency =
|
|
237
|
+
currency = "$",
|
|
238
238
|
{
|
|
239
|
-
currencyPosition =
|
|
239
|
+
currencyPosition = "start",
|
|
240
240
|
decimal = 2,
|
|
241
|
-
decimalSeparator =
|
|
242
|
-
thousandSeparator =
|
|
241
|
+
decimalSeparator = ".",
|
|
242
|
+
thousandSeparator = ",",
|
|
243
243
|
} = {},
|
|
244
244
|
) {
|
|
245
|
-
const value = this.getValue(
|
|
246
|
-
const [int, dec =
|
|
245
|
+
const value = this.getValue("number");
|
|
246
|
+
const [int, dec = ""] = value.toFixed(6).split(".");
|
|
247
247
|
const integer = int.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);
|
|
248
248
|
|
|
249
249
|
const parsedValue =
|
|
250
|
-
|
|
251
|
-
?
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
return `${currencyPosition ===
|
|
257
|
-
currencyPosition ===
|
|
250
|
+
int || dec
|
|
251
|
+
? int === "0"
|
|
252
|
+
? `${parseFloat(`0.${dec}`)}`.replace(".", decimalSeparator)
|
|
253
|
+
: `${integer}${parseInt(dec) ? `${decimalSeparator}${dec.slice(0, decimal)}` : ""}`
|
|
254
|
+
: "0.00";
|
|
255
|
+
|
|
256
|
+
return `${currencyPosition === "start" ? currency : ""}${parsedValue}${
|
|
257
|
+
currencyPosition === "end" ? currency : ""
|
|
258
258
|
}`;
|
|
259
259
|
}
|
|
260
260
|
|
|
@@ -269,7 +269,7 @@ export class BigIntArithmetics {
|
|
|
269
269
|
const valueString = value.toString().substring(isNegative ? 1 : 0);
|
|
270
270
|
const padLength = decimalToUseForConversion - (valueString.length - 1);
|
|
271
271
|
|
|
272
|
-
const parsedValueString = padLength > 0 ?
|
|
272
|
+
const parsedValueString = padLength > 0 ? "0".repeat(padLength) + valueString : valueString;
|
|
273
273
|
|
|
274
274
|
const decimalIndex = parsedValueString.length - decimalToUseForConversion;
|
|
275
275
|
let decimalString = parsedValueString.slice(-decimalToUseForConversion);
|
|
@@ -285,13 +285,13 @@ export class BigIntArithmetics {
|
|
|
285
285
|
decimalString = decimalString.substring(0, bigIntDecimal);
|
|
286
286
|
}
|
|
287
287
|
|
|
288
|
-
return `${isNegative ?
|
|
288
|
+
return `${isNegative ? "-" : ""}${parsedValueString.slice(
|
|
289
289
|
0,
|
|
290
290
|
decimalIndex,
|
|
291
|
-
)}.${decimalString}`.replace(/\.?0*$/,
|
|
291
|
+
)}.${decimalString}`.replace(/\.?0*$/, "");
|
|
292
292
|
}
|
|
293
293
|
|
|
294
|
-
#arithmetics(method:
|
|
294
|
+
#arithmetics(method: "add" | "sub" | "mul" | "div", ...args: InitialisationValueType[]): this {
|
|
295
295
|
const precisionDecimal = this.#retrievePrecisionDecimal(this, ...args);
|
|
296
296
|
const decimal = Math.max(precisionDecimal, decimalFromMultiplier(this.decimalMultiplier));
|
|
297
297
|
const precisionDecimalMultiplier = toMultiplier(decimal);
|
|
@@ -301,9 +301,9 @@ export class BigIntArithmetics {
|
|
|
301
301
|
const value = this.getBigIntValue(arg, decimal);
|
|
302
302
|
|
|
303
303
|
switch (method) {
|
|
304
|
-
case
|
|
304
|
+
case "add":
|
|
305
305
|
return acc + value;
|
|
306
|
-
case
|
|
306
|
+
case "sub":
|
|
307
307
|
return acc - value;
|
|
308
308
|
/**
|
|
309
309
|
* Multiplication & division would end up with wrong result if we don't adjust the value
|
|
@@ -313,10 +313,10 @@ export class BigIntArithmetics {
|
|
|
313
313
|
* 200000000n * 200000000n = 40000000000000000n / 100000000n (decimals) => 400000000n
|
|
314
314
|
* (200000000n * 100000000n (decimals)) / 200000000n => 100000000n
|
|
315
315
|
*/
|
|
316
|
-
case
|
|
316
|
+
case "mul":
|
|
317
317
|
return (acc * value) / precisionDecimalMultiplier;
|
|
318
|
-
case
|
|
319
|
-
if (value === 0n) throw new RangeError(
|
|
318
|
+
case "div": {
|
|
319
|
+
if (value === 0n) throw new RangeError("Division by zero");
|
|
320
320
|
return (acc * precisionDecimalMultiplier) / value;
|
|
321
321
|
}
|
|
322
322
|
default:
|
|
@@ -342,34 +342,34 @@ export class BigIntArithmetics {
|
|
|
342
342
|
});
|
|
343
343
|
}
|
|
344
344
|
|
|
345
|
-
#comparison(method:
|
|
345
|
+
#comparison(method: "gt" | "gte" | "lt" | "lte" | "eqValue", ...args: InitialisationValueType[]) {
|
|
346
346
|
const decimal = this.#retrievePrecisionDecimal(this, ...args);
|
|
347
347
|
const value = this.getBigIntValue(args[0], decimal);
|
|
348
348
|
const compareToValue = this.getBigIntValue(this, decimal);
|
|
349
349
|
|
|
350
350
|
switch (method) {
|
|
351
|
-
case
|
|
351
|
+
case "gt":
|
|
352
352
|
return compareToValue > value;
|
|
353
|
-
case
|
|
353
|
+
case "gte":
|
|
354
354
|
return compareToValue >= value;
|
|
355
|
-
case
|
|
355
|
+
case "lt":
|
|
356
356
|
return compareToValue < value;
|
|
357
|
-
case
|
|
357
|
+
case "lte":
|
|
358
358
|
return compareToValue <= value;
|
|
359
|
-
case
|
|
359
|
+
case "eqValue":
|
|
360
360
|
return compareToValue === value;
|
|
361
361
|
}
|
|
362
362
|
}
|
|
363
363
|
|
|
364
364
|
#setValue(value: InitialisationValueType) {
|
|
365
|
-
const safeValue = toSafeValue(value) ||
|
|
365
|
+
const safeValue = toSafeValue(value) || "0";
|
|
366
366
|
this.bigIntValue = this.#toBigInt(safeValue);
|
|
367
367
|
}
|
|
368
368
|
|
|
369
369
|
#retrievePrecisionDecimal(...args: InitialisationValueType[]) {
|
|
370
370
|
const decimals = args
|
|
371
371
|
.map((arg) => {
|
|
372
|
-
const isObject = typeof arg ===
|
|
372
|
+
const isObject = typeof arg === "object";
|
|
373
373
|
const value = isObject
|
|
374
374
|
? arg.decimal || decimalFromMultiplier(arg.decimalMultiplier)
|
|
375
375
|
: getFloatDecimals(toSafeValue(arg));
|
|
@@ -384,36 +384,36 @@ export class BigIntArithmetics {
|
|
|
384
384
|
#toBigInt(value: string, decimal?: number) {
|
|
385
385
|
const multiplier = decimal ? toMultiplier(decimal) : this.decimalMultiplier;
|
|
386
386
|
const padDecimal = decimalFromMultiplier(multiplier);
|
|
387
|
-
const [integerPart =
|
|
387
|
+
const [integerPart = "", decimalPart = ""] = value.split(".");
|
|
388
388
|
|
|
389
|
-
return BigInt(`${integerPart}${decimalPart.padEnd(padDecimal,
|
|
389
|
+
return BigInt(`${integerPart}${decimalPart.padEnd(padDecimal, "0")}`);
|
|
390
390
|
}
|
|
391
391
|
}
|
|
392
392
|
|
|
393
|
-
const numberFormatter = Intl.NumberFormat(
|
|
393
|
+
const numberFormatter = Intl.NumberFormat("fullwide", {
|
|
394
394
|
useGrouping: false,
|
|
395
395
|
maximumFractionDigits: 20,
|
|
396
396
|
});
|
|
397
397
|
|
|
398
398
|
function toSafeValue(value: InitialisationValueType) {
|
|
399
399
|
const parsedValue =
|
|
400
|
-
typeof value ===
|
|
401
|
-
const splitValue = `${parsedValue}`.replaceAll(
|
|
400
|
+
typeof value === "number" ? numberFormatter.format(value) : getStringValue(value);
|
|
401
|
+
const splitValue = `${parsedValue}`.replaceAll(",", ".").split(".");
|
|
402
402
|
|
|
403
403
|
return splitValue.length > 1
|
|
404
|
-
? `${splitValue.slice(0, -1).join(
|
|
404
|
+
? `${splitValue.slice(0, -1).join("")}.${splitValue.at(-1)}`
|
|
405
405
|
: splitValue[0];
|
|
406
406
|
}
|
|
407
407
|
|
|
408
408
|
function getFloatDecimals(value: string) {
|
|
409
|
-
const decimals = value.split(
|
|
409
|
+
const decimals = value.split(".")[1]?.length || 0;
|
|
410
410
|
return Math.max(decimals, DEFAULT_DECIMAL);
|
|
411
411
|
}
|
|
412
412
|
|
|
413
413
|
function getStringValue(param: SKBigIntParams) {
|
|
414
|
-
return typeof param ===
|
|
415
|
-
?
|
|
416
|
-
? param.getValue(
|
|
414
|
+
return typeof param === "object"
|
|
415
|
+
? "getValue" in param
|
|
416
|
+
? param.getValue("string")
|
|
417
417
|
: param.value
|
|
418
418
|
: param;
|
|
419
419
|
}
|
|
@@ -31,6 +31,7 @@ const errorMessages = {
|
|
|
31
31
|
core_swap_contract_not_supported: 10205,
|
|
32
32
|
core_swap_transaction_error: 10206,
|
|
33
33
|
core_swap_quote_mode_not_supported: 10207,
|
|
34
|
+
core_swap_provider_not_found: 10208,
|
|
34
35
|
/**
|
|
35
36
|
* Core - Transaction
|
|
36
37
|
*/
|
|
@@ -59,6 +60,16 @@ const errorMessages = {
|
|
|
59
60
|
wallet_ledger_device_not_found: 20004,
|
|
60
61
|
wallet_ledger_device_locked: 20005,
|
|
61
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Chainflip
|
|
65
|
+
*/
|
|
66
|
+
chainflip_channel_error: 30001,
|
|
67
|
+
chainflip_broker_recipient_error: 30002,
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* THORChain
|
|
71
|
+
*/
|
|
72
|
+
|
|
62
73
|
/**
|
|
63
74
|
* Helpers
|
|
64
75
|
*/
|
|
@@ -70,10 +81,15 @@ export type ErrorKeys = keyof typeof errorMessages;
|
|
|
70
81
|
export class SwapKitError extends Error {
|
|
71
82
|
constructor(errorKey: ErrorKeys, sourceError?: any) {
|
|
72
83
|
if (sourceError) {
|
|
73
|
-
console.error(sourceError, {
|
|
84
|
+
console.error(sourceError, {
|
|
85
|
+
stack: sourceError?.stack,
|
|
86
|
+
message: sourceError?.message,
|
|
87
|
+
});
|
|
74
88
|
}
|
|
75
89
|
|
|
76
|
-
super(errorKey, {
|
|
90
|
+
super(errorKey, {
|
|
91
|
+
cause: { code: errorMessages[errorKey], message: errorKey },
|
|
92
|
+
});
|
|
77
93
|
Object.setPrototypeOf(this, SwapKitError.prototype);
|
|
78
94
|
}
|
|
79
95
|
}
|
package/src/types.ts
CHANGED
|
@@ -11,20 +11,20 @@ import type {
|
|
|
11
11
|
TraderjoeList,
|
|
12
12
|
UniswapList,
|
|
13
13
|
WoofiList,
|
|
14
|
-
} from
|
|
14
|
+
} from "@swapkit/tokens";
|
|
15
15
|
|
|
16
16
|
export type TokenTax = { buy: number; sell: number };
|
|
17
17
|
|
|
18
18
|
export type TokenNames =
|
|
19
|
-
| (typeof ThorchainList)[
|
|
20
|
-
| (typeof CoinGeckoList)[
|
|
21
|
-
| (typeof MayaList)[
|
|
22
|
-
| (typeof PancakeswapETHList)[
|
|
23
|
-
| (typeof PancakeswapList)[
|
|
24
|
-
| (typeof PangolinList)[
|
|
25
|
-
| (typeof StargateARBList)[
|
|
26
|
-
| (typeof SushiswapList)[
|
|
27
|
-
| (typeof TraderjoeList)[
|
|
28
|
-
| (typeof WoofiList)[
|
|
29
|
-
| (typeof UniswapList)[
|
|
30
|
-
| (typeof ChainflipList)[
|
|
19
|
+
| (typeof ThorchainList)["tokens"][number]["identifier"]
|
|
20
|
+
| (typeof CoinGeckoList)["tokens"][number]["identifier"]
|
|
21
|
+
| (typeof MayaList)["tokens"][number]["identifier"]
|
|
22
|
+
| (typeof PancakeswapETHList)["tokens"][number]["identifier"]
|
|
23
|
+
| (typeof PancakeswapList)["tokens"][number]["identifier"]
|
|
24
|
+
| (typeof PangolinList)["tokens"][number]["identifier"]
|
|
25
|
+
| (typeof StargateARBList)["tokens"][number]["identifier"]
|
|
26
|
+
| (typeof SushiswapList)["tokens"][number]["identifier"]
|
|
27
|
+
| (typeof TraderjoeList)["tokens"][number]["identifier"]
|
|
28
|
+
| (typeof WoofiList)["tokens"][number]["identifier"]
|
|
29
|
+
| (typeof UniswapList)["tokens"][number]["identifier"]
|
|
30
|
+
| (typeof ChainflipList)["tokens"][number]["identifier"];
|