@chain-registry/client 1.20.0 → 1.21.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.
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.ChainInfo = void 0;
8
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
9
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+ var _utils = require("@chain-registry/utils");
12
+ var ChainInfo = /*#__PURE__*/function () {
13
+ function ChainInfo(options) {
14
+ (0, _classCallCheck2["default"])(this, ChainInfo);
15
+ (0, _defineProperty2["default"])(this, "chainName", void 0);
16
+ (0, _defineProperty2["default"])(this, "fetcher", void 0);
17
+ (0, _defineProperty2["default"])(this, "_chain", void 0);
18
+ (0, _defineProperty2["default"])(this, "_assetList", void 0);
19
+ (0, _defineProperty2["default"])(this, "_assetLists", void 0);
20
+ (0, _defineProperty2["default"])(this, "_ibcData", []);
21
+ this.chainName = options.chainName;
22
+ this.fetcher = options.fetcher;
23
+ this.refresh();
24
+ }
25
+ (0, _createClass2["default"])(ChainInfo, [{
26
+ key: "refresh",
27
+ value: function refresh() {
28
+ this._assetList = this.fetcher.getChainAssetList(this.chainName);
29
+ this._ibcData = this.fetcher.getChainIbcData(this.chainName);
30
+ this._chain = this.fetcher.getChain(this.chainName);
31
+ var supportedChains = this._ibcData.reduce(function (m, v) {
32
+ if (!m.includes(v.chain_1.chain_name)) m.push(v.chain_1.chain_name);
33
+ if (!m.includes(v.chain_2.chain_name)) m.push(v.chain_2.chain_name);
34
+ return m;
35
+ }, []);
36
+ this._assetLists = this.fetcher.assetLists.filter(function (list) {
37
+ return supportedChains.includes(list.chain_name);
38
+ });
39
+ }
40
+ }, {
41
+ key: "chain",
42
+ get: function get() {
43
+ return this._chain;
44
+ }
45
+ }, {
46
+ key: "nativeAssetList",
47
+ get: function get() {
48
+ return this._assetList;
49
+ }
50
+ }, {
51
+ key: "assetLists",
52
+ get: function get() {
53
+ return (0, _utils.getAssetLists)(this.chainName, this._ibcData, this._assetLists);
54
+ }
55
+ }]);
56
+ return ChainInfo;
57
+ }();
58
+ exports.ChainInfo = ChainInfo;
@@ -4,25 +4,29 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.ChainRegistryUtils = void 0;
7
+ exports.ChainRegistryChainUtil = void 0;
8
8
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
9
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
10
10
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
11
11
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
12
12
  var _utils = require("@chain-registry/utils");
