@xchainjs/xchain-midgard-query 0.1.11 → 0.1.13

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
@@ -130,6 +130,43 @@ class Midgard {
130
130
  throw Error(`Midgard not responding`);
131
131
  });
132
132
  }
133
+ /**
134
+ * List actions along with their related transactions. An action is generated by one or more inbound transactions
135
+ * with the intended action set in the transaction memo. The action may result in one or more outbound transactions.
136
+ * Results are paginated by sets of 50. Filters may be applied to query actions.
137
+ *
138
+ * @param {GetActionsParams} params - Get actions params
139
+ * @param {string} params.address - Comma separated list. Address of sender or recipient of any in/out transaction
140
+ * related to the action.
141
+ * @param {string} params.txid - ID of any in/out tx related to the action
142
+ * @param {string} params.asset - Comma separated list. Any asset that is part of the action (CHAIN.SYMBOL) Additionally,
143
+ * synth, nosynth, and norune filters can be used for swap, add/withdraw actions.
144
+ * @param {ActionType} params.type - One or more comma separated unique types of action (swap, addLiquidity, withdraw,
145
+ * donate, refund, switch)
146
+ * @param {string} params.affiliate - Comma separated list. Affiliate address of the action (swap, refund)
147
+ * @param {number} params.limit - Number of actions returned, default is 50
148
+ * @param {number} params.offset - Pagination offset, default is 0
149
+ * @param {number} params.nextPageToken - If this is given, the actions for the next page will be given
150
+ * @param {number} params.timestamp - If this is given, the actions older than the timestamp will be given
151
+ * @param {number} params.height - If this is given, the actions older than the height will be given
152
+ * @param {number} params.prevPageToken - If this is given, the actions for the previous page will be given
153
+ * @param {number} params.fromTimestamp - If this is given, the actions newer than the timestamp will be given
154
+ * @param {number} params.fromHeight - If this is given, the actions newer than the height will be given
155
+ *
156
+ * @returns {ActionHistory} Array of actions for the given filters
157
+ */
158
+ getActions({ address, txid, asset, type, affiliate, limit, offset, nextPageToken, timestamp, height, prevPageToken, fromTimestamp, fromHeight, }) {
159
+ return __awaiter(this, void 0, void 0, function* () {
160
+ for (const api of this.midgardApis) {
161
+ try {
162
+ const response = yield api.getActions(address, txid, asset, type, affiliate, limit, offset, nextPageToken, timestamp, height, prevPageToken, fromTimestamp, fromHeight);
163
+ return response.data;
164
+ }
165
+ catch (e) { }
166
+ }
167
+ throw new Error(`Midgard not responding`);
168
+ });
169
+ }
133
170
  }
134
171
 
135
172
  const MILLISECOND_CACHE_POOLS = 5000;
@@ -276,6 +313,50 @@ class MidgardQuery {
276
313
  return Number(pool.nativeDecimal);
277
314
  });
278
315
  }
316
+ /**
317
+ * List actions along with their related transactions. An action is generated by one or more inbound transactions
318
+ * with the intended action set in the transaction memo. The action may result in one or more outbound transactions.
319
+ * Results are paginated by sets of 50. Filters may be applied to query actions.
320
+ *
321
+ * @param {GetActionsParams} params - Get actions params
322
+ * @param {string} params.address - Comma separated list. Address of sender or recipient of any in/out transaction
323
+ * related to the action.
324
+ * @param {string} params.txid - ID of any in/out tx related to the action
325
+ * @param {string} params.asset - Comma separated list. Any asset that is part of the action (CHAIN.SYMBOL) Additionally,
326
+ * synth, nosynth, and norune filters can be used for swap, add/withdraw actions.
327
+ * @param {ActionType} params.type - One or more comma separated unique types of action (swap, addLiquidity, withdraw,
328
+ * donate, refund, switch)
329
+ * @param {string} params.affiliate - Comma separated list. Affiliate address of the action (swap, refund)
330
+ * @param {number} params.limit - Number of actions returned, default is 50
331
+ * @param {number} params.offset - Pagination offset, default is 0
332
+ * @param {number} params.nextPageToken - If this is given, the actions for the next page will be given
333
+ * @param {number} params.timestamp - If this is given, the actions older than the timestamp will be given
334
+ * @param {number} params.height - If this is given, the actions older than the height will be given
335
+ * @param {number} params.prevPageToken - If this is given, the actions for the previous page will be given
336
+ * @param {number} params.fromTimestamp - If this is given, the actions newer than the timestamp will be given
337
+ * @param {number} params.fromHeight - If this is given, the actions newer than the height will be given
338
+ *
339
+ * @returns {ActionHistory} Array of actions for the given filters
340
+ */
341
+ getActions({ address, txid, asset, type, affiliate, limit, offset, nextPageToken, timestamp, height, prevPageToken, fromTimestamp, fromHeight, }) {
342
+ return __awaiter(this, void 0, void 0, function* () {
343
+ return this.midgardCache.midgard.getActions({
344
+ address,
345
+ txid,
346
+ asset,
347
+ type,
348
+ affiliate,
349
+ limit,
350
+ offset,
351
+ nextPageToken,
352
+ timestamp,
353
+ height,
354
+ prevPageToken,
355
+ fromTimestamp,
356
+ fromHeight,
357
+ });
358
+ });
359
+ }
279
360
  }
280
361
 
