@pioneer-platform/helpers 0.0.0 → 0.0.1
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/lib/helpers/asset.d.ts +36 -0
- package/lib/helpers/asset.js +270 -0
- package/lib/helpers/liquidity.d.ts +58 -0
- package/lib/helpers/liquidity.js +112 -0
- package/lib/helpers/memo.d.ts +46 -0
- package/lib/helpers/memo.js +46 -0
- package/lib/helpers/others.d.ts +3 -0
- package/lib/helpers/others.js +24 -0
- package/lib/helpers/request.d.ts +5 -0
- package/lib/helpers/request.js +74 -0
- package/lib/helpers/validators.d.ts +1 -0
- package/lib/helpers/validators.js +17 -0
- package/lib/index.d.ts +15 -0
- package/lib/index.js +31 -0
- package/lib/modules/__tests__/assetValue.test.d.ts +1 -0
- package/lib/modules/__tests__/assetValue.test.js +399 -0
- package/lib/modules/__tests__/swapKitNumber.test.d.ts +1 -0
- package/lib/modules/__tests__/swapKitNumber.test.js +425 -0
- package/lib/modules/assetValue.d.ts +55 -0
- package/lib/modules/assetValue.js +389 -0
- package/lib/modules/bigIntArithmetics.d.ts +58 -0
- package/lib/modules/bigIntArithmetics.js +344 -0
- package/lib/modules/swapKitError.d.ts +64 -0
- package/lib/modules/swapKitError.js +90 -0
- package/lib/modules/swapKitNumber.d.ts +6 -0
- package/lib/modules/swapKitNumber.js +36 -0
- package/package.json +22 -35
- package/src/helpers/asset.ts +232 -0
- package/src/helpers/liquidity.ts +174 -0
- package/src/helpers/memo.ts +90 -0
- package/src/helpers/others.ts +20 -0
- package/src/helpers/request.ts +33 -0
- package/src/helpers/validators.ts +17 -0
- package/src/index.ts +16 -6
- package/src/modules/assetValue.ts +358 -0
- package/src/modules/bigIntArithmetics.ts +416 -0
- package/src/{exceptions/SwapKitError.ts → modules/swapKitError.ts} +15 -4
- package/src/modules/swapKitNumber.ts +16 -0
- package/tsconfig.json +13 -0
- package/LICENSE +0 -21
- package/src/__tests__/asset.test.ts +0 -33
- package/src/__tests__/derivationPath.test.ts +0 -16
- package/src/amount.ts +0 -29
- package/src/asset.ts +0 -13
- package/src/derivationPath.ts +0 -5
- package/src/exceptions/index.ts +0 -1
- package/src/fees.ts +0 -13
- package/src/request.ts +0 -34
@@ -0,0 +1,416 @@
|
|
1
|
+
import { BaseDecimal } from '@coinmasters/types';
|
2
|
+
import type { SwapKitNumber } from './swapKitNumber';
|
3
|
+
|
4
|
+
type NumberPrimitivesType = {
|
5
|
+
bigint: bigint;
|
6
|
+
number: number;
|
7
|
+
string: string;
|
8
|
+
};
|
9
|
+
export type NumberPrimitives = bigint | number | string;
|
10
|
+
type InitialisationValueType = NumberPrimitives | BigIntArithmetics | SwapKitNumber;
|
11
|
+
|
12
|
+
type SKBigIntParams = InitialisationValueType | { decimal?: number; value: number | string };
|
13
|
+
type AllowedNumberTypes = 'bigint' | 'number' | 'string';
|
14
|
+
|
15
|
+
const DEFAULT_DECIMAL = 8;
|
16
|
+
|
17
|
+
const bigintPow = (base: bigint, exponent: number): bigint => {
|
18
|
+
let result = BigInt(1);
|
19
|
+
while (exponent > 0) {
|
20
|
+
if (exponent % 2 === 1) {
|
21
|
+
result *= base;
|
22
|
+
}
|
23
|
+
base *= base;
|
24
|
+
exponent = Math.floor(exponent / 2);
|
25
|
+
}
|
26
|
+
return result;
|
27
|
+
};
|
28
|
+
|
29
|
+
const toMultiplier = (decimal: number): bigint => {
|
30
|
+
console.log('toMultiplier input decimal:', decimal);
|
31
|
+
try {
|
32
|
+
const result = bigintPow(BigInt(10), decimal);
|
33
|
+
console.log('toMultiplier result:', result);
|
34
|
+
return result;
|
35
|
+
} catch (error) {
|
36
|
+
console.error('Error in toMultiplier:', error);
|
37
|
+
return BigInt(10);
|
38
|
+
}
|
39
|
+
};
|
40
|
+
|
41
|
+
const decimalFromMultiplier = (multiplier: bigint): number => Math.log10(Number(multiplier));
|
42
|
+
|
43
|
+
export function formatBigIntToSafeValue({
|
44
|
+
value,
|
45
|
+
bigIntDecimal = DEFAULT_DECIMAL,
|
46
|
+
decimal = DEFAULT_DECIMAL,
|
47
|
+
}: {
|
48
|
+
value: bigint;
|
49
|
+
bigIntDecimal?: number;
|
50
|
+
decimal?: number;
|
51
|
+
}): string {
|
52
|
+
const isNegative = value < BigInt(0);
|
53
|
+
let valueString = value.toString().substring(isNegative ? 1 : 0);
|
54
|
+
|
55
|
+
const padLength = decimal - (valueString.length - 1);
|
56
|
+
|
57
|
+
if (padLength > 0) {
|
58
|
+
valueString = '0'.repeat(padLength) + valueString;
|
59
|
+
}
|
60
|
+
|
61
|
+
const decimalIndex = valueString.length - decimal;
|
62
|
+
let decimalString = valueString.slice(-decimal);
|
63
|
+
|
64
|
+
if (parseInt(decimalString[bigIntDecimal]) >= 5) {
|
65
|
+
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(
|
66
|
+
parseInt(decimalString[bigIntDecimal - 1]) + 1
|
67
|
+
).toString()}`;
|
68
|
+
} else {
|
69
|
+
decimalString = decimalString.substring(0, bigIntDecimal);
|
70
|
+
}
|
71
|
+
|
72
|
+
return `${isNegative ? '-' : ''}${valueString.slice(0, decimalIndex)}.${decimalString}`.replace(
|
73
|
+
/\.?0*$/,
|
74
|
+
'',
|
75
|
+
);
|
76
|
+
}
|
77
|
+
|
78
|
+
export class BigIntArithmetics {
|
79
|
+
private decimalMultiplier: bigint = bigintPow(BigInt(10), 8);
|
80
|
+
private bigIntValue: bigint = BigInt(0);
|
81
|
+
private decimal?: number;
|
82
|
+
|
83
|
+
static fromBigInt(value: bigint, decimal?: number): BigIntArithmetics {
|
84
|
+
return new BigIntArithmetics({
|
85
|
+
decimal,
|
86
|
+
value: formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal }),
|
87
|
+
});
|
88
|
+
}
|
89
|
+
|
90
|
+
static shiftDecimals({
|
91
|
+
value,
|
92
|
+
from,
|
93
|
+
to,
|
94
|
+
}: {
|
95
|
+
value: InstanceType<typeof SwapKitNumber>;
|
96
|
+
from: number;
|
97
|
+
to: number;
|
98
|
+
}): BigIntArithmetics {
|
99
|
+
return this.fromBigInt(
|
100
|
+
(value.getBaseValue('bigint') * toMultiplier(to)) / toMultiplier(from),
|
101
|
+
to,
|
102
|
+
);
|
103
|
+
}
|
104
|
+
|
105
|
+
constructor(params: SKBigIntParams) {
|
106
|
+
console.log('Constructor Params:', params);
|
107
|
+
const value = getStringValue(params);
|
108
|
+
console.log('String Value:', value);
|
109
|
+
const isComplex = typeof params === 'object' && params !== null;
|
110
|
+
this.decimal = isComplex ? (params as { decimal?: number }).decimal : undefined;
|
111
|
+
console.log('Decimal:', this.decimal);
|
112
|
+
|
113
|
+
if (isComplex && 'decimalMultiplier' in (params as any)) {
|
114
|
+
this.decimalMultiplier = (params as any).decimalMultiplier;
|
115
|
+
} else {
|
116
|
+
const maxDecimals = Math.max(getFloatDecimals(toSafeValue(value)), this.decimal || 0);
|
117
|
+
this.decimalMultiplier = toMultiplier(maxDecimals);
|
118
|
+
}
|
119
|
+
console.log('Decimal Multiplier:', this.decimalMultiplier);
|
120
|
+
|
121
|
+
this.setValue(value);
|
122
|
+
console.log('BigInt Value:', this.bigIntValue);
|
123
|
+
}
|
124
|
+
|
125
|
+
set(value: SKBigIntParams): this {
|
126
|
+
// @ts-ignore
|
127
|
+
return new BigIntArithmetics({ decimal: this.decimal, value }) as this;
|
128
|
+
}
|
129
|
+
|
130
|
+
add(...args: InitialisationValueType[]): this {
|
131
|
+
return this.arithmetics('add', ...args);
|
132
|
+
}
|
133
|
+
|
134
|
+
sub(...args: InitialisationValueType[]): this {
|
135
|
+
return this.arithmetics('sub', ...args);
|
136
|
+
}
|
137
|
+
|
138
|
+
mul(...args: InitialisationValueType[]): this {
|
139
|
+
return this.arithmetics('mul', ...args);
|
140
|
+
}
|
141
|
+
|
142
|
+
div(...args: InitialisationValueType[]): this {
|
143
|
+
return this.arithmetics('div', ...args);
|
144
|
+
}
|
145
|
+
|
146
|
+
gt(value: InitialisationValueType): boolean {
|
147
|
+
return this.comparison('gt', value);
|
148
|
+
}
|
149
|
+
|
150
|
+
gte(value: InitialisationValueType): boolean {
|
151
|
+
return this.comparison('gte', value);
|
152
|
+
}
|
153
|
+
|
154
|
+
lt(value: InitialisationValueType): boolean {
|
155
|
+
return this.comparison('lt', value);
|
156
|
+
}
|
157
|
+
|
158
|
+
lte(value: InitialisationValueType): boolean {
|
159
|
+
return this.comparison('lte', value);
|
160
|
+
}
|
161
|
+
|
162
|
+
eqValue(value: InitialisationValueType): boolean {
|
163
|
+
return this.comparison('eqValue', value);
|
164
|
+
}
|
165
|
+
|
166
|
+
// @ts-ignore
|
167
|
+
getValue<T extends AllowedNumberTypes>(type: T): NumberPrimitivesType[T] {
|
168
|
+
const value = formatBigIntToSafeValue({
|
169
|
+
value: this.bigIntValue,
|
170
|
+
bigIntDecimal: this.decimal || decimalFromMultiplier(this.decimalMultiplier),
|
171
|
+
});
|
172
|
+
|
173
|
+
switch (type) {
|
174
|
+
case 'number':
|
175
|
+
return Number(value) as NumberPrimitivesType[T];
|
176
|
+
case 'string':
|
177
|
+
return value as NumberPrimitivesType[T];
|
178
|
+
case 'bigint':
|
179
|
+
return ((this.bigIntValue * bigintPow(BigInt(10), this.decimal || 8)) /
|
180
|
+
this.decimalMultiplier) as NumberPrimitivesType[T];
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
// @ts-ignore
|
185
|
+
getBaseValue<T extends AllowedNumberTypes>(type: T): NumberPrimitivesType[T] {
|
186
|
+
const divisor = this.decimalMultiplier / toMultiplier(this.decimal || BaseDecimal.THOR);
|
187
|
+
const baseValue = this.bigIntValue / divisor;
|
188
|
+
|
189
|
+
switch (type) {
|
190
|
+
case 'number':
|
191
|
+
return Number(baseValue) as NumberPrimitivesType[T];
|
192
|
+
case 'string':
|
193
|
+
return baseValue.toString() as NumberPrimitivesType[T];
|
194
|
+
case 'bigint':
|
195
|
+
return baseValue as NumberPrimitivesType[T];
|
196
|
+
}
|
197
|
+
}
|
198
|
+
|
199
|
+
getBigIntValue(value: InitialisationValueType, decimal?: number): bigint {
|
200
|
+
if (!decimal && typeof value === 'object' && value !== null) {
|
201
|
+
return (value as BigIntArithmetics).bigIntValue;
|
202
|
+
}
|
203
|
+
|
204
|
+
const stringValue = getStringValue(value);
|
205
|
+
const safeValue = toSafeValue(stringValue);
|
206
|
+
|
207
|
+
if (safeValue === '0' || safeValue === 'undefined') return BigInt(0);
|
208
|
+
return this.toBigInt(safeValue, decimal);
|
209
|
+
}
|
210
|
+
|
211
|
+
toSignificant(significantDigits: number = 6): string {
|
212
|
+
const [int, dec] = this.getValue('string').split('.');
|
213
|
+
const integer = int || '';
|
214
|
+
const decimal = dec || '';
|
215
|
+
const valueLength = parseInt(integer) ? integer.length + decimal.length : decimal.length;
|
216
|
+
|
217
|
+
if (valueLength <= significantDigits) {
|
218
|
+
return this.getValue('string');
|
219
|
+
}
|
220
|
+
|
221
|
+
if (integer.length >= significantDigits) {
|
222
|
+
return integer.slice(0, significantDigits).padEnd(integer.length, '0');
|
223
|
+
}
|
224
|
+
|
225
|
+
if (parseInt(integer)) {
|
226
|
+
return `${integer}.${decimal.slice(0, significantDigits - integer.length)}`.padEnd(
|
227
|
+
significantDigits - integer.length,
|
228
|
+
'0',
|
229
|
+
);
|
230
|
+
}
|
231
|
+
|
232
|
+
const trimmedDecimal = parseInt(decimal);
|
233
|
+
const slicedDecimal = `${trimmedDecimal}`.slice(0, significantDigits);
|
234
|
+
|
235
|
+
return `0.${slicedDecimal.padStart(
|
236
|
+
decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length,
|
237
|
+
'0',
|
238
|
+
)}`;
|
239
|
+
}
|
240
|
+
|
241
|
+
toFixed(fixedDigits: number = 6): string {
|
242
|
+
const [int, dec] = this.getValue('string').split('.');
|
243
|
+
const integer = int || '';
|
244
|
+
const decimal = dec || '';
|
245
|
+
|
246
|
+
if (parseInt(integer)) {
|
247
|
+
return `${integer}.${decimal.slice(0, fixedDigits)}`.padEnd(fixedDigits, '0');
|
248
|
+
}
|
249
|
+
|
250
|
+
const trimmedDecimal = parseInt(decimal);
|
251
|
+
const slicedDecimal = `${trimmedDecimal}`.slice(0, fixedDigits);
|
252
|
+
|
253
|
+
return `0.${slicedDecimal.padStart(
|
254
|
+
decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length,
|
255
|
+
'0',
|
256
|
+
)}`;
|
257
|
+
}
|
258
|
+
|
259
|
+
toAbbreviation(digits = 2): string {
|
260
|
+
const value = this.getValue('number');
|
261
|
+
const abbreviations = ['', 'K', 'M', 'B', 'T', 'Q', 'Qi', 'S'];
|
262
|
+
const tier = Math.floor(Math.log10(Math.abs(value)) / 3);
|
263
|
+
const suffix = abbreviations[tier];
|
264
|
+
|
265
|
+
if (!suffix) return this.getValue('string');
|
266
|
+
|
267
|
+
const scale = 10 ** (tier * 3);
|
268
|
+
const scaled = value / scale;
|
269
|
+
|
270
|
+
return `${scaled.toFixed(digits)}${suffix}`;
|
271
|
+
}
|
272
|
+
|
273
|
+
toCurrency(
|
274
|
+
currency = '$',
|
275
|
+
{
|
276
|
+
currencyPosition = 'start',
|
277
|
+
decimal = 2,
|
278
|
+
decimalSeparator = '.',
|
279
|
+
thousandSeparator = ',',
|
280
|
+
} = {},
|
281
|
+
): string {
|
282
|
+
const value = this.getValue('number');
|
283
|
+
const [int, dec = ''] = value.toFixed(6).split('.');
|
284
|
+
const integer = int.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);
|
285
|
+
|
286
|
+
const parsedValue =
|
287
|
+
!int && !dec
|
288
|
+
? '0.00'
|
289
|
+
: int === '0'
|
290
|
+
? `${parseFloat(`0.${dec}`)}`.replace('.', decimalSeparator)
|
291
|
+
: `${integer}${parseInt(dec) ? `${decimalSeparator}${dec.slice(0, decimal)}` : ''}`;
|
292
|
+
|
293
|
+
return `${currencyPosition === 'start' ? currency : ''}${parsedValue}${
|
294
|
+
currencyPosition === 'end' ? currency : ''
|
295
|
+
}`;
|
296
|
+
}
|
297
|
+
|
298
|
+
private arithmetics(method: 'add' | 'sub' | 'mul' | 'div', ...args: InitialisationValueType[]): this {
|
299
|
+
const precisionDecimal = this.retrievePrecisionDecimal(this, ...args);
|
300
|
+
const decimal = Math.max(precisionDecimal, decimalFromMultiplier(this.decimalMultiplier));
|
301
|
+
const precisionDecimalMultiplier = toMultiplier(decimal);
|
302
|
+
|
303
|
+
const result = args.reduce(
|
304
|
+
(acc: bigint, arg) => {
|
305
|
+
const value = this.getBigIntValue(arg, decimal);
|
306
|
+
|
307
|
+
switch (method) {
|
308
|
+
case 'add':
|
309
|
+
return acc + value;
|
310
|
+
case 'sub':
|
311
|
+
return acc - value;
|
312
|
+
case 'mul':
|
313
|
+
return (acc * value) / precisionDecimalMultiplier;
|
314
|
+
case 'div': {
|
315
|
+
if (value === BigInt(0)) throw new RangeError('Division by zero');
|
316
|
+
return (acc * precisionDecimalMultiplier) / value;
|
317
|
+
}
|
318
|
+
default:
|
319
|
+
return acc;
|
320
|
+
}
|
321
|
+
},
|
322
|
+
(this.bigIntValue * precisionDecimalMultiplier) / this.decimalMultiplier,
|
323
|
+
);
|
324
|
+
|
325
|
+
const formattedValue = formatBigIntToSafeValue({
|
326
|
+
bigIntDecimal: decimal,
|
327
|
+
decimal,
|
328
|
+
value: result,
|
329
|
+
});
|
330
|
+
|
331
|
+
return new BigIntArithmetics({
|
332
|
+
decimalMultiplier: toMultiplier(decimal),
|
333
|
+
decimal: this.decimal,
|
334
|
+
value: formattedValue,
|
335
|
+
}) as this;
|
336
|
+
}
|
337
|
+
|
338
|
+
private comparison(method: 'gt' | 'gte' | 'lt' | 'lte' | 'eqValue', ...args: InitialisationValueType[]): boolean {
|
339
|
+
const decimal = this.retrievePrecisionDecimal(this, ...args);
|
340
|
+
const value = this.getBigIntValue(args[0], decimal);
|
341
|
+
const compareToValue = this.getBigIntValue(this, decimal);
|
342
|
+
|
343
|
+
switch (method) {
|
344
|
+
case 'gt':
|
345
|
+
return compareToValue > value;
|
346
|
+
case 'gte':
|
347
|
+
return compareToValue >= value;
|
348
|
+
case 'lt':
|
349
|
+
return compareToValue < value;
|
350
|
+
case 'lte':
|
351
|
+
return compareToValue <= value;
|
352
|
+
case 'eqValue':
|
353
|
+
return compareToValue === value;
|
354
|
+
}
|
355
|
+
}
|
356
|
+
|
357
|
+
private setValue(value: InitialisationValueType): void {
|
358
|
+
const safeValue = toSafeValue(value) || '0';
|
359
|
+
this.bigIntValue = this.toBigInt(safeValue);
|
360
|
+
}
|
361
|
+
|
362
|
+
private retrievePrecisionDecimal(...args: InitialisationValueType[]): number {
|
363
|
+
const decimals = args
|
364
|
+
.map((arg) => {
|
365
|
+
const isObject = typeof arg === 'object' && arg !== null;
|
366
|
+
const value = isObject
|
367
|
+
? (arg as any).decimal || decimalFromMultiplier((arg as any).decimalMultiplier)
|
368
|
+
: getFloatDecimals(toSafeValue(arg));
|
369
|
+
|
370
|
+
return value;
|
371
|
+
})
|
372
|
+
.filter(Boolean) as number[];
|
373
|
+
|
374
|
+
return Math.max(...decimals, DEFAULT_DECIMAL);
|
375
|
+
}
|
376
|
+
|
377
|
+
private toBigInt(value: string, decimal?: number): bigint {
|
378
|
+
const multiplier = decimal ? toMultiplier(decimal) : this.decimalMultiplier;
|
379
|
+
const padDecimal = decimalFromMultiplier(multiplier);
|
380
|
+
const [integerPart = '', decimalPart = ''] = value.split('.');
|
381
|
+
|
382
|
+
return BigInt(`${integerPart}${decimalPart.padEnd(padDecimal, '0')}`);
|
383
|
+
}
|
384
|
+
}
|
385
|
+
|
386
|
+
const numberFormatter = Intl.NumberFormat('fullwide', {
|
387
|
+
useGrouping: false,
|
388
|
+
maximumFractionDigits: 20,
|
389
|
+
});
|
390
|
+
|
391
|
+
function toSafeValue(value: InitialisationValueType): string {
|
392
|
+
const parsedValue =
|
393
|
+
typeof value === 'number' ? numberFormatter.format(value) : getStringValue(value);
|
394
|
+
const splitValue = `${parsedValue}`.replace(/,/g, '.').split('.');
|
395
|
+
|
396
|
+
return splitValue.length > 1
|
397
|
+
? `${splitValue.slice(0, -1).join('')}.${splitValue.at(-1)}`
|
398
|
+
: splitValue[0];
|
399
|
+
}
|
400
|
+
|
401
|
+
function getFloatDecimals(value: string): number {
|
402
|
+
let decimals = 0;
|
403
|
+
const parts = value.split('.');
|
404
|
+
if (parts.length > 1 && parts[1].length > 0) {
|
405
|
+
decimals = parts[1].length;
|
406
|
+
}
|
407
|
+
return Math.max(decimals, DEFAULT_DECIMAL);
|
408
|
+
}
|
409
|
+
|
410
|
+
function getStringValue(param: SKBigIntParams): string {
|
411
|
+
return typeof param === 'object' && param !== null
|
412
|
+
? 'getValue' in param
|
413
|
+
? (param as SwapKitNumber).getValue('string')
|
414
|
+
: (param as any).value
|
415
|
+
: (param as string);
|
416
|
+
}
|
@@ -19,6 +19,7 @@ const errorMessages = {
|
|
19
19
|
core_wallet_trezor_not_installed: 10106,
|
20
20
|
core_wallet_keplr_not_installed: 10107,
|
21
21
|
core_wallet_okx_not_installed: 10108,
|
22
|
+
core_wallet_keepkey_not_installed: 10109,
|
22
23
|
/**
|
23
24
|
* Core - Swap
|
24
25
|
*/
|
@@ -28,7 +29,6 @@ const errorMessages = {
|
|
28
29
|
core_swap_contract_not_found: 10203,
|
29
30
|
core_swap_route_transaction_not_found: 10204,
|
30
31
|
core_swap_contract_not_supported: 10205,
|
31
|
-
core_swap_transaction_error: 10206,
|
32
32
|
core_swap_quote_mode_not_supported: 10207,
|
33
33
|
/**
|
34
34
|
* Core - Transaction
|
@@ -43,19 +43,30 @@ const errorMessages = {
|
|
43
43
|
core_transaction_add_liquidity_asset_error: 10308,
|
44
44
|
core_transaction_withdraw_error: 10309,
|
45
45
|
core_transaction_deposit_to_pool_error: 10310,
|
46
|
+
core_transaction_deposit_insufficient_funds_error: 10311,
|
47
|
+
core_transaction_deposit_gas_error: 10312,
|
48
|
+
core_transaction_invalid_sender_address: 10313,
|
49
|
+
core_transaction_deposit_server_error: 10314,
|
46
50
|
|
51
|
+
core_swap_transaction_error: 10400,
|
47
52
|
/**
|
48
53
|
* Wallets
|
49
54
|
*/
|
50
|
-
wallet_ledger_connection_error:
|
55
|
+
wallet_ledger_connection_error: 20001,
|
56
|
+
|
57
|
+
/**
|
58
|
+
* Helpers
|
59
|
+
*/
|
60
|
+
helpers_number_different_decimals: 99101,
|
51
61
|
} as const;
|
52
62
|
|
53
|
-
type Keys = keyof typeof errorMessages;
|
63
|
+
export type Keys = keyof typeof errorMessages;
|
54
64
|
|
55
65
|
export class SwapKitError extends Error {
|
56
66
|
constructor(errorKey: Keys, sourceError?: any) {
|
57
|
-
console.error(sourceError);
|
67
|
+
console.error(sourceError, { stack: sourceError?.stack, message: sourceError?.message });
|
58
68
|
|
69
|
+
// @ts-ignore
|
59
70
|
super(errorKey, { cause: { code: errorMessages[errorKey], message: errorKey } });
|
60
71
|
Object.setPrototypeOf(this, SwapKitError.prototype);
|
61
72
|
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { BigIntArithmetics, formatBigIntToSafeValue } from './bigIntArithmetics';
|
2
|
+
|
3
|
+
export type SwapKitValueType = BigIntArithmetics | string | number;
|
4
|
+
|
5
|
+
export class SwapKitNumber extends BigIntArithmetics {
|
6
|
+
eq(value: SwapKitValueType) {
|
7
|
+
return this.eqValue(value);
|
8
|
+
}
|
9
|
+
|
10
|
+
static fromBigInt(value: bigint, decimal?: number) {
|
11
|
+
return new SwapKitNumber({
|
12
|
+
decimal,
|
13
|
+
value: formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal }),
|
14
|
+
});
|
15
|
+
}
|
16
|
+
}
|
package/tsconfig.json
ADDED
package/LICENSE
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2023 THORSwap
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
13
|
-
copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
-
SOFTWARE.
|
@@ -1,33 +0,0 @@
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
2
|
-
import { assetFromString, assetToString } from '../asset.ts';
|
3
|
-
import { Chain } from '@pioneer-platform/types';
|
4
|
-
|
5
|
-
describe('assetToString', () => {
|
6
|
-
it('should return the correct string representation of an asset', () => {
|
7
|
-
const asset = { chain: Chain.Ethereum, symbol: 'USDT' };
|
8
|
-
expect(assetToString(asset)).toBe('ETH.USDT');
|
9
|
-
|
10
|
-
const synthAsset = { chain: Chain.THORChain, symbol: 'BNB/BNB' };
|
11
|
-
expect(assetToString(synthAsset)).toBe('THOR.BNB/BNB');
|
12
|
-
});
|
13
|
-
});
|
14
|
-
|
15
|
-
describe('assetFromString', () => {
|
16
|
-
it('should return the correct asset object from a string', () => {
|
17
|
-
const assetString = 'ETH.USDT';
|
18
|
-
const expectedAsset = { chain: Chain.Ethereum, symbol: 'USDT', ticker: 'USDT', synth: false };
|
19
|
-
expect(assetFromString(assetString)).toEqual(expectedAsset);
|
20
|
-
});
|
21
|
-
|
22
|
-
it('should return the correct asset object from a synth string', () => {
|
23
|
-
const assetString = 'THOR.BNB/BNB';
|
24
|
-
const expectedAsset = { chain: Chain.THORChain, symbol: 'BNB/BNB', ticker: 'BNB/BNB', synth: true };
|
25
|
-
expect(assetFromString(assetString)).toEqual(expectedAsset);
|
26
|
-
});
|
27
|
-
|
28
|
-
it('should return the correct asset object from a string with a hyphen', () => {
|
29
|
-
const assetString = 'ETH.USDT-0XA3910454BF2CB59B8B3A401589A3BACC5CA42306';
|
30
|
-
const expectedAsset = { chain: Chain.Ethereum, symbol: 'USDT-0XA3910454BF2CB59B8B3A401589A3BACC5CA42306', ticker: 'USDT', synth: false };
|
31
|
-
expect(assetFromString(assetString)).toEqual(expectedAsset);
|
32
|
-
});
|
33
|
-
});
|
@@ -1,16 +0,0 @@
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
2
|
-
import { derivationPathToString } from '../derivationPath.ts';
|
3
|
-
|
4
|
-
describe('derivationPathToString', () => {
|
5
|
-
it('should return the correct string for a full path', () => {
|
6
|
-
const path = [1, 2, 3, 4, 5];
|
7
|
-
const result = derivationPathToString(path);
|
8
|
-
expect(result).toEqual("1'/2'/3'/4/5");
|
9
|
-
});
|
10
|
-
|
11
|
-
it('should return the correct string for a short path', () => {
|
12
|
-
const path = [1, 2, 3, 4];
|
13
|
-
const result = derivationPathToString(path);
|
14
|
-
expect(result).toEqual("1'/2'/3'/4");
|
15
|
-
});
|
16
|
-
});
|
package/src/amount.ts
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
import { BigNumber, BigNumberish } from '@ethersproject/bignumber';
|
2
|
-
import type { AmountWithBaseDenom } from '@pioneer-platform/types';
|
3
|
-
|
4
|
-
type Value = BigNumberish | AmountWithBaseDenom;
|
5
|
-
|
6
|
-
const isBigNumberValue = (v: unknown): v is BigNumberish =>
|
7
|
-
typeof v === 'string' || typeof v === 'number' || v instanceof BigNumber;
|
8
|
-
|
9
|
-
export const baseAmount = (value?: BigNumberish, decimal: number = 8): AmountWithBaseDenom => {
|
10
|
-
const amount = BigNumber.from(value || 0);
|
11
|
-
|
12
|
-
return {
|
13
|
-
amount: () => amount,
|
14
|
-
plus: (v: Value, d: number = decimal) =>
|
15
|
-
baseAmount(amount.add(isBigNumberValue(v) ? v : (v as AmountWithBaseDenom).amount()), d),
|
16
|
-
minus: (v: Value, d: number = decimal) =>
|
17
|
-
baseAmount(amount.sub(isBigNumberValue(v) ? v : (v as AmountWithBaseDenom).amount()), d),
|
18
|
-
times: (v: Value, d: number = decimal) =>
|
19
|
-
baseAmount(amount.mul(isBigNumberValue(v) ? v : (v as AmountWithBaseDenom).amount()), d),
|
20
|
-
div: (v: Value, d: number = decimal) =>
|
21
|
-
baseAmount(amount.div(isBigNumberValue(v) ? v : (v as AmountWithBaseDenom).amount()), d),
|
22
|
-
lt: (v: Value) => amount.lt(isBigNumberValue(v) ? v : (v as AmountWithBaseDenom).amount()),
|
23
|
-
lte: (v: Value) => amount.lte(isBigNumberValue(v) ? v : (v as AmountWithBaseDenom).amount()),
|
24
|
-
gt: (v: Value) => amount.gt(isBigNumberValue(v) ? v : (v as AmountWithBaseDenom).amount()),
|
25
|
-
gte: (v: Value) => amount.gte(isBigNumberValue(v) ? v : (v as AmountWithBaseDenom).amount()),
|
26
|
-
eq: (v: Value) => amount.eq(isBigNumberValue(v) ? v : (v as AmountWithBaseDenom).amount()),
|
27
|
-
decimal,
|
28
|
-
};
|
29
|
-
};
|
package/src/asset.ts
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
import { Chain } from '@pioneer-platform/types';
|
2
|
-
|
3
|
-
export const assetToString = ({ chain, symbol }: { chain: Chain; symbol: string }) =>
|
4
|
-
`${chain}.${symbol}`;
|
5
|
-
|
6
|
-
export const assetFromString = (assetString: string) => {
|
7
|
-
const [chain, ...symbolArray] = assetString.split('.') as [Chain, ...(string | undefined)[]];
|
8
|
-
const synth = assetString.includes('/');
|
9
|
-
const symbol = symbolArray.join('.');
|
10
|
-
const ticker = symbol?.split('-')?.[0];
|
11
|
-
|
12
|
-
return { chain, symbol, ticker, synth };
|
13
|
-
};
|
package/src/derivationPath.ts
DELETED
package/src/exceptions/index.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export * from './SwapKitError.ts';
|
package/src/fees.ts
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
import { type AmountWithBaseDenom, FeeOption } from '@pioneer-platform/types';
|
2
|
-
|
3
|
-
export const singleFee = (fee: AmountWithBaseDenom): Record<FeeOption, AmountWithBaseDenom> => ({
|
4
|
-
[FeeOption.Average]: fee,
|
5
|
-
[FeeOption.Fast]: fee,
|
6
|
-
[FeeOption.Fastest]: fee,
|
7
|
-
});
|
8
|
-
|
9
|
-
export const gasFeeMultiplier: Record<FeeOption, number> = {
|
10
|
-
[FeeOption.Average]: 1.2,
|
11
|
-
[FeeOption.Fast]: 1.5,
|
12
|
-
[FeeOption.Fastest]: 2,
|
13
|
-
};
|
package/src/request.ts
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
export const getRequest = async <T>(
|
2
|
-
url: string,
|
3
|
-
params?: { [key in string]?: any },
|
4
|
-
): Promise<T> => {
|
5
|
-
const queryParams = Object.entries(params || {}).reduce((acc, [key, value]) => {
|
6
|
-
if (value) {
|
7
|
-
acc[key] = value;
|
8
|
-
}
|
9
|
-
|
10
|
-
return acc;
|
11
|
-
}, {} as { [key in string]: any });
|
12
|
-
|
13
|
-
const response = await fetch(
|
14
|
-
`${url}${params ? `?${new URLSearchParams(queryParams).toString()}` : ''}`,
|
15
|
-
{ method: 'GET', mode: 'cors', credentials: 'omit' },
|
16
|
-
);
|
17
|
-
|
18
|
-
return response.json();
|
19
|
-
};
|
20
|
-
|
21
|
-
export const postRequest = async <T>(
|
22
|
-
url: string,
|
23
|
-
data: string,
|
24
|
-
headers?: Record<string, string>,
|
25
|
-
parseAsString = false,
|
26
|
-
): Promise<T> => {
|
27
|
-
const response = await fetch(`${url}`, {
|
28
|
-
method: 'POST',
|
29
|
-
headers,
|
30
|
-
body: data,
|
31
|
-
});
|
32
|
-
|
33
|
-
return parseAsString ? response.text() : response.json();
|
34
|
-
};
|