@xchainjs/xchain-midgard-query 2.0.8 → 2.0.10
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/lib/index.esm.js +88 -8
- package/lib/index.js +88 -8
- package/lib/midgard-query.d.ts +8 -1
- package/lib/types.d.ts +1 -1
- package/package.json +5 -5
package/lib/index.esm.js
CHANGED
|
@@ -69,8 +69,8 @@ class Midgard {
|
|
|
69
69
|
const saverDetails = (yield api.getSaverDetail(address)).data;
|
|
70
70
|
return saverDetails;
|
|
71
71
|
}
|
|
72
|
-
catch (
|
|
73
|
-
// console.error(
|
|
72
|
+
catch (_e) {
|
|
73
|
+
// console.error(_e)
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
throw Error(`Midgard not responding`);
|
|
@@ -86,8 +86,8 @@ class Midgard {
|
|
|
86
86
|
try {
|
|
87
87
|
return (yield api.getPools()).data;
|
|
88
88
|
}
|
|
89
|
-
catch (
|
|
90
|
-
// console.error(
|
|
89
|
+
catch (_e) {
|
|
90
|
+
// console.error(_e)
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
throw new Error(`Midgard not responding`);
|
|
@@ -169,7 +169,7 @@ class Midgard {
|
|
|
169
169
|
const response = yield api.getActions(address, txid, asset, type, txType, affiliate, limit, offset, nextPageToken, timestamp, height, prevPageToken, fromTimestamp, fromHeight);
|
|
170
170
|
return response.data;
|
|
171
171
|
}
|
|
172
|
-
catch (
|
|
172
|
+
catch (_e) { }
|
|
173
173
|
}
|
|
174
174
|
throw new Error(`Midgard not responding`);
|
|
175
175
|
});
|
|
@@ -244,8 +244,9 @@ class MidgardQuery {
|
|
|
244
244
|
* @param midgardCache - An instance of the MidgardCache (could be pointing to stagenet, testnet, mainnet).
|
|
245
245
|
* @returns MidgardQuery
|
|
246
246
|
*/
|
|
247
|
-
constructor(midgardCache = defaultCache) {
|
|
247
|
+
constructor(midgardCache = defaultCache, overrideDecimals = {}) {
|
|
248
248
|
this.midgardCache = midgardCache;
|
|
249
|
+
this.overrideDecimals = overrideDecimals;
|
|
249
250
|
}
|
|
250
251
|
/**
|
|
251
252
|
* Get pool by asset.
|
|
@@ -264,6 +265,75 @@ class MidgardQuery {
|
|
|
264
265
|
return pool;
|
|
265
266
|
});
|
|
266
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Provides fallback decimal values for common assets when Midgard is unavailable.
|
|
270
|
+
* @param {CompatibleAsset} asset - The asset to get fallback decimals for.
|
|
271
|
+
* @returns {number} - Standard decimal places for the asset.
|
|
272
|
+
*/
|
|
273
|
+
getFallbackDecimals(asset) {
|
|
274
|
+
const assetString = assetToString(asset);
|
|
275
|
+
// Map of assets to their actual decimal places from THORChain pools
|
|
276
|
+
// Data sourced from https://thornode-v2.ninerealms.com/thorchain/pools
|
|
277
|
+
const fallbackDecimalMap = {
|
|
278
|
+
// Bitcoin and forks
|
|
279
|
+
'BTC.BTC': 8,
|
|
280
|
+
'BCH.BCH': 8,
|
|
281
|
+
'LTC.LTC': 8,
|
|
282
|
+
'DOGE.DOGE': 8,
|
|
283
|
+
// Ethereum and tokens
|
|
284
|
+
'ETH.ETH': 18,
|
|
285
|
+
'ETH.USDT-0xdAC17F958D2ee523a2206206994597C13D831ec7': 6,
|
|
286
|
+
'ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48': 6,
|
|
287
|
+
'ETH.DAI-0x6B175474E89094C44Da98b954EedeAC495271d0F': 18,
|
|
288
|
+
'ETH.WBTC-0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599': 8,
|
|
289
|
+
'ETH.GUSD-0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd': 2,
|
|
290
|
+
// Binance Smart Chain
|
|
291
|
+
'BSC.BNB': 18,
|
|
292
|
+
'BSC.BUSD-0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56': 18,
|
|
293
|
+
'BSC.USDT-0x55d398326f99059fF775485246999027B3197955': 6,
|
|
294
|
+
'BSC.USDC-0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d': 6,
|
|
295
|
+
// Cosmos ecosystem
|
|
296
|
+
'GAIA.ATOM': 6,
|
|
297
|
+
// Avalanche
|
|
298
|
+
'AVAX.AVAX': 18,
|
|
299
|
+
'AVAX.USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E': 6,
|
|
300
|
+
'AVAX.USDT-0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7': 6,
|
|
301
|
+
// Base
|
|
302
|
+
'BASE.USDC-0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913': 6,
|
|
303
|
+
// Tron
|
|
304
|
+
'TRON.TRX': 6,
|
|
305
|
+
'TRON.USDT-0xdAC17F958D2ee523a2206206994597C13D831ec7': 6,
|
|
306
|
+
// XRP
|
|
307
|
+
'XRP.XRP': 6,
|
|
308
|
+
};
|
|
309
|
+
// Return specific fallback or use chain-based defaults
|
|
310
|
+
if (fallbackDecimalMap[assetString]) {
|
|
311
|
+
return fallbackDecimalMap[assetString];
|
|
312
|
+
}
|
|
313
|
+
// Chain-based fallback decimals based on typical chain standards
|
|
314
|
+
const chainDefaults = {
|
|
315
|
+
BTC: 8,
|
|
316
|
+
BCH: 8,
|
|
317
|
+
LTC: 8,
|
|
318
|
+
DOGE: 8,
|
|
319
|
+
ETH: 18,
|
|
320
|
+
BSC: 18,
|
|
321
|
+
BNB: 8,
|
|
322
|
+
GAIA: 6,
|
|
323
|
+
AVAX: 18,
|
|
324
|
+
BASE: 18,
|
|
325
|
+
TRON: 6,
|
|
326
|
+
XRP: 6,
|
|
327
|
+
THOR: DEFAULT_THORCHAIN_DECIMALS,
|
|
328
|
+
};
|
|
329
|
+
const chainDefault = chainDefaults[asset.chain];
|
|
330
|
+
if (chainDefault !== undefined) {
|
|
331
|
+
return chainDefault;
|
|
332
|
+
}
|
|
333
|
+
// Ultimate fallback - use 8 decimals (most common for crypto)
|
|
334
|
+
console.warn(`No fallback decimal configured for ${assetString}, using 8 decimals`);
|
|
335
|
+
return 8;
|
|
336
|
+
}
|
|
267
337
|
/**
|
|
268
338
|
* Get saver positions by an array of saver descriptions.
|
|
269
339
|
*
|
|
@@ -314,10 +384,20 @@ class MidgardQuery {
|
|
|
314
384
|
*/
|
|
315
385
|
getDecimalForAsset(asset) {
|
|
316
386
|
return __awaiter(this, void 0, void 0, function* () {
|
|
387
|
+
if (this.overrideDecimals[assetToString(asset)]) {
|
|
388
|
+
return this.overrideDecimals[assetToString(asset)];
|
|
389
|
+
}
|
|
317
390
|
if (isAssetRuneNative(asset) || isSynthAsset(asset) || isTradeAsset(asset) || isSecuredAsset(asset))
|
|
318
391
|
return DEFAULT_THORCHAIN_DECIMALS;
|
|
319
|
-
|
|
320
|
-
|
|
392
|
+
try {
|
|
393
|
+
const pool = yield this.getPool(assetToString(asset));
|
|
394
|
+
return Number(pool.nativeDecimal);
|
|
395
|
+
}
|
|
396
|
+
catch (error) {
|
|
397
|
+
// Fallback: if Midgard is down, use standard decimal values for common assets
|
|
398
|
+
console.warn(`Midgard unavailable for decimal lookup, using fallback for ${assetToString(asset)}:`, error);
|
|
399
|
+
return this.getFallbackDecimals(asset);
|
|
400
|
+
}
|
|
321
401
|
});
|
|
322
402
|
}
|
|
323
403
|
/**
|
package/lib/index.js
CHANGED
|
@@ -76,8 +76,8 @@ class Midgard {
|
|
|
76
76
|
const saverDetails = (yield api.getSaverDetail(address)).data;
|
|
77
77
|
return saverDetails;
|
|
78
78
|
}
|
|
79
|
-
catch (
|
|
80
|
-
// console.error(
|
|
79
|
+
catch (_e) {
|
|
80
|
+
// console.error(_e)
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
throw Error(`Midgard not responding`);
|
|
@@ -93,8 +93,8 @@ class Midgard {
|
|
|
93
93
|
try {
|
|
94
94
|
return (yield api.getPools()).data;
|
|
95
95
|
}
|
|
96
|
-
catch (
|
|
97
|
-
// console.error(
|
|
96
|
+
catch (_e) {
|
|
97
|
+
// console.error(_e)
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
throw new Error(`Midgard not responding`);
|
|
@@ -176,7 +176,7 @@ class Midgard {
|
|
|
176
176
|
const response = yield api.getActions(address, txid, asset, type, txType, affiliate, limit, offset, nextPageToken, timestamp, height, prevPageToken, fromTimestamp, fromHeight);
|
|
177
177
|
return response.data;
|
|
178
178
|
}
|
|
179
|
-
catch (
|
|
179
|
+
catch (_e) { }
|
|
180
180
|
}
|
|
181
181
|
throw new Error(`Midgard not responding`);
|
|
182
182
|
});
|
|
@@ -251,8 +251,9 @@ class MidgardQuery {
|
|
|
251
251
|
* @param midgardCache - An instance of the MidgardCache (could be pointing to stagenet, testnet, mainnet).
|
|
252
252
|
* @returns MidgardQuery
|
|
253
253
|
*/
|
|
254
|
-
constructor(midgardCache = defaultCache) {
|
|
254
|
+
constructor(midgardCache = defaultCache, overrideDecimals = {}) {
|
|
255
255
|
this.midgardCache = midgardCache;
|
|
256
|
+
this.overrideDecimals = overrideDecimals;
|
|
256
257
|
}
|
|
257
258
|
/**
|
|
258
259
|
* Get pool by asset.
|
|
@@ -271,6 +272,75 @@ class MidgardQuery {
|
|
|
271
272
|
return pool;
|
|
272
273
|
});
|
|
273
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* Provides fallback decimal values for common assets when Midgard is unavailable.
|
|
277
|
+
* @param {CompatibleAsset} asset - The asset to get fallback decimals for.
|
|
278
|
+
* @returns {number} - Standard decimal places for the asset.
|
|
279
|
+
*/
|
|
280
|
+
getFallbackDecimals(asset) {
|
|
281
|
+
const assetString = xchainUtil.assetToString(asset);
|
|
282
|
+
// Map of assets to their actual decimal places from THORChain pools
|
|
283
|
+
// Data sourced from https://thornode-v2.ninerealms.com/thorchain/pools
|
|
284
|
+
const fallbackDecimalMap = {
|
|
285
|
+
// Bitcoin and forks
|
|
286
|
+
'BTC.BTC': 8,
|
|
287
|
+
'BCH.BCH': 8,
|
|
288
|
+
'LTC.LTC': 8,
|
|
289
|
+
'DOGE.DOGE': 8,
|
|
290
|
+
// Ethereum and tokens
|
|
291
|
+
'ETH.ETH': 18,
|
|
292
|
+
'ETH.USDT-0xdAC17F958D2ee523a2206206994597C13D831ec7': 6,
|
|
293
|
+
'ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48': 6,
|
|
294
|
+
'ETH.DAI-0x6B175474E89094C44Da98b954EedeAC495271d0F': 18,
|
|
295
|
+
'ETH.WBTC-0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599': 8,
|
|
296
|
+
'ETH.GUSD-0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd': 2,
|
|
297
|
+
// Binance Smart Chain
|
|
298
|
+
'BSC.BNB': 18,
|
|
299
|
+
'BSC.BUSD-0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56': 18,
|
|
300
|
+
'BSC.USDT-0x55d398326f99059fF775485246999027B3197955': 6,
|
|
301
|
+
'BSC.USDC-0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d': 6,
|
|
302
|
+
// Cosmos ecosystem
|
|
303
|
+
'GAIA.ATOM': 6,
|
|
304
|
+
// Avalanche
|
|
305
|
+
'AVAX.AVAX': 18,
|
|
306
|
+
'AVAX.USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E': 6,
|
|
307
|
+
'AVAX.USDT-0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7': 6,
|
|
308
|
+
// Base
|
|
309
|
+
'BASE.USDC-0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913': 6,
|
|
310
|
+
// Tron
|
|
311
|
+
'TRON.TRX': 6,
|
|
312
|
+
'TRON.USDT-0xdAC17F958D2ee523a2206206994597C13D831ec7': 6,
|
|
313
|
+
// XRP
|
|
314
|
+
'XRP.XRP': 6,
|
|
315
|
+
};
|
|
316
|
+
// Return specific fallback or use chain-based defaults
|
|
317
|
+
if (fallbackDecimalMap[assetString]) {
|
|
318
|
+
return fallbackDecimalMap[assetString];
|
|
319
|
+
}
|
|
320
|
+
// Chain-based fallback decimals based on typical chain standards
|
|
321
|
+
const chainDefaults = {
|
|
322
|
+
BTC: 8,
|
|
323
|
+
BCH: 8,
|
|
324
|
+
LTC: 8,
|
|
325
|
+
DOGE: 8,
|
|
326
|
+
ETH: 18,
|
|
327
|
+
BSC: 18,
|
|
328
|
+
BNB: 8,
|
|
329
|
+
GAIA: 6,
|
|
330
|
+
AVAX: 18,
|
|
331
|
+
BASE: 18,
|
|
332
|
+
TRON: 6,
|
|
333
|
+
XRP: 6,
|
|
334
|
+
THOR: DEFAULT_THORCHAIN_DECIMALS,
|
|
335
|
+
};
|
|
336
|
+
const chainDefault = chainDefaults[asset.chain];
|
|
337
|
+
if (chainDefault !== undefined) {
|
|
338
|
+
return chainDefault;
|
|
339
|
+
}
|
|
340
|
+
// Ultimate fallback - use 8 decimals (most common for crypto)
|
|
341
|
+
console.warn(`No fallback decimal configured for ${assetString}, using 8 decimals`);
|
|
342
|
+
return 8;
|
|
343
|
+
}
|
|
274
344
|
/**
|
|
275
345
|
* Get saver positions by an array of saver descriptions.
|
|
276
346
|
*
|
|
@@ -321,10 +391,20 @@ class MidgardQuery {
|
|
|
321
391
|
*/
|
|
322
392
|
getDecimalForAsset(asset) {
|
|
323
393
|
return __awaiter(this, void 0, void 0, function* () {
|
|
394
|
+
if (this.overrideDecimals[xchainUtil.assetToString(asset)]) {
|
|
395
|
+
return this.overrideDecimals[xchainUtil.assetToString(asset)];
|
|
396
|
+
}
|
|
324
397
|
if (isAssetRuneNative(asset) || xchainUtil.isSynthAsset(asset) || xchainUtil.isTradeAsset(asset) || xchainUtil.isSecuredAsset(asset))
|
|
325
398
|
return DEFAULT_THORCHAIN_DECIMALS;
|
|
326
|
-
|
|
327
|
-
|
|
399
|
+
try {
|
|
400
|
+
const pool = yield this.getPool(xchainUtil.assetToString(asset));
|
|
401
|
+
return Number(pool.nativeDecimal);
|
|
402
|
+
}
|
|
403
|
+
catch (error) {
|
|
404
|
+
// Fallback: if Midgard is down, use standard decimal values for common assets
|
|
405
|
+
console.warn(`Midgard unavailable for decimal lookup, using fallback for ${xchainUtil.assetToString(asset)}:`, error);
|
|
406
|
+
return this.getFallbackDecimals(asset);
|
|
407
|
+
}
|
|
328
408
|
});
|
|
329
409
|
}
|
|
330
410
|
/**
|
package/lib/midgard-query.d.ts
CHANGED
|
@@ -5,13 +5,14 @@ import { ActionHistory, CompatibleAsset, GetActionsParams, SaversPosition, getSa
|
|
|
5
5
|
*/
|
|
6
6
|
export declare class MidgardQuery {
|
|
7
7
|
readonly midgardCache: MidgardCache;
|
|
8
|
+
readonly overrideDecimals: Record<string, number>;
|
|
8
9
|
/**
|
|
9
10
|
* Constructor to create a MidgardQuery.
|
|
10
11
|
*
|
|
11
12
|
* @param midgardCache - An instance of the MidgardCache (could be pointing to stagenet, testnet, mainnet).
|
|
12
13
|
* @returns MidgardQuery
|
|
13
14
|
*/
|
|
14
|
-
constructor(midgardCache?: MidgardCache);
|
|
15
|
+
constructor(midgardCache?: MidgardCache, overrideDecimals?: Record<string, number>);
|
|
15
16
|
/**
|
|
16
17
|
* Get pool by asset.
|
|
17
18
|
*
|
|
@@ -20,6 +21,12 @@ export declare class MidgardQuery {
|
|
|
20
21
|
* @throws {Error} - Can't find pool for asset.
|
|
21
22
|
*/
|
|
22
23
|
private getPool;
|
|
24
|
+
/**
|
|
25
|
+
* Provides fallback decimal values for common assets when Midgard is unavailable.
|
|
26
|
+
* @param {CompatibleAsset} asset - The asset to get fallback decimals for.
|
|
27
|
+
* @returns {number} - Standard decimal places for the asset.
|
|
28
|
+
*/
|
|
29
|
+
private getFallbackDecimals;
|
|
23
30
|
/**
|
|
24
31
|
* Get saver positions by an array of saver descriptions.
|
|
25
32
|
*
|
package/lib/types.d.ts
CHANGED
|
@@ -36,7 +36,7 @@ export type ActionType = 'swap' | 'addLiquidity' | 'withdraw' | 'donate' | 'refu
|
|
|
36
36
|
/**
|
|
37
37
|
* Action Tx type
|
|
38
38
|
*/
|
|
39
|
-
export type ActionTxType = 'unknown' | 'add' | 'withdraw' | '
|
|
39
|
+
export type ActionTxType = 'unknown' | 'add' | 'withdraw' | 'swap' | 'limitOrder' | 'outbound' | 'donate' | 'bond' | 'unbond' | 'leave' | 'yggdrasilFund' | 'yggdrasilReturn' | 'reserve' | 'refund' | 'migrate' | 'ragnarok' | 'switch' | 'noOp' | 'consolidate' | 'thorname' | 'loanOpen' | 'loanRepayment';
|
|
40
40
|
/**
|
|
41
41
|
* Get action params
|
|
42
42
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xchainjs/xchain-midgard-query",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.10",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Module that is responsible for get data from Midgard API",
|
|
6
6
|
"keywords": [
|
|
@@ -28,14 +28,14 @@
|
|
|
28
28
|
"build:release": "yarn exec rm -rf release && yarn pack && yarn exec \"mkdir release && tar zxvf package.tgz --directory release && rm package.tgz\"",
|
|
29
29
|
"test": "jest",
|
|
30
30
|
"e2e": "jest --config jest.config.e2e.mjs",
|
|
31
|
-
"lint": "eslint \"{src,__tests__}/**/*.ts\" --fix --max-warnings 0",
|
|
31
|
+
"lint": "eslint --config ../../eslint.config.mjs \"{src,__tests__}/**/*.ts\" --fix --max-warnings 0",
|
|
32
32
|
"postversion": "git push --follow-tags"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@xchainjs/xchain-client": "2.0.
|
|
35
|
+
"@xchainjs/xchain-client": "2.0.9",
|
|
36
36
|
"@xchainjs/xchain-midgard": "1.0.4",
|
|
37
|
-
"@xchainjs/xchain-util": "2.0.
|
|
38
|
-
"axios": "
|
|
37
|
+
"@xchainjs/xchain-util": "2.0.5",
|
|
38
|
+
"axios": "1.12.1",
|
|
39
39
|
"axios-retry": "^3.9.1"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|