@chain-registry/client 1.0.1

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 ADDED
@@ -0,0 +1,8 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## 1.0.1 (2022-10-20)
7
+
8
+ **Note:** Version bump only for package @chain-registry/client
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Dan Lynch <pyramation@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # @chain-registry/client
2
+
3
+ <p align="center" width="100%">
4
+ <img height="90" src="https://user-images.githubusercontent.com/545047/190171475-b416f99e-2831-4786-9ba3-a7ff4d95b0d3.svg" />
5
+ </p>
6
+
7
+ <p align="center" width="100%">
8
+ <a href="https://github.com/cosmology-tech/chain-registry/actions/workflows/run-tests.yml">
9
+ <img height="20" src="https://github.com/cosmology-tech/chain-registry/actions/workflows/run-tests.yml/badge.svg" />
10
+ </a>
11
+ <a href="https://github.com/cosmology-tech/chain-registry/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
12
+ <a href="https://www.npmjs.com/package/@chain-registry/client"><img height="20" src="https://img.shields.io/github/package-json/v/cosmology-tech/chain-registry?filename=packages%2Fclient%2Fpackage.json"></a>
13
+ </p>
14
+
15
+ Client for the chain-registry
16
+
17
+
18
+ ## Usage
19
+
20
+ ```js
21
+ import { ChainRegistryFetcher, ChainRegistryFetcherOptions } from '@chain-registry/client';
22
+
23
+ const options: ChainRegistryFetcherOptions = {
24
+ urls: [
25
+ 'https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/chain.json',
26
+ 'https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/assetlist.json',
27
+ 'https://raw.githubusercontent.com/cosmos/chain-registry/master/juno/assetlist.json',
28
+ 'https://raw.githubusercontent.com/cosmos/chain-registry/master/secretnetwork/assetlist.json',
29
+ 'https://raw.githubusercontent.com/cosmos/chain-registry/master/_IBC/juno-osmosis.json',
30
+ 'https://raw.githubusercontent.com/cosmos/chain-registry/master/_IBC/osmosis-secretnetwork.json'
31
+ ]
32
+ };
33
+ const registry = new ChainRegistry(options);
34
+ await registry.fetchUrls();
35
+ ```
36
+
37
+ ## Fetching Schemata
38
+
39
+ We currently only support fetching JSON schemas as defined in https://github.com/cosmos/chain-registry. Supported are `assetlist.schema.json`, `chain.schema.json` and `ibc_data.schema.json`.
40
+
41
+ ### fetchUrls
42
+
43
+ You can set the `ChainRegistry.urls` property and call `ChainRegistry.fetchUrls()`
44
+
45
+ ```js
46
+ registry.urls = [
47
+ // urls to fetch
48
+ ];
49
+ await registry.fetchUrls();
50
+ ```
51
+
52
+ ### fetch
53
+
54
+ Or, you can simply call `ChainRegistry.fetch()`
55
+
56
+ ```js
57
+ await registry.fetch('https://some-json-schema.com/some-schema.json');
58
+ ```
59
+
60
+ ## Asset lists
61
+
62
+ You can get generated asset lists directly from the registry:
63
+
64
+ ```js
65
+ // generated asset lists
66
+ const generated: AssetList[] = registry.getGeneratedAssetLists('osmosis');
67
+ ```
68
+
69
+ You can get generated `AssetList[]` objects directly from the `ChainRegistry` via the `assetLists` method:
70
+
71
+ ```js
72
+ // you can also get generated assets from ChainInfo object
73
+ const chainInfo: Chain = registry.getChainInfo('osmosis');
74
+ const generatedAssets: AssetList[] = chainInfo.assetLists;
75
+ ```
76
+ ## Chain info
77
+
78
+ You can get `Chain` object directly from the `ChainRegistry` via the `getChain` method:
79
+
80
+ ```js
81
+ // get Chain from registry
82
+ const chain: Chain = registry.getChain('osmosis');
83
+ ```
84
+
85
+ or get the `ChainInfo` object:
86
+
87
+ ```js
88
+ const chainInfo: ChainInfo = registry.getChainInfo('osmosis');
89
+
90
+ // AssetList[] of the generated assets
91
+ const assetes: AssetList[] = chainInfo.assetLists;
92
+
93
+ // Chain
94
+ const chain: Chain = chainInfo.chain;
95
+
96
+ // AssetList[] of the native assets
97
+ const assetes: AssetList[] = chainInfo.nativeAssetLists;
98
+ ```
99
+
100
+ ## Credits
101
+
102
+ 🛠 Built by Cosmology — if you like our tools, please consider delegating to [our validator ⚛️](https://cosmology.tech/validator)
package/main/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _registry = require("./registry");
8
+
9
+ Object.keys(_registry).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _registry[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _registry[key];
16
+ }
17
+ });
18
+ });
@@ -0,0 +1,313 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.ChainRegistryFetcher = exports.ChainInfo = void 0;
9
+
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+
14
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
15
+
16
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
17
+
18
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
19
+
20
+ var _utils = require("@chain-registry/utils");
21
+
22
+ var _bfsPath = require("bfs-path");
23
+
24
+ var _crossFetch = _interopRequireDefault(require("cross-fetch"));
25
+
26
+ var fetchUrl = function fetchUrl(url) {
27
+ return (0, _crossFetch["default"])(url).then(function (res) {
28
+ if (res.status >= 400) {
29
+ throw new Error('Bad response');
30
+ }
31
+
32
+ return res.json();
33
+ });
34
+ };
35
+
36
+ var ChainInfo = /*#__PURE__*/function () {
37
+ function ChainInfo(options) {
38
+ (0, _classCallCheck2["default"])(this, ChainInfo);
39
+ (0, _defineProperty2["default"])(this, "chain_name", void 0);
40
+ (0, _defineProperty2["default"])(this, "fetcher", void 0);
41
+ (0, _defineProperty2["default"])(this, "_asset_list", void 0);
42
+ (0, _defineProperty2["default"])(this, "_asset_lists", void 0);
43
+ (0, _defineProperty2["default"])(this, "_chain", void 0);
44
+ (0, _defineProperty2["default"])(this, "_ibc_data", []);
45
+ this.chain_name = options.chain_name;
46
+ this.fetcher = options.fetcher;
47
+ this.refresh();
48
+ }
49
+
50
+ (0, _createClass2["default"])(ChainInfo, [{
51
+ key: "refresh",
52
+ value: function refresh() {
53
+ this._asset_list = this.fetcher.getChainAssetList(this.chain_name);
54
+ this._ibc_data = this.fetcher.getChainIbcData(this.chain_name);
55
+ this._chain = this.fetcher.getChain(this.chain_name);
56
+
57
+ var supportedChains = this._ibc_data.reduce(function (m, v) {
58
+ if (!m.includes(v.chain_1.chain_name)) m.push(v.chain_1.chain_name);
59
+ if (!m.includes(v.chain_2.chain_name)) m.push(v.chain_2.chain_name);
60
+ return m;
61
+ }, []);
62
+
63
+ this._asset_lists = this.fetcher.assetLists.filter(function (list) {
64
+ return supportedChains.includes(list.chain_name);
65
+ });
66
+ }
67
+ }, {
68
+ key: "nativeAssetLists",
69
+ get: function get() {
70
+ return this._asset_list;
71
+ }
72
+ }, {
73
+ key: "chain",
74
+ get: function get() {
75
+ return this._chain;
76
+ }
77
+ }, {
78
+ key: "assetLists",
79
+ get: function get() {
80
+ return (0, _utils.getAssetLists)(this.chain_name, this._ibc_data, this._asset_lists);
81
+ }
82
+ }]);
83
+ return ChainInfo;
84
+ }(); // QUESTION: should ChainRegistryFetcher just be ChainRegistry?
85
+
86
+
87
+ exports.ChainInfo = ChainInfo;
88
+
89
+ var ChainRegistryFetcher = /*#__PURE__*/function () {
90
+ function ChainRegistryFetcher() {
91
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
92
+ (0, _classCallCheck2["default"])(this, ChainRegistryFetcher);
93
+ (0, _defineProperty2["default"])(this, "_asset_lists", []);
94
+ (0, _defineProperty2["default"])(this, "_chains", []);
95
+ (0, _defineProperty2["default"])(this, "_ibc_data", []);
96
+ (0, _defineProperty2["default"])(this, "urls", []);
97
+
98
+ //
99
+ if (options.assetLists) {
100
+ this._asset_lists = options.assetLists;
101
+ }
102
+
103
+ if (options.chains) {
104
+ this._chains = options.chains;
105
+ }
106
+
107
+ if (options.ibcData) {
108
+ this._ibc_data = options.ibcData;
109
+ }
110
+
111
+ if (options.urls) {
112
+ this.urls = options.urls;
113
+ }
114
+ }
115
+
116
+ (0, _createClass2["default"])(ChainRegistryFetcher, [{
117
+ key: "assetLists",
118
+ get: function get() {
119
+ return this._asset_lists;
120
+ }
121
+ }, {
122
+ key: "getChainAssetList",
123
+ value: function getChainAssetList(chainName) {
124
+ return this._asset_lists.find(function (list) {
125
+ return list.chain_name === chainName;
126
+ });
127
+ }
128
+ }, {
129
+ key: "getGeneratedAssetLists",
130
+ value: function getGeneratedAssetLists(chainName) {
131
+ return (0, _utils.getAssetLists)(chainName, this._ibc_data, this._asset_lists);
132
+ }
133
+ }, {
134
+ key: "chains",
135
+ get: function get() {
136
+ return this._chains;
137
+ }
138
+ }, {
139
+ key: "getChain",
140
+ value: function getChain(chainName) {
141
+ return this._chains.find(function (chain) {
142
+ return chain.chain_name === chainName;
143
+ });
144
+ }
145
+ }, {
146
+ key: "ibcData",
147
+ get: function get() {
148
+ return this._ibc_data;
149
+ }
150
+ }, {
151
+ key: "getChainIbcData",
152
+ value: function getChainIbcData(chainName) {
153
+ return this._ibc_data.filter(function (info) {
154
+ return info.chain_1.chain_name === chainName || info.chain_2.chain_name === chainName;
155
+ });
156
+ }
157
+ }, {
158
+ key: "getChainInfo",
159
+ value: function getChainInfo(chainName) {
160
+ return new ChainInfo({
161
+ chain_name: chainName,
162
+ fetcher: this
163
+ });
164
+ }
165
+ }, {
166
+ key: "upsertChain",
167
+ value: function upsertChain(data) {
168
+ var found = this._chains.find(function (chain) {
169
+ return chain.chain_name === data.chain_name && chain.network_type === data.network_type;
170
+ });
171
+
172
+ if (!found) {
173
+ this._chains.push(data);
174
+
175
+ return;
176
+ }
177
+
178
+ this._chains = this._chains.map(function (chain) {
179
+ if (chain.chain_name === data.chain_name && chain.network_type === data.network_type) {
180
+ return data;
181
+ } else {
182
+ return chain;
183
+ }
184
+ });
185
+ }
186
+ }, {
187
+ key: "updateAssetList",
188
+ value: function updateAssetList(data) {
189
+ var found = this._asset_lists.find(function (list) {
190
+ return list.chain_name === data.chain_name;
191
+ });
192
+
193
+ if (!found) {
194
+ this._asset_lists.push(data);
195
+
196
+ return;
197
+ }
198
+
199
+ this._asset_lists = this._asset_lists.map(function (list) {
200
+ if (list.chain_name === data.chain_name) {
201
+ return data;
202
+ } else {
203
+ return list;
204
+ }
205
+ });
206
+ }
207
+ }, {
208
+ key: "upsertIbcData",
209
+ value: function upsertIbcData(data) {
210
+ var found = this._ibc_data.find(function (info) {
211
+ return info.chain_1.chain_name === data.chain_1.chain_name && info.chain_2.chain_name === data.chain_2.chain_name;
212
+ });
213
+
214
+ if (!found) {
215
+ this._ibc_data.push(data);
216
+
217
+ return;
218
+ }
219
+
220
+ this._ibc_data = this._ibc_data.map(function (info) {
221
+ if (info.chain_1.chain_name === data.chain_1.chain_name && info.chain_2.chain_name === data.chain_2.chain_name) {
222
+ return data;
223
+ } else {
224
+ return info;
225
+ }
226
+ });
227
+ }
228
+ }, {
229
+ key: "update",
230
+ value: function update(data) {
231
+ if (!data.$schema) throw new Error('not a registered JSON schema type');
232
+ var type = (0, _bfsPath.basename)(data.$schema, '.schema.json');
233
+
234
+ switch (type) {
235
+ case 'chain':
236
+ this.upsertChain(data);
237
+ break;
238
+
239
+ case 'assetlist':
240
+ this.updateAssetList(data);
241
+ break;
242
+
243
+ case 'ibc_data':
244
+ this.upsertIbcData(data);
245
+ break;
246
+
247
+ default:
248
+ throw new Error('unknown schema type');
249
+ }
250
+ }
251
+ }, {
252
+ key: "fetch",
253
+ value: function () {
254
+ var _fetch = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(url) {
255
+ var data;
256
+ return _regenerator["default"].wrap(function _callee$(_context) {
257
+ while (1) {
258
+ switch (_context.prev = _context.next) {
259
+ case 0:
260
+ _context.next = 2;
261
+ return fetchUrl(url);
262
+
263
+ case 2:
264
+ data = _context.sent;
265
+ this.update(data);
266
+
267
+ case 4:
268
+ case "end":
269
+ return _context.stop();
270
+ }
271
+ }
272
+ }, _callee, this);
273
+ }));
274
+
275
+ function fetch(_x) {
276
+ return _fetch.apply(this, arguments);
277
+ }
278
+
279
+ return fetch;
280
+ }()
281
+ }, {
282
+ key: "fetchUrls",
283
+ value: function () {
284
+ var _fetchUrls = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
285
+ var _this = this;
286
+
287
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
288
+ while (1) {
289
+ switch (_context2.prev = _context2.next) {
290
+ case 0:
291
+ return _context2.abrupt("return", Promise.all(this.urls.map(function (url) {
292
+ return _this.fetch(url);
293
+ })));
294
+
295
+ case 1:
296
+ case "end":
297
+ return _context2.stop();
298
+ }
299
+ }
300
+ }, _callee2, this);
301
+ }));
302
+
303
+ function fetchUrls() {
304
+ return _fetchUrls.apply(this, arguments);
305
+ }
306
+
307
+ return fetchUrls;
308
+ }()
309
+ }]);
310
+ return ChainRegistryFetcher;
311
+ }();
312
+
313
+ exports.ChainRegistryFetcher = ChainRegistryFetcher;
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "@chain-registry/client",
3
+ "version": "1.0.1",
4
+ "description": "Chain Registry Client",
5
+ "author": "Dan Lynch <pyramation@gmail.com>",
6
+ "homepage": "https://github.com/cosmology-tech/chain-registry",
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
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/cosmology-tech/chain-registry"
38
+ },
39
+ "keywords": [],
40
+ "bugs": {
41
+ "url": "https://github.com/cosmology-tech/chain-registry/issues"
42
+ },
43
+ "devDependencies": {
44
+ "@babel/cli": "7.18.10",
45
+ "@babel/core": "7.19.1",
46
+ "@babel/eslint-parser": "^7.19.1",
47
+ "@babel/node": "^7.19.1",
48
+ "@babel/plugin-proposal-class-properties": "7.18.6",
49
+ "@babel/plugin-proposal-export-default-from": "7.18.10",
50
+ "@babel/plugin-proposal-object-rest-spread": "7.18.9",
51
+ "@babel/plugin-transform-runtime": "7.19.1",
52
+ "@babel/preset-env": "7.19.1",
53
+ "@babel/preset-typescript": "^7.17.12",
54
+ "@types/jest": "^29.0.2",
55
+ "@types/sha.js": "^2.4.0",
56
+ "@typescript-eslint/eslint-plugin": "5.37.0",
57
+ "@typescript-eslint/parser": "5.37.0",
58
+ "babel-core": "7.0.0-bridge.0",
59
+ "babel-jest": "29.0.3",
60
+ "babel-watch": "^7.0.0",
61
+ "cross-env": "^7.0.2",
62
+ "eslint": "8.23.1",
63
+ "eslint-config-prettier": "^8.5.0",
64
+ "eslint-plugin-prettier": "^4.0.0",
65
+ "eslint-plugin-simple-import-sort": "8.0.0",
66
+ "eslint-plugin-unused-imports": "2.0.0",
67
+ "jest": "^29.0.3",
68
+ "long": "^5.2.0",
69
+ "prettier": "^2.7.0",
70
+ "regenerator-runtime": "^0.13.7",
71
+ "ts-jest": "^29.0.1",
72
+ "typescript": "^4.8.3"
73
+ },
74
+ "dependencies": {
75
+ "@babel/runtime": "^7.19.0",
76
+ "@chain-registry/types": "^0.11.1",
77
+ "@chain-registry/utils": "^1.0.1",
78
+ "bfs-path": "^1.0.2",
79
+ "cross-fetch": "^3.1.5"
80
+ },
81
+ "gitHead": "1dc04b938a9bbfb226dc3f91092c98c2c2d4c15a"
82
+ }