13
- var ChainRegistryUtils = /*#__PURE__*/function () {
14
- function ChainRegistryUtils(options) {
15
- var _options$assetList, _options$ibcAssetList;
16
- (0, _classCallCheck2["default"])(this, ChainRegistryUtils);
17
- (0, _defineProperty2["default"])(this, "_chainName", void 0);
13
+ var ChainRegistryChainUtil = /*#__PURE__*/function () {
14
+ function ChainRegistryChainUtil(options) {
15
+ (0, _classCallCheck2["default"])(this, ChainRegistryChainUtil);
18
16
  (0, _defineProperty2["default"])(this, "_assets", []);
17
+ (0, _defineProperty2["default"])(this, "_chainName", void 0);
18
+ var chainName = options.chainName,
19
+ chainInfo = options.chainInfo;
19
20
  this._chainName = options.chainName;
20
21
  this._assets = [{
21
- assets: [].concat((0, _toConsumableArray2["default"])(((_options$assetList = options.assetList) === null || _options$assetList === void 0 ? void 0 : _options$assetList.assets) || []), (0, _toConsumableArray2["default"])(((_options$ibcAssetList = options.ibcAssetList) === null || _options$ibcAssetList === void 0 ? void 0 : _options$ibcAssetList.assets) || [])),
22
- chain_name: options.chainName
22
+ assets: [].concat((0, _toConsumableArray2["default"])(chainInfo.nativeAssetList.assets), (0, _toConsumableArray2["default"])(chainInfo.assetLists.flatMap(function (_ref) {
23
+ var assets = _ref.assets;
24
+ return assets;
25
+ }))),
26
+ chain_name: chainName
23
27
  }];
24
28
  }
25
- (0, _createClass2["default"])(ChainRegistryUtils, [{
29
+ (0, _createClass2["default"])(ChainRegistryChainUtil, [{
26
30
  key: "getAssetByDenom",
27
31
  value: function getAssetByDenom(denom) {
28
32
  return (0, _utils.getAssetByDenom)(this._assets, denom, this._chainName);
@@ -105,6 +109,6 @@ var ChainRegistryUtils = /*#__PURE__*/function () {
105
109
  return (0, _utils.convertDisplayUnitToBaseUnit)(this._assets, symbol, amount, this._chainName);
106
110
  }
107
111
  }]);
108
- return ChainRegistryUtils;
112
+ return ChainRegistryChainUtil;
109
113
  }();
110
- exports.ChainRegistryUtils = ChainRegistryUtils;
114
+ exports.ChainRegistryChainUtil = ChainRegistryChainUtil;
@@ -0,0 +1,230 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.ChainRegistryFetcher = void 0;
8
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
10
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
12
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
+ var _utils = require("@chain-registry/utils");
14
+ var _bfsPath = require("bfs-path");
15
+ var _crossFetch = _interopRequireDefault(require("cross-fetch"));
16
+ var _chainInfo = require("./chain-info");
17
+ var fetchUrl = function fetchUrl(url) {
18
+ return (0, _crossFetch["default"])(url).then(function (res) {
19
+ if (res.status >= 400) {
20
+ throw new Error('Bad response');
21
+ }
22
+ return res.json();
23
+ });
24
+ };
25
+ // QUESTION: should ChainRegistryFetcher just be ChainRegistry?
26
+ var ChainRegistryFetcher = /*#__PURE__*/function () {
27
+ function ChainRegistryFetcher() {
28
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
29
+ (0, _classCallCheck2["default"])(this, ChainRegistryFetcher);
30
+ (0, _defineProperty2["default"])(this, "urls", []);
31
+ (0, _defineProperty2["default"])(this, "_assetLists", []);
32
+ (0, _defineProperty2["default"])(this, "_chains", []);
33
+ (0, _defineProperty2["default"])(this, "_ibcData", []);
34
+ (0, _defineProperty2["default"])(this, "chainInfoList", []);
35
+ //
36
+ if (options.chains) {
37
+ this._chains = options.chains;
38
+ }
39
+ if (options.assetLists) {
40
+ this._assetLists = options.assetLists;
41
+ }
42
+ if (options.ibcData) {
43
+ this._ibcData = options.ibcData;
44
+ }
45
+ if (options.urls) {
46
+ this.urls = options.urls;
47
+ }
48
+ }
49
+ (0, _createClass2["default"])(ChainRegistryFetcher, [{
50
+ key: "chains",
51
+ get: function get() {
52
+ return this._chains;
53
+ }
54
+ }, {
55
+ key: "assetLists",
56
+ get: function get() {
57
+ return this._assetLists;
58
+ }
59
+ }, {
60
+ key: "ibcData",
61
+ get: function get() {
62
+ return this._ibcData;
63
+ }
64
+ }, {
65
+ key: "getChain",
66
+ value: function getChain(chainName) {
67
+ return this._chains.find(function (chain) {
68
+ return chain.chain_name === chainName;
69
+ });
70
+ }
71
+ }, {
72
+ key: "getChainAssetList",
73
+ value: function getChainAssetList(chainName) {
74
+ return this._assetLists.find(function (list) {
75
+ return list.chain_name === chainName;
76
+ });
77
+ }
78
+ }, {
79
+ key: "getGeneratedAssetLists",
80
+ value: function getGeneratedAssetLists(chainName) {
81
+ return (0, _utils.getAssetLists)(chainName, this._ibcData, this._assetLists);
82
+ }
83
+ }, {
84
+ key: "getChainIbcData",
85
+ value: function getChainIbcData(chainName) {
86
+ return this._ibcData.filter(function (info) {
87
+ return info.chain_1.chain_name === chainName || info.chain_2.chain_name === chainName;
88
+ });
89
+ }
90
+ }, {
91
+ key: "getChainInfo",
92
+ value: function getChainInfo(chainName) {
93
+ var chainInfo = this.chainInfoList.find(function (it) {
94
+ return it.chainName === chainName;
95
+ });
96
+ if (!chainInfo) {
97
+ chainInfo = new _chainInfo.ChainInfo({
98
+ chainName: chainName,
99
+ fetcher: this
100
+ });
101
+ this.chainInfoList.push(chainInfo);
102
+ }
103
+ return chainInfo;
104
+ }
105
+ }, {
106
+ key: "upsertChain",
107
+ value: function upsertChain(data) {
108
+ var found = this._chains.find(function (chain) {
109
+ return chain.chain_name === data.chain_name && chain.network_type === data.network_type;
110
+ });
111
+ if (!found) {
112
+ this._chains.push(data);
113
+ return;
114
+ }
115
+ this._chains = this._chains.map(function (chain) {
116
+ if (chain.chain_name === data.chain_name && chain.network_type === data.network_type) {
117
+ return data;
118
+ } else {
119
+ return chain;
120
+ }
121
+ });
122
+ }
123
+ }, {
124
+ key: "updateAssetList",
125
+ value: function updateAssetList(data) {
126
+ var found = this._assetLists.find(function (list) {
127
+ return list.chain_name === data.chain_name;
128
+ });
129
+ if (!found) {
130
+ this._assetLists.push(data);
131
+ return;
132
+ }
133
+ this._assetLists = this._assetLists.map(function (list) {
134
+ if (list.chain_name === data.chain_name) {
135
+ return data;
136
+ } else {
137
+ return list;
138
+ }
139
+ });
140
+ }
141
+ }, {
142
+ key: "upsertIbcData",
143
+ value: function upsertIbcData(data) {
144
+ var found = this._ibcData.find(function (info) {
145
+ return info.chain_1.chain_name === data.chain_1.chain_name && info.chain_2.chain_name === data.chain_2.chain_name;
146
+ });
147
+ if (!found) {
148
+ this._ibcData.push(data);
149
+ return;
150
+ }
151
+ this._ibcData = this._ibcData.map(function (info) {
152
+ if (info.chain_1.chain_name === data.chain_1.chain_name && info.chain_2.chain_name === data.chain_2.chain_name) {
153
+ return data;
154
+ } else {
155
+ return info;
156
+ }
157
+ });
158
+ }
159
+ }, {
160
+ key: "update",
161
+ value: function update(data) {
162
+ if (!data.$schema) throw new Error('not a registered JSON schema type');
163
+ var type = (0, _bfsPath.basename)(data.$schema, '.schema.json');
164
+ switch (type) {
165
+ case 'chain':
166
+ this.upsertChain(data);
167
+ break;
168
+ case 'assetlist':
169
+ this.updateAssetList(data);
170
+ break;
171
+ case 'ibc_data':
172
+ this.upsertIbcData(data);
173
+ break;
174
+ default:
175
+ throw new Error('unknown schema type');
176
+ }
177
+ this.chainInfoList.forEach(function (chainInfo) {
178
+ chainInfo.refresh();
179
+ });
180
+ }
181
+ }, {
182
+ key: "fetch",
183
+ value: function () {
184
+ var _fetch = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(url) {
185
+ var data;
186
+ return _regenerator["default"].wrap(function _callee$(_context) {
187
+ while (1) switch (_context.prev = _context.next) {
188
+ case 0:
189
+ _context.next = 2;
190
+ return fetchUrl(url);
191
+ case 2:
192
+ data = _context.sent;
193
+ this.update(data);
194
+ case 4:
195
+ case "end":
196
+ return _context.stop();
197
+ }
198
+ }, _callee, this);
199
+ }));
200
+ function fetch(_x) {
201
+ return _fetch.apply(this, arguments);
202
+ }
203
+ return fetch;
204
+ }()
205
+ }, {
206
+ key: "fetchUrls",
207
+ value: function () {
208
+ var _fetchUrls = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
209
+ var _this = this;
210
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
211
+ while (1) switch (_context2.prev = _context2.next) {
212
+ case 0:
213
+ return _context2.abrupt("return", Promise.all(this.urls.map(function (url) {
214
+ return _this.fetch(url);
215
+ })));
216
+ case 1:
217
+ case "end":
218
+ return _context2.stop();
219
+ }
220
+ }, _callee2, this);
221
+ }));
222
+ function fetchUrls() {
223
+ return _fetchUrls.apply(this, arguments);
224
+ }
225
+ return fetchUrls;
226
+ }()
227
+ }]);
228
+ return ChainRegistryFetcher;
229
+ }();
230
+ exports.ChainRegistryFetcher = ChainRegistryFetcher;
package/main/index.js CHANGED
@@ -3,25 +3,47 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- var _utils = require("./utils");
7
- Object.keys(_utils).forEach(function (key) {
6
+ var _chainInfo = require("./chain-info");
7
+ Object.keys(_chainInfo).forEach(function (key) {
8
8
  if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _utils[key]) return;
9
+ if (key in exports && exports[key] === _chainInfo[key]) return;
10
10
  Object.defineProperty(exports, key, {
11
11
  enumerable: true,
12
12
  get: function get() {
13
- return _utils[key];
13
+ return _chainInfo[key];
14
14
  }
15
15
  });
16
16
  });
17
- var _client = require("./client");
18
- Object.keys(_client).forEach(function (key) {
17
+ var _chainUtil = require("./chain-util");
18
+ Object.keys(_chainUtil).forEach(function (key) {
19
19
  if (key === "default" || key === "__esModule") return;
20
- if (key in exports && exports[key] === _client[key]) return;
20
+ if (key in exports && exports[key] === _chainUtil[key]) return;
21
21
  Object.defineProperty(exports, key, {
22
22
  enumerable: true,
23
23
  get: function get() {
24
- return _client[key];
24
+ return _chainUtil[key];
25
+ }
26
+ });
27
+ });
28
+ var _fetcher = require("./fetcher");
29
+ Object.keys(_fetcher).forEach(function (key) {
30
+ if (key === "default" || key === "__esModule") return;
31
+ if (key in exports && exports[key] === _fetcher[key]) return;
32
+ Object.defineProperty(exports, key, {
33
+ enumerable: true,
34
+ get: function get() {
35
+ return _fetcher[key];
36
+ }
37
+ });
38
+ });
39
+ var _registry = require("./registry");
40
+ Object.keys(_registry).forEach(function (key) {
41
+ if (key === "default" || key === "__esModule") return;
42
+ if (key in exports && exports[key] === _registry[key]) return;
43
+ Object.defineProperty(exports, key, {
44
+ enumerable: true,
45
+ get: function get() {
46
+ return _registry[key];
25
47
  }
26
48
  });
27
49
  });
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.ChainRegistryClient = void 0;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
+ var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
10
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
12
+ var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
13
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
14
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
15
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
16
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
17
+ var _chainUtil = require("./chain-util");
18
+ var _fetcher = require("./fetcher");
19
+ var _excluded = ["chainNames", "assetListNames", "ibcNamePairs", "baseUrl"];
20
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
21
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
22
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
23
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
24
+ var ChainRegistryClient = /*#__PURE__*/function (_ChainRegistryFetcher) {
25
+ (0, _inherits2["default"])(ChainRegistryClient, _ChainRegistryFetcher);
26
+ var _super = _createSuper(ChainRegistryClient);
27
+ function ChainRegistryClient(options) {
28
+ var _this;
29
+ (0, _classCallCheck2["default"])(this, ChainRegistryClient);
30
+ var chainNames = options.chainNames,
31
+ assetListNames = options.assetListNames,
32
+ ibcNamePairs = options.ibcNamePairs,
33
+ baseUrl = options.baseUrl,
34
+ restOptions = (0, _objectWithoutProperties2["default"])(options, _excluded);
35
+ _this = _super.call(this, restOptions);
36
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_options", {
37
+ chainNames: [],
38
+ baseUrl: 'https://raw.githubusercontent.com/cosmos/chain-registry/master'
39
+ });
40
+ _this._options = _objectSpread(_objectSpread({}, _this._options), {}, {
41
+ chainNames: chainNames || _this._options.chainNames,
42
+ assetListNames: assetListNames || _this._options.assetListNames,
43
+ ibcNamePairs: ibcNamePairs || _this._options.ibcNamePairs,
44
+ baseUrl: baseUrl || _this._options.baseUrl
45
+ });
46
+ _this.generateUrls();
47
+ return _this;
48
+ }
49
+ (0, _createClass2["default"])(ChainRegistryClient, [{
50
+ key: "generateUrls",
51
+ value: function generateUrls() {
52
+ var _this$_options = this._options,
53
+ chainNames = _this$_options.chainNames,
54
+ assetListNames = _this$_options.assetListNames,
55
+ ibcNamePairs = _this$_options.ibcNamePairs,
56
+ baseUrl = _this$_options.baseUrl;
57
+ var chainUrls = chainNames.map(function (chain) {
58
+ return "".concat(baseUrl, "/").concat(chain, "/chain.json");
59
+ });
60
+ var assetlistUrls = (assetListNames || chainNames).map(function (chain) {
61
+ return "".concat(baseUrl, "/").concat(chain, "/assetlist.json");
62
+ });
63
+ var namePairs = ibcNamePairs;
64
+ if (!namePairs) {
65
+ namePairs = [];
66
+ for (var i = 0; i < chainNames.length; i++) {
67
+ for (var j = i + 1; j < chainNames.length; j++) {
68
+ namePairs.push([chainNames[i], chainNames[j]]);
69
+ }
70
+ }
71
+ }
72
+ var ibcUrls = namePairs.map(function (namePair) {
73
+ var fileName = namePair[0].localeCompare(namePair[1]) <= 0 ? "".concat(namePair[0], "-").concat(namePair[1], ".json") : "".concat(namePair[1], "-").concat(namePair[0], ".json");
74
+ return "".concat(baseUrl, "/_IBC/").concat(fileName);
75
+ });
76
+ this.urls = (0, _toConsumableArray2["default"])(new Set([].concat((0, _toConsumableArray2["default"])(chainUrls), (0, _toConsumableArray2["default"])(assetlistUrls), (0, _toConsumableArray2["default"])(ibcUrls), (0, _toConsumableArray2["default"])(this.urls || []))));
77
+ }
78
+ }, {
79
+ key: "getChainUtil",
80
+ value: function getChainUtil(chainName) {
81
+ var chainInfo = this.getChainInfo(chainName);
82
+ return new _chainUtil.ChainRegistryChainUtil({
83
+ chainName: chainName,
84
+ chainInfo: chainInfo
85
+ });
86
+ }
87
+ }]);
88
+ return ChainRegistryClient;
89
+ }(_fetcher.ChainRegistryFetcher);
90
+ exports.ChainRegistryClient = ChainRegistryClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chain-registry/client",
3
- "version": "1.20.0",
3
+ "version": "1.21.0",
4
4
  "description": "Chain Registry Client",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/cosmology-tech/chain-registry",
@@ -75,10 +75,10 @@
75
75
  },
