@chain-registry/utils 1.16.1 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [1.17.0](https://github.com/cosmology-tech/chain-registry/compare/@chain-registry/utils@1.16.1...@chain-registry/utils@1.17.0) (2023-12-20)
7
+
8
+
9
+ ### Features
10
+
11
+ * add chainName as optional param ([d415f33](https://github.com/cosmology-tech/chain-registry/commit/d415f33dc96651e5bb4ede689220d89ad11fe9d9))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [1.16.1](https://github.com/cosmology-tech/chain-registry/compare/@chain-registry/utils@1.16.0...@chain-registry/utils@1.16.1) (2023-12-15)
7
18
 
8
19
  **Note:** Version bump only for package @chain-registry/utils
@@ -16,75 +16,74 @@ exports.getExponentByDenom = getExponentByDenom;
16
16
  exports.getSymbolByChainDenom = getSymbolByChainDenom;
17
17
  exports.noDecimals = noDecimals;
18
18
  var _bignumber = _interopRequireDefault(require("bignumber.js"));
19
- function getAssetByDenom(assets, denom) {
20
- var asset = assets.find(function (asset) {
21
- return asset.base === denom;
19
+ function getAssetByKeyValue(assets, key, value, chainName) {
20
+ var filteredAssets = assets.filter(function (_ref) {
21
+ var chain_name = _ref.chain_name;
22
+ return !chainName || chain_name === chainName;
23
+ }).flatMap(function (_ref2) {
24
+ var assets = _ref2.assets;
25
+ return assets;
26
+ });
27
+ var matchingAssets = filteredAssets.filter(function (asset) {
28
+ return asset[key] === value;
22
29
  });
23
- if (!asset) {
24
- throw new Error("Asset not found: ".concat(denom));
30
+ if (matchingAssets.length === 0) {
31
+ throw new Error("No asset found for ".concat(key, " '").concat(value, "'"));
25
32
  }
26
- return asset;
33
+ if (matchingAssets.length > 1) {
34
+ throw new Error("Ambiguity: ".concat(matchingAssets.length, " assets found for ").concat(key, " '").concat(value, "'"));
35
+ }
36
+ return matchingAssets[0];
37
+ }
38
+ function getAssetByDenom(assets, denom, chainName) {
39
+ return getAssetByKeyValue(assets, 'base', denom, chainName);
27
40
  }
28
- function getDenomByCoinGeckoId(assets, coinGeckoId) {
29
- return assets.find(function (asset) {
30
- return asset.coingecko_id === coinGeckoId;
31
- }).base;
41
+ function getDenomByCoinGeckoId(assets, coinGeckoId, chainName) {
42
+ return getAssetByKeyValue(assets, 'coingecko_id', coinGeckoId, chainName).base;
32
43
  }
33
44
  function getCoinGeckoIdByDenom(assets, denom) {
34
45
  var _asset$coingecko_id;
35
- var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
36
- _ref$allowTestnet = _ref.allowTestnet,
37
- allowTestnet = _ref$allowTestnet === void 0 ? false : _ref$allowTestnet,
38
- _ref$customAssetFilte = _ref.customAssetFilter,
39
- customAssetFilter = _ref$customAssetFilte === void 0 ? function () {
46
+ var _ref3 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
47
+ chainName = _ref3.chainName,
48
+ _ref3$allowTestnet = _ref3.allowTestnet,
49
+ allowTestnet = _ref3$allowTestnet === void 0 ? false : _ref3$allowTestnet,
50
+ _ref3$customAssetFilt = _ref3.customAssetFilter,
51
+ customAssetFilter = _ref3$customAssetFilt === void 0 ? function () {
40
52
  return true;
41
- } : _ref$customAssetFilte,
42
- _ref$excludedChainNam = _ref.excludedChainNames,
43
- excludedChainNames = _ref$excludedChainNam === void 0 ? [] : _ref$excludedChainNam;
44
- var filteredAssetLists = assets.filter(function (_ref2) {
45
- var chain_name = _ref2.chain_name;
46
- return (allowTestnet || !chain_name.includes('testnet')) && !excludedChainNames.includes(chain_name);
53
+ } : _ref3$customAssetFilt,
54
+ _ref3$excludedChainNa = _ref3.excludedChainNames,
55
+ excludedChainNames = _ref3$excludedChainNa === void 0 ? [] : _ref3$excludedChainNa;
56
+ var filteredAssetLists = assets.filter(function (_ref4) {
57
+ var chain_name = _ref4.chain_name;
58
+ return (!chainName || chain_name === chainName) && (allowTestnet || !chain_name.includes('testnet')) && !excludedChainNames.includes(chain_name);
47
59
  });
48
- var filteredAssets = filteredAssetLists.flatMap(function (_ref3) {
49
- var assets = _ref3.assets;
60
+ var filteredAssets = filteredAssetLists.flatMap(function (_ref5) {
61
+ var assets = _ref5.assets;
50
62
  return assets;
51
- }).filter(function (_ref4) {
52
- var coingecko_id = _ref4.coingecko_id;
63
+ }).filter(function (_ref6) {
64
+ var coingecko_id = _ref6.coingecko_id;
53
65
  return coingecko_id;
54
66
  }).filter(customAssetFilter);
55
- var asset = filteredAssets.find(function (_ref5) {
56
- var base = _ref5.base;
67
+ var asset = filteredAssets.find(function (_ref7) {
68
+ var base = _ref7.base;
57
69
  return base === denom;
58
70
  });
59
71
  return (_asset$coingecko_id = asset === null || asset === void 0 ? void 0 : asset.coingecko_id) !== null && _asset$coingecko_id !== void 0 ? _asset$coingecko_id : null;
60
72
  }
61
- function getSymbolByChainDenom(assets, denom) {
62
- var asset = getAssetByDenom(assets, denom);
63
- var symbol = asset.symbol;
64
- if (!symbol) {
65
- return denom;
66
- }
67
- return symbol;
73
+ function getSymbolByChainDenom(assets, denom, chainName) {
74
+ return getAssetByDenom(assets, denom, chainName).symbol;
68
75
  }
69
- function getChainDenomBySymbol(assets, token) {
70
- var asset = assets.find(function (_ref6) {
71
- var symbol = _ref6.symbol;
72
- return symbol === token;
73
- });
74
- var base = asset === null || asset === void 0 ? void 0 : asset.base;
75
- if (!base) {
76
- console.log("cannot find base for token ".concat(token));
77
- return null;
78
- }
79
- return base;
76
+ function getChainDenomBySymbol(assets, symbol, chainName) {
77
+ return getAssetByKeyValue(assets, 'symbol', symbol, chainName).base;
80
78
  }
81
- function getExponentByDenom(assets, denom) {
82
- var asset = getAssetByDenom(assets, denom);
83
- var unit = asset.denom_units.find(function (_ref7) {
84
- var denom = _ref7.denom;
79
+ function getExponentByDenom(assets, denom, chainName) {
80
+ var _unit$exponent;
81
+ var asset = getAssetByDenom(assets, denom, chainName);
82
+ var unit = asset.denom_units.find(function (_ref8) {
83
+ var denom = _ref8.denom;
85
84
  return denom === asset.display;
86
85
  });
87
- return (unit === null || unit === void 0 ? void 0 : unit.exponent) || 0;
86
+ return (_unit$exponent = unit === null || unit === void 0 ? void 0 : unit.exponent) !== null && _unit$exponent !== void 0 ? _unit$exponent : 0;
88
87
  }
89
88
  function convertCoinGeckoPricesToDenomPriceMap(assets, prices) {
90
89
  return Object.keys(prices).reduce(function (res, geckoId) {
@@ -96,15 +95,15 @@ function convertCoinGeckoPricesToDenomPriceMap(assets, prices) {
96
95
  function noDecimals(num) {
97
96
  return new _bignumber["default"](num).decimalPlaces(0, _bignumber["default"].ROUND_DOWN).toString();
98
97
  }
99
- function convertBaseUnitsToDollarValue(assets, prices, symbol, amount) {
100
- var denom = getChainDenomBySymbol(assets, symbol);
101
- return new _bignumber["default"](amount).shiftedBy(-getExponentByDenom(assets, denom)).multipliedBy(prices[denom]).toString();
98
+ function convertBaseUnitsToDollarValue(assets, prices, symbol, amount, chainName) {
99
+ var denom = getChainDenomBySymbol(assets, symbol, chainName);
100
+ return new _bignumber["default"](amount).shiftedBy(-getExponentByDenom(assets, denom, chainName)).multipliedBy(prices[denom]).toString();
102
101
  }
103
- function convertDollarValueToDenomUnits(assets, prices, symbol, value) {
104
- var denom = getChainDenomBySymbol(assets, symbol);
105
- return new _bignumber["default"](value).dividedBy(prices[denom]).shiftedBy(getExponentByDenom(assets, denom)).toString();
102
+ function convertDollarValueToDenomUnits(assets, prices, symbol, value, chainName) {
103
+ var denom = getChainDenomBySymbol(assets, symbol, chainName);
104
+ return new _bignumber["default"](value).dividedBy(prices[denom]).shiftedBy(getExponentByDenom(assets, denom, chainName)).toString();
106
105
  }
107
- function convertBaseUnitsToDisplayUnits(assets, symbol, amount) {
108
- var denom = getChainDenomBySymbol(assets, symbol);
109
- return new _bignumber["default"](amount).shiftedBy(-getExponentByDenom(assets, denom)).toString();
106
+ function convertBaseUnitsToDisplayUnits(assets, symbol, amount, chainName) {
107
+ var denom = getChainDenomBySymbol(assets, symbol, chainName);
108
+ return new _bignumber["default"](amount).shiftedBy(-getExponentByDenom(assets, denom, chainName)).toString();
110
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chain-registry/utils",
3
- "version": "1.16.1",
3
+ "version": "1.17.0",
4
4
  "description": "Chain Registry Utils",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/cosmology-tech/chain-registry",
@@ -76,5 +76,5 @@
76
76
  "bignumber.js": "9.1.1",
77
77
  "sha.js": "^2.4.11"
78
78
  },
79
- "gitHead": "4be96c12e66434cf3fd49ab5dc00102ccc8de551"
79
+ "gitHead": "c8644d2e748b506abf7e174f2c50bfdb77277ca8"
80
80
  }
@@ -1,4 +1,4 @@
1
- import { Asset, AssetDenomUnit } from '@chain-registry/types';
1
+ import { Asset, AssetDenomUnit, AssetList } from '@chain-registry/types';
2
2
  export type CoinDenom = AssetDenomUnit['denom'];
3
3
  export type Exponent = AssetDenomUnit['exponent'];
4
4
  export interface CoinGeckoUSD {
@@ -7,13 +7,21 @@ export interface CoinGeckoUSD {
7
7
  export interface PriceHash {
8
8
  [key: CoinDenom]: number;
9
9
  }
10
- export declare function getAssetByDenom(assets: Asset[], denom: CoinDenom): Asset;
11
- export declare function getDenomByCoinGeckoId(assets: Asset[], coinGeckoId: string): CoinDenom;
12
- export declare function getSymbolByChainDenom(assets: Asset[], denom: CoinDenom): string;
13
- export declare function getChainDenomBySymbol(assets: Asset[], token: string): CoinDenom;
14
- export declare function getExponentByDenom(assets: Asset[], denom: CoinDenom): Exponent;
15
- export declare function convertCoinGeckoPricesToDenomPriceMap(assets: Asset[], prices: Record<string, CoinGeckoUSD>): PriceHash;
10
+ export declare function getAssetByDenom(assets: AssetList[], denom: CoinDenom, chainName?: string): Asset;
11
+ export declare function getDenomByCoinGeckoId(assets: AssetList[], coinGeckoId: string, chainName?: string): CoinDenom;
12
+ type GetCoinGeckoIdByDenomOptions = {
13
+ chainName?: string;
14
+ allowTestnet?: boolean;
15
+ customAssetFilter?: (asset: Asset) => boolean;
16
+ excludedChainNames?: string[];
17
+ };
18
+ export declare function getCoinGeckoIdByDenom(assets: AssetList[], denom: CoinDenom, { chainName, allowTestnet, customAssetFilter, excludedChainNames }?: GetCoinGeckoIdByDenomOptions): string | null;
19
+ export declare function getSymbolByChainDenom(assets: AssetList[], denom: CoinDenom, chainName?: string): string;
20
+ export declare function getChainDenomBySymbol(assets: AssetList[], symbol: string, chainName?: string): CoinDenom;
21
+ export declare function getExponentByDenom(assets: AssetList[], denom: CoinDenom, chainName?: string): Exponent;
22
+ export declare function convertCoinGeckoPricesToDenomPriceMap(assets: AssetList[], prices: Record<string, CoinGeckoUSD>): PriceHash;
16
23
  export declare function noDecimals(num: number | string): string;
17
- export declare function convertBaseUnitsToDollarValue(assets: Asset[], prices: PriceHash, symbol: string, amount: string | number): string;
18
- export declare function convertDollarValueToDenomUnits(assets: Asset[], prices: PriceHash, symbol: string, value: string | number): string;
19
- export declare function convertBaseUnitsToDisplayUnits(assets: Asset[], symbol: string, amount: string | number): string;
24
+ export declare function convertBaseUnitsToDollarValue(assets: AssetList[], prices: PriceHash, symbol: string, amount: string | number, chainName?: string): string;
25
+ export declare function convertDollarValueToDenomUnits(assets: AssetList[], prices: PriceHash, symbol: string, value: string | number, chainName?: string): string;
26
+ export declare function convertBaseUnitsToDisplayUnits(assets: AssetList[], symbol: string, amount: string | number, chainName?: string): string;
27
+ export {};