@chain-registry/utils 1.29.4 → 1.29.5

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/README.md CHANGED
@@ -124,10 +124,10 @@ import {
124
124
  getDenomBySymbol,
125
125
  getExponentByDenom,
126
126
  getExponentBySymbol,
127
- getNativeTokenByChainName,
127
+ getNativeAssetByChainName,
128
128
  getSymbolByDenom,
129
- getTokenLogoByDenom,
130
- getTokenNameByDenom
129
+ getAssetLogoByDenom,
130
+ getAssetNameByDenom
131
131
  } from '@chain-registry/utils';
132
132
 
133
133
  // import from chain-registry or your own AssetList[]
@@ -169,9 +169,9 @@ console.log(coinGeckoId); // 'osmosis'
169
169
  - `getDenomBySymbol`: Get the denomination of an asset by its symbol.
170
170
  - `getExponentByDenom`: Find the exponent for a denomination.
171
171
  - `getExponentBySymbol`: Get the exponent for a symbol.
172
- - `getNativeTokenByChainName`: Retrieve the native token for a given chain name.
173
- - `getTokenLogoByDenom`: Get the logo URL for a token by its denomination.
174
- - `getTokenNameByDenom`: Find the name of a token by its denomination.
172
+ - `getNativeAssetByChainName`: Retrieve the native token for a given chain name.
173
+ - `getAssetLogoByDenom`: Get the logo URL for a token by its denomination.
174
+ - `getAssetNameByDenom`: Find the name of a token by its denomination.
175
175
 
176
176
  ### IBC Utilities
177
177
 
package/assets.d.ts CHANGED
@@ -4,6 +4,7 @@ export type Exponent = AssetDenomUnit['exponent'];
4
4
  export declare const getAssetByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => Asset | undefined;
5
5
  export declare const getAssetBySymbol: (assets: AssetList[], symbol: string, chainName?: string) => Asset | undefined;
6
6
  export declare const getDenomByCoinGeckoId: (assets: AssetList[], coinGeckoId: string, chainName?: string) => Denom | undefined;
