@chain-registry/utils 1.12.0 → 1.13.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 +8 -0
- package/main/asset-list-util.js +81 -0
- package/main/index.js +11 -0
- package/package.json +3 -2
- package/types/asset-list-util.d.ts +65 -0
- package/types/index.d.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
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.13.0](https://github.com/cosmology-tech/chain-registry/compare/@chain-registry/utils@1.12.0...@chain-registry/utils@1.13.0) (2023-07-11)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @chain-registry/utils
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
# [1.12.0](https://github.com/cosmology-tech/chain-registry/compare/@chain-registry/utils@1.11.0...@chain-registry/utils@1.12.0) (2023-07-08)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @chain-registry/utils
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.convertBaseUnitsToDisplayUnits = convertBaseUnitsToDisplayUnits;
|
|
8
|
+
exports.convertBaseUnitsToDollarValue = convertBaseUnitsToDollarValue;
|
|
9
|
+
exports.convertCoinGeckoPricesToDenomPriceMap = convertCoinGeckoPricesToDenomPriceMap;
|
|
10
|
+
exports.convertDollarValueToDenomUnits = convertDollarValueToDenomUnits;
|
|
11
|
+
exports.getAssetByDenom = getAssetByDenom;
|
|
12
|
+
exports.getChainDenomBySymbol = getChainDenomBySymbol;
|
|
13
|
+
exports.getDenomByCoinGeckoId = getDenomByCoinGeckoId;
|
|
14
|
+
exports.getExponentByDenom = getExponentByDenom;
|
|
15
|
+
exports.getSymbolByChainDenom = getSymbolByChainDenom;
|
|
16
|
+
exports.noDecimals = noDecimals;
|
|
17
|
+
var _bignumber = _interopRequireDefault(require("bignumber.js"));
|
|
18
|
+
function getAssetByDenom(assets, denom) {
|
|
19
|
+
var asset = assets.find(function (asset) {
|
|
20
|
+
return asset.base === denom;
|
|
21
|
+
});
|
|
22
|
+
if (!asset) {
|
|
23
|
+
throw new Error("Asset not found: ".concat(denom));
|
|
24
|
+
}
|
|
25
|
+
return asset;
|
|
26
|
+
}
|
|
27
|
+
function getDenomByCoinGeckoId(assets, coinGeckoId) {
|
|
28
|
+
return assets.find(function (asset) {
|
|
29
|
+
return asset.coingecko_id === coinGeckoId;
|
|
30
|
+
}).base;
|
|
31
|
+
}
|
|
32
|
+
function getSymbolByChainDenom(assets, denom) {
|
|
33
|
+
var asset = getAssetByDenom(assets, denom);
|
|
34
|
+
var symbol = asset.symbol;
|
|
35
|
+
if (!symbol) {
|
|
36
|
+
return denom;
|
|
37
|
+
}
|
|
38
|
+
return symbol;
|
|
39
|
+
}
|
|
40
|
+
function getChainDenomBySymbol(assets, token) {
|
|
41
|
+
var asset = assets.find(function (_ref) {
|
|
42
|
+
var symbol = _ref.symbol;
|
|
43
|
+
return symbol === token;
|
|
44
|
+
});
|
|
45
|
+
var base = asset === null || asset === void 0 ? void 0 : asset.base;
|
|
46
|
+
if (!base) {
|
|
47
|
+
console.log("cannot find base for token ".concat(token));
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
return base;
|
|
51
|
+
}
|
|
52
|
+
function getExponentByDenom(assets, denom) {
|
|
53
|
+
var asset = getAssetByDenom(assets, denom);
|
|
54
|
+
var unit = asset.denom_units.find(function (_ref2) {
|
|
55
|
+
var denom = _ref2.denom;
|
|
56
|
+
return denom === asset.display;
|
|
57
|
+
});
|
|
58
|
+
return (unit === null || unit === void 0 ? void 0 : unit.exponent) || 0;
|
|
59
|
+
}
|
|
60
|
+
function convertCoinGeckoPricesToDenomPriceMap(assets, prices) {
|
|
61
|
+
return Object.keys(prices).reduce(function (res, geckoId) {
|
|
62
|
+
var denom = getDenomByCoinGeckoId(assets, geckoId);
|
|
63
|
+
res[denom] = prices[geckoId].usd;
|
|
64
|
+
return res;
|
|
65
|
+
}, {});
|
|
66
|
+
}
|
|
67
|
+
function noDecimals(num) {
|
|
68
|
+
return new _bignumber["default"](num).decimalPlaces(0, _bignumber["default"].ROUND_DOWN).toString();
|
|
69
|
+
}
|
|
70
|
+
function convertBaseUnitsToDollarValue(assets, prices, symbol, amount) {
|
|
71
|
+
var denom = getChainDenomBySymbol(assets, symbol);
|
|
72
|
+
return new _bignumber["default"](amount).shiftedBy(-getExponentByDenom(assets, denom)).multipliedBy(prices[denom]).toString();
|
|
73
|
+
}
|
|
74
|
+
function convertDollarValueToDenomUnits(assets, prices, symbol, value) {
|
|
75
|
+
var denom = getChainDenomBySymbol(assets, symbol);
|
|
76
|
+
return new _bignumber["default"](value).dividedBy(prices[denom]).shiftedBy(getExponentByDenom(assets, denom)).toString();
|
|
77
|
+
}
|
|
78
|
+
function convertBaseUnitsToDisplayUnits(assets, symbol, amount) {
|
|
79
|
+
var denom = getChainDenomBySymbol(assets, symbol);
|
|
80
|
+
return new _bignumber["default"](amount).shiftedBy(-getExponentByDenom(assets, denom)).toString();
|
|
81
|
+
}
|
package/main/index.js
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
var _assetListUtil = require("./asset-list-util");
|
|
7
|
+
Object.keys(_assetListUtil).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _assetListUtil[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function get() {
|
|
13
|
+
return _assetListUtil[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
6
17
|
var _fees = require("./fees");
|
|
7
18
|
Object.keys(_fees).forEach(function (key) {
|
|
8
19
|
if (key === "default" || key === "__esModule") return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chain-registry/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.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",
|
|
@@ -74,7 +74,8 @@
|
|
|
74
74
|
"dependencies": {
|
|
75
75
|
"@babel/runtime": "^7.21.0",
|
|
76
76
|
"@chain-registry/types": "^0.16.0",
|
|
77
|
+
"bignumber.js": "9.1.1",
|
|
77
78
|
"sha.js": "^2.4.11"
|
|
78
79
|
},
|
|
79
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "235baabaa618def6fc1bd1de9cd84660c30d1d71"
|
|
80
81
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Asset, AssetDenomUnit } from '@chain-registry/types';
|
|
2
|
+
|
|
3
|
+
export type CoinDenom = AssetDenomUnit['denom'];
|
|
4
|
+
|
|
5
|
+
export type Exponent = AssetDenomUnit['exponent'];
|
|
6
|
+
|
|
7
|
+
export interface CoinGeckoUSD {
|
|
8
|
+
usd: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface PriceHash {
|
|
12
|
+
[key: CoinDenom]: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export declare function getAssetByDenom(
|
|
16
|
+
assets: Asset[],
|
|
17
|
+
denom: CoinDenom
|
|
18
|
+
): Asset;
|
|
19
|
+
|
|
20
|
+
export declare function getDenomByCoinGeckoId(
|
|
21
|
+
assets: Asset[],
|
|
22
|
+
coinGeckoId: string
|
|
23
|
+
): CoinDenom;
|
|
24
|
+
|
|
25
|
+
export declare function getSymbolByChainDenom(
|
|
26
|
+
assets: Asset[],
|
|
27
|
+
denom: CoinDenom
|
|
28
|
+
): string;
|
|
29
|
+
|
|
30
|
+
export declare function getChainDenomBySymbol(
|
|
31
|
+
assets: Asset[],
|
|
32
|
+
token: string
|
|
33
|
+
): CoinDenom;
|
|
34
|
+
|
|
35
|
+
export declare function getExponentByDenom(
|
|
36
|
+
assets: Asset[],
|
|
37
|
+
denom: CoinDenom
|
|
38
|
+
): Exponent;
|
|
39
|
+
|
|
40
|
+
export declare function convertCoinGeckoPricesToDenomPriceMap(
|
|
41
|
+
assets: Asset[],
|
|
42
|
+
prices: Record<string, CoinGeckoUSD>
|
|
43
|
+
): PriceHash;
|
|
44
|
+
|
|
45
|
+
export declare function noDecimals(num: number | string): string;
|
|
46
|
+
|
|
47
|
+
export declare function convertBaseUnitsToDollarValue(
|
|
48
|
+
assets: Asset[],
|
|
49
|
+
prices: PriceHash,
|
|
50
|
+
symbol: string,
|
|
51
|
+
amount: string | number
|
|
52
|
+
): string;
|
|
53
|
+
|
|
54
|
+
export declare function convertDollarValueToDenomUnits(
|
|
55
|
+
assets: Asset[],
|
|
56
|
+
prices: PriceHash,
|
|
57
|
+
symbol: string,
|
|
58
|
+
value: string | number
|
|
59
|
+
): string;
|
|
60
|
+
|
|
61
|
+
export declare function convertBaseUnitsToDisplayUnits(
|
|
62
|
+
assets: Asset[],
|
|
63
|
+
symbol: string,
|
|
64
|
+
amount: string | number
|
|
65
|
+
): string;
|
package/types/index.d.ts
CHANGED