@xchainjs/xchain-midgard-query 2.0.9 → 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 CHANGED
@@ -265,6 +265,75 @@ class MidgardQuery {
265
265
  return pool;
266
266
  });
267
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
+ }
268
337
  /**
269
338
  * Get saver positions by an array of saver descriptions.
270
339
  *
@@ -320,8 +389,15 @@ class MidgardQuery {
320
389
  }
321
390
  if (isAssetRuneNative(asset) || isSynthAsset(asset) || isTradeAsset(asset) || isSecuredAsset(asset))
322
391
  return DEFAULT_THORCHAIN_DECIMALS;
323
- const pool = yield this.getPool(assetToString(asset));
324
- return Number(pool.nativeDecimal);
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
+ }
325
401
  });
326
402
  }
327
403
  /**
package/lib/index.js CHANGED
@@ -272,6 +272,75 @@ class MidgardQuery {
272
272
  return pool;
273
273
  });
274
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
+ }
275
344
  /**
276
345
  * Get saver positions by an array of saver descriptions.
277
346
  *
@@ -327,8 +396,15 @@ class MidgardQuery {
327
396
  }
328
397
  if (isAssetRuneNative(asset) || xchainUtil.isSynthAsset(asset) || xchainUtil.isTradeAsset(asset) || xchainUtil.isSecuredAsset(asset))
329
398
  return DEFAULT_THORCHAIN_DECIMALS;
330
- const pool = yield this.getPool(xchainUtil.assetToString(asset));
331
- return Number(pool.nativeDecimal);
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
+ }
332
408
  });
333
409
  }
334
410
  /**
@@ -21,6 +21,12 @@ export declare class MidgardQuery {
21
21
  * @throws {Error} - Can't find pool for asset.
22
22
  */
23
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;
24
30
  /**
25
31
  * Get saver positions by an array of saver descriptions.
26
32
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-midgard-query",
3
- "version": "2.0.9",
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": [