7
+ export declare const getDenomsByCoinGeckoId: (assets: AssetList[], coinGeckoId: string, chainName?: string) => Denom[];
7
8
  type GetCoinGeckoIdByDenomOptions = {
8
9
  chainName?: string;
9
10
  allowTestnet?: boolean;
@@ -14,12 +15,13 @@ export declare const getCoinGeckoIdByDenom: (assets: AssetList[], denom: Denom,
14
15
  export declare const getSymbolByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => string | undefined;
15
16
  export declare const getDenomBySymbol: (assets: AssetList[], symbol: string, chainName?: string) => Denom | undefined;
16
17
  export declare const getExponentFromAsset: (asset: Asset) => number | undefined;
18
+ export declare const getExponentByDenomFromAsset: (asset: Asset, denom: string) => number | undefined;
17
19
  export declare const getExponentByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => Exponent | undefined;
18
20
  export declare const getExponentBySymbol: (assets: AssetList[], symbol: string, chainName?: string) => Exponent | undefined;
19
- export declare const getNativeTokenByChainName: (assets: AssetList[], chainName: string) => Asset | undefined;
20
- export declare const getTokenLogoByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => string | undefined;
21
+ export declare const getNativeAssetByChainName: (assets: AssetList[], chainName: string) => Asset | undefined;
22
+ export declare const getAssetLogoByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => string | undefined;
21
23
  export declare const getChainLogo: (assets: AssetList[], chainName: string) => string | undefined;
22
- export declare const getTokenNameByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => string | undefined;
24
+ export declare const getAssetNameByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => string | undefined;
23
25
  export declare const getChainNameByDenom: (assets: AssetList[], denom: Denom) => string | undefined;
24
26
  export declare const getChainByStakingDenom: (chains: Chain[], denom: Denom) => Chain | undefined;
25
27
  export declare const getChainNameByStakingDenom: (chains: Chain[], denom: Denom) => string | undefined;
package/assets.js CHANGED
@@ -1,15 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getChainNameByStakingDenom = exports.getChainByStakingDenom = exports.getChainNameByDenom = exports.getTokenNameByDenom = exports.getChainLogo = exports.getTokenLogoByDenom = exports.getNativeTokenByChainName = exports.getExponentBySymbol = exports.getExponentByDenom = exports.getExponentFromAsset = exports.getDenomBySymbol = exports.getSymbolByDenom = exports.getCoinGeckoIdByDenom = exports.getDenomByCoinGeckoId = exports.getAssetBySymbol = exports.getAssetByDenom = void 0;
3
+ exports.getChainNameByStakingDenom = exports.getChainByStakingDenom = exports.getChainNameByDenom = exports.getAssetNameByDenom = exports.getChainLogo = exports.getAssetLogoByDenom = exports.getNativeAssetByChainName = exports.getExponentBySymbol = exports.getExponentByDenom = exports.getExponentByDenomFromAsset = exports.getExponentFromAsset = exports.getDenomBySymbol = exports.getSymbolByDenom = exports.getCoinGeckoIdByDenom = exports.getDenomsByCoinGeckoId = exports.getDenomByCoinGeckoId = exports.getAssetBySymbol = exports.getAssetByDenom = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  const getAssetByKeyValue = (assets, key, value, chainName) => {
6
6
  const filteredAssets = assets
7
7
  .filter(({ chain_name }) => !chainName || chain_name === chainName)
8
8
  .flatMap(({ assets }) => assets);
9
- return (0, utils_1.customFind)(filteredAssets, (asset) => asset[key] === value);
9
+ return (0, utils_1.customFind)(filteredAssets, (asset) => asset[key] === value, `key:${key} value:${value} chainName:${chainName}`);
10
10
  };
11
11
  const getAssetByDenom = (assets, denom, chainName) => {
12
- return getAssetByKeyValue(assets, 'base', denom, chainName);
12
+ const filteredAssets = assets
13
+ .filter(({ chain_name }) => !chainName || chain_name === chainName)
14
+ .flatMap(({ assets }) => assets);
15
+ return (0, utils_1.customFind)(filteredAssets, (asset) => {
16
+ const found = asset.denom_units.find(unit => unit.denom === denom);
17
+ return !!found;
18
+ }, `denom:${denom} chainName:${chainName}`);
13
19
  };
14
20
  exports.getAssetByDenom = getAssetByDenom;
15
21
  const getAssetBySymbol = (assets, symbol, chainName) => {
@@ -21,6 +27,16 @@ const getDenomByCoinGeckoId = (assets, coinGeckoId, chainName) => {
21
27
  ?.base;
22
28
  };
23
29
  exports.getDenomByCoinGeckoId = getDenomByCoinGeckoId;
30
+ const getDenomsByCoinGeckoId = (assets, coinGeckoId, chainName) => {
31
+ const filteredAssets = assets
32
+ .filter(({ chain_name }) => !chainName || chain_name === chainName)
33
+ .flatMap(({ assets }) => assets);
34
+ const denoms = filteredAssets
35
+ .filter(a => a.coingecko_id === coinGeckoId)
36
+ .map(a => a.base);
37
+ return denoms;
38
+ };
39
+ exports.getDenomsByCoinGeckoId = getDenomsByCoinGeckoId;
24
40
  const getCoinGeckoIdByDenom = (assets, denom, { chainName, allowTestnet = false, customAssetFilter = () => true, excludedChainNames = [] } = {}) => {
25
41
  const filteredAssetLists = assets.filter(({ chain_name }) => {
26
42
  return ((!chainName || chain_name === chainName) &&
@@ -48,6 +64,13 @@ const getExponentFromAsset = (asset) => {
48
64
  ?.exponent;
49
65
  };
50
66
  exports.getExponentFromAsset = getExponentFromAsset;
67
+ const getExponentByDenomFromAsset = (asset, denom) => {
68
+ return asset.denom_units.find((unit) => {
69
+ return unit.denom === denom || unit?.aliases?.find(a => a === denom);
70
+ })
71
+ ?.exponent;
72
+ };
73
+ exports.getExponentByDenomFromAsset = getExponentByDenomFromAsset;
51
74
  const getExponentByDenom = (assets, denom, chainName) => {
52
75
  const asset = (0, exports.getAssetByDenom)(assets, denom, chainName);
53
76
  return asset ? (0, exports.getExponentFromAsset)(asset) : undefined;
@@ -58,27 +81,27 @@ const getExponentBySymbol = (assets, symbol, chainName) => {
58
81
  return asset ? (0, exports.getExponentFromAsset)(asset) : undefined;
59
82
  };
60
83
  exports.getExponentBySymbol = getExponentBySymbol;
61
- const getNativeTokenByChainName = (assets, chainName) => {
84
+ const getNativeAssetByChainName = (assets, chainName) => {
62
85
  const assetList = (0, utils_1.customFind)(assets, (assetList) => assetList.chain_name === chainName &&
63
- !assetList.assets[0].base.startsWith('ibc/'));
86
+ !assetList.assets[0].base.startsWith('ibc/'), `chainName:${chainName}`);
64
87
  return assetList?.assets[0];
65
88
  };
66
- exports.getNativeTokenByChainName = getNativeTokenByChainName;
67
- const getTokenLogoByDenom = (assets, denom, chainName) => {
89
+ exports.getNativeAssetByChainName = getNativeAssetByChainName;
90
+ const getAssetLogoByDenom = (assets, denom, chainName) => {
68
91
  const asset = (0, exports.getAssetByDenom)(assets, denom, chainName);
69
92
  return Object.values(asset?.logo_URIs || {})[0];
70
93
  };
71
- exports.getTokenLogoByDenom = getTokenLogoByDenom;
94
+ exports.getAssetLogoByDenom = getAssetLogoByDenom;
72
95
  const getChainLogo = (assets, chainName) => {
73
- const nativeToken = (0, exports.getNativeTokenByChainName)(assets, chainName);
74
- return Object.values(nativeToken?.logo_URIs || {})[0];
96
+ const nativeAsset = (0, exports.getNativeAssetByChainName)(assets, chainName);
97
+ return Object.values(nativeAsset?.logo_URIs || {})[0];
75
98
  };
76
99
  exports.getChainLogo = getChainLogo;
77
- const getTokenNameByDenom = (assets, denom, chainName) => {
100
+ const getAssetNameByDenom = (assets, denom, chainName) => {
78
101
  const asset = (0, exports.getAssetByDenom)(assets, denom, chainName);
79
102
  return asset?.name;
80
103
  };
81
- exports.getTokenNameByDenom = getTokenNameByDenom;
104
+ exports.getAssetNameByDenom = getAssetNameByDenom;
82
105
  const getChainNameByDenom = (assets, denom) => {
83
106
  const isIbcDenom = denom.startsWith('ibc/');
84
107
  if (isIbcDenom) {
@@ -86,10 +109,10 @@ const getChainNameByDenom = (assets, denom) => {
86
109
  return asset?.traces?.find((t) => t.type === 'ibc')?.counterparty
87
110
  ?.chain_name;
88
111
  }
89
- return (0, utils_1.customFind)(assets, (assetList) => assetList.assets.some((asset) => asset.base === denom))?.chain_name;
112
+ return (0, utils_1.customFind)(assets, (assetList) => assetList.assets.some((asset) => asset.base === denom), `denom:${denom}`)?.chain_name;
90
113
  };
91
114
  exports.getChainNameByDenom = getChainNameByDenom;
92
- const getChainByStakingDenom = (chains, denom) => (0, utils_1.customFind)(chains, (chain) => !!chain.staking?.staking_tokens.find((token) => token.denom === denom));
115
+ const getChainByStakingDenom = (chains, denom) => (0, utils_1.customFind)(chains, (chain) => !!chain.staking?.staking_tokens.find((token) => token.denom === denom), `denom:${denom}`);
93
116
  exports.getChainByStakingDenom = getChainByStakingDenom;
94
117
  const getChainNameByStakingDenom = (chains, denom) => (0, exports.getChainByStakingDenom)(chains, denom)?.chain_name;
95
118
  exports.getChainNameByStakingDenom = getChainNameByStakingDenom;
package/calc.d.ts CHANGED
@@ -9,6 +9,8 @@ export interface DenomPriceMap {
9
9
  export declare const mapCoinGeckoPricesToDenoms: (assets: AssetList[], prices: Record<string, CoinGeckoUSDPrice>) => DenomPriceMap;
10
10
  export declare const roundDown: (value: number | string) => string;
11
11
  export declare const convertBaseUnitToDollarValue: (assets: AssetList[], prices: DenomPriceMap, symbol: string, amount: string | number, chainName?: string) => string;
12
+ export declare const convertBaseUnitToDollarValueByDenom: (assets: AssetList[], prices: DenomPriceMap, denom: string, amount: string | number, chainName?: string) => string;
12
13
  export declare const convertDollarValueToBaseUnit: (assets: AssetList[], prices: DenomPriceMap, symbol: string, value: string | number, chainName?: string) => string;
13
14
  export declare const convertBaseUnitToDisplayUnit: (assets: AssetList[], symbol: string, amount: string | number, chainName?: string) => string;
15
+ export declare const convertBaseUnitToDisplayUnitByDenom: (assets: AssetList[], denom: string, amount: string | number, chainName?: string) => string;
14
16
  export declare const convertDisplayUnitToBaseUnit: (assets: AssetList[], symbol: string, amount: string | number, chainName?: string) => string;
package/calc.js CHANGED
@@ -3,16 +3,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.convertDisplayUnitToBaseUnit = exports.convertBaseUnitToDisplayUnit = exports.convertDollarValueToBaseUnit = exports.convertBaseUnitToDollarValue = exports.roundDown = exports.mapCoinGeckoPricesToDenoms = void 0;
6
+ exports.convertDisplayUnitToBaseUnit = exports.convertBaseUnitToDisplayUnitByDenom = exports.convertBaseUnitToDisplayUnit = exports.convertDollarValueToBaseUnit = exports.convertBaseUnitToDollarValueByDenom = exports.convertBaseUnitToDollarValue = exports.roundDown = exports.mapCoinGeckoPricesToDenoms = void 0;
7
7
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
8
8
  const assets_1 = require("./assets");
9
9
  const mapCoinGeckoPricesToDenoms = (assets, prices) => {
10
10
  return Object.keys(prices).reduce((res, geckoId) => {
11
- const denom = (0, assets_1.getDenomByCoinGeckoId)(assets, geckoId);
12
- if (!denom) {
11
+ const denoms = (0, assets_1.getDenomsByCoinGeckoId)(assets, geckoId);
12
+ if (!denoms.length) {
13
13
  throw new Error(`No denom found for CoinGecko ID: ${geckoId}`);
14
14
  }
15
- res[denom] = prices[geckoId].usd;
15
+ // some tokens list same coingeckoId
16
+ denoms.forEach(d => {
17
+ res[d] = prices[geckoId].usd;
18
+ });
16
19
  return res;
17
20
  }, {});
18
21
  };
@@ -25,11 +28,19 @@ const getAssetInfo = (assets, symbol, chainName) => {
25
28
  const asset = (0, assets_1.getAssetBySymbol)(assets, symbol, chainName);
26
29
  const denom = asset?.base;
27
30
  const exponent = asset ? (0, assets_1.getExponentFromAsset)(asset) : undefined;
28
- if (!denom || !exponent) {
31
+ if (!denom || exponent === undefined) {
29
32
  throw new Error(`No denom or exponent found for symbol: ${symbol}`);
30
33
  }
31
34
  return { denom, exponent };
32
35
  };
36
+ const getAssetInfoByDenom = (assets, denom, chainName) => {
37
+ const asset = (0, assets_1.getAssetByDenom)(assets, denom, chainName);
38
+ const exponent = asset ? (0, assets_1.getExponentByDenomFromAsset)(asset, denom) : undefined;
39
+ if (exponent === undefined) {
40
+ throw new Error(`No denom or exponent found for denom: ${denom}`);
41
+ }
42
+ return { denom, exponent };
43
+ };
33
44
  const shiftDecimalPlaces = (amount, exponent, direction = 1) => {
34
45
  return new bignumber_js_1.default(amount).shiftedBy(exponent * direction).toString();
35
46
  };
@@ -39,6 +50,25 @@ const convertBaseUnitToDollarValue = (assets, prices, symbol, amount, chainName)
39
50
  return new bignumber_js_1.default(baseAmount).multipliedBy(prices[denom]).toString();
40
51
  };
41
52
  exports.convertBaseUnitToDollarValue = convertBaseUnitToDollarValue;
53
+ const convertBaseUnitToDollarValueByDenom = (assets, prices, denom, amount, chainName) => {
54
+ const asset = (0, assets_1.getAssetByDenom)(assets, denom, chainName);
55
+ if (!asset) {
56
+ throw new Error(`no asset found from denom:${denom}`);
57
+ }
58
+ const assetBase = asset.base;
59
+ const assetDisplay = asset.display;
60
+ if (!(assetBase in prices)) {
61
+ console.warn(`no price found from denom:${denom}`);
62
+ return '0';
63
+ }
64
+ const exponent = (0, assets_1.getExponentByDenomFromAsset)(asset, assetDisplay);
65
+ if (exponent === undefined) {
66
+ throw new Error(`No exponent found for denom: ${denom}`);
67
+ }
68
+ const baseAmount = shiftDecimalPlaces(amount, exponent, -1);
69
+ return new bignumber_js_1.default(baseAmount).multipliedBy(prices[assetBase]).toString();
70
+ };
71
+ exports.convertBaseUnitToDollarValueByDenom = convertBaseUnitToDollarValueByDenom;
42
72
  const convertDollarValueToBaseUnit = (assets, prices, symbol, value, chainName) => {
43
73
  const { denom, exponent } = getAssetInfo(assets, symbol, chainName);
44
74
  const baseAmount = new bignumber_js_1.default(value).dividedBy(prices[denom]).toString();
@@ -50,6 +80,19 @@ const convertBaseUnitToDisplayUnit = (assets, symbol, amount, chainName) => {
50
80
  return shiftDecimalPlaces(amount, exponent, -1);
51
81
  };
52
82
  exports.convertBaseUnitToDisplayUnit = convertBaseUnitToDisplayUnit;
83
+ const convertBaseUnitToDisplayUnitByDenom = (assets, denom, amount, chainName) => {
84
+ const asset = (0, assets_1.getAssetByDenom)(assets, denom, chainName);
85
+ if (!asset) {
86
+ throw new Error(`no asset found from denom:${denom}`);
87
+ }
88
+ const assetDisplay = asset.display;
89
+ const exponent = (0, assets_1.getExponentByDenomFromAsset)(asset, assetDisplay);
90
+ if (exponent === undefined) {
91
+ throw new Error(`No exponent found for denom: ${denom}`);
92
+ }
93
+ return shiftDecimalPlaces(amount, exponent, -1);
94
+ };
95
+ exports.convertBaseUnitToDisplayUnitByDenom = convertBaseUnitToDisplayUnitByDenom;
53
96
  const convertDisplayUnitToBaseUnit = (assets, symbol, amount, chainName) => {
54
97
  const { exponent } = getAssetInfo(assets, symbol, chainName);
55
98
  return (0, exports.roundDown)(shiftDecimalPlaces(amount, exponent));
package/chains.js CHANGED
@@ -12,11 +12,11 @@ const getGasPriceRangesFromChain = (chain) => {
12
12
  };
13
13
  exports.getGasPriceRangesFromChain = getGasPriceRangesFromChain;
14
14
  const getChainByChainName = (chains, chainName) => {
15
- return (0, utils_1.customFind)(chains, (chain) => chain.chain_name === chainName);
15
+ return (0, utils_1.customFind)(chains, (chain) => chain.chain_name === chainName, `chainName:${chainName}`);
16
16
  };
17
17
  exports.getChainByChainName = getChainByChainName;
18
18
  const getChainByChainId = (chains, chainId) => {
19
- return (0, utils_1.customFind)(chains, (chain) => chain.chain_id === chainId);
19
+ return (0, utils_1.customFind)(chains, (chain) => chain.chain_id === chainId, `chainId:${chainId}`);
20
20
  };
21
21
  exports.getChainByChainId = getChainByChainId;
22
22
  const getChainNameByChainId = (chains, chainId) => {
package/esm/assets.js CHANGED
@@ -3,10 +3,16 @@ const getAssetByKeyValue = (assets, key, value, chainName) => {
3
3
  const filteredAssets = assets
4
4
  .filter(({ chain_name }) => !chainName || chain_name === chainName)
5
5
  .flatMap(({ assets }) => assets);
6
- return customFind(filteredAssets, (asset) => asset[key] === value);
6
+ return customFind(filteredAssets, (asset) => asset[key] === value, `key:${key} value:${value} chainName:${chainName}`);
7
7
  };
8
8
  export const getAssetByDenom = (assets, denom, chainName) => {
9
- return getAssetByKeyValue(assets, 'base', denom, chainName);
9
+ const filteredAssets = assets
10
+ .filter(({ chain_name }) => !chainName || chain_name === chainName)
11
+ .flatMap(({ assets }) => assets);
12
+ return customFind(filteredAssets, (asset) => {
13
+ const found = asset.denom_units.find(unit => unit.denom === denom);
14
+ return !!found;
15
+ }, `denom:${denom} chainName:${chainName}`);
10
16
  };
11
17
  export const getAssetBySymbol = (assets, symbol, chainName) => {
12
18
  return getAssetByKeyValue(assets, 'symbol', symbol, chainName);
@@ -15,6 +21,15 @@ export const getDenomByCoinGeckoId = (assets, coinGeckoId, chainName) => {
15
21
  return getAssetByKeyValue(assets, 'coingecko_id', coinGeckoId, chainName)
16
22
  ?.base;
17
23
  };
24
+ export const getDenomsByCoinGeckoId = (assets, coinGeckoId, chainName) => {
25
+ const filteredAssets = assets
26
+ .filter(({ chain_name }) => !chainName || chain_name === chainName)
27
+ .flatMap(({ assets }) => assets);
28
+ const denoms = filteredAssets
29
+ .filter(a => a.coingecko_id === coinGeckoId)
30
+ .map(a => a.base);
31
+ return denoms;
32
+ };
18
33
  export const getCoinGeckoIdByDenom = (assets, denom, { chainName, allowTestnet = false, customAssetFilter = () => true, excludedChainNames = [] } = {}) => {
19
34
  const filteredAssetLists = assets.filter(({ chain_name }) => {
20
35
  return ((!chainName || chain_name === chainName) &&
@@ -38,6 +53,12 @@ export const getExponentFromAsset = (asset) => {
38
53
  return asset.denom_units.find(({ denom }) => denom === asset.display)
39
54
  ?.exponent;
40
55
  };
56
+ export const getExponentByDenomFromAsset = (asset, denom) => {
57
+ return asset.denom_units.find((unit) => {
58
+ return unit.denom === denom || unit?.aliases?.find(a => a === denom);
59
+ })
60
+ ?.exponent;
61
+ };
41
62
  export const getExponentByDenom = (assets, denom, chainName) => {
42
63
  const asset = getAssetByDenom(assets, denom, chainName);
43
64
  return asset ? getExponentFromAsset(asset) : undefined;
@@ -46,20 +67,20 @@ export const getExponentBySymbol = (assets, symbol, chainName) => {
46
67
  const asset = getAssetBySymbol(assets, symbol, chainName);
47
68
  return asset ? getExponentFromAsset(asset) : undefined;
48
69
  };
49
- export const getNativeTokenByChainName = (assets, chainName) => {
70
+ export const getNativeAssetByChainName = (assets, chainName) => {
50
71
  const assetList = customFind(assets, (assetList) => assetList.chain_name === chainName &&
51
- !assetList.assets[0].base.startsWith('ibc/'));
72
+ !assetList.assets[0].base.startsWith('ibc/'), `chainName:${chainName}`);
52
73
  return assetList?.assets[0];
53
74
  };
54
- export const getTokenLogoByDenom = (assets, denom, chainName) => {
75
+ export const getAssetLogoByDenom = (assets, denom, chainName) => {
55
76
  const asset = getAssetByDenom(assets, denom, chainName);
56
77
  return Object.values(asset?.logo_URIs || {})[0];
57
78
  };
58
79
  export const getChainLogo = (assets, chainName) => {
59
- const nativeToken = getNativeTokenByChainName(assets, chainName);
60
- return Object.values(nativeToken?.logo_URIs || {})[0];
80
+ const nativeAsset = getNativeAssetByChainName(assets, chainName);
81
+ return Object.values(nativeAsset?.logo_URIs || {})[0];
61
82
  };
62
- export const getTokenNameByDenom = (assets, denom, chainName) => {
83
+ export const getAssetNameByDenom = (assets, denom, chainName) => {
63
84
  const asset = getAssetByDenom(assets, denom, chainName);
64
85
  return asset?.name;
65
86
  };
@@ -70,7 +91,7 @@ export const getChainNameByDenom = (assets, denom) => {
70
91
  return asset?.traces?.find((t) => t.type === 'ibc')?.counterparty
71
92
  ?.chain_name;
72
93
  }
73
- return customFind(assets, (assetList) => assetList.assets.some((asset) => asset.base === denom))?.chain_name;
94
+ return customFind(assets, (assetList) => assetList.assets.some((asset) => asset.base === denom), `denom:${denom}`)?.chain_name;
74
95
  };
75
- export const getChainByStakingDenom = (chains, denom) => customFind(chains, (chain) => !!chain.staking?.staking_tokens.find((token) => token.denom === denom));
96
+ export const getChainByStakingDenom = (chains, denom) => customFind(chains, (chain) => !!chain.staking?.staking_tokens.find((token) => token.denom === denom), `denom:${denom}`);
76
97
  export const getChainNameByStakingDenom = (chains, denom) => getChainByStakingDenom(chains, denom)?.chain_name;
package/esm/calc.js CHANGED
@@ -1,12 +1,15 @@
1
1
  import BigNumber from 'bignumber.js';
2
- import { getAssetBySymbol, getDenomByCoinGeckoId, getExponentFromAsset } from './assets';
2
+ import { getAssetByDenom, getAssetBySymbol, getDenomsByCoinGeckoId, getExponentByDenomFromAsset, getExponentFromAsset } from './assets';
3
3
  export const mapCoinGeckoPricesToDenoms = (assets, prices) => {
4
4
  return Object.keys(prices).reduce((res, geckoId) => {
5
- const denom = getDenomByCoinGeckoId(assets, geckoId);
6
- if (!denom) {
5
+ const denoms = getDenomsByCoinGeckoId(assets, geckoId);
6
+ if (!denoms.length) {
7
7
  throw new Error(`No denom found for CoinGecko ID: ${geckoId}`);
8
8
  }
9
- res[denom] = prices[geckoId].usd;
9
+ // some tokens list same coingeckoId
10
+ denoms.forEach(d => {
11
+ res[d] = prices[geckoId].usd;
12
+ });
10
13
  return res;
11
14
  }, {});
12
15
  };
@@ -17,11 +20,19 @@ const getAssetInfo = (assets, symbol, chainName) => {
17
20
  const asset = getAssetBySymbol(assets, symbol, chainName);
18
21
  const denom = asset?.base;
19
22
  const exponent = asset ? getExponentFromAsset(asset) : undefined;
20
- if (!denom || !exponent) {
23
+ if (!denom || exponent === undefined) {
21
24
  throw new Error(`No denom or exponent found for symbol: ${symbol}`);
22
25
  }
23
26
  return { denom, exponent };
24
27
  };
28
+ const getAssetInfoByDenom = (assets, denom, chainName) => {
29
+ const asset = getAssetByDenom(assets, denom, chainName);
30
+ const exponent = asset ? getExponentByDenomFromAsset(asset, denom) : undefined;
31
+ if (exponent === undefined) {
32
+ throw new Error(`No denom or exponent found for denom: ${denom}`);
33
+ }
34
+ return { denom, exponent };
35
+ };
25
36
  const shiftDecimalPlaces = (amount, exponent, direction = 1) => {
26
37
  return new BigNumber(amount).shiftedBy(exponent * direction).toString();
27
38
  };
@@ -30,6 +41,24 @@ export const convertBaseUnitToDollarValue = (assets, prices, symbol, amount, cha
30
41
  const baseAmount = shiftDecimalPlaces(amount, exponent, -1);
31
42
  return new BigNumber(baseAmount).multipliedBy(prices[denom]).toString();
32
43
  };
44
+ export const convertBaseUnitToDollarValueByDenom = (assets, prices, denom, amount, chainName) => {
45
+ const asset = getAssetByDenom(assets, denom, chainName);
46
+ if (!asset) {
47
+ throw new Error(`no asset found from denom:${denom}`);
48
+ }
49
+ const assetBase = asset.base;
50
+ const assetDisplay = asset.display;
51
+ if (!(assetBase in prices)) {
52
+ console.warn(`no price found from denom:${denom}`);
53
+ return '0';
54
+ }
55
+ const exponent = getExponentByDenomFromAsset(asset, assetDisplay);
56
+ if (exponent === undefined) {
57
+ throw new Error(`No exponent found for denom: ${denom}`);
58
+ }
59
+ const baseAmount = shiftDecimalPlaces(amount, exponent, -1);
60
+ return new BigNumber(baseAmount).multipliedBy(prices[assetBase]).toString();
61
+ };
33
62
  export const convertDollarValueToBaseUnit = (assets, prices, symbol, value, chainName) => {
34
63
  const { denom, exponent } = getAssetInfo(assets, symbol, chainName);
35
64
  const baseAmount = new BigNumber(value).dividedBy(prices[denom]).toString();
@@ -39,6 +68,18 @@ export const convertBaseUnitToDisplayUnit = (assets, symbol, amount, chainName)
39
68
  const { exponent } = getAssetInfo(assets, symbol, chainName);
40
69
  return shiftDecimalPlaces(amount, exponent, -1);
41
70
  };
71
+ export const convertBaseUnitToDisplayUnitByDenom = (assets, denom, amount, chainName) => {
72
+ const asset = getAssetByDenom(assets, denom, chainName);
73
+ if (!asset) {
74
+ throw new Error(`no asset found from denom:${denom}`);
75
+ }
76
+ const assetDisplay = asset.display;
77
+ const exponent = getExponentByDenomFromAsset(asset, assetDisplay);
78
+ if (exponent === undefined) {
79
+ throw new Error(`No exponent found for denom: ${denom}`);
80
+ }
81
+ return shiftDecimalPlaces(amount, exponent, -1);
82
+ };
42
83
  export const convertDisplayUnitToBaseUnit = (assets, symbol, amount, chainName) => {
43
84
  const { exponent } = getAssetInfo(assets, symbol, chainName);
44
85
  return roundDown(shiftDecimalPlaces(amount, exponent));
package/esm/chains.js CHANGED
@@ -8,10 +8,10 @@ export const getGasPriceRangesFromChain = (chain) => {
8
8
  };
9
9
  };
10
10
  export const getChainByChainName = (chains, chainName) => {
11
- return customFind(chains, (chain) => chain.chain_name === chainName);
11
+ return customFind(chains, (chain) => chain.chain_name === chainName, `chainName:${chainName}`);
12
12
  };
13
13
  export const getChainByChainId = (chains, chainId) => {
14
- return customFind(chains, (chain) => chain.chain_id === chainId);
14
+ return customFind(chains, (chain) => chain.chain_id === chainId, `chainId:${chainId}`);
15
15
  };
16
16
  export const getChainNameByChainId = (chains, chainId) => {
17
17
  return getChainByChainId(chains, chainId)?.chain_name;
package/esm/utils.js CHANGED
@@ -1,8 +1,8 @@
1
- export const customFind = (array, filterFn) => {
1
+ export const customFind = (array, filterFn, message) => {
2
2
  const filteredItems = array.filter(filterFn);
3
3
  const filterCount = filteredItems.length;
4
4
  if (filterCount > 1) {
5
- throw new Error(`Ambiguity Error: ${filterCount} items found.`);
5
+ throw new Error(`Ambiguity Error: ${filterCount} items found ${message}.`);
6
6
  }
7
7
  return filteredItems[0];
8
8
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chain-registry/utils",
3
- "version": "1.29.4",
3
+ "version": "1.29.5",
4
4
  "description": "Chain Registry Utils",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/cosmology-tech/chain-registry",
@@ -31,7 +31,7 @@
31
31
  "@types/sha.js": "^2.4.0"
32
32
  },
33
33
  "dependencies": {
34
- "@chain-registry/types": "^0.28.4",
34
+ "@chain-registry/types": "^0.28.5",
35
35
  "bignumber.js": "9.1.1",
36
36
  "sha.js": "^2.4.11"
37
37
  },
@@ -41,5 +41,5 @@
41
41
  "cosmos",
42
42
  "interchain"
43
43
  ],
44
- "gitHead": "2e0598f8583ab48784ddb85468e21d83966e8f93"
44
+ "gitHead": "63df544ec7e864f893bdc7a4605a1666ca292da3"
45
45
  }
package/utils.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { AssetList } from "@chain-registry/types";
2
- export declare const customFind: <T>(array: T[], filterFn: (item: T) => boolean) => T | undefined;
2
+ export declare const customFind: <T>(array: T[], filterFn: (item: T) => boolean, message: string) => T | undefined;
3
3
  export declare const getNativeAssets: (assets: AssetList[]) => AssetList[];
package/utils.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getNativeAssets = exports.customFind = void 0;
4
- const customFind = (array, filterFn) => {
4
+ const customFind = (array, filterFn, message) => {
5
5
  const filteredItems = array.filter(filterFn);
6
6
  const filterCount = filteredItems.length;
7
7
  if (filterCount > 1) {
8
- throw new Error(`Ambiguity Error: ${filterCount} items found.`);
8
+ throw new Error(`Ambiguity Error: ${filterCount} items found ${message}.`);
9
9
  }
10
10
  return filteredItems[0];
11
11
  };