@chain-registry/utils 0.5.1 → 0.6.2

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,312 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.ChainRegistry = 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 _bfsPath = require("bfs-path");
21
+
22
+ var _crossFetch = _interopRequireDefault(require("cross-fetch"));
23
+
24
+ var _utils = require("./utils");
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, "registry", 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.registry = options.registry;
47
+ this.refresh();
48
+ }
49
+
50
+ (0, _createClass2["default"])(ChainInfo, [{
51
+ key: "refresh",
52
+ value: function refresh() {
53
+ this._asset_list = this.registry.getChainAssetList(this.chain_name);
54
+ this._ibc_data = this.registry.getChainIbcData(this.chain_name);
55
+ this._chain = this.registry.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.registry.assetLists().filter(function (list) {
64
+ return supportedChains.includes(list.chain_name);
65
+ });
66
+ }
67
+ }, {
68
+ key: "nativeAssetLists",
69
+ value: function nativeAssetLists() {
70
+ return this._asset_list;
71
+ }
72
+ }, {
73
+ key: "chain",
74
+ value: function chain() {
75
+ return this._chain;
76
+ }
77
+ }, {
78
+ key: "assetLists",
79
+ value: function assetLists() {
80
+ return (0, _utils.getAssetLists)(this.chain_name, this._ibc_data, this._asset_lists);
81
+ }
82
+ }]);
83
+ return ChainInfo;
84
+ }();
85
+
86
+ exports.ChainInfo = ChainInfo;
87
+
88
+ var ChainRegistry = /*#__PURE__*/function () {
89
+ function ChainRegistry() {
90
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
91
+ (0, _classCallCheck2["default"])(this, ChainRegistry);
92
+ (0, _defineProperty2["default"])(this, "_asset_lists", []);
93
+ (0, _defineProperty2["default"])(this, "_chains", []);
94
+ (0, _defineProperty2["default"])(this, "_ibc_data", []);
95
+ (0, _defineProperty2["default"])(this, "urls", []);
96
+
97
+ //
98
+ if (options.assetLists) {
99
+ this._asset_lists = options.assetLists;
100
+ }
101
+
102
+ if (options.chains) {
103
+ this._chains = options.chains;
104
+ }
105
+
106
+ if (options.ibcData) {
107
+ this._ibc_data = options.ibcData;
108
+ }
109
+
110
+ if (options.urls) {
111
+ this.urls = options.urls;
112
+ }
113
+ }
114
+
115
+ (0, _createClass2["default"])(ChainRegistry, [{
116
+ key: "assetLists",
117
+ value: function assetLists() {
118
+ return this._asset_lists;
119
+ }
120
+ }, {
121
+ key: "getChainAssetList",
122
+ value: function getChainAssetList(chainName) {
123
+ return this._asset_lists.find(function (list) {
124
+ return list.chain_name === chainName;
125
+ });
126
+ }
127
+ }, {
128
+ key: "getGeneratedAssetLists",
129
+ value: function getGeneratedAssetLists(chainName) {
130
+ return (0, _utils.getAssetLists)(chainName, this._ibc_data, this._asset_lists);
131
+ }
132
+ }, {
133
+ key: "chains",
134
+ value: function chains() {
135
+ return this._chains;
136
+ }
137
+ }, {
138
+ key: "getChain",
139
+ value: function getChain(chainName) {
140
+ return this._chains.find(function (chain) {
141
+ return chain.chain_name === chainName;
142
+ });
143
+ }
144
+ }, {
145
+ key: "ibcData",
146
+ value: function ibcData() {
147
+ return this._ibc_data;
148
+ }
149
+ }, {
150
+ key: "getChainIbcData",
151
+ value: function getChainIbcData(chainName) {
152
+ return this._ibc_data.filter(function (info) {
153
+ return info['chain-1']['chain-name'] === chainName || info['chain-2']['chain-name'] === chainName;
154
+ });
155
+ }
156
+ }, {
157
+ key: "getChainInfo",
158
+ value: function getChainInfo(chainName) {
159
+ return new ChainInfo({
160
+ chain_name: chainName,
161
+ registry: this
162
+ });
163
+ }
164
+ }, {
165
+ key: "upsertChain",
166
+ value: function upsertChain(data) {
167
+ var found = this._chains.find(function (chain) {
168
+ return chain.chain_name === data.chain_name && chain.network_type === data.network_type;
169
+ });
170
+
171
+ if (!found) {
172
+ this._chains.push(data);
173
+
174
+ return;
175
+ }
176
+
177
+ this._chains = this._chains.map(function (chain) {
178
+ if (chain.chain_name === data.chain_name && chain.network_type === data.network_type) {
179
+ return data;
180
+ } else {
181
+ return chain;
182
+ }
183
+ });
184
+ }
185
+ }, {
186
+ key: "updateAssetList",
187
+ value: function updateAssetList(data) {
188
+ var found = this._asset_lists.find(function (list) {
189
+ return list.chain_name === data.chain_name;
190
+ });
191
+
192
+ if (!found) {
193
+ this._asset_lists.push(data);
194
+
195
+ return;
196
+ }
197
+
198
+ this._asset_lists = this._asset_lists.map(function (list) {
199
+ if (list.chain_name === data.chain_name) {
200
+ return data;
201
+ } else {
202
+ return list;
203
+ }
204
+ });
205
+ }
206
+ }, {
207
+ key: "upsertIbcData",
208
+ value: function upsertIbcData(data) {
209
+ var found = this._ibc_data.find(function (info) {
210
+ return info['chain-1']['chain-name'] === data['chain-1']['chain-name'] && info['chain-2']['chain-name'] === data['chain-2']['chain-name'];
211
+ });
212
+
213
+ if (!found) {
214
+ this._ibc_data.push(data);
215
+
216
+ return;
217
+ }
218
+
219
+ this._ibc_data = this._ibc_data.map(function (info) {
220
+ if (info['chain-1']['chain-name'] === data['chain-1']['chain-name'] && info['chain-2']['chain-name'] === data['chain-2']['chain-name']) {
221
+ return data;
222
+ } else {
223
+ return info;
224
+ }
225
+ });
226
+ }
227
+ }, {
228
+ key: "update",
229
+ value: function update(data) {
230
+ if (!data.$schema) throw new Error('not a registered JSON schema type');
231
+ var type = (0, _bfsPath.basename)(data.$schema, '.schema.json');
232
+
233
+ switch (type) {
234
+ case 'chain':
235
+ this.upsertChain(data);
236
+ break;
237
+
238
+ case 'assetlist':
239
+ this.updateAssetList(data);
240
+ break;
241
+
242
+ case 'ibc_data':
243
+ this.upsertIbcData(data);
244
+ break;
245
+
246
+ default:
247
+ throw new Error('unknown schema type');
248
+ }
249
+ }
250
+ }, {
251
+ key: "fetch",
252
+ value: function () {
253
+ var _fetch = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(url) {
254
+ var data;
255
+ return _regenerator["default"].wrap(function _callee$(_context) {
256
+ while (1) {
257
+ switch (_context.prev = _context.next) {
258
+ case 0:
259
+ _context.next = 2;
260
+ return fetchUrl(url);
261
+
262
+ case 2:
263
+ data = _context.sent;
264
+ this.update(data);
265
+
266
+ case 4:
267
+ case "end":
268
+ return _context.stop();
269
+ }
270
+ }
271
+ }, _callee, this);
272
+ }));
273
+
274
+ function fetch(_x) {
275
+ return _fetch.apply(this, arguments);
276
+ }
277
+
278
+ return fetch;
279
+ }()
280
+ }, {
281
+ key: "fetchUrls",
282
+ value: function () {
283
+ var _fetchUrls = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
284
+ var _this = this;
285
+
286
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
287
+ while (1) {
288
+ switch (_context2.prev = _context2.next) {
289
+ case 0:
290
+ return _context2.abrupt("return", Promise.all(this.urls.map(function (url) {
291
+ return _this.fetch(url);
292
+ })));
293
+
294
+ case 1:
295
+ case "end":
296
+ return _context2.stop();
297
+ }
298
+ }
299
+ }, _callee2, this);
300
+ }));
301
+
302
+ function fetchUrls() {
303
+ return _fetchUrls.apply(this, arguments);
304
+ }
305
+
306
+ return fetchUrls;
307
+ }()
308
+ }]);
309
+ return ChainRegistry;
310
+ }();
311
+
312
+ exports.ChainRegistry = ChainRegistry;