76
76
  "dependencies": {
77
77
  "@babel/runtime": "^7.21.0",
78
- "@chain-registry/types": "^0.18.1",
79
- "@chain-registry/utils": "^1.19.0",
78
+ "@chain-registry/types": "^0.18.2",
79
+ "@chain-registry/utils": "^1.19.1",
80
80
  "bfs-path": "^1.0.2",
81
81
  "cross-fetch": "^3.1.5"
82
82
  },
83
- "gitHead": "e57916824aad1598342fe9175e9e87b1ba0babcb"
83
+ "gitHead": "9f414e64137713ec7729edbf3715ce2e458eff3b"
84
84
  }
@@ -0,0 +1,19 @@
1
+ import { AssetList, Chain, IBCInfo } from '@chain-registry/types';
2
+ import { ChainRegistryFetcher } from './fetcher';
3
+ export interface ChainInfoOptions {
4
+ chainName: string;
5
+ fetcher: ChainRegistryFetcher;
6
+ }
7
+ export declare class ChainInfo {
8
+ chainName: string;
9
+ fetcher: ChainRegistryFetcher;
10
+ protected _chain: Chain;
11
+ protected _assetList: AssetList;
12
+ protected _assetLists: AssetList[];
13
+ protected _ibcData: IBCInfo[];
14
+ constructor(options: ChainInfoOptions);
15
+ refresh(): void;
16
+ get chain(): Chain;
17
+ get nativeAssetList(): AssetList;
18
+ get assetLists(): any[];
19
+ }
@@ -1,14 +1,14 @@
1
- import { Asset, AssetList } from '@chain-registry/types';
1
+ import { Asset } from '@chain-registry/types';
2
2
  import type { Denom, Exponent, DenomPriceMap, CoinGeckoUSDPrice } from '@chain-registry/utils';