281
362
  export { Midgard, MidgardCache, MidgardQuery };
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/utils/midgard.ts","../src/midgard-cache.ts","../src/utils/const.ts","../src/midgard-query.ts"],"sourcesContent":["import { Network } from '@xchainjs/xchain-client'\nimport {\n Configuration,\n MidgardApi,\n PoolDetail,\n ReverseTHORNames,\n SaverDetails,\n THORNameDetails,\n} from '@xchainjs/xchain-midgard'\nimport axios from 'axios'\nimport axiosRetry from 'axios-retry'\n\nimport { MidgardConfig } from '../types'\n\nconst defaultMidgardConfig: Record<Network, MidgardConfig> = {\n mainnet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://midgard.ninerealms.com'],\n },\n stagenet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://stagenet-midgard.ninerealms.com'],\n },\n testnet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://testnet.midgard.thorchain.info'],\n },\n}\n\nexport class Midgard {\n private config: MidgardConfig\n readonly network: Network\n private midgardApis: MidgardApi[]\n\n constructor(network: Network = Network.Mainnet, config?: MidgardConfig) {\n this.network = network\n this.config = config ?? defaultMidgardConfig[this.network]\n axiosRetry(axios, { retries: this.config.apiRetries, retryDelay: axiosRetry.exponentialDelay })\n this.midgardApis = this.config.midgardBaseUrls.map(\n (url: string) => new MidgardApi(new Configuration({ basePath: url })),\n )\n }\n\n /**\n * Function to return member details based on valid liquidity position\n * @param address - query can also be multiple addresses should be separated by comma\n * @returns - object type of Member Detail\n */\n public async getSavers(address: string): Promise<SaverDetails> {\n for (const api of this.midgardApis) {\n try {\n const saverDetails = (await api.getSaverDetail(address)).data\n return saverDetails\n } catch (e) {\n // console.error(e)\n }\n }\n throw Error(`Midgard not responding`)\n }\n\n /**\n *\n * @returns an array of Pools\n */\n async getPools(): Promise<PoolDetail[]> {\n for (const api of this.midgardApis) {\n try {\n return (await api.getPools()).data\n } catch (e) {\n // console.error(e)\n }\n }\n throw new Error(`Midgard not responding`)\n }\n\n public async getTHORNameDetails(name: string): Promise<THORNameDetails | undefined> {\n for (const api of this.midgardApis) {\n try {\n const resp = await api.getTHORNameDetail(name)\n if (resp.status == 404) {\n return undefined\n } else if (resp.status == 200) {\n return resp.data\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n if (e.response.status == 404) {\n return undefined\n }\n }\n }\n throw Error(`Midgard not responding`)\n }\n\n public async getTHORNameReverseLookup(address: string): Promise<ReverseTHORNames | undefined> {\n for (const api of this.midgardApis) {\n try {\n const resp = await api.getTHORNamesByAddress(address)\n if (resp.status == 404) {\n return []\n } else if (resp.status == 200) {\n return resp.data\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n if (e.response.status == 404) {\n return []\n }\n }\n }\n throw Error(`Midgard not responding`)\n }\n}\n","import { PoolDetail, SaverDetails } from '@xchainjs/xchain-midgard'\nimport { CachedValue } from '@xchainjs/xchain-util'\n\nimport { Midgard } from './utils/midgard'\n\nconst MILLISECOND_CACHE_POOLS = 5000\nconst MILLISECOND_CACHE_SAVERS = 5000\n\nconst defaultMidgard = new Midgard()\n\n/**\n * This class retrieves raw information from Midgard API and cached it\n */\n/**\n * Class for caching and retrieving data from Midgard API related to pools and savers.\n */\nexport class MidgardCache {\n readonly midgard: Midgard\n private readonly cachedPools: CachedValue<PoolDetail[]>\n private readonly cachedSavers: CachedValue<SaverDetails>\n\n /**\n * Constructor to create a MidgardCache.\n *\n * @param {Midgard} midgard - An instance of the Midgard API (could be pointing to stagenet, testnet, mainnet).\n * @returns MidgardCache\n */\n constructor(midgard = defaultMidgard) {\n this.midgard = midgard\n this.cachedPools = new CachedValue<PoolDetail[]>(() => this.midgard.getPools(), MILLISECOND_CACHE_POOLS)\n this.cachedSavers = new CachedValue<SaverDetails>(() => this.midgard.getSavers(''), MILLISECOND_CACHE_SAVERS)\n }\n\n /**\n * Get information about existing pools in the protocol from Midgard API.\n *\n * @returns {PoolDetail[]} Array of pools\n */\n public async getPools(): Promise<PoolDetail[]> {\n return this.cachedPools.getValue()\n }\n\n /**\n * Returns the information of all the positions of a set of addresses in the THORChain savers product.\n *\n * @param {String} address - Comma-separated list of addresses.\n * @returns {SaverDetails} Array of savers positions\n */\n public async getSavers(address: string): Promise<SaverDetails> {\n return this.cachedSavers.getValue(address)\n }\n}\n","import { Asset, assetFromStringEx, assetToString } from '@xchainjs/xchain-util'\n\nexport const AssetAVAX = assetFromStringEx('AVAX.AVAX')\nexport const AssetBTC = assetFromStringEx('BTC.BTC')\nexport const AssetATOM = assetFromStringEx('GAIA.ATOM')\nexport const AssetRuneNative = assetFromStringEx('THOR.RUNE')\n\nexport const BTCChain = 'BTC'\nexport const GAIAChain = 'GAIA'\nexport const AVAXChain = 'AVAX'\n\nexport const isAssetRuneNative = (asset: Asset): boolean => assetToString(asset) === assetToString(AssetRuneNative)\n","import { PoolDetail } from '@xchainjs/xchain-midgard'\nimport { Asset, CryptoAmount, assetFromString, assetToString, baseAmount } from '@xchainjs/xchain-util'\n\nimport { MidgardCache } from './midgard-cache'\nimport { SaversPosition, getSaver } from './types'\nimport { isAssetRuneNative } from './utils/const'\n\n/**\n * Default number of decimals used for THORChain assets.\n */\nconst DEFAULT_THORCHAIN_DECIMALS = 8\n\n/**\n * Default cache instance for Midgard queries.\n */\nconst defaultCache = new MidgardCache()\n\n/**\n * Class for retrieving and processing data from the Midgard API using MidgardCache to optimize request numbers (THORChain L2 API).\n */\nexport class MidgardQuery {\n readonly midgardCache: MidgardCache\n\n /**\n * Constructor to create a MidgardQuery.\n *\n * @param midgardCache - An instance of the MidgardCache (could be pointing to stagenet, testnet, mainnet).\n * @returns MidgardQuery\n */\n constructor(midgardCache = defaultCache) {\n this.midgardCache = midgardCache\n }\n\n /**\n * Get pool by asset.\n *\n * @param {string} asset - For example: BTC.BTC.\n * @returns {PoolDetail} - Details of the selected pool.\n * @throws {Error} - Can't find pool for asset.\n */\n private async getPool(asset: string): Promise<PoolDetail> {\n const pools = await this.midgardCache.getPools()\n const pool = pools.find((pool) => pool.asset === asset)\n if (!pool) {\n throw new Error(`Can't find pool for asset: ${asset}`)\n }\n return pool\n }\n\n /**\n * Get saver positions by an array of saver descriptions.\n *\n * @param {getSaver[]} params - Array of search conditions.\n * @returns {SaversPosition[]} - Information on the positions found.\n */\n public async getSaverPositions(params: getSaver[]): Promise<SaversPosition[]> {\n const addresses: Set<string> = new Set<string>()\n params.forEach((param) => addresses.add(param.address))\n const addressesString: string = Array.from(addresses).join(',')\n const saversDetail = await this.midgardCache.getSavers(addressesString)\n const errors: string[] = []\n\n const saversPositions: SaversPosition[] = []\n const allPositionsPromises = saversDetail.pools.map(async (saver) => {\n const asset = assetFromString(saver.pool)\n\n if (asset) {\n const poolDetail = await this.getPool(saver.pool)\n const depositAmount = new CryptoAmount(baseAmount(saver.assetAdded).minus(saver.assetWithdrawn), asset)\n const ownerUnits = Number(saver?.saverUnits)\n const saverUnits = Number(poolDetail.saversUnits)\n const assetDepth = Number(poolDetail.saversDepth)\n const redeemableValue = (ownerUnits / saverUnits) * assetDepth\n const redeemableAssetAmount = new CryptoAmount(baseAmount(redeemableValue), asset)\n const saverGrowth = redeemableAssetAmount.minus(depositAmount).div(depositAmount).times(100)\n const saversAge = (Date.now() / 1000 - Number(saver.dateLastAdded)) / (365 * 86400)\n\n saversPositions.push({\n depositValue: depositAmount,\n redeemableValue: redeemableAssetAmount,\n lastAddHeight: -1,\n percentageGrowth: saverGrowth.assetAmount.amount().toNumber(),\n ageInYears: saversAge,\n ageInDays: saversAge * 365,\n asset,\n errors,\n })\n }\n })\n await Promise.all(allPositionsPromises)\n return saversPositions\n }\n\n /**\n * Returns the number of decimals for a given asset.\n *\n * @param {Asset} asset - The asset for getting decimals.\n * @returns {number} - Number of decimals from Midgard. Reference: https://gitlab.com/thorchain/midgard#refresh-native-decimals\n */\n public async getDecimalForAsset(asset: Asset): Promise<number> {\n if (isAssetRuneNative(asset) || asset.synth) return DEFAULT_THORCHAIN_DECIMALS\n\n const pool = await this.getPool(assetToString(asset))\n return Number(pool.nativeDecimal)\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,MAAM,oBAAoB,GAAmC;AAC3D,IAAA,OAAO,EAAE;AACP,QAAA,UAAU,EAAE,CAAC;QACb,eAAe,EAAE,CAAC,gCAAgC,CAAC;AACpD,KAAA;AACD,IAAA,QAAQ,EAAE;AACR,QAAA,UAAU,EAAE,CAAC;QACb,eAAe,EAAE,CAAC,yCAAyC,CAAC;AAC7D,KAAA;AACD,IAAA,OAAO,EAAE;AACP,QAAA,UAAU,EAAE,CAAC;QACb,eAAe,EAAE,CAAC,wCAAwC,CAAC;AAC5D,KAAA;CACF,CAAA;MAEY,OAAO,CAAA;AAKlB,IAAA,WAAA,CAAY,OAAmB,GAAA,OAAO,CAAC,OAAO,EAAE,MAAsB,EAAA;AACpE,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;AACtB,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM,aAAN,MAAM,KAAA,KAAA,CAAA,GAAN,MAAM,GAAI,oBAAoB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AAC1D,QAAA,UAAU,CAAC,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,CAAC,gBAAgB,EAAE,CAAC,CAAA;AAC/F,QAAA,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAChD,CAAC,GAAW,KAAK,IAAI,UAAU,CAAC,IAAI,aAAa,CAAC,EAAE,QAAQ,EAAE,GAAG,E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AAC,IAAI,KAAK,CAAC,KAAK;AAAE,gBAAA,OAAO,0BAA0B,CAAA;AAE9E,YAAA,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAA;AACrD,YAAA,OAAO,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;SAClC,CAAA,CAAA;AAAA,KAAA;AACF;;;;"}
1
+ {"version":3,"file":"index.esm.js","sources":["../src/utils/midgard.ts","../src/midgard-cache.ts","../src/utils/const.ts","../src/midgard-query.ts"],"sourcesContent":["import { Network } from '@xchainjs/xchain-client'\nimport {\n Configuration,\n GetActions200Response as ActionHistory,\n MidgardApi,\n PoolDetail,\n ReverseTHORNames,\n SaverDetails,\n THORNameDetails,\n} from '@xchainjs/xchain-midgard'\nimport axios from 'axios'\nimport axiosRetry from 'axios-retry'\n\nimport { GetActionsParams, MidgardConfig } from '../types'\n\nconst defaultMidgardConfig: Record<Network, MidgardConfig> = {\n mainnet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://midgard.ninerealms.com'],\n },\n stagenet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://stagenet-midgard.ninerealms.com'],\n },\n testnet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://testnet.midgard.thorchain.info'],\n },\n}\n\nexport class Midgard {\n private config: MidgardConfig\n readonly network: Network\n private midgardApis: MidgardApi[]\n\n constructor(network: Network = Network.Mainnet, config?: MidgardConfig) {\n this.network = network\n this.config = config ?? defaultMidgardConfig[this.network]\n axiosRetry(axios, { retries: this.config.apiRetries, retryDelay: axiosRetry.exponentialDelay })\n this.midgardApis = this.config.midgardBaseUrls.map(\n (url: string) => new MidgardApi(new Configuration({ basePath: url })),\n )\n }\n\n /**\n * Function to return member details based on valid liquidity position\n * @param address - query can also be multiple addresses should be separated by comma\n * @returns - object type of Member Detail\n */\n public async getSavers(address: string): Promise<SaverDetails> {\n for (const api of this.midgardApis) {\n try {\n const saverDetails = (await api.getSaverDetail(address)).data\n return saverDetails\n } catch (e) {\n // console.error(e)\n }\n }\n throw Error(`Midgard not responding`)\n }\n\n /**\n *\n * @returns an array of Pools\n */\n async getPools(): Promise<PoolDetail[]> {\n for (const api of this.midgardApis) {\n try {\n return (await api.getPools()).data\n } catch (e) {\n // console.error(e)\n }\n }\n throw new Error(`Midgard not responding`)\n }\n\n public async getTHORNameDetails(name: string): Promise<THORNameDetails | undefined> {\n for (const api of this.midgardApis) {\n try {\n const resp = await api.getTHORNameDetail(name)\n if (resp.status == 404) {\n return undefined\n } else if (resp.status == 200) {\n return resp.data\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n if (e.response.status == 404) {\n return undefined\n }\n }\n }\n throw Error(`Midgard not responding`)\n }\n\n public async getTHORNameReverseLookup(address: string): Promise<ReverseTHORNames | undefined> {\n for (const api of this.midgardApis) {\n try {\n const resp = await api.getTHORNamesByAddress(address)\n if (resp.status == 404) {\n return []\n } else if (resp.status == 200) {\n return resp.data\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n if (e.response.status == 404) {\n return []\n }\n }\n }\n throw Error(`Midgard not responding`)\n }\n\n /**\n * List actions along with their related transactions. An action is generated by one or more inbound transactions\n * with the intended action set in the transaction memo. The action may result in one or more outbound transactions.\n * Results are paginated by sets of 50. Filters may be applied to query actions.\n *\n * @param {GetActionsParams} params - Get actions params\n * @param {string} params.address - Comma separated list. Address of sender or recipient of any in/out transaction\n * related to the action.\n * @param {string} params.txid - ID of any in/out tx related to the action\n * @param {string} params.asset - Comma separated list. Any asset that is part of the action (CHAIN.SYMBOL) Additionally,\n * synth, nosynth, and norune filters can be used for swap, add/withdraw actions.\n * @param {ActionType} params.type - One or more comma separated unique types of action (swap, addLiquidity, withdraw,\n * donate, refund, switch)\n * @param {string} params.affiliate - Comma separated list. Affiliate address of the action (swap, refund)\n * @param {number} params.limit - Number of actions returned, default is 50\n * @param {number} params.offset - Pagination offset, default is 0\n * @param {number} params.nextPageToken - If this is given, the actions for the next page will be given\n * @param {number} params.timestamp - If this is given, the actions older than the timestamp will be given\n * @param {number} params.height - If this is given, the actions older than the height will be given\n * @param {number} params.prevPageToken - If this is given, the actions for the previous page will be given\n * @param {number} params.fromTimestamp - If this is given, the actions newer than the timestamp will be given\n * @param {number} params.fromHeight - If this is given, the actions newer than the height will be given\n *\n * @returns {ActionHistory} Array of actions for the given filters\n */\n public async getActions({\n address,\n txid,\n asset,\n type,\n affiliate,\n limit,\n offset,\n nextPageToken,\n timestamp,\n height,\n prevPageToken,\n fromTimestamp,\n fromHeight,\n }: GetActionsParams): Promise<ActionHistory> {\n for (const api of this.midgardApis) {\n try {\n const response = await api.getActions(\n address,\n txid,\n asset,\n type,\n affiliate,\n limit,\n offset,\n nextPageToken,\n timestamp,\n height,\n prevPageToken,\n fromTimestamp,\n fromHeight,\n )\n return response.data\n } catch (e) {}\n }\n throw new Error(`Midgard not responding`)\n }\n}\n","import { PoolDetail, SaverDetails } from '@xchainjs/xchain-midgard'\nimport { CachedValue } from '@xchainjs/xchain-util'\n\nimport { Midgard } from './utils/midgard'\n\nconst MILLISECOND_CACHE_POOLS = 5000\nconst MILLISECOND_CACHE_SAVERS = 5000\n\nconst defaultMidgard = new Midgard()\n\n/**\n * This class retrieves raw information from Midgard API and cached it\n */\n/**\n * Class for caching and retrieving data from Midgard API related to pools and savers.\n */\nexport class MidgardCache {\n readonly midgard: Midgard\n private readonly cachedPools: CachedValue<PoolDetail[]>\n private readonly cachedSavers: CachedValue<SaverDetails>\n\n /**\n * Constructor to create a MidgardCache.\n *\n * @param {Midgard} midgard - An instance of the Midgard API (could be pointing to stagenet, testnet, mainnet).\n * @returns MidgardCache\n */\n constructor(midgard = defaultMidgard) {\n this.midgard = midgard\n this.cachedPools = new CachedValue<PoolDetail[]>(() => this.midgard.getPools(), MILLISECOND_CACHE_POOLS)\n this.cachedSavers = new CachedValue<SaverDetails>(() => this.midgard.getSavers(''), MILLISECOND_CACHE_SAVERS)\n }\n\n /**\n * Get information about existing pools in the protocol from Midgard API.\n *\n * @returns {PoolDetail[]} Array of pools\n */\n public async getPools(): Promise<PoolDetail[]> {\n return this.cachedPools.getValue()\n }\n\n /**\n * Returns the information of all the positions of a set of addresses in the THORChain savers product.\n *\n * @param {String} address - Comma-separated list of addresses.\n * @returns {SaverDetails} Array of savers positions\n */\n public async getSavers(address: string): Promise<SaverDetails> {\n return this.cachedSavers.getValue(address)\n }\n}\n","import { Asset, assetFromStringEx, assetToString } from '@xchainjs/xchain-util'\n\nexport const AssetAVAX = assetFromStringEx('AVAX.AVAX')\nexport const AssetBTC = assetFromStringEx('BTC.BTC')\nexport const AssetATOM = assetFromStringEx('GAIA.ATOM')\nexport const AssetRuneNative = assetFromStringEx('THOR.RUNE')\n\nexport const BTCChain = 'BTC'\nexport const GAIAChain = 'GAIA'\nexport const AVAXChain = 'AVAX'\n\nexport const isAssetRuneNative = (asset: Asset): boolean => assetToString(asset) === assetToString(AssetRuneNative)\n","import { PoolDetail } from '@xchainjs/xchain-midgard'\nimport { Asset, CryptoAmount, assetFromString, assetToString, baseAmount } from '@xchainjs/xchain-util'\n\nimport { MidgardCache } from './midgard-cache'\nimport { ActionHistory, GetActionsParams, SaversPosition, getSaver } from './types'\nimport { isAssetRuneNative } from './utils/const'\n\n/**\n * Default number of decimals used for THORChain assets.\n */\nconst DEFAULT_THORCHAIN_DECIMALS = 8\n\n/**\n * Default cache instance for Midgard queries.\n */\nconst defaultCache = new MidgardCache()\n\n/**\n * Class for retrieving and processing data from the Midgard API using MidgardCache to optimize request numbers (THORChain L2 API).\n */\nexport class MidgardQuery {\n readonly midgardCache: MidgardCache\n\n /**\n * Constructor to create a MidgardQuery.\n *\n * @param midgardCache - An instance of the MidgardCache (could be pointing to stagenet, testnet, mainnet).\n * @returns MidgardQuery\n */\n constructor(midgardCache = defaultCache) {\n this.midgardCache = midgardCache\n }\n\n /**\n * Get pool by asset.\n *\n * @param {string} asset - For example: BTC.BTC.\n * @returns {PoolDetail} - Details of the selected pool.\n * @throws {Error} - Can't find pool for asset.\n */\n private async getPool(asset: string): Promise<PoolDetail> {\n const pools = await this.midgardCache.getPools()\n const pool = pools.find((pool) => pool.asset === asset)\n if (!pool) {\n throw new Error(`Can't find pool for asset: ${asset}`)\n }\n return pool\n }\n\n /**\n * Get saver positions by an array of saver descriptions.\n *\n * @param {getSaver[]} params - Array of search conditions.\n * @returns {SaversPosition[]} - Information on the positions found.\n */\n public async getSaverPositions(params: getSaver[]): Promise<SaversPosition[]> {\n const addresses: Set<string> = new Set<string>()\n params.forEach((param) => addresses.add(param.address))\n const addressesString: string = Array.from(addresses).join(',')\n const saversDetail = await this.midgardCache.getSavers(addressesString)\n const errors: string[] = []\n\n const saversPositions: SaversPosition[] = []\n const allPositionsPromises = saversDetail.pools.map(async (saver) => {\n const asset = assetFromString(saver.pool)\n\n if (asset) {\n const poolDetail = await this.getPool(saver.pool)\n const depositAmount = new CryptoAmount(baseAmount(saver.assetAdded).minus(saver.assetWithdrawn), asset)\n const ownerUnits = Number(saver?.saverUnits)\n const saverUnits = Number(poolDetail.saversUnits)\n const assetDepth = Number(poolDetail.saversDepth)\n const redeemableValue = (ownerUnits / saverUnits) * assetDepth\n const redeemableAssetAmount = new CryptoAmount(baseAmount(redeemableValue), asset)\n const saverGrowth = redeemableAssetAmount.minus(depositAmount).div(depositAmount).times(100)\n const saversAge = (Date.now() / 1000 - Number(saver.dateLastAdded)) / (365 * 86400)\n\n saversPositions.push({\n depositValue: depositAmount,\n redeemableValue: redeemableAssetAmount,\n lastAddHeight: -1,\n percentageGrowth: saverGrowth.assetAmount.amount().toNumber(),\n ageInYears: saversAge,\n ageInDays: saversAge * 365,\n asset,\n errors,\n })\n }\n })\n await Promise.all(allPositionsPromises)\n return saversPositions\n }\n\n /**\n * Returns the number of decimals for a given asset.\n *\n * @param {Asset} asset - The asset for getting decimals.\n * @returns {number} - Number of decimals from Midgard. Reference: https://gitlab.com/thorchain/midgard#refresh-native-decimals\n */\n public async getDecimalForAsset(asset: Asset): Promise<number> {\n if (isAssetRuneNative(asset) || asset.synth) return DEFAULT_THORCHAIN_DECIMALS\n\n const pool = await this.getPool(assetToString(asset))\n return Number(pool.nativeDecimal)\n }\n\n /**\n * List actions along with their related transactions. An action is generated by one or more inbound transactions\n * with the intended action set in the transaction memo. The action may result in one or more outbound transactions.\n * Results are paginated by sets of 50. Filters may be applied to query actions.\n *\n * @param {GetActionsParams} params - Get actions params\n * @param {string} params.address - Comma separated list. Address of sender or recipient of any in/out transaction\n * related to the action.\n * @param {string} params.txid - ID of any in/out tx related to the action\n * @param {string} params.asset - Comma separated list. Any asset that is part of the action (CHAIN.SYMBOL) Additionally,\n * synth, nosynth, and norune filters can be used for swap, add/withdraw actions.\n * @param {ActionType} params.type - One or more comma separated unique types of action (swap, addLiquidity, withdraw,\n * donate, refund, switch)\n * @param {string} params.affiliate - Comma separated list. Affiliate address of the action (swap, refund)\n * @param {number} params.limit - Number of actions returned, default is 50\n * @param {number} params.offset - Pagination offset, default is 0\n * @param {number} params.nextPageToken - If this is given, the actions for the next page will be given\n * @param {number} params.timestamp - If this is given, the actions older than the timestamp will be given\n * @param {number} params.height - If this is given, the actions older than the height will be given\n * @param {number} params.prevPageToken - If this is given, the actions for the previous page will be given\n * @param {number} params.fromTimestamp - If this is given, the actions newer than the timestamp will be given\n * @param {number} params.fromHeight - If this is given, the actions newer than the height will be given\n *\n * @returns {ActionHistory} Array of actions for the given filters\n */\n public async getActions({\n address,\n txid,\n asset,\n type,\n affiliate,\n limit,\n offset,\n nextPageToken,\n timestamp,\n height,\n prevPageToken,\n fromTimestamp,\n fromHeight,\n }: GetActionsParams): Promise<ActionHistory> {\n return this.midgardCache.midgard.getActions({\n address,\n txid,\n asset,\n type,\n affiliate,\n limit,\n offset,\n nextPageToken,\n timestamp,\n height,\n prevPageToken,\n fromTimestamp,\n fromHeight,\n })\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;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EAAE,CAAC,QAAQ,EAAE;AAC7D,wBAAA,UAAU,EAAE,SAAS;wBACrB,SAAS,EAAE,SAAS,GAAG,GAAG;wBAC1B,KAAK;wBACL,MAAM;AACP,qBAAA,CAAC,CAAA;AACH,iBAAA;aACF,CAAA,CAAC,CAAA;AACF,YAAA,MAAM,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAA;AACvC,YAAA,OAAO,eAAe,CAAA;SACvB,CAAA,CAAA;AAAA,KAAA;AAED;;;;;AAKG;AACU,IAAA,kBAAkB,CAAC,KAAY,EAAA;;AAC1C,YAAA,IAAI,iBAAiB,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,KAAK;AAAE,gBAAA,OAAO,0BAA0B,CAAA;AAE9E,YAAA,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAA;AACrD,YAAA,OAAO,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;SAClC,CAAA,CAAA;AAAA,KAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;AAwBG;IACU,UAAU,CAAC,EACtB,OAAO,EACP,IAAI,EACJ,KAAK,EACL,IAAI,EACJ,SAAS,EACT,KAAK,EACL,MAAM,EACN,aAAa,EACb,SAAS,EACT,MAAM,EACN,aAAa,EACb,aAAa,EACb,UAAU,GACO,EAAA;;AACjB,YAAA,OAAO,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,UAAU,CAAC;gBAC1C,OAAO;gBACP,IAAI;gBACJ,KAAK;gBACL,IAAI;gBACJ,SAAS;gBACT,KAAK;gBACL,MAAM;gBACN,aAAa;gBACb,SAAS;gBACT,MAAM;gBACN,aAAa;gBACb,aAAa;gBACb,UAAU;AACX,aAAA,CAAC,CAAA;SACH,CAAA,CAAA;AAAA,KAAA;AACF;;;;"}
package/lib/index.js CHANGED
@@ -139,6 +139,43 @@ class Midgard {
139
139
  throw Error(`Midgard not responding`);
140
140
  });
141
141
  }
