@swapkit/helpers 1.0.0-rc.99 → 1.0.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/dist/index.js +2 -2441
- package/dist/index.js.map +24 -23
- package/package.json +7 -14
- package/src/helpers/__tests__/asset.test.ts +40 -8
- package/src/helpers/__tests__/memo.test.ts +55 -74
- package/src/helpers/__tests__/others.test.ts +5 -3
- package/src/helpers/asset.ts +80 -26
- package/src/helpers/derivationPath.ts +5 -4
- package/src/helpers/memo.ts +157 -46
- package/src/helpers/others.ts +27 -2
- package/src/helpers/validators.ts +8 -3
- package/src/helpers/web3wallets.ts +34 -4
- package/src/modules/__tests__/assetValue.test.ts +183 -114
- package/src/modules/assetValue.ts +143 -101
- package/src/modules/bigIntArithmetics.ts +2 -6
- package/src/modules/requestClient.ts +22 -12
- package/src/modules/swapKitError.ts +77 -21
- package/src/types/chains.ts +12 -18
- package/src/types/commonTypes.ts +4 -0
- package/src/types/derivationPath.ts +4 -2
- package/src/types/errors/apiV1.ts +0 -0
- package/src/types/index.ts +2 -0
- package/src/types/network.ts +5 -3
- package/src/types/quotes.ts +391 -0
- package/src/types/radix.ts +14 -0
- package/src/types/sdk.ts +87 -5
- package/src/types/tokens.ts +18 -14
- package/src/types/wallet.ts +44 -19
|
@@ -1,18 +1,40 @@
|
|
|
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";
|
|
6
12
|
|
|
7
13
|
import type { NumberPrimitives } from "./bigIntArithmetics.ts";
|
|
8
14
|
import { BigIntArithmetics, formatBigIntToSafeValue } from "./bigIntArithmetics.ts";
|
|
9
|
-
import {
|
|
15
|
+
import { SwapKitError } from "./swapKitError.ts";
|
|
16
|
+
import type { SwapKitValueType } from "./swapKitNumber.ts";
|
|
10
17
|
|
|
11
18
|
const staticTokensMap = new Map<
|
|
12
19
|
TokenNames,
|
|
13
20
|
{ tax?: TokenTax; decimal: number; identifier: string }
|
|
14
21
|
>();
|
|
15
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
|
+
|
|
16
38
|
export class AssetValue extends BigIntArithmetics {
|
|
17
39
|
address?: string;
|
|
18
40
|
chain: Chain;
|
|
@@ -58,122 +80,71 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
58
80
|
return this.isSynthetic ? `${this.chain}.${this.symbol.replace("/", ".")}` : this.toString();
|
|
59
81
|
}
|
|
60
82
|
|
|
61
|
-
|
|
83
|
+
eqAsset({ chain, symbol }: { chain: Chain; symbol: string }) {
|
|
62
84
|
return this.chain === chain && this.symbol === symbol;
|
|
63
85
|
}
|
|
64
86
|
|
|
87
|
+
eq(assetValue: AssetValue) {
|
|
88
|
+
return this.eqAsset(assetValue) && this.eqValue(assetValue);
|
|
89
|
+
}
|
|
90
|
+
|
|
65
91
|
// THOR.RUNE
|
|
66
92
|
// THOR.ETH.ETH
|
|
67
93
|
// ETH.THOR-0x1234567890
|
|
68
94
|
static fromUrl(urlAsset: string, value: NumberPrimitives = 0) {
|
|
69
95
|
const [chain, ticker, symbol] = urlAsset.split(".");
|
|
70
|
-
if (!(chain && ticker))
|
|
96
|
+
if (!(chain && ticker)) {
|
|
97
|
+
throw new SwapKitError({
|
|
98
|
+
errorKey: "helpers_invalid_asset_url",
|
|
99
|
+
info: { urlAsset },
|
|
100
|
+
});
|
|
101
|
+
}
|
|
71
102
|
|
|
72
|
-
const
|
|
73
|
-
chain === Chain.THORChain && symbol ? `${chain}.${ticker}/${symbol}` : urlAsset;
|
|
103
|
+
const asset = chain === Chain.THORChain && symbol ? `${chain}.${ticker}/${symbol}` : urlAsset;
|
|
74
104
|
|
|
75
|
-
return
|
|
105
|
+
return AssetValue.from({ asset, value });
|
|
76
106
|
}
|
|
77
107
|
|
|
78
|
-
static
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
| `${Chain}.${string}-${string}`
|
|
86
|
-
| TokenNames,
|
|
87
|
-
value: NumberPrimitives = 0,
|
|
88
|
-
) {
|
|
89
|
-
return createAssetValue(assetString, value);
|
|
90
|
-
}
|
|
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;
|
|
91
115
|
|
|
92
|
-
|
|
93
|
-
const { chain, isSynthetic } = getAssetInfo(assetString);
|
|
94
|
-
const tokenInfo = staticTokensMap.get(assetString.toUpperCase() as TokenNames);
|
|
95
|
-
|
|
96
|
-
if (isSynthetic) return createSyntheticAssetValue(assetString, value);
|
|
97
|
-
// TODO: write logger that will only run in dev mode with some flag
|
|
98
|
-
// if (!tokenInfo) {
|
|
99
|
-
// console.error(
|
|
100
|
-
// `Asset ${assetString} is not loaded. Use AssetValue.loadStaticAssets() to load it`,
|
|
101
|
-
// );
|
|
102
|
-
// }
|
|
103
|
-
|
|
104
|
-
const { tax, decimal, identifier } = tokenInfo || {
|
|
105
|
-
decimal: BaseDecimal[chain],
|
|
106
|
-
identifier: assetString,
|
|
107
|
-
};
|
|
116
|
+
const isFromChain = "chain" in fromAssetOrChain;
|
|
108
117
|
|
|
109
|
-
|
|
110
|
-
tax,
|
|
111
|
-
value: safeValue(value, decimal),
|
|
112
|
-
identifier: isSynthetic ? assetString : identifier,
|
|
113
|
-
decimal: isSynthetic ? 8 : decimal,
|
|
114
|
-
});
|
|
115
|
-
}
|
|
118
|
+
const assetOrChain = isFromChain ? fromAssetOrChain.chain : fromAssetOrChain.asset;
|
|
116
119
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
baseDecimal: number = BaseDecimal.THOR,
|
|
121
|
-
) {
|
|
122
|
-
const shiftedAmount = BigIntArithmetics.shiftDecimals({
|
|
123
|
-
value: SwapKitNumber.fromBigInt(BigInt(value)),
|
|
124
|
-
from: 0,
|
|
125
|
-
to: baseDecimal,
|
|
126
|
-
}).getBaseValue("string");
|
|
127
|
-
const assetValue = await AssetValue.fromString(assetString, value);
|
|
128
|
-
|
|
129
|
-
return assetValue.set(shiftedAmount);
|
|
130
|
-
}
|
|
120
|
+
const isFromCommonAssetOrChain =
|
|
121
|
+
isFromChain ||
|
|
122
|
+
CommonAssetStrings.includes(assetOrChain as (typeof CommonAssetStrings)[number]);
|
|
131
123
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
baseDecimal: number = BaseDecimal.THOR,
|
|
136
|
-
) {
|
|
137
|
-
const { chain, isSynthetic } = getAssetInfo(assetString);
|
|
138
|
-
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 };
|
|
139
127
|
|
|
140
|
-
|
|
128
|
+
const { chain, isSynthetic } = getAssetInfo(unsafeIdentifier);
|
|
141
129
|
|
|
142
|
-
const { tax, decimal, identifier } =
|
|
143
|
-
|
|
144
|
-
|
|
130
|
+
const { tax, decimal, identifier } = staticTokensMap.get(
|
|
131
|
+
unsafeIdentifier.toUpperCase() as TokenNames,
|
|
132
|
+
) || {
|
|
133
|
+
decimal: commonAssetDecimal || BaseDecimal[chain],
|
|
134
|
+
identifier: unsafeIdentifier,
|
|
145
135
|
};
|
|
146
136
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
identifier,
|
|
151
|
-
decimal,
|
|
152
|
-
});
|
|
153
|
-
}
|
|
137
|
+
const adjustedValue = fromBaseDecimal
|
|
138
|
+
? safeValue(BigInt(parsedValue), fromBaseDecimal)
|
|
139
|
+
: safeValue(parsedValue, decimal);
|
|
154
140
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
// TODO: write logger that will only run in dev mode with some flag
|
|
161
|
-
// if (!tokenInfo) {
|
|
162
|
-
// console.error(
|
|
163
|
-
// `Asset ${assetString} is not loaded. - Loading with base Chain. Use AssetValue.loadStaticAssets() to load it`,
|
|
164
|
-
// );
|
|
165
|
-
// }
|
|
166
|
-
|
|
167
|
-
const { tax, decimal, identifier } = tokenInfo || {
|
|
168
|
-
decimal: BaseDecimal[chain],
|
|
169
|
-
identifier: assetString,
|
|
170
|
-
};
|
|
171
|
-
return new AssetValue({ tax, decimal, identifier, value: safeValue(value, decimal) });
|
|
172
|
-
}
|
|
141
|
+
const assetValue = asyncTokenLookup
|
|
142
|
+
? createAssetValue(identifier, fromBaseDecimal ? adjustedValue : parsedValue)
|
|
143
|
+
: isSynthetic
|
|
144
|
+
? createSyntheticAssetValue(identifier, parsedValue)
|
|
145
|
+
: new AssetValue({ tax, decimal, identifier, value: adjustedValue });
|
|
173
146
|
|
|
174
|
-
|
|
175
|
-
const { decimal, identifier } = getCommonAssetInfo(assetString);
|
|
176
|
-
return new AssetValue({ value: safeValue(value, decimal), decimal, identifier });
|
|
147
|
+
return assetValue as ConditionalAssetValueReturn<T>;
|
|
177
148
|
}
|
|
178
149
|
|
|
179
150
|
static loadStaticAssets() {
|
|
@@ -204,15 +175,73 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
204
175
|
},
|
|
205
176
|
);
|
|
206
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
|
+
}
|
|
207
235
|
}
|
|
208
236
|
|
|
209
237
|
export function getMinAmountByChain(chain: Chain) {
|
|
210
|
-
const asset = AssetValue.
|
|
238
|
+
const asset = AssetValue.from({ chain });
|
|
211
239
|
|
|
212
240
|
switch (chain) {
|
|
213
241
|
case Chain.Bitcoin:
|
|
214
242
|
case Chain.Litecoin:
|
|
215
243
|
case Chain.BitcoinCash:
|
|
244
|
+
case Chain.Dash:
|
|
216
245
|
return asset.set(0.00010001);
|
|
217
246
|
|
|
218
247
|
case Chain.Dogecoin:
|
|
@@ -220,6 +249,8 @@ export function getMinAmountByChain(chain: Chain) {
|
|
|
220
249
|
|
|
221
250
|
case Chain.Avalanche:
|
|
222
251
|
case Chain.Ethereum:
|
|
252
|
+
case Chain.Arbitrum:
|
|
253
|
+
case Chain.BinanceSmartChain:
|
|
223
254
|
return asset.set(0.00000001);
|
|
224
255
|
|
|
225
256
|
case Chain.THORChain:
|
|
@@ -227,6 +258,7 @@ export function getMinAmountByChain(chain: Chain) {
|
|
|
227
258
|
return asset.set(0);
|
|
228
259
|
|
|
229
260
|
case Chain.Cosmos:
|
|
261
|
+
case Chain.Kujira:
|
|
230
262
|
return asset.set(0.000001);
|
|
231
263
|
|
|
232
264
|
default:
|
|
@@ -252,7 +284,12 @@ function createSyntheticAssetValue(identifier: string, value: NumberPrimitives =
|
|
|
252
284
|
? identifier.split(".").slice(1).join().split("/")
|
|
253
285
|
: identifier.split("/");
|
|
254
286
|
|
|
255
|
-
if (!(synthChain && symbol))
|
|
287
|
+
if (!(synthChain && symbol)) {
|
|
288
|
+
throw new SwapKitError({
|
|
289
|
+
errorKey: "helpers_invalid_asset_identifier",
|
|
290
|
+
info: { identifier },
|
|
291
|
+
});
|
|
292
|
+
}
|
|
256
293
|
|
|
257
294
|
return new AssetValue({
|
|
258
295
|
decimal: 8,
|
|
@@ -279,7 +316,12 @@ function getAssetInfo(identifier: string) {
|
|
|
279
316
|
? identifier.split(".").slice(1).join().split("/")
|
|
280
317
|
: identifier.split("/");
|
|
281
318
|
|
|
282
|
-
if (isSynthetic && !(synthChain && synthSymbol))
|
|
319
|
+
if (isSynthetic && !(synthChain && synthSymbol)) {
|
|
320
|
+
throw new SwapKitError({
|
|
321
|
+
errorKey: "helpers_invalid_asset_identifier",
|
|
322
|
+
info: { identifier },
|
|
323
|
+
});
|
|
324
|
+
}
|
|
283
325
|
|
|
284
326
|
const adjustedIdentifier =
|
|
285
327
|
identifier.includes(".") && !isSynthetic
|
|
@@ -300,9 +342,9 @@ function getAssetInfo(identifier: string) {
|
|
|
300
342
|
chain,
|
|
301
343
|
isGasAsset: isGasAsset({ chain, symbol }),
|
|
302
344
|
isSynthetic,
|
|
345
|
+
ticker,
|
|
303
346
|
symbol:
|
|
304
347
|
(isSynthetic ? `${synthChain}/` : "") +
|
|
305
348
|
(address ? `${ticker}-${address?.toLowerCase() ?? ""}` : symbol),
|
|
306
|
-
ticker,
|
|
307
349
|
};
|
|
308
350
|
}
|
|
@@ -42,9 +42,7 @@ export function formatBigIntToSafeValue({
|
|
|
42
42
|
// Check if we need to round up
|
|
43
43
|
if (Number.parseInt(decimalString[bigIntDecimal] || "0") >= 5) {
|
|
44
44
|
// Increment the last decimal place and slice off the rest
|
|
45
|
-
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(
|
|
46
|
-
Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1
|
|
47
|
-
).toString()}`;
|
|
45
|
+
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1).toString()}`;
|
|
48
46
|
} else {
|
|
49
47
|
// Just slice off the extra digits
|
|
50
48
|
decimalString = decimalString.substring(0, bigIntDecimal);
|
|
@@ -277,9 +275,7 @@ export class BigIntArithmetics {
|
|
|
277
275
|
// Check if we need to round up
|
|
278
276
|
if (Number.parseInt(decimalString[bigIntDecimal] || "0") >= 5) {
|
|
279
277
|
// Increment the last decimal place and slice off the rest
|
|
280
|
-
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(
|
|
281
|
-
Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1
|
|
282
|
-
).toString()}`;
|
|
278
|
+
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1).toString()}`;
|
|
283
279
|
} else {
|
|
284
280
|
// Just slice off the extra digits
|
|
285
281
|
decimalString = decimalString.substring(0, bigIntDecimal);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { Options } from "ky";
|
|
1
|
+
import type { KyInstance, Options } from "ky";
|
|
2
2
|
import ky from "ky";
|
|
3
3
|
|
|
4
|
-
let
|
|
4
|
+
let kyClientConfig: Options & { apiKey?: string } = {};
|
|
5
5
|
|
|
6
6
|
export const defaultRequestHeaders =
|
|
7
7
|
typeof window !== "undefined"
|
|
@@ -9,21 +9,31 @@ export const defaultRequestHeaders =
|
|
|
9
9
|
: { referrer: "https://sk.thorswap.net", referer: "https://sk.thorswap.net" };
|
|
10
10
|
|
|
11
11
|
export function setRequestClientConfig({ apiKey, ...config }: Options & { apiKey?: string }) {
|
|
12
|
-
|
|
12
|
+
kyClientConfig = { ...config, apiKey };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function getKyClient() {
|
|
16
|
+
const { apiKey, ...config } = kyClientConfig;
|
|
17
|
+
return ky.create({
|
|
13
18
|
...config,
|
|
14
19
|
headers: { ...defaultRequestHeaders, ...config.headers, "x-api-key": apiKey },
|
|
15
20
|
});
|
|
16
21
|
}
|
|
17
22
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
const getTypedBaseRequestClient = (ky: KyInstance) => ({
|
|
24
|
+
get: async <T>(url: string | URL | Request, options?: Options) =>
|
|
25
|
+
(await ky.get(url, options)).json<T>(),
|
|
26
|
+
post: async <T>(url: string | URL | Request, options?: Options) =>
|
|
27
|
+
(await ky.post(url, options)).json<T>(),
|
|
28
|
+
});
|
|
23
29
|
|
|
24
30
|
export const RequestClient = {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
...getTypedBaseRequestClient(getKyClient()),
|
|
32
|
+
extend: (options: Options) => {
|
|
33
|
+
const extendedClient = getKyClient().extend(options);
|
|
34
|
+
return {
|
|
35
|
+
...getTypedBaseRequestClient(extendedClient),
|
|
36
|
+
extend: RequestClient.extend,
|
|
37
|
+
};
|
|
38
|
+
},
|
|
29
39
|
};
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
const
|
|
1
|
+
const errorCodes = {
|
|
2
2
|
/**
|
|
3
3
|
* Core
|
|
4
4
|
*/
|
|
5
|
-
core_wallet_connection_not_found: 10001,
|
|
6
5
|
core_estimated_max_spendable_chain_not_supported: 10002,
|
|
7
6
|
core_extend_error: 10003,
|
|
8
7
|
core_inbound_data_not_found: 10004,
|
|
@@ -10,11 +9,10 @@ const errorMessages = {
|
|
|
10
9
|
core_plugin_not_found: 10006,
|
|
11
10
|
core_plugin_swap_not_found: 10007,
|
|
12
11
|
core_approve_asset_target_invalid: 10008,
|
|
12
|
+
core_explorer_unsupported_chain: 10009,
|
|
13
13
|
core_chain_halted: 10099,
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
* Core - Wallet Connection
|
|
17
|
-
*/
|
|
15
|
+
core_wallet_connection_not_found: 10100,
|
|
18
16
|
core_wallet_xdefi_not_installed: 10101,
|
|
19
17
|
core_wallet_evmwallet_not_installed: 10102,
|
|
20
18
|
core_wallet_walletconnect_not_installed: 10103,
|
|
@@ -39,12 +37,12 @@ const errorMessages = {
|
|
|
39
37
|
* Core - Transaction
|
|
40
38
|
*/
|
|
41
39
|
core_transaction_deposit_error: 10301,
|
|
42
|
-
|
|
40
|
+
core_transaction_create_liquidity_base_error: 10302,
|
|
43
41
|
core_transaction_create_liquidity_asset_error: 10303,
|
|
44
42
|
core_transaction_create_liquidity_invalid_params: 10304,
|
|
45
43
|
core_transaction_add_liquidity_invalid_params: 10305,
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
core_transaction_add_liquidity_base_address: 10306,
|
|
45
|
+
core_transaction_add_liquidity_base_error: 10307,
|
|
48
46
|
core_transaction_add_liquidity_asset_error: 10308,
|
|
49
47
|
core_transaction_withdraw_error: 10309,
|
|
50
48
|
core_transaction_deposit_to_pool_error: 10310,
|
|
@@ -53,36 +51,90 @@ const errorMessages = {
|
|
|
53
51
|
core_transaction_invalid_sender_address: 10313,
|
|
54
52
|
core_transaction_deposit_server_error: 10314,
|
|
55
53
|
core_transaction_user_rejected: 10315,
|
|
56
|
-
|
|
57
54
|
/**
|
|
58
55
|
* Wallets
|
|
59
56
|
*/
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
57
|
+
wallet_connection_rejected_by_user: 20000,
|
|
58
|
+
wallet_missing_api_key: 20001,
|
|
59
|
+
wallet_chain_not_supported: 20002,
|
|
60
|
+
wallet_missing_params: 20003,
|
|
61
|
+
wallet_provider_not_found: 20004,
|
|
62
|
+
wallet_failed_to_add_or_switch_network: 20005,
|
|
63
|
+
wallet_ledger_connection_error: 20101,
|
|
64
|
+
wallet_ledger_connection_claimed: 20102,
|
|
65
|
+
wallet_ledger_get_address_error: 20103,
|
|
66
|
+
wallet_ledger_device_not_found: 20104,
|
|
67
|
+
wallet_ledger_device_locked: 20105,
|
|
68
|
+
wallet_phantom_not_found: 20201,
|
|
69
|
+
wallet_xdefi_not_found: 20301,
|
|
70
|
+
wallet_xdefi_send_transaction_no_address: 20302,
|
|
71
|
+
wallet_xdefi_contract_address_not_provided: 20303,
|
|
72
|
+
wallet_xdefi_asset_not_defined: 20304,
|
|
73
|
+
wallet_walletconnect_project_id_not_specified: 20401,
|
|
74
|
+
wallet_walletconnect_connection_not_established: 20402,
|
|
75
|
+
wallet_walletconnect_namespace_not_supported: 20403,
|
|
76
|
+
wallet_trezor_failed_to_sign_transaction: 20501,
|
|
77
|
+
wallet_trezor_derivation_path_not_supported: 20502,
|
|
78
|
+
wallet_trezor_failed_to_get_address: 20503,
|
|
79
|
+
wallet_talisman_not_enabled: 20601,
|
|
80
|
+
wallet_talisman_not_found: 20602,
|
|
66
81
|
/**
|
|
67
82
|
* Chainflip
|
|
68
83
|
*/
|
|
69
84
|
chainflip_channel_error: 30001,
|
|
70
|
-
|
|
71
|
-
|
|
85
|
+
chainflip_unknown_asset: 30002,
|
|
86
|
+
chainflip_broker_invalid_params: 30100,
|
|
87
|
+
chainflip_broker_recipient_error: 30101,
|
|
88
|
+
chainflip_broker_register: 30102,
|
|
89
|
+
chainflip_broker_tx_error: 30103,
|
|
90
|
+
chainflip_broker_withdraw: 30104,
|
|
91
|
+
chainflip_broker_fund_only_flip_supported: 30105,
|
|
92
|
+
chainflip_broker_fund_invalid_address: 30106,
|
|
72
93
|
/**
|
|
73
94
|
* THORChain
|
|
74
95
|
*/
|
|
96
|
+
thorchain_chain_halted: 40001,
|
|
97
|
+
thorchain_trading_halted: 40002,
|
|
98
|
+
thorchain_swapin_router_required: 40100,
|
|
99
|
+
thorchain_swapin_vault_required: 40101,
|
|
100
|
+
thorchain_swapin_memo_required: 40102,
|
|
101
|
+
thorchain_swapin_token_required: 40103,
|
|
102
|
+
/**
|
|
103
|
+
* SwapKit API
|
|
104
|
+
*/
|
|
105
|
+
api_v2_invalid_response: 50001,
|
|
75
106
|
|
|
107
|
+
/**
|
|
108
|
+
* Toolboxes
|
|
109
|
+
*/
|
|
110
|
+
toolbox_cosmos_signer_not_defined: 90101,
|
|
111
|
+
toolbox_cosmos_no_accounts_found: 90102,
|
|
76
112
|
/**
|
|
77
113
|
* Helpers
|
|
78
114
|
*/
|
|
79
|
-
|
|
115
|
+
helpers_invalid_number_different_decimals: 99000,
|
|
116
|
+
helpers_invalid_number_of_years: 99001,
|
|
117
|
+
helpers_invalid_identifier: 99002,
|
|
118
|
+
helpers_invalid_asset_url: 99003,
|
|
119
|
+
helpers_invalid_asset_identifier: 99004,
|
|
120
|
+
helpers_invalid_memo_type: 99005,
|
|
121
|
+
helpers_failed_to_switch_network: 99103,
|
|
122
|
+
helpers_not_found_provider: 99200,
|
|
80
123
|
} as const;
|
|
81
124
|
|
|
82
|
-
export type ErrorKeys = keyof typeof
|
|
125
|
+
export type ErrorKeys = keyof typeof errorCodes;
|
|
83
126
|
|
|
84
127
|
export class SwapKitError extends Error {
|
|
85
|
-
|
|
128
|
+
static ErrorCode = errorCodes;
|
|
129
|
+
|
|
130
|
+
constructor(
|
|
131
|
+
errorOrErrorKey: ErrorKeys | { errorKey: ErrorKeys; info?: Record<string, NotWorth> },
|
|
132
|
+
sourceError?: NotWorth,
|
|
133
|
+
) {
|
|
134
|
+
const isErrorString = typeof errorOrErrorKey === "string";
|
|
135
|
+
|
|
136
|
+
const errorKey = isErrorString ? errorOrErrorKey : errorOrErrorKey.errorKey;
|
|
137
|
+
|
|
86
138
|
if (sourceError) {
|
|
87
139
|
console.error(sourceError, {
|
|
88
140
|
stack: sourceError?.stack,
|
|
@@ -91,8 +143,12 @@ export class SwapKitError extends Error {
|
|
|
91
143
|
}
|
|
92
144
|
|
|
93
145
|
super(errorKey, {
|
|
94
|
-
cause: {
|
|
146
|
+
cause: {
|
|
147
|
+
code: SwapKitError.ErrorCode[errorKey],
|
|
148
|
+
message: `${errorKey}${isErrorString ? "" : `: ${JSON.stringify(errorOrErrorKey.info)}`}`,
|
|
149
|
+
},
|
|
95
150
|
});
|
|
151
|
+
|
|
96
152
|
Object.setPrototypeOf(this, SwapKitError.prototype);
|
|
97
153
|
}
|
|
98
154
|
}
|
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",
|
|
@@ -18,15 +17,18 @@ export enum Chain {
|
|
|
18
17
|
Polkadot = "DOT",
|
|
19
18
|
Chainflip = "FLIP",
|
|
20
19
|
Polygon = "MATIC",
|
|
20
|
+
Radix = "XRD",
|
|
21
21
|
THORChain = "THOR",
|
|
22
|
+
Solana = "SOL",
|
|
22
23
|
}
|
|
23
24
|
|
|
25
|
+
export type WalletChain = Exclude<Chain, Chain.Chainflip | Chain.Radix>;
|
|
26
|
+
|
|
24
27
|
export enum ChainId {
|
|
25
28
|
Arbitrum = "42161",
|
|
26
29
|
ArbitrumHex = "0xa4b1",
|
|
27
30
|
Avalanche = "43114",
|
|
28
31
|
AvalancheHex = "0xa86a",
|
|
29
|
-
Binance = "Binance-Chain-Tigris",
|
|
30
32
|
BinanceSmartChain = "56",
|
|
31
33
|
BinanceSmartChainHex = "0x38",
|
|
32
34
|
Bitcoin = "bitcoin",
|
|
@@ -46,8 +48,10 @@ export enum ChainId {
|
|
|
46
48
|
Polkadot = "polkadot",
|
|
47
49
|
Polygon = "137",
|
|
48
50
|
PolygonHex = "0x89",
|
|
51
|
+
Radix = "radix-mainnet",
|
|
49
52
|
THORChain = "thorchain-mainnet-v1",
|
|
50
53
|
THORChainStagenet = "thorchain-stagenet-v2",
|
|
54
|
+
Solana = "solana",
|
|
51
55
|
}
|
|
52
56
|
|
|
53
57
|
export const ChainIdToChain: Record<ChainId, Chain> = {
|
|
@@ -57,7 +61,6 @@ export const ChainIdToChain: Record<ChainId, Chain> = {
|
|
|
57
61
|
[ChainId.Avalanche]: Chain.Avalanche,
|
|
58
62
|
[ChainId.BinanceSmartChainHex]: Chain.BinanceSmartChain,
|
|
59
63
|
[ChainId.BinanceSmartChain]: Chain.BinanceSmartChain,
|
|
60
|
-
[ChainId.Binance]: Chain.Binance,
|
|
61
64
|
[ChainId.BitcoinCash]: Chain.BitcoinCash,
|
|
62
65
|
[ChainId.Bitcoin]: Chain.Bitcoin,
|
|
63
66
|
[ChainId.Chainflip]: Chain.Chainflip,
|
|
@@ -75,8 +78,10 @@ export const ChainIdToChain: Record<ChainId, Chain> = {
|
|
|
75
78
|
[ChainId.Polkadot]: Chain.Polkadot,
|
|
76
79
|
[ChainId.PolygonHex]: Chain.Polygon,
|
|
77
80
|
[ChainId.Polygon]: Chain.Polygon,
|
|
81
|
+
[ChainId.Radix]: Chain.Radix,
|
|
78
82
|
[ChainId.THORChainStagenet]: Chain.THORChain,
|
|
79
83
|
[ChainId.THORChain]: Chain.THORChain,
|
|
84
|
+
[ChainId.Solana]: Chain.Solana,
|
|
80
85
|
};
|
|
81
86
|
|
|
82
87
|
type ChainNameType = keyof typeof Chain;
|
|
@@ -87,7 +92,6 @@ export enum BaseDecimal {
|
|
|
87
92
|
ARB = 18,
|
|
88
93
|
AVAX = 18,
|
|
89
94
|
BCH = 8,
|
|
90
|
-
BNB = 8,
|
|
91
95
|
BSC = 18,
|
|
92
96
|
BTC = 8,
|
|
93
97
|
DASH = 8,
|
|
@@ -101,7 +105,9 @@ export enum BaseDecimal {
|
|
|
101
105
|
MATIC = 18,
|
|
102
106
|
MAYA = 10,
|
|
103
107
|
OP = 18,
|
|
108
|
+
SOL = 9,
|
|
104
109
|
THOR = 8,
|
|
110
|
+
XRD = 18,
|
|
105
111
|
ZEC = 8,
|
|
106
112
|
}
|
|
107
113
|
|
|
@@ -138,23 +144,11 @@ export const UTXOChains = [
|
|
|
138
144
|
Chain.Litecoin,
|
|
139
145
|
] as const;
|
|
140
146
|
|
|
141
|
-
export type CosmosChain =
|
|
142
|
-
|
|
143
|
-
| Chain.THORChain
|
|
144
|
-
| Chain.Binance
|
|
145
|
-
| Chain.Maya
|
|
146
|
-
| Chain.Kujira;
|
|
147
|
-
export const CosmosChains = [
|
|
148
|
-
Chain.Cosmos,
|
|
149
|
-
Chain.THORChain,
|
|
150
|
-
Chain.Binance,
|
|
151
|
-
Chain.Maya,
|
|
152
|
-
Chain.Kujira,
|
|
153
|
-
] 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;
|
|
154
149
|
|
|
155
150
|
export const TCSupportedChains = [
|
|
156
151
|
Chain.Avalanche,
|
|
157
|
-
Chain.Binance,
|
|
158
152
|
Chain.BinanceSmartChain,
|
|
159
153
|
Chain.Bitcoin,
|
|
160
154
|
Chain.BitcoinCash,
|
package/src/types/commonTypes.ts
CHANGED