@swapkit/helpers 1.0.0-rc.7 → 1.0.0-rc.71
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 +2 -1
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +96 -73
- package/dist/index.es.js +1015 -502
- package/dist/index.es.js.map +1 -0
- package/package.json +16 -13
- package/src/helpers/__tests__/memo.test.ts +3 -3
- package/src/helpers/asset.ts +56 -56
- package/src/helpers/liquidity.ts +46 -38
- package/src/helpers/memo.ts +19 -16
- package/src/helpers/others.ts +6 -54
- package/src/helpers/validators.ts +4 -3
- package/src/index.ts +2 -0
- package/src/modules/__tests__/assetValue.test.ts +232 -38
- package/src/modules/__tests__/bigIntArithmetics.test.ts +30 -0
- package/src/modules/__tests__/swapKitNumber.test.ts +174 -48
- package/src/modules/assetValue.ts +203 -141
- package/src/modules/bigIntArithmetics.ts +185 -116
- package/src/modules/swapKitError.ts +13 -4
- package/src/modules/swapKitNumber.ts +8 -1
- package/src/types.ts +30 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { Chain } from '@swapkit/types';
|
|
2
|
+
import type { ChainflipList } from '@swapkit/tokens';
|
|
3
|
+
import { ChainId } from '@swapkit/types';
|
|
2
4
|
import type { CoinGeckoList } from '@swapkit/tokens';
|
|
3
5
|
import { FeeOption } from '@swapkit/types';
|
|
4
6
|
import type { MayaList } from '@swapkit/tokens';
|
|
@@ -13,6 +15,8 @@ import type { TraderjoeList } from '@swapkit/tokens';
|
|
|
13
15
|
import type { UniswapList } from '@swapkit/tokens';
|
|
14
16
|
import type { WoofiList } from '@swapkit/tokens';
|
|
15
17
|
|
|
18
|
+
declare type AllowedNumberTypes = 'bigint' | 'number' | 'string';
|
|
19
|
+
|
|
16
20
|
export declare const assetFromString: (assetString: string) => {
|
|
17
21
|
chain: Chain;
|
|
18
22
|
symbol: string;
|
|
@@ -23,25 +27,40 @@ export declare const assetFromString: (assetString: string) => {
|
|
|
23
27
|
export declare class AssetValue extends BigIntArithmetics {
|
|
24
28
|
address?: string;
|
|
25
29
|
chain: Chain;
|
|
26
|
-
isSynthetic: boolean;
|
|
27
30
|
isGasAsset: boolean;
|
|
31
|
+
isSynthetic: boolean;
|
|
28
32
|
symbol: string;
|
|
33
|
+
tax?: TokenTax;
|
|
29
34
|
ticker: string;
|
|
30
35
|
type: ReturnType<typeof getAssetType>;
|
|
31
|
-
constructor(
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
constructor({ value, decimal, tax, chain, symbol, identifier, }: {
|
|
37
|
+
decimal: number;
|
|
38
|
+
value: SwapKitValueType;
|
|
39
|
+
tax?: TokenTax;
|
|
40
|
+
} & ({
|
|
41
|
+
chain: Chain;
|
|
42
|
+
symbol: string;
|
|
43
|
+
identifier?: never;
|
|
44
|
+
} | {
|
|
45
|
+
identifier: string;
|
|
46
|
+
chain?: never;
|
|
47
|
+
symbol?: never;
|
|
48
|
+
}));
|
|
49
|
+
toString(): string;
|
|
50
|
+
toUrl(): string;
|
|
34
51
|
eq({ chain, symbol }: {
|
|
35
52
|
chain: Chain;
|
|
36
53
|
symbol: string;
|
|
37
54
|
}): boolean;
|
|
38
|
-
|
|
39
|
-
static
|
|
40
|
-
static
|
|
41
|
-
static
|
|
42
|
-
static
|
|
43
|
-
static
|
|
44
|
-
static
|
|
55
|
+
chainId(): ChainId;
|
|
56
|
+
static fromUrl(urlAsset: string, value?: NumberPrimitives): Promise<AssetValue>;
|
|
57
|
+
static fromString(assetString: string, value?: NumberPrimitives): Promise<AssetValue>;
|
|
58
|
+
static fromIdentifier(assetString: `${Chain}.${string}` | `${Chain}/${string}` | `${Chain}.${string}-${string}` | TokenNames, value?: NumberPrimitives): Promise<AssetValue>;
|
|
59
|
+
static fromStringSync(assetString: string, value?: NumberPrimitives): AssetValue;
|
|
60
|
+
static fromStringWithBase(assetString: string, value?: NumberPrimitives, baseDecimal?: number): Promise<AssetValue>;
|
|
61
|
+
static fromStringWithBaseSync(assetString: string, value?: NumberPrimitives, baseDecimal?: number): AssetValue;
|
|
62
|
+
static fromIdentifierSync(assetString: TokenNames, value?: NumberPrimitives): AssetValue;
|
|
63
|
+
static fromChainOrSignature(assetString: CommonAssetString, value?: NumberPrimitives): AssetValue;
|
|
45
64
|
static loadStaticAssets(): Promise<{
|
|
46
65
|
ok: true;
|
|
47
66
|
} | {
|
|
@@ -51,16 +70,6 @@ export declare class AssetValue extends BigIntArithmetics {
|
|
|
51
70
|
}>;
|
|
52
71
|
}
|
|
53
72
|
|
|
54
|
-
declare type AssetValueParams = {
|
|
55
|
-
decimal: number;
|
|
56
|
-
value: SwapKitValueType;
|
|
57
|
-
} & ({
|
|
58
|
-
chain: Chain;
|
|
59
|
-
symbol: string;
|
|
60
|
-
} | {
|
|
61
|
-
identifier: string;
|
|
62
|
-
});
|
|
63
|
-
|
|
64
73
|
export declare class BigIntArithmetics {
|
|
65
74
|
|
|
66
75
|
decimalMultiplier: bigint;
|
|
@@ -68,24 +77,12 @@ export declare class BigIntArithmetics {
|
|
|
68
77
|
decimal?: number;
|
|
69
78
|
static fromBigInt(value: bigint, decimal?: number): BigIntArithmetics;
|
|
70
79
|
static shiftDecimals({ value, from, to, }: {
|
|
71
|
-
value:
|
|
80
|
+
value: InstanceType<typeof SwapKitNumber>;
|
|
72
81
|
from: number;
|
|
73
82
|
to: number;
|
|
74
83
|
}): BigIntArithmetics;
|
|
75
84
|
constructor(params: SKBigIntParams);
|
|
76
|
-
|
|
77
|
-
* @deprecated Use `getBaseValue('string')` instead
|
|
78
|
-
*/
|
|
79
|
-
get baseValue(): string;
|
|
80
|
-
/**
|
|
81
|
-
* @deprecated Use `getBaseValue('number')` instead
|
|
82
|
-
*/
|
|
83
|
-
get baseValueNumber(): number;
|
|
84
|
-
/**
|
|
85
|
-
* @deprecated Use `getBaseValue('bigint')` instead
|
|
86
|
-
*/
|
|
87
|
-
get baseValueBigInt(): bigint;
|
|
88
|
-
set(value: SKBigIntParams): any;
|
|
85
|
+
set(value: SKBigIntParams): this;
|
|
89
86
|
add(...args: InitialisationValueType[]): this;
|
|
90
87
|
sub(...args: InitialisationValueType[]): this;
|
|
91
88
|
mul(...args: InitialisationValueType[]): this;
|
|
@@ -95,16 +92,26 @@ export declare class BigIntArithmetics {
|
|
|
95
92
|
lt(value: InitialisationValueType): boolean;
|
|
96
93
|
lte(value: InitialisationValueType): boolean;
|
|
97
94
|
eqValue(value: InitialisationValueType): boolean;
|
|
98
|
-
getValue<T extends
|
|
99
|
-
getBaseValue<T extends
|
|
95
|
+
getValue<T extends AllowedNumberTypes>(type: T): NumberPrimitivesType[T];
|
|
96
|
+
getBaseValue<T extends AllowedNumberTypes>(type: T): NumberPrimitivesType[T];
|
|
100
97
|
getBigIntValue(value: InitialisationValueType, decimal?: number): bigint;
|
|
101
|
-
formatBigIntToSafeValue(value: bigint, decimal?: number): string;
|
|
102
98
|
toSignificant(significantDigits?: number): string;
|
|
99
|
+
toFixed(fixedDigits?: number): string;
|
|
100
|
+
toAbbreviation(digits?: number): string;
|
|
101
|
+
toCurrency(currency?: string, { currencyPosition, decimal, decimalSeparator, thousandSeparator, }?: {
|
|
102
|
+
currencyPosition?: string | undefined;
|
|
103
|
+
decimal?: number | undefined;
|
|
104
|
+
decimalSeparator?: string | undefined;
|
|
105
|
+
thousandSeparator?: string | undefined;
|
|
106
|
+
}): string;
|
|
107
|
+
formatBigIntToSafeValue(value: bigint, decimal?: number): string;
|
|
103
108
|
}
|
|
104
109
|
|
|
105
|
-
export declare type CommonAssetString =
|
|
110
|
+
export declare type CommonAssetString = `${Chain.Maya}.MAYA` | `${Chain.Ethereum}.THOR` | `${Chain.Ethereum}.vTHOR` | `${Chain.Kujira}.USK` | Chain;
|
|
111
|
+
|
|
112
|
+
export declare function derivationPathToString([network, chainId, account, change, index]: number[]): string;
|
|
106
113
|
|
|
107
|
-
export declare
|
|
114
|
+
export declare type ErrorKeys = keyof typeof errorMessages;
|
|
108
115
|
|
|
109
116
|
declare const errorMessages: {
|
|
110
117
|
/**
|
|
@@ -127,6 +134,7 @@ declare const errorMessages: {
|
|
|
127
134
|
readonly core_wallet_trezor_not_installed: 10106;
|
|
128
135
|
readonly core_wallet_keplr_not_installed: 10107;
|
|
129
136
|
readonly core_wallet_okx_not_installed: 10108;
|
|
137
|
+
readonly core_wallet_keepkey_not_installed: 10109;
|
|
130
138
|
/**
|
|
131
139
|
* Core - Swap
|
|
132
140
|
*/
|
|
@@ -153,18 +161,34 @@ declare const errorMessages: {
|
|
|
153
161
|
readonly core_transaction_deposit_to_pool_error: 10310;
|
|
154
162
|
readonly core_transaction_deposit_insufficient_funds_error: 10311;
|
|
155
163
|
readonly core_transaction_deposit_gas_error: 10312;
|
|
156
|
-
readonly
|
|
164
|
+
readonly core_transaction_invalid_sender_address: 10313;
|
|
165
|
+
readonly core_transaction_deposit_server_error: 10314;
|
|
166
|
+
readonly core_transaction_user_rejected: 10315;
|
|
157
167
|
/**
|
|
158
168
|
* Wallets
|
|
159
169
|
*/
|
|
160
170
|
readonly wallet_ledger_connection_error: 20001;
|
|
171
|
+
readonly wallet_ledger_connection_claimed: 20002;
|
|
172
|
+
readonly wallet_ledger_get_address_error: 20003;
|
|
173
|
+
readonly wallet_ledger_device_not_found: 20004;
|
|
174
|
+
readonly wallet_ledger_device_locked: 20005;
|
|
161
175
|
/**
|
|
162
176
|
* Helpers
|
|
163
177
|
*/
|
|
164
178
|
readonly helpers_number_different_decimals: 99101;
|
|
165
179
|
};
|
|
166
180
|
|
|
167
|
-
export declare const filterAssets: (
|
|
181
|
+
export declare const filterAssets: (tokens: {
|
|
182
|
+
value: string;
|
|
183
|
+
decimal: number;
|
|
184
|
+
chain: Chain;
|
|
185
|
+
symbol: string;
|
|
186
|
+
}[]) => {
|
|
187
|
+
value: string;
|
|
188
|
+
decimal: number;
|
|
189
|
+
chain: Chain;
|
|
190
|
+
symbol: string;
|
|
191
|
+
}[];
|
|
168
192
|
|
|
169
193
|
export declare function formatBigIntToSafeValue({ value, bigIntDecimal, decimal, }: {
|
|
170
194
|
value: bigint;
|
|
@@ -179,14 +203,14 @@ export declare const getAssetType: ({ chain, symbol }: {
|
|
|
179
203
|
symbol: string;
|
|
180
204
|
}) => Chain.Avalanche | Chain.Cosmos | Chain.Kujira | "Synth" | "Native" | "BEP2" | "BEP20" | "ERC20" | "POLYGON" | "ARBITRUM" | "OPTIMISM";
|
|
181
205
|
|
|
182
|
-
export declare
|
|
206
|
+
export declare function getAsymmetricAssetShare({ liquidityUnits, poolUnits, assetDepth, }: ShareParams<{
|
|
183
207
|
assetDepth: string;
|
|
184
|
-
}>)
|
|
208
|
+
}>): SwapKitNumber;
|
|
185
209
|
|
|
186
|
-
export declare
|
|
210
|
+
export declare function getAsymmetricAssetWithdrawAmount({ percent, assetDepth, liquidityUnits, poolUnits, }: ShareParams<{
|
|
187
211
|
percent: number;
|
|
188
212
|
assetDepth: string;
|
|
189
|
-
}>)
|
|
213
|
+
}>): SwapKitNumber;
|
|
190
214
|
|
|
191
215
|
/**
|
|
192
216
|
* Ref: https://gitlab.com/thorchain/thornode/-/issues/657
|
|
@@ -199,14 +223,14 @@ export declare const getAsymmetricAssetWithdrawAmount: ({ percent, assetDepth, l
|
|
|
199
223
|
* share = (s * A * (2 * T^2 - 2 * T * s + s^2))/T^3
|
|
200
224
|
* (part1 * (part2 - part3 + part4)) / part5
|
|
201
225
|
*/
|
|
202
|
-
export declare
|
|
226
|
+
export declare function getAsymmetricRuneShare({ liquidityUnits, poolUnits, runeDepth, }: ShareParams<{
|
|
203
227
|
runeDepth: string;
|
|
204
|
-
}>)
|
|
228
|
+
}>): SwapKitNumber;
|
|
205
229
|
|
|
206
|
-
export declare
|
|
230
|
+
export declare function getAsymmetricRuneWithdrawAmount({ percent, runeDepth, liquidityUnits, poolUnits, }: ShareParams<{
|
|
207
231
|
percent: number;
|
|
208
232
|
runeDepth: string;
|
|
209
|
-
}>)
|
|
233
|
+
}>): SwapKitNumber;
|
|
210
234
|
|
|
211
235
|
export declare const getCommonAssetInfo: (assetString: CommonAssetString) => {
|
|
212
236
|
identifier: string;
|
|
@@ -218,40 +242,36 @@ export declare const getDecimal: ({ chain, symbol }: {
|
|
|
218
242
|
symbol: string;
|
|
219
243
|
}) => Promise<number>;
|
|
220
244
|
|
|
221
|
-
export declare
|
|
245
|
+
export declare function getEstimatedPoolShare({ runeDepth, poolUnits, assetDepth, liquidityUnits, runeAmount, assetAmount, }: ShareParams<{
|
|
222
246
|
runeAmount: string;
|
|
223
247
|
assetAmount: string;
|
|
224
248
|
runeDepth: string;
|
|
225
249
|
assetDepth: string;
|
|
226
|
-
}>)
|
|
250
|
+
}>): number;
|
|
227
251
|
|
|
228
|
-
export declare
|
|
252
|
+
export declare function getLiquiditySlippage({ runeAmount, assetAmount, runeDepth, assetDepth, }: PoolParams): number;
|
|
229
253
|
|
|
230
254
|
export declare const getMemoFor: <T extends MemoType>(memoType: T, options: MemoOptions<T>) => string;
|
|
231
255
|
|
|
232
|
-
export declare
|
|
256
|
+
export declare function getMinAmountByChain(chain: Chain): AssetValue;
|
|
233
257
|
|
|
234
|
-
export declare
|
|
235
|
-
[x: string]: any;
|
|
236
|
-
} | undefined) => Promise<T>;
|
|
237
|
-
|
|
238
|
-
export declare const getSymmetricPoolShare: ({ liquidityUnits, poolUnits, runeDepth, assetDepth, }: ShareParams<{
|
|
258
|
+
export declare function getSymmetricPoolShare({ liquidityUnits, poolUnits, runeDepth, assetDepth, }: ShareParams<{
|
|
239
259
|
runeDepth: string;
|
|
240
260
|
assetDepth: string;
|
|
241
|
-
}>)
|
|
261
|
+
}>): {
|
|
242
262
|
assetAmount: SwapKitNumber;
|
|
243
263
|
runeAmount: SwapKitNumber;
|
|
244
264
|
};
|
|
245
265
|
|
|
246
|
-
export declare
|
|
266
|
+
export declare function getSymmetricWithdraw({ liquidityUnits, poolUnits, runeDepth, assetDepth, percent, }: ShareParams<{
|
|
247
267
|
runeDepth: string;
|
|
248
268
|
assetDepth: string;
|
|
249
269
|
percent: number;
|
|
250
|
-
}>)
|
|
270
|
+
}>): {
|
|
251
271
|
[k: string]: SwapKitNumber;
|
|
252
272
|
};
|
|
253
273
|
|
|
254
|
-
export declare
|
|
274
|
+
export declare function getTHORNameCost(year: number): number;
|
|
255
275
|
|
|
256
276
|
declare type InitialisationValueType = NumberPrimitives | BigIntArithmetics | SwapKitNumber;
|
|
257
277
|
|
|
@@ -260,8 +280,6 @@ export declare const isGasAsset: ({ chain, symbol }: {
|
|
|
260
280
|
symbol: string;
|
|
261
281
|
}) => boolean;
|
|
262
282
|
|
|
263
|
-
export declare type Keys = keyof typeof errorMessages;
|
|
264
|
-
|
|
265
283
|
export declare type MemoOptions<T extends MemoType> = {
|
|
266
284
|
[MemoType.BOND]: WithAddress;
|
|
267
285
|
[MemoType.LEAVE]: WithAddress;
|
|
@@ -291,7 +309,7 @@ export declare type MemoOptions<T extends MemoType> = {
|
|
|
291
309
|
[MemoType.THORNAME_REGISTER]: Omit<ThornameRegisterParam, 'preferredAsset' | 'expiryBlock'>;
|
|
292
310
|
}[T];
|
|
293
311
|
|
|
294
|
-
declare type NumberPrimitives = bigint | number | string;
|
|
312
|
+
export declare type NumberPrimitives = bigint | number | string;
|
|
295
313
|
|
|
296
314
|
declare type NumberPrimitivesType = {
|
|
297
315
|
bigint: bigint;
|
|
@@ -306,8 +324,6 @@ declare type PoolParams<T = {}> = T & {
|
|
|
306
324
|
assetDepth: string;
|
|
307
325
|
};
|
|
308
326
|
|
|
309
|
-
export declare const postRequest: <T>(url: string, body: string, headers?: Record<string, string>, parseAsString?: boolean) => Promise<T>;
|
|
310
|
-
|
|
311
327
|
declare type ShareParams<T = {}> = T & {
|
|
312
328
|
liquidityUnits: string;
|
|
313
329
|
poolUnits: string;
|
|
@@ -319,17 +335,16 @@ declare type SKBigIntParams = InitialisationValueType | {
|
|
|
319
335
|
};
|
|
320
336
|
|
|
321
337
|
export declare class SwapKitError extends Error {
|
|
322
|
-
constructor(errorKey:
|
|
338
|
+
constructor(errorKey: ErrorKeys, sourceError?: any);
|
|
323
339
|
}
|
|
324
340
|
|
|
325
341
|
export declare class SwapKitNumber extends BigIntArithmetics {
|
|
326
342
|
eq(value: SwapKitValueType): boolean;
|
|
343
|
+
static fromBigInt(value: bigint, decimal?: number): SwapKitNumber;
|
|
327
344
|
}
|
|
328
345
|
|
|
329
346
|
export declare type SwapKitValueType = BigIntArithmetics | string | number;
|
|
330
347
|
|
|
331
|
-
declare type TCTokenNames = (typeof ThorchainList)['tokens'][number]['identifier'];
|
|
332
|
-
|
|
333
348
|
export declare type ThornameRegisterParam = {
|
|
334
349
|
name: string;
|
|
335
350
|
chain: string;
|
|
@@ -339,9 +354,14 @@ export declare type ThornameRegisterParam = {
|
|
|
339
354
|
expiryBlock?: string;
|
|
340
355
|
};
|
|
341
356
|
|
|
342
|
-
declare type TokenNames =
|
|
357
|
+
declare type TokenNames = (typeof ThorchainList)['tokens'][number]['identifier'] | (typeof CoinGeckoList)['tokens'][number]['identifier'] | (typeof MayaList)['tokens'][number]['identifier'] | (typeof PancakeswapETHList)['tokens'][number]['identifier'] | (typeof PancakeswapList)['tokens'][number]['identifier'] | (typeof PangolinList)['tokens'][number]['identifier'] | (typeof StargateARBList)['tokens'][number]['identifier'] | (typeof SushiswapList)['tokens'][number]['identifier'] | (typeof TraderjoeList)['tokens'][number]['identifier'] | (typeof WoofiList)['tokens'][number]['identifier'] | (typeof UniswapList)['tokens'][number]['identifier'] | (typeof ChainflipList)['tokens'][number]['identifier'];
|
|
343
358
|
|
|
344
|
-
|
|
359
|
+
declare type TokenTax = {
|
|
360
|
+
buy: number;
|
|
361
|
+
sell: number;
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
export declare function validateTHORName(name: string): boolean;
|
|
345
365
|
|
|
346
366
|
declare type WithAddress<T = {}> = T & {
|
|
347
367
|
address: string;
|
|
@@ -351,4 +371,7 @@ declare type WithChain<T = {}> = T & {
|
|
|
351
371
|
chain: Chain;
|
|
352
372
|
};
|
|
353
373
|
|
|
374
|
+
|
|
375
|
+
export * from "@swapkit/api";
|
|
376
|
+
|
|
354
377
|
export { }
|