@swapkit/helpers 1.0.0-rc.118 → 1.0.0-rc.119
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.js +2 -2
- package/dist/index.js.map +10 -10
- package/package.json +3 -3
- package/src/helpers/__tests__/asset.test.ts +0 -7
- package/src/helpers/asset.ts +28 -27
- package/src/modules/__tests__/assetValue.test.ts +116 -117
- package/src/modules/assetValue.ts +119 -94
- package/src/types/chains.ts +2 -18
- package/src/types/derivationPath.ts +0 -2
- package/src/types/network.ts +0 -2
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
type CommonAssetString,
|
|
3
|
+
CommonAssetStrings,
|
|
4
|
+
getAssetType,
|
|
5
|
+
getCommonAssetInfo,
|
|
6
|
+
getDecimal,
|
|
7
|
+
isGasAsset,
|
|
8
|
+
} from "../helpers/asset.ts";
|
|
3
9
|
import { validateIdentifier } from "../helpers/validators.ts";
|
|
4
10
|
import { BaseDecimal, Chain, type ChainId, ChainToChainId } from "../types/chains.ts";
|
|
5
11
|
import type { TokenNames, TokenTax } from "../types/tokens.ts";
|
|
@@ -7,13 +13,28 @@ import type { TokenNames, TokenTax } from "../types/tokens.ts";
|
|
|
7
13
|
import type { NumberPrimitives } from "./bigIntArithmetics.ts";
|
|
8
14
|
import { BigIntArithmetics, formatBigIntToSafeValue } from "./bigIntArithmetics.ts";
|
|
9
15
|
import { SwapKitError } from "./swapKitError.ts";
|
|
10
|
-
import {
|
|
16
|
+
import type { SwapKitValueType } from "./swapKitNumber.ts";
|
|
11
17
|
|
|
12
18
|
const staticTokensMap = new Map<
|
|
13
19
|
TokenNames,
|
|
14
20
|
{ tax?: TokenTax; decimal: number; identifier: string }
|
|
15
21
|
>();
|
|
16
22
|
|
|
23
|
+
type ConditionalAssetValueReturn<T extends { asyncTokenLookup?: boolean }> =
|
|
24
|
+
T["asyncTokenLookup"] extends true ? Promise<AssetValue> : AssetValue;
|
|
25
|
+
|
|
26
|
+
type AssetIdentifier =
|
|
27
|
+
| { asset: CommonAssetString }
|
|
28
|
+
| { asset: TokenNames }
|
|
29
|
+
| { asset: string }
|
|
30
|
+
| { chain: Chain };
|
|
31
|
+
|
|
32
|
+
type AssetValueFromParams = AssetIdentifier & {
|
|
33
|
+
value?: NumberPrimitives | SwapKitValueType;
|
|
34
|
+
fromBaseDecimal?: number;
|
|
35
|
+
asyncTokenLookup?: boolean;
|
|
36
|
+
};
|
|
37
|
+
|
|
17
38
|
export class AssetValue extends BigIntArithmetics {
|
|
18
39
|
address?: string;
|
|
19
40
|
chain: Chain;
|
|
@@ -79,108 +100,51 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
79
100
|
});
|
|
80
101
|
}
|
|
81
102
|
|
|
82
|
-
const
|
|
83
|
-
chain === Chain.THORChain && symbol ? `${chain}.${ticker}/${symbol}` : urlAsset;
|
|
103
|
+
const asset = chain === Chain.THORChain && symbol ? `${chain}.${ticker}/${symbol}` : urlAsset;
|
|
84
104
|
|
|
85
|
-
return
|
|
105
|
+
return AssetValue.from({ asset, value });
|
|
86
106
|
}
|
|
87
107
|
|
|
88
|
-
static
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return createAssetValue(assetString, value);
|
|
96
|
-
}
|
|
108
|
+
static from<T extends {}>({
|
|
109
|
+
value = 0,
|
|
110
|
+
fromBaseDecimal,
|
|
111
|
+
asyncTokenLookup,
|
|
112
|
+
...fromAssetOrChain
|
|
113
|
+
}: T & AssetValueFromParams): ConditionalAssetValueReturn<T> {
|
|
114
|
+
const parsedValue = value instanceof BigIntArithmetics ? value.getValue("string") : value;
|
|
97
115
|
|
|
98
|
-
|
|
99
|
-
const { chain, isSynthetic } = getAssetInfo(assetString);
|
|
100
|
-
const tokenInfo = staticTokensMap.get(assetString.toUpperCase() as TokenNames);
|
|
101
|
-
|
|
102
|
-
if (isSynthetic) return createSyntheticAssetValue(assetString, value);
|
|
103
|
-
// TODO: write logger that will only run in dev mode with some flag
|
|
104
|
-
// if (!tokenInfo) {
|
|
105
|
-
// console.error(
|
|
106
|
-
// `Asset ${assetString} is not loaded. Use AssetValue.loadStaticAssets() to load it`,
|
|
107
|
-
// );
|
|
108
|
-
// }
|
|
109
|
-
|
|
110
|
-
const { tax, decimal, identifier } = tokenInfo || {
|
|
111
|
-
decimal: BaseDecimal[chain],
|
|
112
|
-
identifier: assetString,
|
|
113
|
-
};
|
|
116
|
+
const isFromChain = "chain" in fromAssetOrChain;
|
|
114
117
|
|
|
115
|
-
|
|
116
|
-
tax,
|
|
117
|
-
value: safeValue(value, decimal),
|
|
118
|
-
identifier: isSynthetic ? assetString : identifier,
|
|
119
|
-
decimal: isSynthetic ? 8 : decimal,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
118
|
+
const assetOrChain = isFromChain ? fromAssetOrChain.chain : fromAssetOrChain.asset;
|
|
122
119
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
baseDecimal: number = BaseDecimal.THOR,
|
|
127
|
-
) {
|
|
128
|
-
const shiftedAmount = BigIntArithmetics.shiftDecimals({
|
|
129
|
-
value: SwapKitNumber.fromBigInt(BigInt(value)),
|
|
130
|
-
from: 0,
|
|
131
|
-
to: baseDecimal,
|
|
132
|
-
}).getBaseValue("string");
|
|
133
|
-
const assetValue = await AssetValue.fromString(assetString);
|
|
134
|
-
|
|
135
|
-
return assetValue.set(shiftedAmount);
|
|
136
|
-
}
|
|
120
|
+
const isFromCommonAssetOrChain =
|
|
121
|
+
isFromChain ||
|
|
122
|
+
CommonAssetStrings.includes(assetOrChain as (typeof CommonAssetStrings)[number]);
|
|
137
123
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
baseDecimal: number = BaseDecimal.THOR,
|
|
142
|
-
) {
|
|
143
|
-
const { chain, isSynthetic } = getAssetInfo(assetString);
|
|
144
|
-
const tokenInfo = staticTokensMap.get(assetString.toUpperCase() as TokenNames);
|
|
124
|
+
const { identifier: unsafeIdentifier, decimal: commonAssetDecimal } = isFromCommonAssetOrChain
|
|
125
|
+
? getCommonAssetInfo(assetOrChain as CommonAssetString)
|
|
126
|
+
: { identifier: assetOrChain, decimal: undefined };
|
|
145
127
|
|
|
146
|
-
|
|
128
|
+
const { chain, isSynthetic } = getAssetInfo(unsafeIdentifier);
|
|
147
129
|
|
|
148
|
-
const { tax, decimal, identifier } =
|
|
149
|
-
|
|
150
|
-
|
|
130
|
+
const { tax, decimal, identifier } = staticTokensMap.get(
|
|
131
|
+
unsafeIdentifier.toUpperCase() as TokenNames,
|
|
132
|
+
) || {
|
|
133
|
+
decimal: commonAssetDecimal || BaseDecimal[chain],
|
|
134
|
+
identifier: unsafeIdentifier,
|
|
151
135
|
};
|
|
152
136
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
identifier,
|
|
157
|
-
decimal,
|
|
158
|
-
});
|
|
159
|
-
}
|
|
137
|
+
const adjustedValue = fromBaseDecimal
|
|
138
|
+
? safeValue(BigInt(parsedValue), fromBaseDecimal)
|
|
139
|
+
: safeValue(parsedValue, decimal);
|
|
160
140
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
// TODO: write logger that will only run in dev mode with some flag
|
|
167
|
-
// if (!tokenInfo) {
|
|
168
|
-
// console.error(
|
|
169
|
-
// `Asset ${assetString} is not loaded. - Loading with base Chain. Use AssetValue.loadStaticAssets() to load it`,
|
|
170
|
-
// );
|
|
171
|
-
// }
|
|
172
|
-
|
|
173
|
-
const { tax, decimal, identifier } = tokenInfo || {
|
|
174
|
-
decimal: BaseDecimal[chain],
|
|
175
|
-
identifier: assetString,
|
|
176
|
-
};
|
|
177
|
-
return new AssetValue({ tax, decimal, identifier, value: safeValue(value, decimal) });
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
static fromChainOrSignature(assetString: CommonAssetString, value: NumberPrimitives = 0) {
|
|
181
|
-
const { decimal, identifier } = getCommonAssetInfo(assetString);
|
|
141
|
+
const assetValue = asyncTokenLookup
|
|
142
|
+
? createAssetValue(identifier, fromBaseDecimal ? adjustedValue : parsedValue)
|
|
143
|
+
: isSynthetic
|
|
144
|
+
? createSyntheticAssetValue(identifier, parsedValue)
|
|
145
|
+
: new AssetValue({ tax, decimal, identifier, value: adjustedValue });
|
|
182
146
|
|
|
183
|
-
return
|
|
147
|
+
return assetValue as ConditionalAssetValueReturn<T>;
|
|
184
148
|
}
|
|
185
149
|
|
|
186
150
|
static loadStaticAssets() {
|
|
@@ -211,15 +175,73 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
211
175
|
},
|
|
212
176
|
);
|
|
213
177
|
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* @deprecated use AssetValue.from({ asset, value, asyncTokenLookup: true })
|
|
181
|
+
*/
|
|
182
|
+
static fromString(asset: string, value: NumberPrimitives = 0) {
|
|
183
|
+
return AssetValue.from({ asset, value, asyncTokenLookup: true });
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* @deprecated use AssetValue.from({ asset, value, asyncTokenLookup: true })
|
|
187
|
+
*/
|
|
188
|
+
static fromIdentifier(
|
|
189
|
+
asset: `${Chain}.${string}` | `${Chain}/${string}` | TokenNames,
|
|
190
|
+
value: NumberPrimitives = 0,
|
|
191
|
+
) {
|
|
192
|
+
return AssetValue.from({ asset: asset as TokenNames, value, asyncTokenLookup: true });
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* @deprecated use AssetValue.from({ asset, value })
|
|
196
|
+
*/
|
|
197
|
+
static fromStringSync(asset: string, value: NumberPrimitives = 0) {
|
|
198
|
+
return AssetValue.from({ asset, value });
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* @deprecated use AssetValue.from({ asset, value, fromBaseDecimal, asyncTokenLookup: true })
|
|
202
|
+
*/
|
|
203
|
+
static fromStringWithBase(
|
|
204
|
+
asset: string,
|
|
205
|
+
value: string | bigint = 0n,
|
|
206
|
+
fromBaseDecimal: number = BaseDecimal.THOR,
|
|
207
|
+
) {
|
|
208
|
+
return AssetValue.from({ asyncTokenLookup: true, asset, value, fromBaseDecimal });
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* @deprecated use AssetValue.from({ asset, value, fromBaseDecimal, asyncTokenLookup: true })
|
|
212
|
+
*/
|
|
213
|
+
static fromStringWithBaseSync(
|
|
214
|
+
asset: string,
|
|
215
|
+
value: string | bigint = 0n,
|
|
216
|
+
fromBaseDecimal: number = BaseDecimal.THOR,
|
|
217
|
+
) {
|
|
218
|
+
return AssetValue.from({ asset, value, fromBaseDecimal });
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* @deprecated use AssetValue.from({ asset, value })
|
|
222
|
+
*/
|
|
223
|
+
static fromIdentifierSync(asset: TokenNames, value: NumberPrimitives = 0) {
|
|
224
|
+
return AssetValue.from({ asset, value });
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* @deprecated use AssetValue.from({ asset, value }) or AssetValue.from({ chain, value })
|
|
228
|
+
*/
|
|
229
|
+
static fromChainOrSignature(assetOrChain: CommonAssetString, value: NumberPrimitives = 0) {
|
|
230
|
+
if (assetOrChain in Chain) {
|
|
231
|
+
return AssetValue.from({ chain: assetOrChain as Chain, value });
|
|
232
|
+
}
|
|
233
|
+
return AssetValue.from({ asset: assetOrChain, value });
|
|
234
|
+
}
|
|
214
235
|
}
|
|
215
236
|
|
|
216
237
|
export function getMinAmountByChain(chain: Chain) {
|
|
217
|
-
const asset = AssetValue.
|
|
238
|
+
const asset = AssetValue.from({ chain });
|
|
218
239
|
|
|
219
240
|
switch (chain) {
|
|
220
241
|
case Chain.Bitcoin:
|
|
221
242
|
case Chain.Litecoin:
|
|
222
243
|
case Chain.BitcoinCash:
|
|
244
|
+
case Chain.Dash:
|
|
223
245
|
return asset.set(0.00010001);
|
|
224
246
|
|
|
225
247
|
case Chain.Dogecoin:
|
|
@@ -227,6 +249,8 @@ export function getMinAmountByChain(chain: Chain) {
|
|
|
227
249
|
|
|
228
250
|
case Chain.Avalanche:
|
|
229
251
|
case Chain.Ethereum:
|
|
252
|
+
case Chain.Arbitrum:
|
|
253
|
+
case Chain.BinanceSmartChain:
|
|
230
254
|
return asset.set(0.00000001);
|
|
231
255
|
|
|
232
256
|
case Chain.THORChain:
|
|
@@ -234,6 +258,7 @@ export function getMinAmountByChain(chain: Chain) {
|
|
|
234
258
|
return asset.set(0);
|
|
235
259
|
|
|
236
260
|
case Chain.Cosmos:
|
|
261
|
+
case Chain.Kujira:
|
|
237
262
|
return asset.set(0.000001);
|
|
238
263
|
|
|
239
264
|
default:
|
|
@@ -317,9 +342,9 @@ function getAssetInfo(identifier: string) {
|
|
|
317
342
|
chain,
|
|
318
343
|
isGasAsset: isGasAsset({ chain, symbol }),
|
|
319
344
|
isSynthetic,
|
|
345
|
+
ticker,
|
|
320
346
|
symbol:
|
|
321
347
|
(isSynthetic ? `${synthChain}/` : "") +
|
|
322
348
|
(address ? `${ticker}-${address?.toLowerCase() ?? ""}` : symbol),
|
|
323
|
-
ticker,
|
|
324
349
|
};
|
|
325
350
|
}
|
package/src/types/chains.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { ExplorerUrl, RPCUrl } from "./network";
|
|
|
3
3
|
export enum Chain {
|
|
4
4
|
Arbitrum = "ARB",
|
|
5
5
|
Avalanche = "AVAX",
|
|
6
|
-
Binance = "BNB",
|
|
7
6
|
BinanceSmartChain = "BSC",
|
|
8
7
|
Bitcoin = "BTC",
|
|
9
8
|
BitcoinCash = "BCH",
|
|
@@ -30,7 +29,6 @@ export enum ChainId {
|
|
|
30
29
|
ArbitrumHex = "0xa4b1",
|
|
31
30
|
Avalanche = "43114",
|
|
32
31
|
AvalancheHex = "0xa86a",
|
|
33
|
-
Binance = "Binance-Chain-Tigris",
|
|
34
32
|
BinanceSmartChain = "56",
|
|
35
33
|
BinanceSmartChainHex = "0x38",
|
|
36
34
|
Bitcoin = "bitcoin",
|
|
@@ -63,7 +61,6 @@ export const ChainIdToChain: Record<ChainId, Chain> = {
|
|
|
63
61
|
[ChainId.Avalanche]: Chain.Avalanche,
|
|
64
62
|
[ChainId.BinanceSmartChainHex]: Chain.BinanceSmartChain,
|
|
65
63
|
[ChainId.BinanceSmartChain]: Chain.BinanceSmartChain,
|
|
66
|
-
[ChainId.Binance]: Chain.Binance,
|
|
67
64
|
[ChainId.BitcoinCash]: Chain.BitcoinCash,
|
|
68
65
|
[ChainId.Bitcoin]: Chain.Bitcoin,
|
|
69
66
|
[ChainId.Chainflip]: Chain.Chainflip,
|
|
@@ -95,7 +92,6 @@ export enum BaseDecimal {
|
|
|
95
92
|
ARB = 18,
|
|
96
93
|
AVAX = 18,
|
|
97
94
|
BCH = 8,
|
|
98
|
-
BNB = 8,
|
|
99
95
|
BSC = 18,
|
|
100
96
|
BTC = 8,
|
|
101
97
|
DASH = 8,
|
|
@@ -148,23 +144,11 @@ export const UTXOChains = [
|
|
|
148
144
|
Chain.Litecoin,
|
|
149
145
|
] as const;
|
|
150
146
|
|
|
151
|
-
export type CosmosChain =
|
|
152
|
-
|
|
153
|
-
| Chain.THORChain
|
|
154
|
-
| Chain.Binance
|
|
155
|
-
| Chain.Maya
|
|
156
|
-
| Chain.Kujira;
|
|
157
|
-
export const CosmosChains = [
|
|
158
|
-
Chain.Cosmos,
|
|
159
|
-
Chain.THORChain,
|
|
160
|
-
Chain.Binance,
|
|
161
|
-
Chain.Maya,
|
|
162
|
-
Chain.Kujira,
|
|
163
|
-
] as const;
|
|
147
|
+
export type CosmosChain = Chain.Cosmos | Chain.THORChain | Chain.Maya | Chain.Kujira;
|
|
148
|
+
export const CosmosChains = [Chain.Cosmos, Chain.THORChain, Chain.Maya, Chain.Kujira] as const;
|
|
164
149
|
|
|
165
150
|
export const TCSupportedChains = [
|
|
166
151
|
Chain.Avalanche,
|
|
167
|
-
Chain.Binance,
|
|
168
152
|
Chain.BinanceSmartChain,
|
|
169
153
|
Chain.Bitcoin,
|
|
170
154
|
Chain.BitcoinCash,
|
|
@@ -13,7 +13,6 @@ export enum DerivationPath {
|
|
|
13
13
|
ARB = "m/44'/60'/0'/0",
|
|
14
14
|
AVAX = "m/44'/60'/0'/0",
|
|
15
15
|
BCH = "m/44'/145'/0'/0",
|
|
16
|
-
BNB = "m/44'/714'/0'/0",
|
|
17
16
|
BSC = "m/44'/60'/0'/0",
|
|
18
17
|
BTC = "m/84'/0'/0'/0",
|
|
19
18
|
DASH = "m/44'/5'/0'/0",
|
|
@@ -38,7 +37,6 @@ export const NetworkDerivationPath: Record<Chain, DerivationPathArray> = {
|
|
|
38
37
|
ARB: [44, 60, 0, 0, 0],
|
|
39
38
|
AVAX: [44, 60, 0, 0, 0],
|
|
40
39
|
BCH: [44, 145, 0, 0, 0],
|
|
41
|
-
BNB: [44, 714, 0, 0, 0],
|
|
42
40
|
BSC: [44, 60, 0, 0, 0],
|
|
43
41
|
BTC: [84, 0, 0, 0, 0],
|
|
44
42
|
DASH: [44, 5, 0, 0, 0],
|
package/src/types/network.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
export enum RPCUrl {
|
|
2
2
|
Arbitrum = "https://arb1.arbitrum.io/rpc",
|
|
3
3
|
Avalanche = "https://node-router.thorswap.net/avalanche-c",
|
|
4
|
-
Binance = "",
|
|
5
4
|
BinanceSmartChain = "https://bsc-dataseed.binance.org",
|
|
6
5
|
Bitcoin = "https://node-router.thorswap.net/bitcoin",
|
|
7
6
|
BitcoinCash = "https://node-router.thorswap.net/bitcoin-cash",
|
|
@@ -26,7 +25,6 @@ export enum RPCUrl {
|
|
|
26
25
|
export enum ExplorerUrl {
|
|
27
26
|
Arbitrum = "https://arbiscan.io",
|
|
28
27
|
Avalanche = "https://snowtrace.io",
|
|
29
|
-
Binance = "https://explorer.binance.org",
|
|
30
28
|
BinanceSmartChain = "https://bscscan.com",
|
|
31
29
|
Bitcoin = "https://blockchair.com/bitcoin",
|
|
32
30
|
BitcoinCash = "https://www.blockchair.com/bitcoin-cash",
|