142
+ /**
143
+ * List actions along with their related transactions. An action is generated by one or more inbound transactions
144
+ * with the intended action set in the transaction memo. The action may result in one or more outbound transactions.
145
+ * Results are paginated by sets of 50. Filters may be applied to query actions.
146
+ *
147
+ * @param {GetActionsParams} params - Get actions params
148
+ * @param {string} params.address - Comma separated list. Address of sender or recipient of any in/out transaction
149
+ * related to the action.
150
+ * @param {string} params.txid - ID of any in/out tx related to the action
151
+ * @param {string} params.asset - Comma separated list. Any asset that is part of the action (CHAIN.SYMBOL) Additionally,
152
+ * synth, nosynth, and norune filters can be used for swap, add/withdraw actions.
153
+ * @param {ActionType} params.type - One or more comma separated unique types of action (swap, addLiquidity, withdraw,
154
+ * donate, refund, switch)
155
+ * @param {string} params.affiliate - Comma separated list. Affiliate address of the action (swap, refund)
156
+ * @param {number} params.limit - Number of actions returned, default is 50
157
+ * @param {number} params.offset - Pagination offset, default is 0
158
+ * @param {number} params.nextPageToken - If this is given, the actions for the next page will be given
159
+ * @param {number} params.timestamp - If this is given, the actions older than the timestamp will be given
160
+ * @param {number} params.height - If this is given, the actions older than the height will be given
161
+ * @param {number} params.prevPageToken - If this is given, the actions for the previous page will be given
162
+ * @param {number} params.fromTimestamp - If this is given, the actions newer than the timestamp will be given
163
+ * @param {number} params.fromHeight - If this is given, the actions newer than the height will be given
164
+ *
165
+ * @returns {ActionHistory} Array of actions for the given filters
166
+ */
167
+ getActions({ address, txid, asset, type, affiliate, limit, offset, nextPageToken, timestamp, height, prevPageToken, fromTimestamp, fromHeight, }) {
168
+ return __awaiter(this, void 0, void 0, function* () {
169
+ for (const api of this.midgardApis) {
170
+ try {
171
+ const response = yield api.getActions(address, txid, asset, type, affiliate, limit, offset, nextPageToken, timestamp, height, prevPageToken, fromTimestamp, fromHeight);
172
+ return response.data;
173
+ }
174
+ catch (e) { }
175
+ }
176
+ throw new Error(`Midgard not responding`);
177
+ });
178
+ }
142
179
  }
