UNPKG

28.1 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.Decorate = void 0;
7
8var _rxjs = require("rxjs");
9
10var _apiDerive = require("@polkadot/api-derive");
11
12var _rpcCore = require("@polkadot/rpc-core");
13
14var _rpcProvider = require("@polkadot/rpc-provider");
15
16var _types = require("@polkadot/types");
17
18var _util = require("@polkadot/util");
19
20var _submittable = require("../submittable");
21
22var _augmentObject = require("../util/augmentObject");
23
24var _decorate = require("../util/decorate");
25
26var _validate = require("../util/validate");
27
28var _Events = require("./Events");
29
30var _find = require("./find");
31
32// Copyright 2017-2022 @polkadot/api authors & contributors
33// SPDX-License-Identifier: Apache-2.0
34// the max amount of keys/values that we will retrieve at once
35const PAGE_SIZE_K = 1000; // limit aligned with the 1k on the node (trie lookups are heavy)
36
37const PAGE_SIZE_V = 250; // limited since the data may be very large (e.g. misfiring elections)
38
39const l = (0, _util.logger)('api/init');
40let instanceCounter = 0;
41
42function getAtQueryFn(api, _ref) {
43 let {
44 method,
45 section
46 } = _ref;
47 return (0, _util.assertReturn)(api.rx.query[section] && api.rx.query[section][method], () => `query.${section}.${method} is not available in this version of the metadata`);
48}
49
50class Decorate extends _Events.Events {
51 #instanceId;
52 #registry; // HACK Use BN import so decorateDerive works... yes, wtf.
53
54 __phantom = new _util.BN(0);
55 _consts = {};
56 _errors = {};
57 _events = {};
58 _extrinsicType = 4; // latest extrinsic version
59
60 _isReady = false;
61 _query = {};
62 _rx = {
63 consts: {},
64 query: {},
65 tx: {}
66 };
67
68 /**
69 * @description Create an instance of the class
70 *
71 * @param options Options object to create API instance or a Provider instance
72 *
73 * @example
74 * <BR>
75 *
76 * ```javascript
77 * import Api from '@polkadot/api/promise';
78 *
79 * const api = new Api().isReady();
80 *
81 * api.rpc.subscribeNewHeads((header) => {
82 * console.log(`new block #${header.number.toNumber()}`);
83 * });
84 * ```
85 */
86 constructor(options, type, decorateMethod) {
87 var _options$source;
88
89 super();
90 this.#instanceId = `${++instanceCounter}`;
91 this.#registry = ((_options$source = options.source) === null || _options$source === void 0 ? void 0 : _options$source.registry) || options.registry || new _types.TypeRegistry();
92
93 this._rx.queryAt = (blockHash, knownVersion) => (0, _rxjs.from)(this.at(blockHash, knownVersion)).pipe((0, _rxjs.map)(a => a.rx.query));
94
95 this._rx.registry = this.#registry;
96 const thisProvider = options.source ? options.source._rpcCore.provider.clone() : options.provider || new _rpcProvider.WsProvider();
97 this._decorateMethod = decorateMethod;
98 this._options = options;
99 this._type = type; // The RPC interface decorates the known interfaces on init
100
101 this._rpcCore = new _rpcCore.RpcCore(this.#instanceId, this.#registry, thisProvider, this._options.rpc);
102 this._isConnected = new _rxjs.BehaviorSubject(this._rpcCore.provider.isConnected);
103 this._rx.hasSubscriptions = this._rpcCore.provider.hasSubscriptions;
104 }
105
106 /**
107 * @description Return the current used registry
108 */
109 get registry() {
110 return this.#registry;
111 }
112 /**
113 * @description Creates an instance of a type as registered
114 */
115
116
117 createType(type) {
118 for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
119 params[_key - 1] = arguments[_key];
120 }
121
122 return this.#registry.createType(type, ...params);
123 }
124 /**
125 * @description Register additional user-defined of chain-specific types in the type registry
126 */
127
128
129 registerTypes(types) {
130 types && this.#registry.register(types);
131 }
132 /**
133 * @returns `true` if the API operates with subscriptions
134 */
135
136
137 get hasSubscriptions() {
138 return this._rpcCore.provider.hasSubscriptions;
139 }
140 /**
141 * @returns `true` if the API decorate multi-key queries
142 */
143
144
145 get supportMulti() {
146 return this._rpcCore.provider.hasSubscriptions || !!this._rpcCore.state.queryStorageAt;
147 }
148
149 _emptyDecorated(registry, blockHash) {
150 return {
151 consts: {},
152 errors: {},
153 events: {},
154 query: {},
155 registry,
156 rx: {
157 query: {}
158 },
159 tx: (0, _submittable.createSubmittable)(this._type, this._rx, this._decorateMethod, registry, blockHash)
160 };
161 }
162
163 _createDecorated(registry, fromEmpty, decoratedApi, blockHash) {
164 if (!decoratedApi) {
165 decoratedApi = this._emptyDecorated(registry.registry, blockHash);
166 }
167
168 if (fromEmpty || !registry.decoratedMeta) {
169 registry.decoratedMeta = (0, _types.expandMetadata)(registry.registry, registry.metadata);
170 }
171
172 const storage = this._decorateStorage(registry.decoratedMeta, this._decorateMethod, blockHash);
173
174 const storageRx = this._decorateStorage(registry.decoratedMeta, this._rxDecorateMethod, blockHash);
175
176 (0, _augmentObject.augmentObject)('consts', registry.decoratedMeta.consts, decoratedApi.consts, fromEmpty);
177 (0, _augmentObject.augmentObject)('errors', registry.decoratedMeta.errors, decoratedApi.errors, fromEmpty);
178 (0, _augmentObject.augmentObject)('events', registry.decoratedMeta.events, decoratedApi.events, fromEmpty);
179 (0, _augmentObject.augmentObject)('query', storage, decoratedApi.query, fromEmpty);
180 (0, _augmentObject.augmentObject)('query', storageRx, decoratedApi.rx.query, fromEmpty);
181
182 decoratedApi.findCall = callIndex => (0, _find.findCall)(registry.registry, callIndex);
183
184 decoratedApi.findError = errorIndex => (0, _find.findError)(registry.registry, errorIndex);
185
186 decoratedApi.queryMulti = blockHash ? this._decorateMultiAt(decoratedApi, this._decorateMethod, blockHash) : this._decorateMulti(this._decorateMethod);
187 return {
188 decoratedApi,
189 decoratedMeta: registry.decoratedMeta
190 };
191 }
192
193 _injectMetadata(registry) {
194 let fromEmpty = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
195
196 // clear the decoration, we are redoing it here
197 if (fromEmpty || !registry.decoratedApi) {
198 registry.decoratedApi = this._emptyDecorated(registry.registry);
199 }
200
201 const {
202 decoratedApi,
203 decoratedMeta
204 } = this._createDecorated(registry, fromEmpty, registry.decoratedApi);
205
206 this._consts = decoratedApi.consts;
207 this._errors = decoratedApi.errors;
208 this._events = decoratedApi.events;
209 this._query = decoratedApi.query;
210 this._rx.query = decoratedApi.rx.query;
211
212 const tx = this._decorateExtrinsics(decoratedMeta, this._decorateMethod);
213
214 const rxtx = this._decorateExtrinsics(decoratedMeta, this._rxDecorateMethod);
215
216 if (fromEmpty || !this._extrinsics) {
217 this._extrinsics = tx;
218 this._rx.tx = rxtx;
219 } else {
220 (0, _augmentObject.augmentObject)('tx', tx, this._extrinsics, false);
221 (0, _augmentObject.augmentObject)(null, rxtx, this._rx.tx, false);
222 }
223
224 (0, _augmentObject.augmentObject)(null, decoratedMeta.consts, this._rx.consts, fromEmpty);
225 this.emit('decorated');
226 }
227 /**
228 * @deprecated
229 * backwards compatible endpoint for metadata injection, may be removed in the future (However, it is still useful for testing injection)
230 */
231
232
233 injectMetadata(metadata, fromEmpty, registry) {
234 this._injectMetadata({
235 metadata,
236 registry: registry || this.#registry,
237 specName: this.#registry.createType('Text'),
238 specVersion: _util.BN_ZERO
239 }, fromEmpty);
240 }
241
242 _decorateFunctionMeta(input, output) {
243 output.meta = input.meta;
244 output.method = input.method;
245 output.section = input.section;
246 output.toJSON = input.toJSON;
247
248 if (input.callIndex) {
249 output.callIndex = input.callIndex;
250 }
251
252 return output;
253 } // Filter all RPC methods based on the results of the rpc_methods call. We do this in the following
254 // manner to cater for both old and new:
255 // - when the number of entries are 0, only remove the ones with isOptional (account & contracts)
256 // - when non-zero, remove anything that is not in the array (we don't do this)
257
258
259 _filterRpc(methods, additional) {
260 // add any specific user-base RPCs
261 if (Object.keys(additional).length !== 0) {
262 this._rpcCore.addUserInterfaces(additional); // re-decorate, only adding any new additional interfaces
263
264
265 this._decorateRpc(this._rpcCore, this._decorateMethod, this._rpc);
266
267 this._decorateRpc(this._rpcCore, this._rxDecorateMethod, this._rx.rpc);
268 }
269
270 this._filterRpcMethods(methods);
271 }
272
273 _filterRpcMethods(exposed) {
274 const hasResults = exposed.length !== 0;
275 const allKnown = [...this._rpcCore.mapping.entries()];
276 const allKeys = [];
277
278 for (let i = 0; i < allKnown.length; i++) {
279 const [, {
280 alias,
281 endpoint,
282 method,
283 pubsub,
284 section
285 }] = allKnown[i];
286 allKeys.push(`${section}_${method}`);
287
288 if (pubsub) {
289 allKeys.push(`${section}_${pubsub[1]}`);
290 allKeys.push(`${section}_${pubsub[2]}`);
291 }
292
293 if (alias) {
294 allKeys.push(...alias);
295 }
296
297 if (endpoint) {
298 allKeys.push(endpoint);
299 }
300 }
301
302 const filterKey = k => !allKeys.includes(k);
303
304 const unknown = exposed.filter(filterKey);
305
306 if (unknown.length) {
307 l.warn(`RPC methods not decorated: ${unknown.join(', ')}`);
308 } // loop through all entries we have (populated in decorate) and filter as required
309 // only remove when we have results and method missing, or with no results if optional
310
311
312 for (let i = 0; i < allKnown.length; i++) {
313 const [k, {
314 method,
315 section
316 }] = allKnown[i];
317
318 if (hasResults && !exposed.includes(k) && k !== 'rpc_methods') {
319 if (this._rpc[section]) {
320 delete this._rpc[section][method];
321 delete this._rx.rpc[section][method];
322 }
323 }
324 }
325 }
326
327 _decorateRpc(rpc, decorateMethod) {
328 let input = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
329 const out = input;
330
331 const decorateFn = (section, method) => {
332 const source = rpc[section][method];
333 const fn = decorateMethod(source, {
334 methodName: method
335 });
336 fn.meta = source.meta;
337 fn.raw = decorateMethod(source.raw, {
338 methodName: method
339 });
340 return fn;
341 };
342
343 for (let s = 0; s < rpc.sections.length; s++) {
344 const section = rpc.sections[s];
345
346 if (!Object.prototype.hasOwnProperty.call(out, section)) {
347 const methods = Object.keys(rpc[section]);
348
349 const decorateInternal = method => decorateFn(section, method);
350
351 for (let m = 0; m < methods.length; m++) {
352 const method = methods[m]; // skip subscriptions where we have a non-subscribe interface
353
354 if (this.hasSubscriptions || !(method.startsWith('subscribe') || method.startsWith('unsubscribe'))) {
355 if (!Object.prototype.hasOwnProperty.call(out, section)) {
356 out[section] = {};
357 }
358
359 (0, _util.lazyMethod)(out[section], method, decorateInternal);
360 }
361 }
362 }
363 }
364
365 return out;
366 } // only be called if supportMulti is true
367
368
369 _decorateMulti(decorateMethod) {
370 // eslint-disable-next-line @typescript-eslint/no-unsafe-return
371 return decorateMethod(calls => (this.hasSubscriptions ? this._rpcCore.state.subscribeStorage : this._rpcCore.state.queryStorageAt)(calls.map(args => Array.isArray(args) ? args[0].creator.meta.type.isPlain ? [args[0].creator] : args[0].creator.meta.type.asMap.hashers.length === 1 ? [args[0].creator, args.slice(1)] : [args[0].creator, ...args.slice(1)] : [args.creator])));
372 }
373
374 _decorateMultiAt(atApi, decorateMethod, blockHash) {
375 // eslint-disable-next-line @typescript-eslint/no-unsafe-return
376 return decorateMethod(calls => this._rpcCore.state.queryStorageAt(calls.map(args => {
377 if (Array.isArray(args)) {
378 const {
379 creator
380 } = getAtQueryFn(atApi, args[0].creator);
381 return creator.meta.type.isPlain ? [creator] : creator.meta.type.asMap.hashers.length === 1 ? [creator, args.slice(1)] : [creator, ...args.slice(1)];
382 }
383
384 return [getAtQueryFn(atApi, args.creator).creator];
385 }), blockHash));
386 }
387
388 _decorateExtrinsics(_ref2, decorateMethod) {
389 let {
390 tx
391 } = _ref2;
392 const result = (0, _submittable.createSubmittable)(this._type, this._rx, decorateMethod);
393
394 const lazySection = section => (0, _util.lazyMethods)({}, Object.keys(tx[section]), method => this._decorateExtrinsicEntry(tx[section][method], result));
395
396 const sections = Object.keys(tx);
397
398 for (let i = 0; i < sections.length; i++) {
399 (0, _util.lazyMethod)(result, sections[i], lazySection);
400 }
401
402 return result;
403 }
404
405 _decorateExtrinsicEntry(method, creator) {
406 const decorated = function () {
407 return creator(method(...arguments));
408 }; // pass through the `.is`
409
410
411 decorated.is = other => method.is(other); // eslint-disable-next-line @typescript-eslint/no-unsafe-return
412
413
414 return this._decorateFunctionMeta(method, decorated);
415 }
416
417 _decorateStorage(_ref3, decorateMethod, blockHash) {
418 let {
419 query,
420 registry
421 } = _ref3;
422 const result = {};
423
424 const lazySection = section => (0, _util.lazyMethods)({}, Object.keys(query[section]), method => blockHash ? this._decorateStorageEntryAt(registry, query[section][method], decorateMethod, blockHash) : this._decorateStorageEntry(query[section][method], decorateMethod));
425
426 const sections = Object.keys(query);
427
428 for (let i = 0; i < sections.length; i++) {
429 (0, _util.lazyMethod)(result, sections[i], lazySection);
430 }
431
432 return result;
433 }
434
435 _decorateStorageEntry(creator, decorateMethod) {
436 var _this = this;
437
438 const getArgs = (args, registry) => (0, _validate.extractStorageArgs)(registry || this.#registry, creator, args);
439
440 const getQueryAt = blockHash => (0, _rxjs.from)(this.at(blockHash)).pipe((0, _rxjs.map)(api => getAtQueryFn(api, creator))); // Disable this where it occurs for each field we are decorating
441
442 /* eslint-disable @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment */
443
444
445 const decorated = this._decorateStorageCall(creator, decorateMethod);
446
447 decorated.creator = creator;
448 decorated.at = decorateMethod(function (blockHash) {
449 for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
450 args[_key2 - 1] = arguments[_key2];
451 }
452
453 return getQueryAt(blockHash).pipe((0, _rxjs.switchMap)(q => q(...args)));
454 });
455 decorated.hash = decorateMethod(function () {
456 for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
457 args[_key3] = arguments[_key3];
458 }
459
460 return _this._rpcCore.state.getStorageHash(getArgs(args));
461 });
462
463 decorated.is = key => key.section === creator.section && key.method === creator.method;
464
465 decorated.key = function () {
466 return (0, _util.u8aToHex)((0, _util.compactStripLength)(creator(...arguments))[1]);
467 };
468
469 decorated.keyPrefix = function () {
470 return (0, _util.u8aToHex)(creator.keyPrefix(...arguments));
471 };
472
473 decorated.range = decorateMethod(function (range) {
474 for (var _len4 = arguments.length, args = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
475 args[_key4 - 1] = arguments[_key4];
476 }
477
478 return _this._decorateStorageRange(decorated, args, range);
479 });
480 decorated.size = decorateMethod(function () {
481 for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
482 args[_key5] = arguments[_key5];
483 }
484
485 return _this._rpcCore.state.getStorageSize(getArgs(args));
486 });
487 decorated.sizeAt = decorateMethod(function (blockHash) {
488 for (var _len6 = arguments.length, args = new Array(_len6 > 1 ? _len6 - 1 : 0), _key6 = 1; _key6 < _len6; _key6++) {
489 args[_key6 - 1] = arguments[_key6];
490 }
491
492 return getQueryAt(blockHash).pipe((0, _rxjs.switchMap)(q => _this._rpcCore.state.getStorageSize(getArgs(args, q.creator.meta.registry), blockHash)));
493 }); // .keys() & .entries() only available on map types
494
495 if (creator.iterKey && creator.meta.type.isMap) {
496 decorated.entries = decorateMethod((0, _rpcCore.memo)(this.#instanceId, function () {
497 for (var _len7 = arguments.length, args = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
498 args[_key7] = arguments[_key7];
499 }
500
501 return _this._retrieveMapEntries(creator, null, args);
502 }));
503 decorated.entriesAt = decorateMethod((0, _rpcCore.memo)(this.#instanceId, function (blockHash) {
504 for (var _len8 = arguments.length, args = new Array(_len8 > 1 ? _len8 - 1 : 0), _key8 = 1; _key8 < _len8; _key8++) {
505 args[_key8 - 1] = arguments[_key8];
506 }
507
508 return getQueryAt(blockHash).pipe((0, _rxjs.switchMap)(q => _this._retrieveMapEntries(q.creator, blockHash, args)));
509 }));
510 decorated.entriesPaged = decorateMethod((0, _rpcCore.memo)(this.#instanceId, opts => this._retrieveMapEntriesPaged(creator, undefined, opts)));
511 decorated.keys = decorateMethod((0, _rpcCore.memo)(this.#instanceId, function () {
512 for (var _len9 = arguments.length, args = new Array(_len9), _key9 = 0; _key9 < _len9; _key9++) {
513 args[_key9] = arguments[_key9];
514 }
515
516 return _this._retrieveMapKeys(creator, null, args);
517 }));
518 decorated.keysAt = decorateMethod((0, _rpcCore.memo)(this.#instanceId, function (blockHash) {
519 for (var _len10 = arguments.length, args = new Array(_len10 > 1 ? _len10 - 1 : 0), _key10 = 1; _key10 < _len10; _key10++) {
520 args[_key10 - 1] = arguments[_key10];
521 }
522
523 return getQueryAt(blockHash).pipe((0, _rxjs.switchMap)(q => _this._retrieveMapKeys(q.creator, blockHash, args)));
524 }));
525 decorated.keysPaged = decorateMethod((0, _rpcCore.memo)(this.#instanceId, opts => this._retrieveMapKeysPaged(creator, undefined, opts)));
526 }
527
528 if (this.supportMulti && creator.meta.type.isMap) {
529 // When using double map storage function, user need to pass double map key as an array
530 decorated.multi = decorateMethod(args => creator.meta.type.asMap.hashers.length === 1 ? this._retrieveMulti(args.map(a => [creator, [a]])) : this._retrieveMulti(args.map(a => [creator, a])));
531 }
532 /* eslint-enable @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment */
533
534
535 return this._decorateFunctionMeta(creator, decorated);
536 }
537
538 _decorateStorageEntryAt(registry, creator, decorateMethod, blockHash) {
539 var _this2 = this;
540
541 const getArgs = args => (0, _validate.extractStorageArgs)(registry, creator, args); // Disable this where it occurs for each field we are decorating
542
543 /* eslint-disable @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment */
544
545
546 const decorated = decorateMethod(function () {
547 for (var _len11 = arguments.length, args = new Array(_len11), _key11 = 0; _key11 < _len11; _key11++) {
548 args[_key11] = arguments[_key11];
549 }
550
551 return _this2._rpcCore.state.getStorage(getArgs(args), blockHash);
552 });
553 decorated.creator = creator;
554 decorated.hash = decorateMethod(function () {
555 for (var _len12 = arguments.length, args = new Array(_len12), _key12 = 0; _key12 < _len12; _key12++) {
556 args[_key12] = arguments[_key12];
557 }
558
559 return _this2._rpcCore.state.getStorageHash(getArgs(args), blockHash);
560 });
561
562 decorated.is = key => key.section === creator.section && key.method === creator.method;
563
564 decorated.key = function () {
565 for (var _len13 = arguments.length, args = new Array(_len13), _key13 = 0; _key13 < _len13; _key13++) {
566 args[_key13] = arguments[_key13];
567 }
568
569 return (0, _util.u8aToHex)((0, _util.compactStripLength)(creator(creator.meta.type.isPlain ? undefined : args))[1]);
570 };
571
572 decorated.keyPrefix = function () {
573 return (0, _util.u8aToHex)(creator.keyPrefix(...arguments));
574 };
575
576 decorated.size = decorateMethod(function () {
577 for (var _len14 = arguments.length, args = new Array(_len14), _key14 = 0; _key14 < _len14; _key14++) {
578 args[_key14] = arguments[_key14];
579 }
580
581 return _this2._rpcCore.state.getStorageSize(getArgs(args), blockHash);
582 }); // .keys() & .entries() only available on map types
583
584 if (creator.iterKey && creator.meta.type.isMap) {
585 decorated.entries = decorateMethod((0, _rpcCore.memo)(this.#instanceId, function () {
586 for (var _len15 = arguments.length, args = new Array(_len15), _key15 = 0; _key15 < _len15; _key15++) {
587 args[_key15] = arguments[_key15];
588 }
589
590 return _this2._retrieveMapEntries(creator, blockHash, args);
591 }));
592 decorated.entriesPaged = decorateMethod((0, _rpcCore.memo)(this.#instanceId, opts => this._retrieveMapEntriesPaged(creator, blockHash, opts)));
593 decorated.keys = decorateMethod((0, _rpcCore.memo)(this.#instanceId, function () {
594 for (var _len16 = arguments.length, args = new Array(_len16), _key16 = 0; _key16 < _len16; _key16++) {
595 args[_key16] = arguments[_key16];
596 }
597
598 return _this2._retrieveMapKeys(creator, blockHash, args);
599 }));
600 decorated.keysPaged = decorateMethod((0, _rpcCore.memo)(this.#instanceId, opts => this._retrieveMapKeysPaged(creator, blockHash, opts)));
601 }
602
603 if (this.supportMulti && creator.meta.type.isMap) {
604 // When using double map storage function, user need to pass double map key as an array
605 decorated.multi = decorateMethod(args => creator.meta.type.asMap.hashers.length === 1 ? this._retrieveMulti(args.map(a => [creator, [a]]), blockHash) : this._retrieveMulti(args.map(a => [creator, a]), blockHash));
606 }
607 /* eslint-enable @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment */
608
609
610 return this._decorateFunctionMeta(creator, decorated);
611 } // Decorate the base storage call. In the case or rxjs or promise-without-callback (await)
612 // we make a subscription, alternatively we push this through a single-shot query
613
614
615 _decorateStorageCall(creator, decorateMethod) {
616 var _this3 = this;
617
618 return decorateMethod(function () {
619 for (var _len17 = arguments.length, args = new Array(_len17), _key17 = 0; _key17 < _len17; _key17++) {
620 args[_key17] = arguments[_key17];
621 }
622
623 return _this3.hasSubscriptions ? _this3._rpcCore.state.subscribeStorage([(0, _validate.extractStorageArgs)(_this3.#registry, creator, args)]).pipe((0, _rxjs.map)(_ref4 => {
624 let [data] = _ref4;
625 return data;
626 }) // extract first/only result from list
627 ) : _this3._rpcCore.state.getStorage((0, _validate.extractStorageArgs)(_this3.#registry, creator, args));
628 }, {
629 methodName: creator.method,
630 overrideNoSub: function () {
631 for (var _len18 = arguments.length, args = new Array(_len18), _key18 = 0; _key18 < _len18; _key18++) {
632 args[_key18] = arguments[_key18];
633 }
634
635 return _this3._rpcCore.state.getStorage((0, _validate.extractStorageArgs)(_this3.#registry, creator, args));
636 }
637 });
638 }
639
640 _decorateStorageRange(decorated, args, range) {
641 const outputType = (0, _types.unwrapStorageType)(this.#registry, decorated.creator.meta.type, decorated.creator.meta.modifier.isOptional);
642 return this._rpcCore.state.queryStorage([decorated.key(...args)], ...range).pipe((0, _rxjs.map)(result => result.map(_ref5 => {
643 let [blockHash, [value]] = _ref5;
644 return [blockHash, this.createType(outputType, value.isSome ? value.unwrap().toHex() : undefined)];
645 })));
646 } // retrieve a set of values for a specific set of keys - here we chunk the keys into PAGE_SIZE sizes
647
648
649 _retrieveMulti(keys, blockHash) {
650 if (!keys.length) {
651 return (0, _rxjs.of)([]);
652 }
653
654 const queryCall = this.hasSubscriptions && !blockHash ? this._rpcCore.state.subscribeStorage : this._rpcCore.state.queryStorageAt;
655 return (0, _rxjs.combineLatest)((0, _util.arrayChunk)(keys, PAGE_SIZE_V).map(k => blockHash ? queryCall(k, blockHash) : queryCall(k))).pipe((0, _rxjs.map)(_util.arrayFlatten));
656 }
657
658 _retrieveMapKeys(_ref6, at, args) {
659 let {
660 iterKey,
661 meta,
662 method,
663 section
664 } = _ref6;
665 (0, _util.assert)(iterKey && meta.type.isMap, 'keys can only be retrieved on maps');
666 const headKey = iterKey(...args).toHex();
667 const startSubject = new _rxjs.BehaviorSubject(headKey);
668 const getKeysPaged = at ? startKey => this._rpcCore.state.getKeysPaged(headKey, PAGE_SIZE_K, startKey, at) : startKey => this._rpcCore.state.getKeysPaged(headKey, PAGE_SIZE_K, startKey);
669
670 const setMeta = key => key.setMeta(meta, section, method);
671
672 return startSubject.pipe((0, _rxjs.switchMap)(getKeysPaged), (0, _rxjs.map)(keys => keys.map(setMeta)), (0, _rxjs.tap)(keys => {
673 setTimeout(() => {
674 keys.length === PAGE_SIZE_K ? startSubject.next(keys[PAGE_SIZE_K - 1].toHex()) : startSubject.complete();
675 }, 0);
676 }), (0, _rxjs.toArray)(), // toArray since we want to startSubject to be completed
677 (0, _rxjs.map)(_util.arrayFlatten));
678 }
679
680 _retrieveMapKeysPaged(_ref7, at, opts) {
681 let {
682 iterKey,
683 meta,
684 method,
685 section
686 } = _ref7;
687 (0, _util.assert)(iterKey && meta.type.isMap, 'keys can only be retrieved on maps');
688
689 const setMeta = key => key.setMeta(meta, section, method);
690
691 const getKeysPaged = at ? headKey => this._rpcCore.state.getKeysPaged(headKey, opts.pageSize, opts.startKey || headKey, at) : headKey => this._rpcCore.state.getKeysPaged(headKey, opts.pageSize, opts.startKey || headKey);
692 return getKeysPaged(iterKey(...opts.args).toHex()).pipe((0, _rxjs.map)(keys => keys.map(setMeta)));
693 }
694
695 _retrieveMapEntries(entry, at, args) {
696 const queryStorageAt = at ? keys => this._rpcCore.state.queryStorageAt(keys, at) : keys => this._rpcCore.state.queryStorageAt(keys);
697 return this._retrieveMapKeys(entry, at, args).pipe((0, _rxjs.switchMap)(keys => keys.length ? (0, _rxjs.combineLatest)((0, _util.arrayChunk)(keys, PAGE_SIZE_V).map(queryStorageAt)).pipe((0, _rxjs.map)(valsArr => (0, _util.arrayFlatten)(valsArr).map((value, index) => [keys[index], value]))) : (0, _rxjs.of)([])));
698 }
699
700 _retrieveMapEntriesPaged(entry, at, opts) {
701 const queryStorageAt = at ? keys => this._rpcCore.state.queryStorageAt(keys, at) : keys => this._rpcCore.state.queryStorageAt(keys);
702 return this._retrieveMapKeysPaged(entry, at, opts).pipe((0, _rxjs.switchMap)(keys => keys.length ? queryStorageAt(keys).pipe((0, _rxjs.map)(valsArr => valsArr.map((value, index) => [keys[index], value]))) : (0, _rxjs.of)([])));
703 }
704
705 _decorateDeriveRx(decorateMethod) {
706 var _this$_runtimeVersion, _this$_options$typesB, _this$_options$typesB2, _this$_options$typesB3;
707
708 const specName = (_this$_runtimeVersion = this._runtimeVersion) === null || _this$_runtimeVersion === void 0 ? void 0 : _this$_runtimeVersion.specName.toString(); // Pull in derive from api-derive
709
710 const available = (0, _apiDerive.getAvailableDerives)(this.#instanceId, this._rx, (0, _util.objectSpread)({}, this._options.derives, (_this$_options$typesB = this._options.typesBundle) === null || _this$_options$typesB === void 0 ? void 0 : (_this$_options$typesB2 = _this$_options$typesB.spec) === null || _this$_options$typesB2 === void 0 ? void 0 : (_this$_options$typesB3 = _this$_options$typesB2[specName || '']) === null || _this$_options$typesB3 === void 0 ? void 0 : _this$_options$typesB3.derives));
711 return (0, _decorate.decorateDeriveSections)(decorateMethod, available);
712 }
713
714 _decorateDerive(decorateMethod) {
715 return (0, _decorate.decorateDeriveSections)(decorateMethod, this._rx.derive);
716 }
717 /**
718 * Put the `this.onCall` function of ApiRx here, because it is needed by
719 * `api._rx`.
720 */
721
722
723 _rxDecorateMethod = method => {
724 return method;
725 };
726}
727
728exports.Decorate = Decorate;
\No newline at end of file