@chain-registry/utils 1.22.1 → 1.24.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/ibc.js ADDED
@@ -0,0 +1,380 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAssetLists = exports.getCw20Assets = exports.getIbcAssets = exports.getIbcDenomByBase = exports.getIbcAssetPath = exports.getWasmChannel = exports.getNonTransferChannel = exports.getTransferChannel = exports.getIbcInfo = exports.ibcDenom = void 0;
4
+ const sha_js_1 = require("sha.js");
5
+ const ibcDenom = (paths, coinMinimalDenom) => {
6
+ const prefixes = [];
7
+ for (const path of paths) {
8
+ prefixes.push(`${path.port_id}/${path.channel_id}`);
9
+ }
10
+ const prefix = prefixes.join('/');
11
+ const denom = `${prefix}/${coinMinimalDenom}`;
12
+ return ('ibc/' +
13
+ Buffer.from(new Uint8Array(new sha_js_1.sha256().update(Buffer.from(denom)).digest()))
14
+ .toString('hex')
15
+ .toUpperCase());
16
+ };
17
+ exports.ibcDenom = ibcDenom;
18
+ const findInfo = (ibc, to, from) => ibc.find((i) => i.chain_1.chain_name === from && i.chain_2.chain_name === to);
19
+ const getIbcInfo = (ibc, chain, counterparty) => {
20
+ return (findInfo(ibc, chain, counterparty) || findInfo(ibc, counterparty, chain));
21
+ };
22
+ exports.getIbcInfo = getIbcInfo;
23
+ const getTransferChannel = (info) => {
24
+ return info.channels.find((channel) => channel.chain_1.port_id === 'transfer' &&
25
+ channel.chain_2.port_id === 'transfer');
26
+ };
27
+ exports.getTransferChannel = getTransferChannel;
28
+ const getNonTransferChannel = (info) => {
29
+ return info.channels.find((channel) => (channel.chain_1.port_id !== 'transfer' &&
30
+ channel.chain_2.port_id === 'transfer') ||
31
+ (channel.chain_1.port_id === 'transfer' &&
32
+ channel.chain_2.port_id !== 'transfer'));
33
+ };
34
+ exports.getNonTransferChannel = getNonTransferChannel;
35
+ const getWasmChannel = (info) => {
36
+ return info.channels.find((channel) => (channel.chain_1.port_id.startsWith('wasm.') &&
37
+ channel.chain_2.port_id === 'transfer') ||
38
+ (channel.chain_1.port_id === 'transfer' &&
39
+ channel.chain_2.port_id.startsWith('wasm')));
40
+ };
41
+ exports.getWasmChannel = getWasmChannel;
42
+ const getIbcAssetPath = (ibc, chain, counterparty, assets, base) => {
43
+ const ibcInfo = (0, exports.getIbcInfo)(ibc, chain, counterparty);
44
+ if (!ibcInfo) {
45
+ return [];
46
+ }
47
+ const channel = base.startsWith('cw20:')
48
+ ? (0, exports.getWasmChannel)(ibcInfo)
49
+ : (0, exports.getTransferChannel)(ibcInfo);
50
+ if (!channel) {
51
+ return [];
52
+ }
53
+ let channelInfo;
54
+ if (ibcInfo.chain_1.chain_name === chain) {
55
+ channelInfo = channel.chain_1;
56
+ }
57
+ else {
58
+ channelInfo = channel.chain_2;
59
+ }
60
+ const memo = [channelInfo];
61
+ const assetList = assets.find(({ chain_name }) => chain_name === counterparty);
62
+ if (!assetList) {
63
+ return memo;
64
+ }
65
+ const asset = assetList.assets.find((asset) => asset.base === base);
66
+ if (!asset) {
67
+ console.log(`no ${base} found in ${counterparty}`);
68
+ return memo;
69
+ }
70
+ const traces = asset.traces?.filter?.((trace) => {
71
+ return trace.type === 'ibc' || trace.type === 'ibc-cw20';
72
+ }) ?? [];
73
+ if (!traces.length) {
74
+ return memo;
75
+ }
76
+ if (traces.length > 1) {
77
+ console.log(traces);
78
+ console.warn('contact maintainers: multi-hop not yet supported');
79
+ }
80
+ const [trace] = traces;
81
+ return [
82
+ ...memo,
83
+ ...(0, exports.getIbcAssetPath)(ibc, counterparty, trace.counterparty.chain_name, assets, trace.counterparty.base_denom
84
+ // base
85
+ )
86
+ ];
87
+ };
88
+ exports.getIbcAssetPath = getIbcAssetPath;
89
+ const getIbcDenomByBase = (ibc, chain, counterparty, assets, base) => {
90
+ const ibcInfo = (0, exports.getIbcInfo)(ibc, chain, counterparty);
91
+ if (ibcInfo) {
92
+ const channel = base.startsWith('cw20:')
93
+ ? (0, exports.getWasmChannel)(ibcInfo)
94
+ : (0, exports.getTransferChannel)(ibcInfo);
95
+ if (!channel) {
96
+ return;
97
+ }
98
+ const ibcPath = (0, exports.getIbcAssetPath)(ibc, chain, counterparty, assets, base);
99
+ const assetList = assets.find(({ chain_name }) => chain_name === counterparty);
100
+ if (!assetList) {
101
+ console.warn(`missing asset list for ${counterparty}`);
102
+ // could be incorrect...
103
+ return (0, exports.ibcDenom)(ibcPath, base);
104
+ }
105
+ const asset = assetList.assets.find((asset) => asset.base === base);
106
+ if (!asset) {
107
+ console.warn(`no ${base} found in ${counterparty}`);
108
+ return (0, exports.ibcDenom)(ibcPath, base);
109
+ }
110
+ const ibcTrace = asset.traces?.find?.((trace) => trace.type === 'ibc');
111
+ const baseDenom = ibcTrace?.counterparty?.base_denom ?? asset.base;
112
+ return (0, exports.ibcDenom)(ibcPath, baseDenom);
113
+ }
114
+ };
115
+ exports.getIbcDenomByBase = getIbcDenomByBase;
116
+ const getIbcAssets = (chainName, ibc, assets) => {
117
+ const chainIbcInfo = ibc.filter((i) => {
118
+ return (i.chain_1.chain_name === chainName || i.chain_2.chain_name === chainName);
119
+ });
120
+ const ibcAssetLists = chainIbcInfo
121
+ .map((ibcInfo) => {
122
+ const counterpartyIs = ibcInfo.chain_1.chain_name === chainName ? 'chain_2' : 'chain_1';
123
+ const chainIs = ibcInfo.chain_1.chain_name === chainName ? 'chain_1' : 'chain_2';
124
+ const counterparty = ibcInfo[counterpartyIs].chain_name;
125
+ const counterpartyIbc = ibcInfo[counterpartyIs];
126
+ const chainIbc = ibcInfo[chainIs];
127
+ const baseCounterpartyAssets = assets.find((a) => {
128
+ return a.chain_name === counterparty;
129
+ });
130
+ if (!baseCounterpartyAssets) {
131
+ // console.warn('asset not found: ' + counterparty);
132
+ return;
133
+ }
134
+ // const counterpartyAssets = baseCounterpartyAssets;
135
+ const counterpartyAssets = {
136
+ ...baseCounterpartyAssets,
137
+ assets: baseCounterpartyAssets.assets.filter((a) => {
138
+ if (
139
+ // https://github.com/cosmos/chain-registry/issues/1535
140
+ baseCounterpartyAssets.chain_name === 'carbon' &&
141
+ a.base.startsWith('ibc/')) {
142
+ return false;
143
+ }
144
+ else {
145
+ return true;
146
+ }
147
+ })
148
+ };
149
+ const ibcAssets = counterpartyAssets.assets
150
+ .filter((a) => !a.base.startsWith('cw20:'))
151
+ .map((asset) => {
152
+ const denom = (0, exports.getIbcDenomByBase)(ibc, chainName, counterparty,
153
+ //
154
+ assets, asset.base);
155
+ const newAsset = {
156
+ ...asset
157
+ };
158
+ newAsset.base = denom;
159
+ newAsset.denom_units = newAsset.denom_units.map((unit) => {
160
+ if (unit.denom === asset.base) {
161
+ const newUnit = {
162
+ ...unit
163
+ };
164
+ newUnit.denom = denom;
165
+ newUnit.aliases = [unit.denom];
166
+ return newUnit;
167
+ }
168
+ return unit;
169
+ });
170
+ return newAsset;
171
+ });
172
+ const channel = (0, exports.getTransferChannel)(ibcInfo);
173
+ if (!channel) {
174
+ return;
175
+ }
176
+ return {
177
+ chain: {
178
+ ...chainIbc,
179
+ ...channel[chainIs]
180
+ },
181
+ counterparty: {
182
+ ...counterpartyIbc,
183
+ ...channel[counterpartyIs]
184
+ },
185
+ assets: ibcAssets
186
+ };
187
+ })
188
+ .filter(Boolean);
189
+ const hash = ibcAssetLists.reduce((m, v) => {
190
+ m[v.chain.chain_name] = m[v.chain.chain_name] || [];
191
+ const assets = v.assets
192
+ .map((asset) => {
193
+ try {
194
+ return {
195
+ ...asset,
196
+ traces: [
197
+ {
198
+ type: 'ibc',
199
+ counterparty: {
200
+ // source_channel
201
+ channel_id: v.counterparty.channel_id,
202
+ // source_denom
203
+ base_denom: asset.denom_units[0]?.aliases?.[0] ??
204
+ asset.denom_units[0].denom,
205
+ chain_name: v.counterparty.chain_name
206
+ // port: v.counterparty.port_id
207
+ },
208
+ chain: {
209
+ // dst_denom
210
+ channel_id: v.chain.channel_id
211
+ // chain_name: v.chain.chain_name,
212
+ // port: v.chain.port_id
213
+ }
214
+ }
215
+ ]
216
+ };
217
+ }
218
+ catch (e) {
219
+ console.log('problem creating assets:');
220
+ console.log(asset);
221
+ }
222
+ })
223
+ .filter(Boolean);
224
+ const obj = {
225
+ ...v,
226
+ assets
227
+ };
228
+ m[v.chain.chain_name].push(obj);
229
+ return m;
230
+ }, {});
231
+ return Object.keys(hash).map((chain) => {
232
+ return {
233
+ chain_name: chain,
234
+ assets: hash[chain].reduce((m, v) => {
235
+ return [...m, ...v.assets];
236
+ }, [])
237
+ };
238
+ });
239
+ };
240
+ exports.getIbcAssets = getIbcAssets;
241
+ const getCw20Assets = (chainName, ibc, assets) => {
242
+ const chainIbcInfo = ibc.filter((i) => {
243
+ return (i.chain_1.chain_name === chainName || i.chain_2.chain_name === chainName);
244
+ });
245
+ const cw20AssetLists = chainIbcInfo
246
+ .map((ibcInfo) => {
247
+ const counterpartyIs = ibcInfo.chain_1.chain_name === chainName ? 'chain_2' : 'chain_1';
248
+ const chainIs = ibcInfo.chain_1.chain_name === chainName ? 'chain_1' : 'chain_2';
249
+ const counterparty = ibcInfo[counterpartyIs].chain_name;
250
+ const counterpartyIbc = ibcInfo[counterpartyIs];
251
+ const chainIbc = ibcInfo[chainIs];
252
+ const counterpartyAssets = assets.find((a) => {
253
+ return a.chain_name === counterparty;
254
+ });
255
+ if (!counterpartyAssets) {
256
+ // console.warn('asset not found: ' + counterparty);
257
+ return;
258
+ }
259
+ const cw20Assets = counterpartyAssets.assets
260
+ .filter((a) => a.base.startsWith('cw20:'))
261
+ .map((asset) => {
262
+ const denom = (0, exports.getIbcDenomByBase)(ibc, chainName, counterparty,
263
+ //
264
+ assets, asset.base);
265
+ const newAsset = {
266
+ ...asset
267
+ };
268
+ newAsset.base = denom;
269
+ newAsset.denom_units = newAsset.denom_units.map((unit) => {
270
+ if (unit.denom === asset.base) {
271
+ const newUnit = {
272
+ ...unit
273
+ };
274
+ newUnit.denom = denom;
275
+ newUnit.aliases = [unit.denom];
276
+ return newUnit;
277
+ }
278
+ return unit;
279
+ });
280
+ return newAsset;
281
+ });
282
+ if (!cw20Assets.length)
283
+ return;
284
+ const channel = (0, exports.getWasmChannel)(ibcInfo);
285
+ if (!channel) {
286
+ // console.warn(
287
+ // chainIbc.chain_name,
288
+ // '<>',
289
+ // counterpartyIbc.chain_name,
290
+ // 'MISSING cw20 IBC info'
291
+ // );
292
+ return;
293
+ }
294
+ return {
295
+ chain: {
296
+ ...chainIbc,
297
+ ...channel[chainIs]
298
+ },
299
+ counterparty: {
300
+ ...counterpartyIbc,
301
+ ...channel[counterpartyIs]
302
+ },
303
+ assets: cw20Assets
304
+ };
305
+ })
306
+ .filter(Boolean);
307
+ const hash = cw20AssetLists.reduce((m, v) => {
308
+ const assetList = v;
309
+ m[assetList.chain.chain_name] = m[assetList.chain.chain_name] || [];
310
+ const assets = assetList.assets
311
+ .map((asset) => {
312
+ try {
313
+ return {
314
+ ...asset,
315
+ traces: [
316
+ {
317
+ type: 'ibc-cw20',
318
+ counterparty: {
319
+ port: v.counterparty.port_id,
320
+ // source_channel
321
+ channel_id: v.counterparty.channel_id,
322
+ // source_denom
323
+ base_denom: asset.denom_units[0]?.aliases?.[0] ??
324
+ asset.denom_units[0].denom,
325
+ chain_name: v.counterparty.chain_name
326
+ },
327
+ chain: {
328
+ // dst_denom
329
+ port: v.chain.port_id,
330
+ channel_id: v.chain.channel_id
331
+ // chain_name: v.chain.chain_name,
332
+ }
333
+ }
334
+ ]
335
+ };
336
+ }
337
+ catch (e) {
338
+ console.log('problem creating cw20 assets');
339
+ console.log(asset);
340
+ }
341
+ })
342
+ .filter(Boolean);
343
+ const obj = {
344
+ ...v,
345
+ assets
346
+ };
347
+ m[v.chain.chain_name].push(obj);
348
+ return m;
349
+ }, {});
350
+ return Object.keys(hash).map((chain) => {
351
+ return {
352
+ chain_name: chain,
353
+ assets: hash[chain].reduce((m, v) => {
354
+ return [...m, ...v.assets];
355
+ }, [])
356
+ };
357
+ });
358
+ };
359
+ exports.getCw20Assets = getCw20Assets;
360
+ const getAssetLists = (chainName, ibc, assets) => {
361
+ const ibcAssetLists = (0, exports.getIbcAssets)(chainName, ibc, assets);
362
+ const cw20Assets = (0, exports.getCw20Assets)(chainName, ibc, assets);
363
+ return ibcAssetLists.reduce((m, v) => {
364
+ const chain = v.chain_name;
365
+ const assets = [...v.assets];
366
+ const cw20 = cw20Assets.find((a) => a.chain_name === chain);
367
+ if (cw20) {
368
+ // @ts-ignore
369
+ [].push.apply(assets, cw20.assets);
370
+ }
371
+ return [
372
+ {
373
+ chain_name: chain,
374
+ assets
375
+ },
376
+ ...m
377
+ ];
378
+ }, []);
379
+ };
380
+ exports.getAssetLists = getAssetLists;
package/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './assets';
2
+ export * from './calc';
3
+ export * from './chains';
4
+ export * from './ibc';
package/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./assets"), exports);
18
+ __exportStar(require("./calc"), exports);
19
+ __exportStar(require("./chains"), exports);
20
+ __exportStar(require("./ibc"), exports);
package/package.json CHANGED
@@ -1,95 +1,45 @@
1
1
  {
2
2
  "name": "@chain-registry/utils",
3
- "version": "1.22.1",
3
+ "version": "1.24.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",
7
7
  "license": "SEE LICENSE IN LICENSE",
8
- "main": "main/index.js",
9
- "typings": "types/index.d.ts",
10
- "directories": {
11
- "lib": "src",
12
- "test": "__tests__"
13
- },
14
- "files": [
15
- "types",
16
- "main"
17
- ],
18
- "scripts": {
19
- "build": "cross-env BABEL_ENV=production babel src --out-dir main --delete-dir-on-start --extensions \".tsx,.ts,.js\"",
20
- "build:ts": "tsc --project ./tsconfig.json",
21
- "buidl": "npm run build && npm run build:ts",
22
- "prepare": "npm run build",
23
- "dev": "cross-env NODE_ENV=development babel-node src/telescope --extensions \".tsx,.ts,.js\"",
24
- "watch": "cross-env NODE_ENV=development babel-watch src/telescope --extensions \".tsx,.ts,.js\"",
25
- "file": "cross-env NODE_ENV=development babel-watch src/file --extensions \".tsx,.ts,.js\"",
26
- "lint": "eslint --ext .ts,.tsx,.js .",
27
- "format": "eslint --fix . --ext .ts,.tsx,.js",
28
- "test": "jest",
29
- "test:watch": "jest --watch",
30
- "test:debug": "node --inspect node_modules/.bin/jest --runInBand"
31
- },
8
+ "main": "index.js",
9
+ "module": "esm/index.js",
10
+ "types": "index.d.ts",
32
11
  "publishConfig": {
33
- "access": "public"
12
+ "access": "public",
13
+ "directory": "dist"
34
14
  },
35
15
  "repository": {
36
16
  "type": "git",
37
17
  "url": "https://github.com/cosmology-tech/chain-registry"
38
18
  },
39
- "keywords": [],
40
19
  "bugs": {
41
20
  "url": "https://github.com/cosmology-tech/chain-registry/issues"
42
21
  },
43
- "jest": {
44
- "preset": "ts-jest",
45
- "testEnvironment": "node",
46
- "transform": {
47
- "^.+\\.ts?$": "ts-jest"
48
- },
49
- "transformIgnorePatterns": [
50
- "<rootDir>/node_modules/"
51
- ],
52
- "testPathIgnorePatterns": [
53
- "main/",
54
- "module/",
55
- "types/"
56
- ]
22
+ "scripts": {
23
+ "copy": "copyfiles -f ../../LICENSE README.md package.json dist",
24
+ "clean": "del dist/**",
25
+ "prepare": "npm run build",
26
+ "build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
27
+ "test": "jest",
28
+ "test:watch": "jest --watch"
57
29
  },
58
30
  "devDependencies": {
59
- "@babel/cli": "7.21.0",
60
- "@babel/core": "7.21.4",
61
- "@babel/eslint-parser": "^7.21.3",
62
- "@babel/node": "^7.20.7",
63
- "@babel/plugin-proposal-class-properties": "7.18.6",
64
- "@babel/plugin-proposal-export-default-from": "7.18.10",
65
- "@babel/plugin-proposal-object-rest-spread": "7.20.7",
66
- "@babel/plugin-transform-runtime": "7.21.4",
67
- "@babel/preset-env": "7.21.4",
68
- "@babel/preset-typescript": "^7.21.4",
69
- "@types/jest": "^29.5.1",
70
- "@types/sha.js": "^2.4.0",
71
- "@typescript-eslint/eslint-plugin": "5.59.0",
72
- "@typescript-eslint/parser": "5.59.0",
73
- "babel-core": "7.0.0-bridge.0",
74
- "babel-jest": "29.5.0",
75
- "babel-watch": "^7.7.2",
76
- "cross-env": "^7.0.2",
77
- "eslint": "8.38.0",
78
- "eslint-config-prettier": "^8.8.0",
79
- "eslint-plugin-prettier": "^4.0.0",
80
- "eslint-plugin-simple-import-sort": "8.0.0",
81
- "eslint-plugin-unused-imports": "2.0.0",
82
- "jest": "^29.5.0",
83
- "prettier": "^2.8.7",
84
- "regenerator-runtime": "^0.13.11",
85
- "ts-jest": "^29.1.0",
86
- "typescript": "^5.0.4"
31
+ "@types/sha.js": "^2.4.0"
87
32
  },
88
33
  "dependencies": {
89
- "@babel/runtime": "^7.21.0",
90
- "@chain-registry/types": "^0.21.1",
34
+ "@chain-registry/types": "^0.23.0",
91
35
  "bignumber.js": "9.1.1",
92
36
  "sha.js": "^2.4.11"
93
37
  },
94
- "gitHead": "bfed96d5fbfd612daf7633f6081818003ee96b09"
38
+ "keywords": [
39
+ "chain-registry",
40
+ "web3",
41
+ "cosmos",
42
+ "interchain"
43
+ ],
44
+ "gitHead": "796f4aef739279d4d8d35b89394afd004c29513e"
95
45
  }
@@ -1 +1 @@
1
- export declare const customFind: <T>(array: T[], filterFn: (item: T) => boolean) => T;
1
+ export declare const customFind: <T>(array: T[], filterFn: (item: T) => boolean) => T | undefined;
package/utils.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.customFind = void 0;
4
+ const customFind = (array, filterFn) => {
5
+ const filteredItems = array.filter(filterFn);
6
+ const filterCount = filteredItems.length;
7
+ if (filterCount > 1) {
8
+ throw new Error(`Ambiguity Error: ${filterCount} items found.`);
9
+ }
10
+ return filteredItems[0];
11
+ };
12
+ exports.customFind = customFind;
package/main/assets.js DELETED
@@ -1,126 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getTokenNameByDenom = exports.getTokenLogoByDenom = exports.getSymbolByDenom = exports.getNativeTokenByChainName = exports.getExponentFromAsset = exports.getExponentBySymbol = exports.getExponentByDenom = exports.getDenomBySymbol = exports.getDenomByCoinGeckoId = exports.getCoinGeckoIdByDenom = exports.getChainNameByStakingDenom = exports.getChainNameByDenom = exports.getChainLogo = exports.getChainByStakingDenom = exports.getAssetBySymbol = exports.getAssetByDenom = void 0;
7
- var _utils = require("./utils");
8
- var getAssetByKeyValue = function getAssetByKeyValue(assets, key, value, chainName) {
9
- var filteredAssets = assets.filter(function (_ref) {
10
- var chain_name = _ref.chain_name;
11
- return !chainName || chain_name === chainName;
12
- }).flatMap(function (_ref2) {
13
- var assets = _ref2.assets;
14
- return assets;
15
- });
16
- return (0, _utils.customFind)(filteredAssets, function (asset) {
17
- return asset[key] === value;
18
- });
19
- };
20
- var getAssetByDenom = exports.getAssetByDenom = function getAssetByDenom(assets, denom, chainName) {
21
- return getAssetByKeyValue(assets, 'base', denom, chainName);
22
- };
23
- var getAssetBySymbol = exports.getAssetBySymbol = function getAssetBySymbol(assets, symbol, chainName) {
24
- return getAssetByKeyValue(assets, 'symbol', symbol, chainName);
25
- };
26
- var getDenomByCoinGeckoId = exports.getDenomByCoinGeckoId = function getDenomByCoinGeckoId(assets, coinGeckoId, chainName) {
27
- var _getAssetByKeyValue;
28
- return (_getAssetByKeyValue = getAssetByKeyValue(assets, 'coingecko_id', coinGeckoId, chainName)) === null || _getAssetByKeyValue === void 0 ? void 0 : _getAssetByKeyValue.base;
29
- };
30
- var getCoinGeckoIdByDenom = exports.getCoinGeckoIdByDenom = function getCoinGeckoIdByDenom(assets, denom) {
31
- var _ref3 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
32
- chainName = _ref3.chainName,
33
- _ref3$allowTestnet = _ref3.allowTestnet,
34
- allowTestnet = _ref3$allowTestnet === void 0 ? false : _ref3$allowTestnet,
35
- _ref3$customAssetFilt = _ref3.customAssetFilter,
36
- customAssetFilter = _ref3$customAssetFilt === void 0 ? function () {
37
- return true;
38
- } : _ref3$customAssetFilt,
39
- _ref3$excludedChainNa = _ref3.excludedChainNames,
40
- excludedChainNames = _ref3$excludedChainNa === void 0 ? [] : _ref3$excludedChainNa;
41
- var filteredAssetLists = assets.filter(function (_ref4) {
42
- var chain_name = _ref4.chain_name;
43
- return (!chainName || chain_name === chainName) && (allowTestnet || !chain_name.includes('testnet')) && !excludedChainNames.includes(chain_name);
44
- });
45
- var filteredAssets = filteredAssetLists.flatMap(function (_ref5) {
46
- var assets = _ref5.assets;
47
- return assets;
48
- }).filter(function (_ref6) {
49
- var coingecko_id = _ref6.coingecko_id;
50
- return coingecko_id;
51
- }).filter(customAssetFilter);
52
- var asset = filteredAssets.find(function (_ref7) {
53
- var base = _ref7.base;
54
- return base === denom;
55
- });
56
- return asset === null || asset === void 0 ? void 0 : asset.coingecko_id;
57
- };
58
- var getSymbolByDenom = exports.getSymbolByDenom = function getSymbolByDenom(assets, denom, chainName) {
59
- var _getAssetByDenom;
60
- return (_getAssetByDenom = getAssetByDenom(assets, denom, chainName)) === null || _getAssetByDenom === void 0 ? void 0 : _getAssetByDenom.symbol;
61
- };
62
- var getDenomBySymbol = exports.getDenomBySymbol = function getDenomBySymbol(assets, symbol, chainName) {
63
- var _getAssetByKeyValue2;
64
- return (_getAssetByKeyValue2 = getAssetByKeyValue(assets, 'symbol', symbol, chainName)) === null || _getAssetByKeyValue2 === void 0 ? void 0 : _getAssetByKeyValue2.base;
65
- };
66
- var getExponentFromAsset = exports.getExponentFromAsset = function getExponentFromAsset(asset) {
67
- var _asset$denom_units$fi;
68
- return (_asset$denom_units$fi = asset.denom_units.find(function (_ref8) {
69
- var denom = _ref8.denom;
70
- return denom === asset.display;
71
- })) === null || _asset$denom_units$fi === void 0 ? void 0 : _asset$denom_units$fi.exponent;
72
- };
73
- var getExponentByDenom = exports.getExponentByDenom = function getExponentByDenom(assets, denom, chainName) {
74
- var asset = getAssetByDenom(assets, denom, chainName);
75
- return asset ? getExponentFromAsset(asset) : undefined;
76
- };
77
- var getExponentBySymbol = exports.getExponentBySymbol = function getExponentBySymbol(assets, symbol, chainName) {
78
- var asset = getAssetBySymbol(assets, symbol, chainName);
79
- return asset ? getExponentFromAsset(asset) : undefined;
80
- };
81
- var getNativeTokenByChainName = exports.getNativeTokenByChainName = function getNativeTokenByChainName(assets, chainName) {
82
- var assetList = (0, _utils.customFind)(assets, function (assetList) {
83
- return assetList.chain_name === chainName && !assetList.assets[0].base.startsWith('ibc/');
84
- });
85
- return assetList === null || assetList === void 0 ? void 0 : assetList.assets[0];
86
- };
87
- var getTokenLogoByDenom = exports.getTokenLogoByDenom = function getTokenLogoByDenom(assets, denom, chainName) {
88
- var asset = getAssetByDenom(assets, denom, chainName);
89
- return Object.values((asset === null || asset === void 0 ? void 0 : asset.logo_URIs) || {})[0];
90
- };
91
- var getChainLogo = exports.getChainLogo = function getChainLogo(assets, chainName) {
92
- var nativeToken = getNativeTokenByChainName(assets, chainName);
93
- return Object.values((nativeToken === null || nativeToken === void 0 ? void 0 : nativeToken.logo_URIs) || {})[0];
94
- };
95
- var getTokenNameByDenom = exports.getTokenNameByDenom = function getTokenNameByDenom(assets, denom, chainName) {
96
- var asset = getAssetByDenom(assets, denom, chainName);
97
- return asset === null || asset === void 0 ? void 0 : asset.name;
98
- };
99
- var getChainNameByDenom = exports.getChainNameByDenom = function getChainNameByDenom(assets, denom) {
100
- var _customFind;
101
- var isIbcDenom = denom.startsWith('ibc/');
102
- if (isIbcDenom) {
103
- var _asset$traces, _asset$traces$find, _asset$traces$find$co;
104
- var _asset = getAssetByDenom(assets, denom);
105
- return _asset === null || _asset === void 0 ? void 0 : (_asset$traces = _asset.traces) === null || _asset$traces === void 0 ? void 0 : (_asset$traces$find = _asset$traces.find(function (t) {
106
- return t.type === 'ibc';
107
- })) === null || _asset$traces$find === void 0 ? void 0 : (_asset$traces$find$co = _asset$traces$find.counterparty) === null || _asset$traces$find$co === void 0 ? void 0 : _asset$traces$find$co.chain_name;
108
- }
109
- return (_customFind = (0, _utils.customFind)(assets, function (assetList) {
110
- return assetList.assets.some(function (asset) {
111
- return asset.base === denom;
112
- });
113
- })) === null || _customFind === void 0 ? void 0 : _customFind.chain_name;
114
- };
115
- var getChainByStakingDenom = exports.getChainByStakingDenom = function getChainByStakingDenom(chains, denom) {
116
- return (0, _utils.customFind)(chains, function (chain) {
117
- var _chain$staking;
118
- return !!((_chain$staking = chain.staking) !== null && _chain$staking !== void 0 && _chain$staking.staking_tokens.find(function (token) {
119
- return token.denom === denom;
120
- }));
121
- });
122
- };
123
- var getChainNameByStakingDenom = exports.getChainNameByStakingDenom = function getChainNameByStakingDenom(chains, denom) {
124
- var _getChainByStakingDen;
125
- return (_getChainByStakingDen = getChainByStakingDenom(chains, denom)) === null || _getChainByStakingDen === void 0 ? void 0 : _getChainByStakingDen.chain_name;
126
- };