143
180
 
144
181
  const MILLISECOND_CACHE_POOLS = 5000;
@@ -285,6 +322,50 @@ class MidgardQuery {
285
322
  return Number(pool.nativeDecimal);
286
323
  });
287
324
  }
325
+ /**
326
+ * List actions along with their related transactions. An action is generated by one or more inbound transactions
327
+ * with the intended action set in the transaction memo. The action may result in one or more outbound transactions.
328
+ * Results are paginated by sets of 50. Filters may be applied to query actions.
329
+ *
330
+ * @param {GetActionsParams} params - Get actions params
331
+ * @param {string} params.address - Comma separated list. Address of sender or recipient of any in/out transaction
332
+ * related to the action.
333
+ * @param {string} params.txid - ID of any in/out tx related to the action
334
+ * @param {string} params.asset - Comma separated list. Any asset that is part of the action (CHAIN.SYMBOL) Additionally,
335
+ * synth, nosynth, and norune filters can be used for swap, add/withdraw actions.
336
+ * @param {ActionType} params.type - One or more comma separated unique types of action (swap, addLiquidity, withdraw,
337
+ * donate, refund, switch)
338
+ * @param {string} params.affiliate - Comma separated list. Affiliate address of the action (swap, refund)
339
+ * @param {number} params.limit - Number of actions returned, default is 50
340
+ * @param {number} params.offset - Pagination offset, default is 0
341
+ * @param {number} params.nextPageToken - If this is given, the actions for the next page will be given
342
+ * @param {number} params.timestamp - If this is given, the actions older than the timestamp will be given
343
+ * @param {number} params.height - If this is given, the actions older than the height will be given
344
+ * @param {number} params.prevPageToken - If this is given, the actions for the previous page will be given
345
+ * @param {number} params.fromTimestamp - If this is given, the actions newer than the timestamp will be given
346
+ * @param {number} params.fromHeight - If this is given, the actions newer than the height will be given
347
+ *
348
+ * @returns {ActionHistory} Array of actions for the given filters
349
+ */
350
+ getActions({ address, txid, asset, type, affiliate, limit, offset, nextPageToken, timestamp, height, prevPageToken, fromTimestamp, fromHeight, }) {
351
+ return __awaiter(this, void 0, void 0, function* () {
352
+ return this.midgardCache.midgard.getActions({
353
+ address,
354
+ txid,
355
+ asset,
356
+ type,
357
+ affiliate,
358
+ limit,
359
+ offset,
360
+ nextPageToken,
361
+ timestamp,
362
+ height,
363
+ prevPageToken,
364
+ fromTimestamp,
365
+ fromHeight,
366
+ });
367
+ });
368
+ }
288
369
  }
