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