3
- export interface ChainRegistryUtilsOptions {
3
+ import { ChainInfo } from './chain-info';
4
+ export interface ChainRegistryChainUtilOptions {
4
5
  chainName: string;
5
- assetList?: AssetList;
6
- ibcAssetList?: AssetList;
6
+ chainInfo: ChainInfo;
7
7
  }
8
- export declare class ChainRegistryUtils {
9
- private _chainName;
8
+ export declare class ChainRegistryChainUtil {
10
9
  private _assets;
11
- constructor(options: ChainRegistryUtilsOptions);
10
+ private _chainName;
11
+ constructor(options: ChainRegistryChainUtilOptions);
12
12
  getAssetByDenom(denom: Denom): Asset | undefined;
13
13
  getAssetBySymbol(symbol: string): Asset | undefined;
14
14
  getDenomByCoinGeckoId(coinGeckoId: string): Denom | undefined;
@@ -0,0 +1,30 @@
1
+ import { AssetList, Chain, ChainRegistry, IBCInfo } from '@chain-registry/types';
2
+ import { ChainInfo } from './chain-info';
3
+ export interface ChainRegistryFetcherOptions {
4
+ assetLists?: AssetList[];
5
+ chains?: Chain[];
6
+ ibcData?: IBCInfo[];
7
+ urls?: string[];
8
+ }
9
+ export declare class ChainRegistryFetcher implements ChainRegistry {
10
+ urls: string[];
11
+ protected _assetLists: AssetList[];
12
+ protected _chains: Chain[];
13
+ protected _ibcData: IBCInfo[];
14
+ private chainInfoList;
15
+ constructor(options?: ChainRegistryFetcherOptions);
16
+ get chains(): Chain[];
17
+ get assetLists(): AssetList[];
18
+ get ibcData(): IBCInfo[];
19
+ getChain(chainName: string): Chain;
20
+ getChainAssetList(chainName: string): AssetList;
21
+ getGeneratedAssetLists(chainName: string): AssetList[];
22
+ getChainIbcData(chainName: string): IBCInfo[];
23
+ getChainInfo(chainName: string): ChainInfo;
24
+ upsertChain(data: Chain): void;
25
+ updateAssetList(data: AssetList): void;
26
+ upsertIbcData(data: IBCInfo): void;
27
+ update(data: Chain | AssetList | IBCInfo): void;
28
+ fetch(url: any): Promise<void>;
29
+ fetchUrls(): Promise<void[]>;
30
+ }
package/types/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
- export * from './utils';
2
- export * from './client';
1
+ export * from './chain-info';
2
+ export * from './chain-util';
3
+ export * from './fetcher';
4
+ export * from './registry';
@@ -0,0 +1,14 @@
1
+ import { ChainRegistryChainUtil } from './chain-util';
2
+ import { ChainRegistryFetcher, ChainRegistryFetcherOptions } from './fetcher';
3
+ export interface ChainRegistryClientOptions extends ChainRegistryFetcherOptions {
4
+ chainNames: string[];
5
+ assetListNames?: string[];
6
+ ibcNamePairs?: string[][];
7
+ baseUrl?: string;
8
+ }
9
+ export declare class ChainRegistryClient extends ChainRegistryFetcher {
10
+ protected _options: ChainRegistryClientOptions;
11
+ constructor(options: ChainRegistryClientOptions);
12
+ generateUrls(): void;
13
+ getChainUtil(chainName: string): ChainRegistryChainUtil;
14
+ }
package/main/client.js DELETED
@@ -1,277 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.ChainRegistryClient = void 0;
8
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
- var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
11
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
12
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
14
- var _crossFetch = _interopRequireDefault(require("cross-fetch"));
15
- var _bfsPath = require("bfs-path");
16
- var _utils = require("@chain-registry/utils");
17
- var _utils2 = require("./utils");
18
- var DEFAULT_BASE_URL = 'https://raw.githubusercontent.com/cosmos/chain-registry/master';
19
- var ChainRegistryClient = /*#__PURE__*/function () {
20
- function ChainRegistryClient() {
21
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
22
- (0, _classCallCheck2["default"])(this, ChainRegistryClient);
23
- (0, _defineProperty2["default"])(this, "_allUrls", []);
24
- (0, _defineProperty2["default"])(this, "_baseUrl", DEFAULT_BASE_URL);
25
- (0, _defineProperty2["default"])(this, "_chainNames", []);
26
- (0, _defineProperty2["default"])(this, "_notFoundUrls", []);
27
- (0, _defineProperty2["default"])(this, "_chains", []);
28
- (0, _defineProperty2["default"])(this, "_assetLists", []);
29
- (0, _defineProperty2["default"])(this, "_ibcInfo", []);
30
- var urls = options.urls,
31
- chainNames = options.chainNames,
32
- baseUrl = options.baseUrl;
33
- this._baseUrl = baseUrl !== null && baseUrl !== void 0 ? baseUrl : this._baseUrl;
34
- this._allUrls = this.generateUrls({
35
- urls: urls,
36
- chainNames: chainNames
37
- });
38
- }
39
- (0, _createClass2["default"])(ChainRegistryClient, [{
40
- key: "urls",
41
- get: function get() {
42
- return this._allUrls;
43
- }
44
- }, {
45
- key: "chains",
46
- get: function get() {
47
- return this._chains;
48
- }
49
- }, {
50
- key: "assetLists",
51
- get: function get() {
52
- return this._assetLists;
53
- }
54
- }, {
55
- key: "ibcInfo",
56
- get: function get() {
57
- return this._ibcInfo;
58
- }
59
- }, {
60
- key: "getChain",
61
- value: function getChain(chainName) {
62
- return this._chains.find(function (_ref) {
63
- var chain_name = _ref.chain_name;
64
- return chain_name === chainName;
65
- });
66
- }
67
- }, {
68
- key: "getChainAssetList",
69
- value: function getChainAssetList(chainName) {
70
- return this._assetLists.find(function (_ref2) {
71
- var chain_name = _ref2.chain_name;
72
- return chain_name === chainName;
73
- });
74
- }
75
- }, {
76
- key: "getChainIbcInfo",
77
- value: function getChainIbcInfo(chainName) {
78
- return this._ibcInfo.filter(function (info) {
79
- return info.chain_1.chain_name === chainName || info.chain_2.chain_name === chainName;
80
- });
81
- }
82
- }, {
83
- key: "getChainIbcAssetList",
84
- value: function getChainIbcAssetList(chainName) {
85
- return (0, _utils.getAssetLists)(chainName, this._ibcInfo, this._assetLists)[0];
86
- }
87
- }, {
88
- key: "getChainFullData",
89
- value: function getChainFullData(chainName) {
90
- return {
91
- chain: this.getChain(chainName),
92
- assetList: this.getChainAssetList(chainName),
93
- ibcInfo: this.getChainIbcInfo(chainName),
94
- ibcAssetList: this.getChainIbcAssetList(chainName)
95
- };
96
- }
97
- }, {
98
- key: "getChainUtils",
99
- value: function getChainUtils(chainName) {
100
- return new _utils2.ChainRegistryUtils({
101
- chainName: chainName,
102
- assetList: this.getChainAssetList(chainName),
103
- ibcAssetList: this.getChainIbcAssetList(chainName)
104
- });
105
- }
106
- }, {
107
- key: "fetch",
108
- value: function () {
109
- var _fetch = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
110
- var _this = this;
111
- var options,
112
- urls,
113
- newUrls,
114
- responses,
115
- _args = arguments;
116
- return _regenerator["default"].wrap(function _callee$(_context) {
117
- while (1) switch (_context.prev = _context.next) {
118
- case 0:
119
- options = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
120
- urls = [];
121
- if (Object.keys(options).length === 0) {
122
- urls = this._allUrls;
123
- } else {
124
- newUrls = this.generateUrls(options);
125
- urls = newUrls;
126
- this._allUrls = (0, _toConsumableArray2["default"])(new Set([].concat((0, _toConsumableArray2["default"])(this._allUrls), (0, _toConsumableArray2["default"])(newUrls))));
127
- }
128
- _context.next = 5;
129
- return Promise.all(urls.map(function (url) {
130
- return _this.fetchUrl(url);
131
- }));
132
- case 5:
133
- responses = _context.sent;
134
- responses.filter(Boolean).forEach(function (data) {
135
- return _this.save(data);
136
- });
137
- case 7:
138
- case "end":
139
- return _context.stop();
140
- }
141
- }, _callee, this);
142
- }));
143
- function fetch() {
144
- return _fetch.apply(this, arguments);
145
- }
146
- return fetch;
147
- }()
148
- }, {
149
- key: "formatChainUrl",
150
- value: function formatChainUrl(chainName) {
151
- return "".concat(this._baseUrl, "/").concat(chainName, "/chain.json");
152
- }
153
- }, {
154
- key: "formatAssetListUrl",
155
- value: function formatAssetListUrl(chainName) {
156
- return "".concat(this._baseUrl, "/").concat(chainName, "/assetlist.json");
157
- }
158
- }, {
159
- key: "formatIbcUrl",
160
- value: function formatIbcUrl(chain1, chain2) {
161
- return "".concat(this._baseUrl, "/_IBC/").concat(chain1, "-").concat(chain2, ".json");
162
- }
163
- }, {
164
- key: "generateNamePairs",
165
- value: function generateNamePairs(chainNames) {
166
- var pairs = [];
167
- for (var i = 0; i < chainNames.length; i++) {
168
- for (var j = i + 1; j < chainNames.length; j++) {
169
- pairs.push([chainNames[i], chainNames[j]].sort());
170
- }
171
- }
172
- return pairs;
173
- }
174
- }, {
175
- key: "generateUrls",
176
- value: function generateUrls(_ref3) {
177
- var _this2 = this;
178
- var _ref3$urls = _ref3.urls,
179
- urls = _ref3$urls === void 0 ? [] : _ref3$urls,
180
- _ref3$chainNames = _ref3.chainNames,
181
- chainNames = _ref3$chainNames === void 0 ? [] : _ref3$chainNames;
182
- var chainUrls = chainNames.map(function (chain) {
183
- return _this2.formatChainUrl(chain);
184
- });
185
- var assetListUrls = chainNames.map(function (chain) {
186
- return _this2.formatAssetListUrl(chain);
187
- });
188
- this._chainNames = (0, _toConsumableArray2["default"])(new Set([].concat((0, _toConsumableArray2["default"])(this._chainNames), (0, _toConsumableArray2["default"])(chainNames))));
189
- var allPairs = this.generateNamePairs(this._chainNames);
190
- var newPairs = allPairs.filter(function (pair) {
191
- return !_this2._ibcInfo.some(function (info) {
192
- return info.chain_1.chain_name === pair[0] && info.chain_2.chain_name === pair[1];
193
- });
194
- });
195
- var ibcUrls = newPairs.map(function (pair) {
196
- return _this2.formatIbcUrl(pair[0], pair[1]);
197
- });
198
- var allUrls = (0, _toConsumableArray2["default"])(new Set([].concat((0, _toConsumableArray2["default"])(chainUrls), (0, _toConsumableArray2["default"])(assetListUrls), (0, _toConsumableArray2["default"])(ibcUrls), (0, _toConsumableArray2["default"])(urls))));
199
- return allUrls.filter(function (url) {
200
- return !_this2._notFoundUrls.includes(url);
201
- });
202
- }
203
- }, {
204
- key: "fetchUrl",
205
- value: function () {
206
- var _fetchUrl = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(url) {
207
- var _this3 = this;
208
- return _regenerator["default"].wrap(function _callee2$(_context2) {
209
- while (1) switch (_context2.prev = _context2.next) {
210
- case 0:
211
- return _context2.abrupt("return", (0, _crossFetch["default"])(url).then(function (res) {
212
- if (res.status === 404) {
213
- console.error("Failed to fetch ".concat(url, " with status 404"));
214
- _this3._notFoundUrls.push(url);
215
- return null;
216
- }
217
- if (res.status >= 400) {
218
- throw new Error("Failed to fetch ".concat(url, " with status ").concat(res.status));
219
- }
220
- return res.json();
221
- }));
222
- case 1:
223
- case "end":
224
- return _context2.stop();
225
- }
226
- }, _callee2);
227
- }));
228
- function fetchUrl(_x) {
229
- return _fetchUrl.apply(this, arguments);
230
- }
231
- return fetchUrl;
232
- }()
233
- }, {
234
- key: "upsert",
235
- value: function upsert(array, element, filterFn) {
236
- var res = array.find(filterFn);
237
- if (!res) {
238
- return [].concat((0, _toConsumableArray2["default"])(array), [element]);
239
- }
240
- return array.map(function (item) {
241
- if (filterFn(item)) {
242
- return element;
243
- }
244
- return item;
245
- });
246
- }
247
- }, {
248
- key: "save",
249
- value: function save(data) {
250
- if (!data.$schema) {
251
- throw new Error('Invalid data: Missing $schema property');
252
- }
253
- var type = (0, _bfsPath.basename)(data.$schema, '.schema.json');
254
- switch (type) {
255
- case 'chain':
256
- this._chains = this.upsert(this._chains, data, function (chain) {
257
- return chain.chain_name === data.chain_name && chain.network_type === data.network_type;
258
- });
259
- break;
260
- case 'assetlist':
261
- this._assetLists = this.upsert(this._assetLists, data, function (assetList) {
262
- return assetList.chain_name === data.chain_name;
263
- });
264
- break;
265
- case 'ibc_data':
266
- this._ibcInfo = this.upsert(this._ibcInfo, data, function (info) {
267
- return info.chain_1.chain_name === data.chain_1.chain_name && info.chain_2.chain_name === data.chain_2.chain_name;
268
- });
269
- break;
270
- default:
271
- throw new Error("Unsupported json schema: ".concat(data.$schema));
272
- }
273
- }
274
- }]);
275
- return ChainRegistryClient;
276
- }();
277
- exports.ChainRegistryClient = ChainRegistryClient;
package/types/client.d.ts DELETED
@@ -1,43 +0,0 @@
1
- import { AssetList, Chain, IBCInfo } from '@chain-registry/types';
2
- import { ChainRegistryUtils } from './utils';
3
- export interface FetchOptions {
4
- urls?: string[];
5
- chainNames?: string[];
6
- }
7
- export interface ChainRegistryClientOptions extends FetchOptions {
8
- baseUrl?: string;
9
- }
10
- export declare class ChainRegistryClient {
11
- private _allUrls;
12
- private _baseUrl;
13
- private _chainNames;
14
- private _notFoundUrls;
15
- private _chains;
16
- private _assetLists;
17
- private _ibcInfo;
18
- constructor(options?: ChainRegistryClientOptions);
19
- get urls(): string[];
20
- get chains(): Chain[];
21
- get assetLists(): AssetList[];
22
- get ibcInfo(): IBCInfo[];
23
- getChain(chainName: string): Chain | undefined;
24
- getChainAssetList(chainName: string): AssetList | undefined;
25
- getChainIbcInfo(chainName: string): IBCInfo[];
26
- getChainIbcAssetList(chainName: string): AssetList;
27
- getChainFullData(chainName: string): {
28
- chain: Chain;
29
- assetList: AssetList;
30
- ibcInfo: IBCInfo[];
31
- ibcAssetList: AssetList;
32
- };
33
- getChainUtils(chainName: string): ChainRegistryUtils;
34
- fetch(options?: FetchOptions): Promise<void>;
35
- private formatChainUrl;
36
- private formatAssetListUrl;
37
- private formatIbcUrl;
38
- private generateNamePairs;
39
- private generateUrls;
40
- private fetchUrl;
41
- private upsert;
42
- private save;
43
- }