289
370
 
290
371
  exports.Midgard = Midgard;
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/utils/midgard.ts","../src/midgard-cache.ts","../src/utils/const.ts","../src/midgard-query.ts"],"sourcesContent":["import { Network } from '@xchainjs/xchain-client'\nimport {\n Configuration,\n MidgardApi,\n PoolDetail,\n ReverseTHORNames,\n SaverDetails,\n THORNameDetails,\n} from '@xchainjs/xchain-midgard'\nimport axios from 'axios'\nimport axiosRetry from 'axios-retry'\n\nimport { MidgardConfig } from '../types'\n\nconst defaultMidgardConfig: Record<Network, MidgardConfig> = {\n mainnet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://midgard.ninerealms.com'],\n },\n stagenet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://stagenet-midgard.ninerealms.com'],\n },\n testnet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://testnet.midgard.thorchain.info'],\n },\n}\n\nexport class Midgard {\n private config: MidgardConfig\n readonly network: Network\n private midgardApis: MidgardApi[]\n\n constructor(network: Network = Network.Mainnet, config?: MidgardConfig) {\n this.network = network\n this.config = config ?? defaultMidgardConfig[this.network]\n axiosRetry(axios, { retries: this.config.apiRetries, retryDelay: axiosRetry.exponentialDelay })\n this.midgardApis = this.config.midgardBaseUrls.map(\n (url: string) => new MidgardApi(new Configuration({ basePath: url })),\n )\n }\n\n /**\n * Function to return member details based on valid liquidity position\n * @param address - query can also be multiple addresses should be separated by comma\n * @returns - object type of Member Detail\n */\n public async getSavers(address: string): Promise<SaverDetails> {\n for (const api of this.midgardApis) {\n try {\n const saverDetails = (await api.getSaverDetail(address)).data\n return saverDetails\n } catch (e) {\n // console.error(e)\n }\n }\n throw Error(`Midgard not responding`)\n }\n\n /**\n *\n * @returns an array of Pools\n */\n async getPools(): Promise<PoolDetail[]> {\n for (const api of this.midgardApis) {\n try {\n return (await api.getPools()).data\n } catch (e) {\n // console.error(e)\n }\n }\n throw new Error(`Midgard not responding`)\n }\n\n public async getTHORNameDetails(name: string): Promise<THORNameDetails | undefined> {\n for (const api of this.midgardApis) {\n try {\n const resp = await api.getTHORNameDetail(name)\n if (resp.status == 404) {\n return undefined\n } else if (resp.status == 200) {\n return resp.data\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n if (e.response.status == 404) {\n return undefined\n }\n }\n }\n throw Error(`Midgard not responding`)\n }\n\n public async getTHORNameReverseLookup(address: string): Promise<ReverseTHORNames | undefined> {\n for (const api of this.midgardApis) {\n try {\n const resp = await api.getTHORNamesByAddress(address)\n if (resp.status == 404) {\n return []\n } else if (resp.status == 200) {\n return resp.data\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n if (e.response.status == 404) {\n return []\n }\n }\n }\n throw Error(`Midgard not responding`)\n }\n}\n","import { PoolDetail, SaverDetails } from '@xchainjs/xchain-midgard'\nimport { CachedValue } from '@xchainjs/xchain-util'\n\nimport { Midgard } from './utils/midgard'\n\nconst MILLISECOND_CACHE_POOLS = 5000\nconst MILLISECOND_CACHE_SAVERS = 5000\n\nconst defaultMidgard = new Midgard()\n\n/**\n * This class retrieves raw information from Midgard API and cached it\n */\n/**\n * Class for caching and retrieving data from Midgard API related to pools and savers.\n */\nexport class MidgardCache {\n readonly midgard: Midgard\n private readonly cachedPools: CachedValue<PoolDetail[]>\n private readonly cachedSavers: CachedValue<SaverDetails>\n\n /**\n * Constructor to create a MidgardCache.\n *\n * @param {Midgard} midgard - An instance of the Midgard API (could be pointing to stagenet, testnet, mainnet).\n * @returns MidgardCache\n */\n constructor(midgard = defaultMidgard) {\n this.midgard = midgard\n this.cachedPools = new CachedValue<PoolDetail[]>(() => this.midgard.getPools(), MILLISECOND_CACHE_POOLS)\n this.cachedSavers = new CachedValue<SaverDetails>(() => this.midgard.getSavers(''), MILLISECOND_CACHE_SAVERS)\n }\n\n /**\n * Get information about existing pools in the protocol from Midgard API.\n *\n * @returns {PoolDetail[]} Array of pools\n */\n public async getPools(): Promise<PoolDetail[]> {\n return this.cachedPools.getValue()\n }\n\n /**\n * Returns the information of all the positions of a set of addresses in the THORChain savers product.\n *\n * @param {String} address - Comma-separated list of addresses.\n * @returns {SaverDetails} Array of savers positions\n */\n public async getSavers(address: string): Promise<SaverDetails> {\n return this.cachedSavers.getValue(address)\n }\n}\n","import { Asset, assetFromStringEx, assetToString } from '@xchainjs/xchain-util'\n\nexport const AssetAVAX = assetFromStringEx('AVAX.AVAX')\nexport const AssetBTC = assetFromStringEx('BTC.BTC')\nexport const AssetATOM = assetFromStringEx('GAIA.ATOM')\nexport const AssetRuneNative = assetFromStringEx('THOR.RUNE')\n\nexport const BTCChain = 'BTC'\nexport const GAIAChain = 'GAIA'\nexport const AVAXChain = 'AVAX'\n\nexport const isAssetRuneNative = (asset: Asset): boolean => assetToString(asset) === assetToString(AssetRuneNative)\n","import { PoolDetail } from '@xchainjs/xchain-midgard'\nimport { Asset, CryptoAmount, assetFromString, assetToString, baseAmount } from '@xchainjs/xchain-util'\n\nimport { MidgardCache } from './midgard-cache'\nimport { SaversPosition, getSaver } from './types'\nimport { isAssetRuneNative } from './utils/const'\n\n/**\n * Default number of decimals used for THORChain assets.\n */\nconst DEFAULT_THORCHAIN_DECIMALS = 8\n\n/**\n * Default cache instance for Midgard queries.\n */\nconst defaultCache = new MidgardCache()\n\n/**\n * Class for retrieving and processing data from the Midgard API using MidgardCache to optimize request numbers (THORChain L2 API).\n */\nexport class MidgardQuery {\n readonly midgardCache: MidgardCache\n\n /**\n * Constructor to create a MidgardQuery.\n *\n * @param midgardCache - An instance of the MidgardCache (could be pointing to stagenet, testnet, mainnet).\n * @returns MidgardQuery\n */\n constructor(midgardCache = defaultCache) {\n this.midgardCache = midgardCache\n }\n\n /**\n * Get pool by asset.\n *\n * @param {string} asset - For example: BTC.BTC.\n * @returns {PoolDetail} - Details of the selected pool.\n * @throws {Error} - Can't find pool for asset.\n */\n private async getPool(asset: string): Promise<PoolDetail> {\n const pools = await this.midgardCache.getPools()\n const pool = pools.find((pool) => pool.asset === asset)\n if (!pool) {\n throw new Error(`Can't find pool for asset: ${asset}`)\n }\n return pool\n }\n\n /**\n * Get saver positions by an array of saver descriptions.\n *\n * @param {getSaver[]} params - Array of search conditions.\n * @returns {SaversPosition[]} - Information on the positions found.\n */\n public async getSaverPositions(params: getSaver[]): Promise<SaversPosition[]> {\n const addresses: Set<string> = new Set<string>()\n params.forEach((param) => addresses.add(param.address))\n const addressesString: string = Array.from(addresses).join(',')\n const saversDetail = await this.midgardCache.getSavers(addressesString)\n const errors: string[] = []\n\n const saversPositions: SaversPosition[] = []\n const allPositionsPromises = saversDetail.pools.map(async (saver) => {\n const asset = assetFromString(saver.pool)\n\n if (asset) {\n const poolDetail = await this.getPool(saver.pool)\n const depositAmount = new CryptoAmount(baseAmount(saver.assetAdded).minus(saver.assetWithdrawn), asset)\n const ownerUnits = Number(saver?.saverUnits)\n const saverUnits = Number(poolDetail.saversUnits)\n const assetDepth = Number(poolDetail.saversDepth)\n const redeemableValue = (ownerUnits / saverUnits) * assetDepth\n const redeemableAssetAmount = new CryptoAmount(baseAmount(redeemableValue), asset)\n const saverGrowth = redeemableAssetAmount.minus(depositAmount).div(depositAmount).times(100)\n const saversAge = (Date.now() / 1000 - Number(saver.dateLastAdded)) / (365 * 86400)\n\n saversPositions.push({\n depositValue: depositAmount,\n redeemableValue: redeemableAssetAmount,\n lastAddHeight: -1,\n percentageGrowth: saverGrowth.assetAmount.amount().toNumber(),\n ageInYears: saversAge,\n ageInDays: saversAge * 365,\n asset,\n errors,\n })\n }\n })\n await Promise.all(allPositionsPromises)\n return saversPositions\n }\n\n /**\n * Returns the number of decimals for a given asset.\n *\n * @param {Asset} asset - The asset for getting decimals.\n * @returns {number} - Number of decimals from Midgard. Reference: https://gitlab.com/thorchain/midgard#refresh-native-decimals\n */\n public async getDecimalForAsset(asset: Asset): Promise<number> {\n if (isAssetRuneNative(asset) || asset.synth) return DEFAULT_THORCHAIN_DECIMALS\n\n const pool = await this.getPool(assetToString(asset))\n return Number(pool.nativeDecimal)\n }\n}\n"],"names":["Network","axiosRetry","axios","MidgardApi","Configuration","CachedValue","assetFromStringEx","assetToString","assetFromString","CryptoAmount","baseAmount"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,MAAM,oBAAoB,GAAmC;AAC3D,IAAA,OAAO,EAAE;AACP,QAAA,UAAU,EAAE,CAAC;QACb,eAAe,EAAE,CAAC,gCAAgC,CAAC;AACpD,KAAA;AACD,IAAA,QAAQ,EAAE;AACR,QAAA,UAAU,EAAE,CAAC;QACb,eAAe,EAAE,CAAC,yCAAyC,CAAC;AAC7D,KAAA;AACD,IAAA,OAAO,EAAE;AACP,QAAA,UAAU,EAAE,CAAC;QACb,eAAe,EAAE,CAAC,wCAAwC,CAAC;AAC5D,KAAA;CACF,CAAA;MAEY,OAAO,CAAA;AAKlB,IAAA,WAAA,CAAY,OAAmB,GAAAA,oBAAO,CAAC,OAAO,EAAE,MAAsB,EAAA;AACpE,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;AACtB,QAAA,IAAI,CAAC,MAAM,GAAG,MAAM,aAAN,MAAM,KAAA,KAAA,CAAA,GAAN,MAAM,GAAI,oBAAoB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AAC1D,QAAAC,8BAAU,CAACC,yBAAK,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,UAAU,EAAED,8BAAU,CAAC,gBAAgB,EAAE,CAAC,CAAA;AAC/F,QAAA,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,GAAG,CAChD,CAAC,GAAW,KAAK,IAAIE,wBAAU,CAAC,IAAIC,2BAAa,CAAC,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC,CAAC,CACtE,CAAA;KACF;AAED;;;;AAIG;AACU,IAAA,SAAS,CAAC,OAAe,EAAA;;AACpC,YAAA,KA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MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,OAAO,CAACH,wBAAa,CAAC,KAAK,CAAC,CAAC,CAAA;AACrD,YAAA,OAAO,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;SAClC,CAAA,CAAA;AAAA,KAAA;AACF;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/utils/midgard.ts","../src/midgard-cache.ts","../src/utils/const.ts","../src/midgard-query.ts"],"sourcesContent":["import { Network } from '@xchainjs/xchain-client'\nimport {\n Configuration,\n GetActions200Response as ActionHistory,\n MidgardApi,\n PoolDetail,\n ReverseTHORNames,\n SaverDetails,\n THORNameDetails,\n} from '@xchainjs/xchain-midgard'\nimport axios from 'axios'\nimport axiosRetry from 'axios-retry'\n\nimport { GetActionsParams, MidgardConfig } from '../types'\n\nconst defaultMidgardConfig: Record<Network, MidgardConfig> = {\n mainnet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://midgard.ninerealms.com'],\n },\n stagenet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://stagenet-midgard.ninerealms.com'],\n },\n testnet: {\n apiRetries: 3,\n midgardBaseUrls: ['https://testnet.midgard.thorchain.info'],\n },\n}\n\nexport class Midgard {\n private config: MidgardConfig\n readonly network: Network\n private midgardApis: MidgardApi[]\n\n constructor(network: Network = Network.Mainnet, config?: MidgardConfig) {\n this.network = network\n this.config = config ?? defaultMidgardConfig[this.network]\n axiosRetry(axios, { retries: this.config.apiRetries, retryDelay: axiosRetry.exponentialDelay })\n this.midgardApis = this.config.midgardBaseUrls.map(\n (url: string) => new MidgardApi(new Configuration({ basePath: url })),\n )\n }\n\n /**\n * Function to return member details based on valid liquidity position\n * @param address - query can also be multiple addresses should be separated by comma\n * @returns - object type of Member Detail\n */\n public async getSavers(address: string): Promise<SaverDetails> {\n for (const api of this.midgardApis) {\n try {\n const saverDetails = (await api.getSaverDetail(address)).data\n return saverDetails\n } catch (e) {\n // console.error(e)\n }\n }\n throw Error(`Midgard not responding`)\n }\n\n /**\n *\n * @returns an array of Pools\n */\n async getPools(): Promise<PoolDetail[]> {\n for (const api of this.midgardApis) {\n try {\n return (await api.getPools()).data\n } catch (e) {\n // console.error(e)\n }\n }\n throw new Error(`Midgard not responding`)\n }\n\n public async getTHORNameDetails(name: string): Promise<THORNameDetails | undefined> {\n for (const api of this.midgardApis) {\n try {\n const resp = await api.getTHORNameDetail(name)\n if (resp.status == 404) {\n return undefined\n } else if (resp.status == 200) {\n return resp.data\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n if (e.response.status == 404) {\n return undefined\n }\n }\n }\n throw Error(`Midgard not responding`)\n }\n\n public async getTHORNameReverseLookup(address: string): Promise<ReverseTHORNames | undefined> {\n for (const api of this.midgardApis) {\n try {\n const resp = await api.getTHORNamesByAddress(address)\n if (resp.status == 404) {\n return []\n } else if (resp.status == 200) {\n return resp.data\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n if (e.response.status == 404) {\n return []\n }\n }\n }\n throw Error(`Midgard not responding`)\n }\n\n /**\n * List actions along with their related transactions. An action is generated by one or more inbound transactions\n * with the intended action set in the transaction memo. The action may result in one or more outbound transactions.\n * Results are paginated by sets of 50. Filters may be applied to query actions.\n *\n * @param {GetActionsParams} params - Get actions params\n * @param {string} params.address - Comma separated list. Address of sender or recipient of any in/out transaction\n * related to the action.\n * @param {string} params.txid - ID of any in/out tx related to the action\n * @param {string} params.asset - Comma separated list. Any asset that is part of the action (CHAIN.SYMBOL) Additionally,\n * synth, nosynth, and norune filters can be used for swap, add/withdraw actions.\n * @param {ActionType} params.type - One or more comma separated unique types of action (swap, addLiquidity, withdraw,\n * donate, refund, switch)\n * @param {string} params.affiliate - Comma separated list. Affiliate address of the action (swap, refund)\n * @param {number} params.limit - Number of actions returned, default is 50\n * @param {number} params.offset - Pagination offset, default is 0\n * @param {number} params.nextPageToken - If this is given, the actions for the next page will be given\n * @param {number} params.timestamp - If this is given, the actions older than the timestamp will be given\n * @param {number} params.height - If this is given, the actions older than the height will be given\n * @param {number} params.prevPageToken - If this is given, the actions for the previous page will be given\n * @param {number} params.fromTimestamp - If this is given, the actions newer than the timestamp will be given\n * @param {number} params.fromHeight - If this is given, the actions newer than the height will be given\n *\n * @returns {ActionHistory} Array of actions for the given filters\n */\n public async getActions({\n address,\n txid,\n asset,\n type,\n affiliate,\n limit,\n offset,\n nextPageToken,\n timestamp,\n height,\n prevPageToken,\n fromTimestamp,\n fromHeight,\n }: GetActionsParams): Promise<ActionHistory> {\n for (const api of this.midgardApis) {\n try {\n const response = await api.getActions(\n address,\n txid,\n asset,\n type,\n affiliate,\n limit,\n offset,\n nextPageToken,\n timestamp,\n height,\n prevPageToken,\n fromTimestamp,\n fromHeight,\n )\n return response.data\n } catch (e) {}\n }\n throw new Error(`Midgard not responding`)\n }\n}\n","import { PoolDetail, SaverDetails } from '@xchainjs/xchain-midgard'\nimport { CachedValue } from '@xchainjs/xchain-util'\n\nimport { Midgard } from './utils/midgard'\n\nconst MILLISECOND_CACHE_POOLS = 5000\nconst MILLISECOND_CACHE_SAVERS = 5000\n\nconst defaultMidgard = new Midgard()\n\n/**\n * This class retrieves raw information from Midgard API and cached it\n */\n/**\n * Class for caching and retrieving data from Midgard API related to pools and savers.\n */\nexport class MidgardCache {\n readonly midgard: Midgard\n private readonly cachedPools: CachedValue<PoolDetail[]>\n private readonly cachedSavers: CachedValue<SaverDetails>\n\n /**\n * Constructor to create a MidgardCache.\n *\n * @param {Midgard} midgard - An instance of the Midgard API (could be pointing to stagenet, testnet, mainnet).\n * @returns MidgardCache\n */\n constructor(midgard = defaultMidgard) {\n this.midgard = midgard\n this.cachedPools = new CachedValue<PoolDetail[]>(() => this.midgard.getPools(), MILLISECOND_CACHE_POOLS)\n this.cachedSavers = new CachedValue<SaverDetails>(() => this.midgard.getSavers(''), MILLISECOND_CACHE_SAVERS)\n }\n\n /**\n * Get information about existing pools in the protocol from Midgard API.\n *\n * @returns {PoolDetail[]} Array of pools\n */\n public async getPools(): Promise<PoolDetail[]> {\n return this.cachedPools.getValue()\n }\n\n /**\n * Returns the information of all the positions of a set of addresses in the THORChain savers product.\n *\n * @param {String} address - Comma-separated list of addresses.\n * @returns {SaverDetails} Array of savers positions\n */\n public async getSavers(address: string): Promise<SaverDetails> {\n return this.cachedSavers.getValue(address)\n }\n}\n","import { Asset, assetFromStringEx, assetToString } from '@xchainjs/xchain-util'\n\nexport const AssetAVAX = assetFromStringEx('AVAX.AVAX')\nexport const AssetBTC = assetFromStringEx('BTC.BTC')\nexport const AssetATOM = assetFromStringEx('GAIA.ATOM')\nexport const AssetRuneNative = assetFromStringEx('THOR.RUNE')\n\nexport const BTCChain = 'BTC'\nexport const GAIAChain = 'GAIA'\nexport const AVAXChain = 'AVAX'\n\nexport const isAssetRuneNative = (asset: Asset): boolean => assetToString(asset) === assetToString(AssetRuneNative)\n","import { PoolDetail } from '@xchainjs/xchain-midgard'\nimport { Asset, CryptoAmount, assetFromString, assetToString, baseAmount } from '@xchainjs/xchain-util'\n\nimport { MidgardCache } from './midgard-cache'\nimport { ActionHistory, GetActionsParams, SaversPosition, getSaver } from './types'\nimport { isAssetRuneNative } from './utils/const'\n\n/**\n * Default number of decimals used for THORChain assets.\n */\nconst DEFAULT_THORCHAIN_DECIMALS = 8\n\n/**\n * Default cache instance for Midgard queries.\n */\nconst defaultCache = new MidgardCache()\n\n/**\n * Class for retrieving and processing data from the Midgard API using MidgardCache to optimize request numbers (THORChain L2 API).\n */\nexport class MidgardQuery {\n readonly midgardCache: MidgardCache\n\n /**\n * Constructor to create a MidgardQuery.\n *\n * @param midgardCache - An instance of the MidgardCache (could be pointing to stagenet, testnet, mainnet).\n * @returns MidgardQuery\n */\n constructor(midgardCache = defaultCache) {\n this.midgardCache = midgardCache\n }\n\n /**\n * Get pool by asset.\n *\n * @param {string} asset - For example: BTC.BTC.\n * @returns {PoolDetail} - Details of the selected pool.\n * @throws {Error} - Can't find pool for asset.\n */\n private async getPool(asset: string): Promise<PoolDetail> {\n const pools = await this.midgardCache.getPools()\n const pool = pools.find((pool) => pool.asset === asset)\n if (!pool) {\n throw new Error(`Can't find pool for asset: ${asset}`)\n }\n return pool\n }\n\n /**\n * Get saver positions by an array of saver descriptions.\n *\n * @param {getSaver[]} params - Array of search conditions.\n * @returns {SaversPosition[]} - Information on the positions found.\n */\n public async getSaverPositions(params: getSaver[]): Promise<SaversPosition[]> {\n const addresses: Set<string> = new Set<string>()\n params.forEach((param) => addresses.add(param.address))\n const addressesString: string = Array.from(addresses).join(',')\n const saversDetail = await this.midgardCache.getSavers(addressesString)\n const errors: string[] = []\n\n const saversPositions: SaversPosition[] = []\n const allPositionsPromises = saversDetail.pools.map(async (saver) => {\n const asset = assetFromString(saver.pool)\n\n if (asset) {\n const poolDetail = await this.getPool(saver.pool)\n const depositAmount = new CryptoAmount(baseAmount(saver.assetAdded).minus(saver.assetWithdrawn), asset)\n const ownerUnits = Number(saver?.saverUnits)\n const saverUnits = Number(poolDetail.saversUnits)\n const assetDepth = Number(poolDetail.saversDepth)\n const redeemableValue = (ownerUnits / saverUnits) * assetDepth\n const redeemableAssetAmount = new CryptoAmount(baseAmount(redeemableValue), asset)\n const saverGrowth = redeemableAssetAmount.minus(depositAmount).div(depositAmount).times(100)\n const saversAge = (Date.now() / 1000 - Number(saver.dateLastAdded)) / (365 * 86400)\n\n saversPositions.push({\n depositValue: depositAmount,\n redeemableValue: redeemableAssetAmount,\n lastAddHeight: -1,\n percentageGrowth: saverGrowth.assetAmount.amount().toNumber(),\n ageInYears: saversAge,\n ageInDays: saversAge * 365,\n asset,\n errors,\n })\n }\n })\n await Promise.all(allPositionsPromises)\n return saversPositions\n }\n\n /**\n * Returns the number of decimals for a given asset.\n *\n * @param {Asset} asset - The asset for getting decimals.\n * @returns {number} - Number of decimals from Midgard. Reference: https://gitlab.com/thorchain/midgard#refresh-native-decimals\n */\n public async getDecimalForAsset(asset: Asset): Promise<number> {\n if (isAssetRuneNative(asset) || asset.synth) return DEFAULT_THORCHAIN_DECIMALS\n\n const pool = await this.getPool(assetToString(asset))\n return Number(pool.nativeDecimal)\n }\n\n /**\n * List actions along with their related transactions. An action is generated by one or more inbound transactions\n * with the intended action set in the transaction memo. The action may result in one or more outbound transactions.\n * Results are paginated by sets of 50. Filters may be applied to query actions.\n *\n * @param {GetActionsParams} params - Get actions params\n * @param {string} params.address - Comma separated list. Address of sender or recipient of any in/out transaction\n * related to the action.\n * @param {string} params.txid - ID of any in/out tx related to the action\n * @param {string} params.asset - Comma separated list. Any asset that is part of the action (CHAIN.SYMBOL) Additionally,\n * synth, nosynth, and norune filters can be used for swap, add/withdraw actions.\n * @param {ActionType} params.type - One or more comma separated unique types of action (swap, addLiquidity, withdraw,\n * donate, refund, switch)\n * @param {string} params.affiliate - Comma separated list. Affiliate address of the action (swap, refund)\n * @param {number} params.limit - Number of actions returned, default is 50\n * @param {number} params.offset - Pagination offset, default is 0\n * @param {number} params.nextPageToken - If this is given, the actions for the next page will be given\n * @param {number} params.timestamp - If this is given, the actions older than the timestamp will be given\n * @param {number} params.height - If this is given, the actions older than the height will be given\n * @param {number} params.prevPageToken - If this is given, the actions for the previous page will be given\n * @param {number} params.fromTimestamp - If this is given, the actions newer than the timestamp will be given\n * @param {number} params.fromHeight - If this is given, the actions newer than the height will be given\n *\n * @returns {ActionHistory} Array of actions for the given filters\n */\n public async getActions({\n address,\n txid,\n asset,\n type,\n affiliate,\n limit,\n offset,\n nextPageToken,\n timestamp,\n height,\n prevPageToken,\n fromTimestamp,\n fromHeight,\n }: GetActionsParams): Promise<ActionHistory> {\n return this.midgardCache.midgard.getActions({\n address,\n txid,\n asset,\n type,\n affiliate,\n limit,\n offset,\n nextPageToken,\n timestamp,\n height,\n prevPageToken,\n fromTimestamp,\n fromHeight,\n })\n }\n}\n"],"names":["Network","axiosRetry","axios","MidgardApi","Configuration","CachedValue","assetFromStringEx","assetToString","assetFromString","CryptoAmount","baseAmount"],"mappings":";;;;;;;;;;;;;;;;;;;;;;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asset } from '@xchainjs/xchain-util';
2
2
  import { MidgardCache } from './midgard-cache';
3
- import { SaversPosition, getSaver } from './types';
3
+ import { ActionHistory, GetActionsParams, SaversPosition, getSaver } from './types';
4
4
  /**
5
5
  * Class for retrieving and processing data from the Midgard API using MidgardCache to optimize request numbers (THORChain L2 API).
6
6
  */
@@ -35,4 +35,30 @@ export declare class MidgardQuery {
35
35
  * @returns {number} - Number of decimals from Midgard. Reference: https://gitlab.com/thorchain/midgard#refresh-native-decimals
36
36
  */
37
37
  getDecimalForAsset(asset: Asset): Promise<number>;
38
+ /**
39
+ * List actions along with their related transactions. An action is generated by one or more inbound transactions
40
+ * with the intended action set in the transaction memo. The action may result in one or more outbound transactions.
41
+ * Results are paginated by sets of 50. Filters may be applied to query actions.
42
+ *
43
+ * @param {GetActionsParams} params - Get actions params
44
+ * @param {string} params.address - Comma separated list. Address of sender or recipient of any in/out transaction
45
+ * related to the action.
46
+ * @param {string} params.txid - ID of any in/out tx related to the action
47
+ * @param {string} params.asset - Comma separated list. Any asset that is part of the action (CHAIN.SYMBOL) Additionally,
48
+ * synth, nosynth, and norune filters can be used for swap, add/withdraw actions.
49
+ * @param {ActionType} params.type - One or more comma separated unique types of action (swap, addLiquidity, withdraw,
50
+ * donate, refund, switch)
51
+ * @param {string} params.affiliate - Comma separated list. Affiliate address of the action (swap, refund)
52
+ * @param {number} params.limit - Number of actions returned, default is 50
53
+ * @param {number} params.offset - Pagination offset, default is 0
54
+ * @param {number} params.nextPageToken - If this is given, the actions for the next page will be given
55
+ * @param {number} params.timestamp - If this is given, the actions older than the timestamp will be given
56
+ * @param {number} params.height - If this is given, the actions older than the height will be given
57
+ * @param {number} params.prevPageToken - If this is given, the actions for the previous page will be given
58
+ * @param {number} params.fromTimestamp - If this is given, the actions newer than the timestamp will be given
59
+ * @param {number} params.fromHeight - If this is given, the actions newer than the height will be given
60
+ *
61
+ * @returns {ActionHistory} Array of actions for the given filters
62
+ */
63
+ getActions({ address, txid, asset, type, affiliate, limit, offset, nextPageToken, timestamp, height, prevPageToken, fromTimestamp, fromHeight, }: GetActionsParams): Promise<ActionHistory>;
38
64
  }
package/lib/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { GetActions200Response } from '@xchainjs/xchain-midgard';
1
2
  import { Address, Asset, CryptoAmount } from '@xchainjs/xchain-util';
2
3
  /**
3
4
  * Search parameters to search for positions within the THORChain SAVER investment product trunks.
@@ -27,3 +28,29 @@ export type MidgardConfig = {
27
28
  apiRetries: number;
28
29
  midgardBaseUrls: string[];
29
30
  };
31
+ /**
32
+ * Action types
33
+ */
34
+ export type ActionType = 'swap' | 'addLiquidity' | 'withdraw' | 'donate' | 'refund' | 'switch';
35
+ /**
36
+ * Get action params
37
+ */
38
+ export type GetActionsParams = Partial<{
39
+ address: string;
40
+ txid: string;
41
+ asset: string;
42
+ type: ActionType;
43
+ affiliate: string;
44
+ limit: number;
45
+ offset: number;
46
+ nextPageToken: number;
47
+ timestamp: number;
48
+ height: number;
49
+ prevPageToken: number;
50
+ fromTimestamp: number;
51
+ fromHeight: number;
52
+ }>;
53
+ /**
54
+ * Action history
55
+ */
56
+ export type ActionHistory = GetActions200Response;
@@ -1,6 +1,6 @@
1
1
  import { Network } from '@xchainjs/xchain-client';
2
- import { PoolDetail, ReverseTHORNames, SaverDetails, THORNameDetails } from '@xchainjs/xchain-midgard';
3
- import { MidgardConfig } from '../types';
2
+ import { GetActions200Response as ActionHistory, PoolDetail, ReverseTHORNames, SaverDetails, THORNameDetails } from '@xchainjs/xchain-midgard';
3
+ import { GetActionsParams, MidgardConfig } from '../types';
4
4
  export declare class Midgard {
5
5
  private config;
6
6
  readonly network: Network;
@@ -19,4 +19,30 @@ export declare class Midgard {
19
19
  getPools(): Promise<PoolDetail[]>;
20
20
  getTHORNameDetails(name: string): Promise<THORNameDetails | undefined>;
21
21
  getTHORNameReverseLookup(address: string): Promise<ReverseTHORNames | undefined>;
22
+ /**
23
+ * List actions along with their related transactions. An action is generated by one or more inbound transactions
24
+ * with the intended action set in the transaction memo. The action may result in one or more outbound transactions.
25
+ * Results are paginated by sets of 50. Filters may be applied to query actions.
26
+ *
27
+ * @param {GetActionsParams} params - Get actions params
28
+ * @param {string} params.address - Comma separated list. Address of sender or recipient of any in/out transaction
29
+ * related to the action.
30
+ * @param {string} params.txid - ID of any in/out tx related to the action
31
+ * @param {string} params.asset - Comma separated list. Any asset that is part of the action (CHAIN.SYMBOL) Additionally,
32
+ * synth, nosynth, and norune filters can be used for swap, add/withdraw actions.
33
+ * @param {ActionType} params.type - One or more comma separated unique types of action (swap, addLiquidity, withdraw,
34
+ * donate, refund, switch)
35
+ * @param {string} params.affiliate - Comma separated list. Affiliate address of the action (swap, refund)
36
+ * @param {number} params.limit - Number of actions returned, default is 50
37
+ * @param {number} params.offset - Pagination offset, default is 0
38
+ * @param {number} params.nextPageToken - If this is given, the actions for the next page will be given
39
+ * @param {number} params.timestamp - If this is given, the actions older than the timestamp will be given
40
+ * @param {number} params.height - If this is given, the actions older than the height will be given
41
+ * @param {number} params.prevPageToken - If this is given, the actions for the previous page will be given
42
+ * @param {number} params.fromTimestamp - If this is given, the actions newer than the timestamp will be given
43
+ * @param {number} params.fromHeight - If this is given, the actions newer than the height will be given
44
+ *
45
+ * @returns {ActionHistory} Array of actions for the given filters
46
+ */
47
+ getActions({ address, txid, asset, type, affiliate, limit, offset, nextPageToken, timestamp, height, prevPageToken, fromTimestamp, fromHeight, }: GetActionsParams): Promise<ActionHistory>;
22
48
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-midgard-query",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
4
4
  "license": "MIT",
5
5
  "description": "Module that is responsible for get data from Midgard API",
6
6
  "keywords": [
@@ -23,17 +23,16 @@
23
23
  "url": "git@github.com:xchainjs/xchainjs-lib.git"
24
24
  },
25
25
  "scripts": {
26
- "clean": "rimraf --glob ./lib/**",
26
+ "clean": "rm -rf .turbo && rm -rf lib",
27
27
  "build": "yarn clean && rollup -c",
28
28
  "test": "jest",
29
29
  "e2e": "jest --config jest.config.e2e.js",
30
30
  "lint": "eslint \"{src,__tests__}/**/*.ts\" --fix --max-warnings 0",
31
- "prepublishOnly": "yarn build",
32
31
  "postversion": "git push --follow-tags"
33
32
  },
34
33
  "devDependencies": {
35
34
  "@xchainjs/xchain-client": "^0.16.2",
36
- "@xchainjs/xchain-midgard": "^0.5.2",
35
+ "@xchainjs/xchain-midgard": "^0.5.4",
37
36
  "@xchainjs/xchain-util": "^0.13.3",
38
37
  "axios": "^1.3.6",
39
38
  "axios-retry": "^3.2.5",
@@ -41,7 +40,7 @@
41
40
  },
42
41
  "peerDependencies": {
43
42
  "@xchainjs/xchain-client": "^0.16.2",
44
- "@xchainjs/xchain-midgard": "^0.5.2",
43
+ "@xchainjs/xchain-midgard": "^0.5.4",
45
44
  "@xchainjs/xchain-util": "^0.13.3",
46
45
  "axios": "^1.3.6",
47
46
  "axios-retry": "^3.2.5",
@@ -50,4 +49,4 @@
50
49
  "publishConfig": {
51
50
  "access": "public"
52
51
  }
53
- }
52
+ }