@swapkit/helpers 1.0.0-rc.0
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/LICENSE +201 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +326 -0
- package/dist/index.es-8503fb04.js +34669 -0
- package/dist/index.es-903b9173.cjs +1 -0
- package/dist/index.es.js +651 -0
- package/package.json +49 -0
- package/src/helpers/__tests__/asset.test.ts +157 -0
- package/src/helpers/__tests__/memo.test.ts +79 -0
- package/src/helpers/__tests__/others.test.ts +59 -0
- package/src/helpers/asset.ts +177 -0
- package/src/helpers/liquidity.ts +157 -0
- package/src/helpers/memo.ts +90 -0
- package/src/helpers/number.ts +40 -0
- package/src/helpers/others.ts +59 -0
- package/src/helpers/validators.ts +17 -0
- package/src/index.ts +15 -0
- package/src/modules/__tests__/assetValue.test.ts +285 -0
- package/src/modules/__tests__/swapKitNumber.test.ts +331 -0
- package/src/modules/assetValue.ts +236 -0
- package/src/modules/bigIntArithmetics.ts +259 -0
- package/src/modules/swapKitError.ts +70 -0
- package/src/modules/swapKitNumber.ts +13 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { DEFAULT_DECIMAL, formatBigIntToSafeValue } from '../helpers/number.ts';
|
|
2
|
+
|
|
3
|
+
type AllowedValueType = bigint | number | string;
|
|
4
|
+
type ArithmeticMethod = 'add' | 'sub' | 'mul' | 'div';
|
|
5
|
+
|
|
6
|
+
const toMultiplier = (decimal: number) => 10n ** BigInt(decimal);
|
|
7
|
+
const decimalFromMultiplier = (multiplier: bigint) => Math.log10(parseFloat(multiplier.toString()));
|
|
8
|
+
|
|
9
|
+
export class BigIntArithmetics {
|
|
10
|
+
decimalMultiplier: bigint = 10n ** 8n;
|
|
11
|
+
bigIntValue: bigint = 0n;
|
|
12
|
+
decimal?: number;
|
|
13
|
+
|
|
14
|
+
static fromBigInt(value: bigint, decimal?: number) {
|
|
15
|
+
return new BigIntArithmetics({
|
|
16
|
+
decimal,
|
|
17
|
+
value: formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal }),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static shiftDecimals({
|
|
22
|
+
value,
|
|
23
|
+
from,
|
|
24
|
+
to,
|
|
25
|
+
}: {
|
|
26
|
+
value: BigIntArithmetics | string | number;
|
|
27
|
+
from: number;
|
|
28
|
+
to: number;
|
|
29
|
+
}) {
|
|
30
|
+
return BigIntArithmetics.fromBigInt(
|
|
31
|
+
(new BigIntArithmetics(value).bigIntValue * toMultiplier(to)) / toMultiplier(from),
|
|
32
|
+
to,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
constructor(
|
|
37
|
+
valueOrParams:
|
|
38
|
+
| BigIntArithmetics
|
|
39
|
+
| string
|
|
40
|
+
| number
|
|
41
|
+
| { decimal?: number; value: number | string },
|
|
42
|
+
) {
|
|
43
|
+
const complexInit = typeof valueOrParams === 'object';
|
|
44
|
+
const value = complexInit ? valueOrParams.value : valueOrParams;
|
|
45
|
+
this.decimal = complexInit ? valueOrParams.decimal : undefined;
|
|
46
|
+
|
|
47
|
+
// use the multiplier to keep track of decimal point - defaults to 8 if lower than 8
|
|
48
|
+
this.decimalMultiplier = toMultiplier(
|
|
49
|
+
Math.max(this.#getFloatDecimals(this.#toSafeValue(value)), this.decimal || 0),
|
|
50
|
+
);
|
|
51
|
+
this.#setValue(value);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get unsafeNumber() {
|
|
55
|
+
return parseFloat((this.bigIntValue / this.decimalMultiplier).toString());
|
|
56
|
+
}
|
|
57
|
+
get baseValue() {
|
|
58
|
+
return this.#getBaseValue('string') as string;
|
|
59
|
+
}
|
|
60
|
+
get baseValueNumber() {
|
|
61
|
+
return this.#getBaseValue('number') as number;
|
|
62
|
+
}
|
|
63
|
+
get baseValueBigInt() {
|
|
64
|
+
return this.#getBaseValue('number') as bigint;
|
|
65
|
+
}
|
|
66
|
+
get value() {
|
|
67
|
+
return this.formatBigIntToSafeValue(
|
|
68
|
+
this.bigIntValue,
|
|
69
|
+
this.decimal || decimalFromMultiplier(this.decimalMultiplier),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
add(...args: (BigIntArithmetics | string | number)[]) {
|
|
74
|
+
return this.#arithmetics('add', ...args);
|
|
75
|
+
}
|
|
76
|
+
sub(...args: (BigIntArithmetics | string | number)[]) {
|
|
77
|
+
return this.#arithmetics('sub', ...args);
|
|
78
|
+
}
|
|
79
|
+
mul(...args: (BigIntArithmetics | string | number)[]) {
|
|
80
|
+
return this.#arithmetics('mul', ...args);
|
|
81
|
+
}
|
|
82
|
+
div(...args: (BigIntArithmetics | string | number)[]) {
|
|
83
|
+
return this.#arithmetics('div', ...args);
|
|
84
|
+
}
|
|
85
|
+
gt(value: BigIntArithmetics | string | number) {
|
|
86
|
+
return this.bigIntValue > this.getBigIntValue(value);
|
|
87
|
+
}
|
|
88
|
+
gte(value: BigIntArithmetics | string | number) {
|
|
89
|
+
return this.bigIntValue >= this.getBigIntValue(value);
|
|
90
|
+
}
|
|
91
|
+
lt(value: BigIntArithmetics | string | number) {
|
|
92
|
+
return this.bigIntValue < this.getBigIntValue(value);
|
|
93
|
+
}
|
|
94
|
+
lte(value: BigIntArithmetics | string | number) {
|
|
95
|
+
return this.bigIntValue <= this.getBigIntValue(value);
|
|
96
|
+
}
|
|
97
|
+
eqValue(value: BigIntArithmetics | string | number) {
|
|
98
|
+
return this.bigIntValue === this.getBigIntValue(value);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
getBigIntValue(value: BigIntArithmetics | string | number, decimal?: number) {
|
|
102
|
+
if (!decimal && typeof value === 'object') return value.bigIntValue;
|
|
103
|
+
|
|
104
|
+
value = typeof value === 'object' ? value.value : value;
|
|
105
|
+
return this.#toBigInt(this.#toSafeValue(value), decimal);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
formatBigIntToSafeValue(value: bigint, decimal?: number) {
|
|
109
|
+
const bigIntDecimal = decimal || this.decimal || DEFAULT_DECIMAL;
|
|
110
|
+
const decimalToUseForConversion = Math.max(
|
|
111
|
+
bigIntDecimal,
|
|
112
|
+
decimalFromMultiplier(this.decimalMultiplier),
|
|
113
|
+
);
|
|
114
|
+
const isNegative = value < 0n;
|
|
115
|
+
|
|
116
|
+
let valueString = value.toString().substring(isNegative ? 1 : 0);
|
|
117
|
+
|
|
118
|
+
const padLength = decimalToUseForConversion - (valueString.length - 1);
|
|
119
|
+
|
|
120
|
+
if (padLength > 0) {
|
|
121
|
+
valueString = '0'.repeat(padLength) + valueString;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const decimalIndex = valueString.length - decimalToUseForConversion;
|
|
125
|
+
let decimalString = valueString.slice(-decimalToUseForConversion);
|
|
126
|
+
|
|
127
|
+
// Check if we need to round up
|
|
128
|
+
if (parseInt(decimalString[bigIntDecimal]) >= 5) {
|
|
129
|
+
// Increment the last decimal place and slice off the rest
|
|
130
|
+
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(
|
|
131
|
+
parseInt(decimalString[bigIntDecimal - 1]) + 1
|
|
132
|
+
).toString()}`;
|
|
133
|
+
} else {
|
|
134
|
+
// Just slice off the extra digits
|
|
135
|
+
decimalString = decimalString.substring(0, bigIntDecimal);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return `${isNegative ? '-' : ''}${valueString.slice(0, decimalIndex)}.${decimalString}`.replace(
|
|
139
|
+
/\.?0*$/,
|
|
140
|
+
'',
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
toSignificant(significantDigits?: number) {
|
|
145
|
+
const value = this.value.split('.');
|
|
146
|
+
const integer = value[0];
|
|
147
|
+
const decimal = value[1];
|
|
148
|
+
|
|
149
|
+
if (decimal) {
|
|
150
|
+
return `${integer}.${decimal.slice(0, significantDigits || this.decimal)}`.replace(
|
|
151
|
+
/\.?0*$/,
|
|
152
|
+
'',
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return integer;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
#arithmetics(method: ArithmeticMethod, ...args: (BigIntArithmetics | string | number)[]): this {
|
|
160
|
+
const precisionDecimal = this.#retrievePrecisionDecimal(this, ...args);
|
|
161
|
+
const precisionDecimalMultiplier = toMultiplier(precisionDecimal);
|
|
162
|
+
|
|
163
|
+
const result = args.reduce(
|
|
164
|
+
(acc, arg) => {
|
|
165
|
+
const value = this.getBigIntValue(arg, precisionDecimal);
|
|
166
|
+
|
|
167
|
+
if ('div' === method && value === 0n) throw new RangeError('Division by zero');
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Normal arithmetic - add & sub => 200000000n +- 200000000n
|
|
171
|
+
*/
|
|
172
|
+
if ('add' === method) return acc + value;
|
|
173
|
+
if ('sub' === method) return acc - value;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Multiplication & division would end up with wrong result if we don't adjust the value
|
|
177
|
+
* 200000000n * 200000000n => 40000000000000000n
|
|
178
|
+
* 200000000n / 200000000n => 1n
|
|
179
|
+
* So we do the following:
|
|
180
|
+
* 200000000n * 200000000n = 40000000000000000n / 100000000n (decimals) => 400000000n
|
|
181
|
+
* (200000000n * 100000000n (decimals)) / 200000000n => 100000000n
|
|
182
|
+
*/
|
|
183
|
+
if ('mul' === method) return (acc * value) / precisionDecimalMultiplier;
|
|
184
|
+
// 'div' === method
|
|
185
|
+
return (acc * precisionDecimalMultiplier) / value;
|
|
186
|
+
},
|
|
187
|
+
//normalize is to precision multiplier base
|
|
188
|
+
(this.bigIntValue * precisionDecimalMultiplier) / this.decimalMultiplier,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const value = formatBigIntToSafeValue({
|
|
192
|
+
bigIntDecimal: precisionDecimal,
|
|
193
|
+
decimal: Math.max(precisionDecimal, decimalFromMultiplier(this.decimalMultiplier)),
|
|
194
|
+
value: result,
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// @ts-expect-error - we know that the constructor is the same as the current class
|
|
198
|
+
return new this.constructor({ decimal: this.decimal, value, identifier: this.toString() });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
#setValue(value: AllowedValueType, bigIntValue?: bigint) {
|
|
202
|
+
const safeValue = this.#toSafeValue(value) || '0';
|
|
203
|
+
this.bigIntValue = bigIntValue || this.#toBigInt(safeValue);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
#retrievePrecisionDecimal(...args: (BigIntArithmetics | AllowedValueType)[]) {
|
|
207
|
+
const decimals = args
|
|
208
|
+
.map((arg) =>
|
|
209
|
+
typeof arg === 'object'
|
|
210
|
+
? arg.decimal || decimalFromMultiplier(arg.decimalMultiplier)
|
|
211
|
+
: this.#getFloatDecimals(this.#toSafeValue(arg)),
|
|
212
|
+
)
|
|
213
|
+
.filter(Boolean) as number[];
|
|
214
|
+
return Math.max(...decimals, DEFAULT_DECIMAL);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
#toBigInt(value: string, decimal?: number) {
|
|
218
|
+
const multiplier = decimal ? toMultiplier(decimal) : this.decimalMultiplier;
|
|
219
|
+
const padDecimal = decimalFromMultiplier(multiplier);
|
|
220
|
+
const [integerPart, decimalPart = ''] = value.split('.');
|
|
221
|
+
|
|
222
|
+
return BigInt(`${integerPart}${decimalPart.padEnd(padDecimal, '0')}`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
#toSafeValue(value: AllowedValueType) {
|
|
226
|
+
const parsedValue =
|
|
227
|
+
typeof value === 'number'
|
|
228
|
+
? Number(value).toLocaleString('fullwide', {
|
|
229
|
+
useGrouping: false,
|
|
230
|
+
maximumFractionDigits: 20,
|
|
231
|
+
})
|
|
232
|
+
: value;
|
|
233
|
+
|
|
234
|
+
const splitValue = `${parsedValue}`.replaceAll(',', '.').split('.');
|
|
235
|
+
|
|
236
|
+
return splitValue.length > 1
|
|
237
|
+
? `${splitValue.slice(0, -1).join('')}.${splitValue.at(-1)}`
|
|
238
|
+
: splitValue[0];
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
#getFloatDecimals(value: string) {
|
|
242
|
+
const decimals = value.split('.')[1]?.length || 0;
|
|
243
|
+
return Math.max(decimals, DEFAULT_DECIMAL);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
#getBaseValue<T extends 'number' | 'string' | 'bigint'>(type: T) {
|
|
247
|
+
const divisor = this.decimalMultiplier / toMultiplier(this.decimal || 0);
|
|
248
|
+
const baseValue = this.bigIntValue / divisor;
|
|
249
|
+
|
|
250
|
+
switch (type) {
|
|
251
|
+
case 'number':
|
|
252
|
+
return Number(baseValue);
|
|
253
|
+
case 'string':
|
|
254
|
+
return baseValue.toString();
|
|
255
|
+
default:
|
|
256
|
+
return baseValue;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const errorMessages = {
|
|
2
|
+
/**
|
|
3
|
+
* Core
|
|
4
|
+
*/
|
|
5
|
+
core_wallet_connection_not_found: 10001,
|
|
6
|
+
core_estimated_max_spendable_chain_not_supported: 10002,
|
|
7
|
+
core_extend_error: 10003,
|
|
8
|
+
core_inbound_data_not_found: 10004,
|
|
9
|
+
core_approve_asset_address_or_from_not_found: 10005,
|
|
10
|
+
core_chain_halted: 10099,
|
|
11
|
+
/**
|
|
12
|
+
* Core - Wallet Connection
|
|
13
|
+
*/
|
|
14
|
+
core_wallet_xdefi_not_installed: 10101,
|
|
15
|
+
core_wallet_evmwallet_not_installed: 10102,
|
|
16
|
+
core_wallet_walletconnect_not_installed: 10103,
|
|
17
|
+
core_wallet_keystore_not_installed: 10104,
|
|
18
|
+
core_wallet_ledger_not_installed: 10105,
|
|
19
|
+
core_wallet_trezor_not_installed: 10106,
|
|
20
|
+
core_wallet_keplr_not_installed: 10107,
|
|
21
|
+
core_wallet_okx_not_installed: 10108,
|
|
22
|
+
/**
|
|
23
|
+
* Core - Swap
|
|
24
|
+
*/
|
|
25
|
+
core_swap_invalid_params: 10200,
|
|
26
|
+
core_swap_route_not_complete: 10201,
|
|
27
|
+
core_swap_asset_not_recognized: 10202,
|
|
28
|
+
core_swap_contract_not_found: 10203,
|
|
29
|
+
core_swap_route_transaction_not_found: 10204,
|
|
30
|
+
core_swap_contract_not_supported: 10205,
|
|
31
|
+
core_swap_transaction_error: 10206,
|
|
32
|
+
core_swap_quote_mode_not_supported: 10207,
|
|
33
|
+
/**
|
|
34
|
+
* Core - Transaction
|
|
35
|
+
*/
|
|
36
|
+
core_transaction_deposit_error: 10301,
|
|
37
|
+
core_transaction_create_liquidity_rune_error: 10302,
|
|
38
|
+
core_transaction_create_liquidity_asset_error: 10303,
|
|
39
|
+
core_transaction_create_liquidity_invalid_params: 10304,
|
|
40
|
+
core_transaction_add_liquidity_invalid_params: 10305,
|
|
41
|
+
core_transaction_add_liquidity_no_rune_address: 10306,
|
|
42
|
+
core_transaction_add_liquidity_rune_error: 10307,
|
|
43
|
+
core_transaction_add_liquidity_asset_error: 10308,
|
|
44
|
+
core_transaction_withdraw_error: 10309,
|
|
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_deposit_server_error: 10313,
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Wallets
|
|
52
|
+
*/
|
|
53
|
+
wallet_ledger_connection_error: 20001,
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Helpers
|
|
57
|
+
*/
|
|
58
|
+
helpers_number_different_decimals: 99101,
|
|
59
|
+
} as const;
|
|
60
|
+
|
|
61
|
+
export type Keys = keyof typeof errorMessages;
|
|
62
|
+
|
|
63
|
+
export class SwapKitError extends Error {
|
|
64
|
+
constructor(errorKey: Keys, sourceError?: any) {
|
|
65
|
+
console.error(sourceError);
|
|
66
|
+
|
|
67
|
+
super(errorKey, { cause: { code: errorMessages[errorKey], message: errorKey } });
|
|
68
|
+
Object.setPrototypeOf(this, SwapKitError.prototype);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BigIntArithmetics } from './bigIntArithmetics.ts';
|
|
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
|
+
toString() {
|
|
11
|
+
return this.value;
|
|
12
|
+
}
|
|
13
|
+
}
|