@pezkuwi/api 16.5.5 → 16.5.6
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/LICENSE +201 -0
- package/README.md +12 -12
- package/augment.js +1 -0
- package/base/Decorate.js +752 -0
- package/base/Events.js +78 -0
- package/base/Getters.js +207 -0
- package/base/Init.js +400 -0
- package/base/find.js +7 -0
- package/{build/base → base}/index.d.ts +1 -1
- package/base/index.js +57 -0
- package/bundle-pezkuwi-api.js +26238 -0
- package/{src/bundle.ts → bundle.js} +0 -9
- package/cjs/augment.d.ts +1 -0
- package/cjs/augment.js +3 -0
- package/cjs/base/Decorate.d.ts +143 -0
- package/cjs/base/Decorate.js +756 -0
- package/cjs/base/Events.d.ts +67 -0
- package/cjs/base/Events.js +82 -0
- package/cjs/base/Getters.d.ts +163 -0
- package/cjs/base/Getters.js +211 -0
- package/cjs/base/Init.d.ts +44 -0
- package/cjs/base/Init.js +404 -0
- package/cjs/base/find.d.ts +3 -0
- package/cjs/base/find.js +11 -0
- package/cjs/base/index.d.ts +47 -0
- package/cjs/base/index.js +61 -0
- package/{src/base/types.ts → cjs/base/types.d.ts} +8 -12
- package/cjs/base/types.js +2 -0
- package/cjs/bundle.d.ts +7 -0
- package/cjs/bundle.js +17 -0
- package/cjs/index.js +5 -0
- package/cjs/package.json +3 -0
- package/cjs/packageDetect.d.ts +1 -0
- package/cjs/packageDetect.js +10 -0
- package/cjs/packageInfo.js +4 -0
- package/{build → cjs}/promise/Api.d.ts +2 -2
- package/cjs/promise/Api.js +195 -0
- package/cjs/promise/Combinator.js +72 -0
- package/cjs/promise/decorateMethod.js +75 -0
- package/cjs/promise/index.js +7 -0
- package/cjs/promise/types.js +2 -0
- package/{build → cjs}/rx/Api.d.ts +2 -2
- package/cjs/rx/Api.js +173 -0
- package/cjs/rx/decorateMethod.js +6 -0
- package/cjs/rx/index.js +7 -0
- package/cjs/submittable/Result.js +84 -0
- package/cjs/submittable/createClass.js +277 -0
- package/cjs/submittable/createSubmittable.js +8 -0
- package/cjs/submittable/index.js +7 -0
- package/cjs/submittable/types.js +2 -0
- package/cjs/types/calls.js +4 -0
- package/cjs/types/consts.js +4 -0
- package/cjs/types/errors.js +4 -0
- package/cjs/types/events.js +4 -0
- package/cjs/types/index.js +13 -0
- package/cjs/types/storage.js +4 -0
- package/cjs/types/submittable.js +4 -0
- package/cjs/util/augmentObject.js +84 -0
- package/cjs/util/decorate.js +17 -0
- package/cjs/util/filterEvents.js +25 -0
- package/cjs/util/index.js +11 -0
- package/cjs/util/isKeyringPair.js +7 -0
- package/cjs/util/logging.js +5 -0
- package/cjs/util/validate.js +26 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +588 -22
- package/packageDetect.d.ts +1 -0
- package/{src/packageDetect.ts → packageDetect.js} +0 -8
- package/packageInfo.d.ts +6 -0
- package/packageInfo.js +1 -0
- package/promise/Api.d.ts +158 -0
- package/promise/Api.js +191 -0
- package/promise/Combinator.d.ts +12 -0
- package/promise/Combinator.js +68 -0
- package/promise/decorateMethod.d.ts +14 -0
- package/promise/decorateMethod.js +71 -0
- package/promise/index.d.ts +2 -0
- package/promise/index.js +2 -0
- package/{src/promise/types.ts → promise/types.d.ts} +0 -4
- package/promise/types.js +1 -0
- package/rx/Api.d.ts +155 -0
- package/{src/rx/Api.ts → rx/Api.js} +67 -84
- package/rx/decorateMethod.d.ts +3 -0
- package/rx/decorateMethod.js +3 -0
- package/rx/index.d.ts +2 -0
- package/rx/index.js +2 -0
- package/submittable/Result.d.ts +31 -0
- package/submittable/Result.js +80 -0
- package/submittable/createClass.d.ts +12 -0
- package/submittable/createClass.js +274 -0
- package/submittable/createSubmittable.d.ts +8 -0
- package/submittable/createSubmittable.js +5 -0
- package/{src/submittable/index.ts → submittable/index.d.ts} +0 -3
- package/submittable/index.js +2 -0
- package/{src/submittable/types.ts → submittable/types.d.ts} +0 -3
- package/submittable/types.js +1 -0
- package/types/calls.d.ts +1 -0
- package/types/calls.js +1 -0
- package/types/consts.d.ts +1 -0
- package/types/consts.js +1 -0
- package/types/errors.d.ts +1 -0
- package/types/errors.js +1 -0
- package/types/events.d.ts +1 -0
- package/types/events.js +1 -0
- package/types/index.d.ts +114 -0
- package/types/index.js +8 -0
- package/types/storage.d.ts +1 -0
- package/types/storage.js +1 -0
- package/types/submittable.d.ts +1 -0
- package/types/submittable.js +1 -0
- package/util/augmentObject.d.ts +9 -0
- package/util/augmentObject.js +81 -0
- package/util/decorate.d.ts +16 -0
- package/util/decorate.js +14 -0
- package/util/filterEvents.d.ts +6 -0
- package/util/filterEvents.js +22 -0
- package/{src/util/index.ts → util/index.d.ts} +0 -6
- package/util/index.js +4 -0
- package/util/isKeyringPair.d.ts +3 -0
- package/util/isKeyringPair.js +4 -0
- package/util/logging.d.ts +1 -0
- package/util/logging.js +2 -0
- package/util/validate.d.ts +3 -0
- package/util/validate.js +23 -0
- package/src/augment.ts +0 -4
- package/src/base/Decorate.ts +0 -1103
- package/src/base/Events.ts +0 -91
- package/src/base/Getters.ts +0 -245
- package/src/base/Init.ts +0 -525
- package/src/base/find.ts +0 -14
- package/src/base/index.ts +0 -85
- package/src/checkTypes.manual.ts +0 -323
- package/src/index.ts +0 -6
- package/src/mod.ts +0 -4
- package/src/packageInfo.ts +0 -6
- package/src/promise/Api.ts +0 -214
- package/src/promise/Combinator.ts +0 -91
- package/src/promise/Combinators.spec.ts +0 -109
- package/src/promise/decorateMethod.ts +0 -118
- package/src/promise/index.spec.ts +0 -167
- package/src/promise/index.ts +0 -5
- package/src/rx/decorateMethod.ts +0 -9
- package/src/rx/index.ts +0 -5
- package/src/submittable/Result.ts +0 -111
- package/src/submittable/createClass.ts +0 -438
- package/src/submittable/createSubmittable.ts +0 -19
- package/src/test/SingleAccountSigner.ts +0 -53
- package/src/test/index.ts +0 -5
- package/src/test/logEvents.ts +0 -24
- package/src/types/calls.ts +0 -4
- package/src/types/consts.ts +0 -4
- package/src/types/errors.ts +0 -4
- package/src/types/events.ts +0 -4
- package/src/types/index.ts +0 -137
- package/src/types/storage.ts +0 -4
- package/src/types/submittable.ts +0 -4
- package/src/util/augmentObject.spec.ts +0 -54
- package/src/util/augmentObject.ts +0 -112
- package/src/util/decorate.ts +0 -43
- package/src/util/filterEvents.ts +0 -34
- package/src/util/isKeyringPair.ts +0 -11
- package/src/util/logging.ts +0 -6
- package/src/util/validate.spec.ts +0 -72
- package/src/util/validate.ts +0 -36
- package/tsconfig.build.json +0 -25
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.spec.json +0 -26
- /package/{build/augment.d.ts → augment.d.ts} +0 -0
- /package/{build/base → base}/Decorate.d.ts +0 -0
- /package/{build/base → base}/Events.d.ts +0 -0
- /package/{build/base → base}/Getters.d.ts +0 -0
- /package/{build/base → base}/Init.d.ts +0 -0
- /package/{build/base → base}/find.d.ts +0 -0
- /package/{build/base → base}/types.d.ts +0 -0
- /package/{build/packageDetect.d.ts → base/types.js} +0 -0
- /package/{build/bundle.d.ts → bundle.d.ts} +0 -0
- /package/{build → cjs}/index.d.ts +0 -0
- /package/{build → cjs}/packageInfo.d.ts +0 -0
- /package/{build → cjs}/promise/Combinator.d.ts +0 -0
- /package/{build → cjs}/promise/decorateMethod.d.ts +0 -0
- /package/{build → cjs}/promise/index.d.ts +0 -0
- /package/{build → cjs}/promise/types.d.ts +0 -0
- /package/{build → cjs}/rx/decorateMethod.d.ts +0 -0
- /package/{build → cjs}/rx/index.d.ts +0 -0
- /package/{build → cjs}/submittable/Result.d.ts +0 -0
- /package/{build → cjs}/submittable/createClass.d.ts +0 -0
- /package/{build → cjs}/submittable/createSubmittable.d.ts +0 -0
- /package/{build → cjs}/submittable/index.d.ts +0 -0
- /package/{build → cjs}/submittable/types.d.ts +0 -0
- /package/{build → cjs}/types/calls.d.ts +0 -0
- /package/{build → cjs}/types/consts.d.ts +0 -0
- /package/{build → cjs}/types/errors.d.ts +0 -0
- /package/{build → cjs}/types/events.d.ts +0 -0
- /package/{build → cjs}/types/index.d.ts +0 -0
- /package/{build → cjs}/types/storage.d.ts +0 -0
- /package/{build → cjs}/types/submittable.d.ts +0 -0
- /package/{build → cjs}/util/augmentObject.d.ts +0 -0
- /package/{build → cjs}/util/decorate.d.ts +0 -0
- /package/{build → cjs}/util/filterEvents.d.ts +0 -0
- /package/{build → cjs}/util/index.d.ts +0 -0
- /package/{build → cjs}/util/isKeyringPair.d.ts +0 -0
- /package/{build → cjs}/util/logging.d.ts +0 -0
- /package/{build → cjs}/util/validate.d.ts +0 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiPromise = void 0;
|
|
4
|
+
const util_1 = require("@pezkuwi/util");
|
|
5
|
+
const index_js_1 = require("../base/index.js");
|
|
6
|
+
const Combinator_js_1 = require("./Combinator.js");
|
|
7
|
+
const decorateMethod_js_1 = require("./decorateMethod.js");
|
|
8
|
+
/**
|
|
9
|
+
* # @pezkuwi/api/promise
|
|
10
|
+
*
|
|
11
|
+
* ## Overview
|
|
12
|
+
*
|
|
13
|
+
* @name ApiPromise
|
|
14
|
+
* @description
|
|
15
|
+
* ApiPromise is a standard JavaScript wrapper around the RPC and interfaces on the Pezkuwi network. As a full Promise-based, all interface calls return Promises, including the static `.create(...)`. Subscription calls utilise `(value) => {}` callbacks to pass through the latest values.
|
|
16
|
+
*
|
|
17
|
+
* The API is well suited to real-time applications where either the single-shot state is needed or use is to be made of the subscription-based features of Pezkuwi (and Bizinikiwi) clients.
|
|
18
|
+
*
|
|
19
|
+
* @see [[ApiRx]]
|
|
20
|
+
*
|
|
21
|
+
* ## Usage
|
|
22
|
+
*
|
|
23
|
+
* Making rpc calls -
|
|
24
|
+
* <BR>
|
|
25
|
+
*
|
|
26
|
+
* ```javascript
|
|
27
|
+
* import ApiPromise from '@pezkuwi/api/promise';
|
|
28
|
+
*
|
|
29
|
+
* // initialise via static create
|
|
30
|
+
* const api = await ApiPromise.create();
|
|
31
|
+
*
|
|
32
|
+
* // make a subscription to the network head
|
|
33
|
+
* api.rpc.chain.subscribeNewHeads((header) => {
|
|
34
|
+
* console.log(`Chain is at #${header.number}`);
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
* <BR>
|
|
38
|
+
*
|
|
39
|
+
* Subscribing to chain state -
|
|
40
|
+
* <BR>
|
|
41
|
+
*
|
|
42
|
+
* ```javascript
|
|
43
|
+
* import { ApiPromise, WsProvider } from '@pezkuwi/api';
|
|
44
|
+
*
|
|
45
|
+
* // initialise a provider with a specific endpoint
|
|
46
|
+
* const provider = new WsProvider('wss://example.com:9944')
|
|
47
|
+
*
|
|
48
|
+
* // initialise via isReady & new with specific provider
|
|
49
|
+
* const api = await new ApiPromise({ provider }).isReady;
|
|
50
|
+
*
|
|
51
|
+
* // retrieve the block target time
|
|
52
|
+
* const blockPeriod = await api.query.timestamp.blockPeriod().toNumber();
|
|
53
|
+
* let last = 0;
|
|
54
|
+
*
|
|
55
|
+
* // subscribe to the current block timestamp, updates automatically (callback provided)
|
|
56
|
+
* api.query.timestamp.now((timestamp) => {
|
|
57
|
+
* const elapsed = last
|
|
58
|
+
* ? `, ${timestamp.toNumber() - last}s since last`
|
|
59
|
+
* : '';
|
|
60
|
+
*
|
|
61
|
+
* last = timestamp.toNumber();
|
|
62
|
+
* console.log(`timestamp ${timestamp}${elapsed} (${blockPeriod}s target)`);
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
* <BR>
|
|
66
|
+
*
|
|
67
|
+
* Submitting a transaction -
|
|
68
|
+
* <BR>
|
|
69
|
+
*
|
|
70
|
+
* ```javascript
|
|
71
|
+
* import ApiPromise from '@pezkuwi/api/promise';
|
|
72
|
+
*
|
|
73
|
+
* ApiPromise.create().then((api) => {
|
|
74
|
+
* const [nonce] = await api.query.system.account(keyring.alice.address);
|
|
75
|
+
*
|
|
76
|
+
* api.tx.balances
|
|
77
|
+
* // create transfer
|
|
78
|
+
* transfer(keyring.bob.address, 12345)
|
|
79
|
+
* // sign the transcation
|
|
80
|
+
* .sign(keyring.alice, { nonce })
|
|
81
|
+
* // send the transaction (optional status callback)
|
|
82
|
+
* .send((status) => {
|
|
83
|
+
* console.log(`current status ${status.type}`);
|
|
84
|
+
* })
|
|
85
|
+
* // retrieve the submitted extrinsic hash
|
|
86
|
+
* .then((hash) => {
|
|
87
|
+
* console.log(`submitted with hash ${hash}`);
|
|
88
|
+
* });
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
class ApiPromise extends index_js_1.ApiBase {
|
|
93
|
+
#isReadyPromise;
|
|
94
|
+
#isReadyOrErrorPromise;
|
|
95
|
+
/**
|
|
96
|
+
* @description Creates an instance of the ApiPromise class
|
|
97
|
+
* @param options Options to create an instance. This can be either [[ApiOptions]] or
|
|
98
|
+
* an [[WsProvider]].
|
|
99
|
+
* @example
|
|
100
|
+
* <BR>
|
|
101
|
+
*
|
|
102
|
+
* ```javascript
|
|
103
|
+
* import Api from '@pezkuwi/api/promise';
|
|
104
|
+
*
|
|
105
|
+
* new Api().isReady.then((api) => {
|
|
106
|
+
* api.rpc.subscribeNewHeads((header) => {
|
|
107
|
+
* console.log(`new block #${header.number.toNumber()}`);
|
|
108
|
+
* });
|
|
109
|
+
* });
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
constructor(options) {
|
|
113
|
+
super(options, 'promise', decorateMethod_js_1.toPromiseMethod);
|
|
114
|
+
this.#isReadyPromise = new Promise((resolve) => {
|
|
115
|
+
super.once('ready', () => resolve(this));
|
|
116
|
+
});
|
|
117
|
+
this.#isReadyOrErrorPromise = new Promise((resolve, reject) => {
|
|
118
|
+
const tracker = (0, decorateMethod_js_1.promiseTracker)(resolve, reject);
|
|
119
|
+
super.once('ready', () => tracker.resolve(this));
|
|
120
|
+
super.once('error', (error) => tracker.reject(error));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* @description Creates an ApiPromise instance using the supplied provider. Returns an Promise containing the actual Api instance.
|
|
125
|
+
* @param options options that is passed to the class contructor. Can be either [[ApiOptions]] or a
|
|
126
|
+
* provider (see the constructor arguments)
|
|
127
|
+
* @example
|
|
128
|
+
* <BR>
|
|
129
|
+
*
|
|
130
|
+
* ```javascript
|
|
131
|
+
* import Api from '@pezkuwi/api/promise';
|
|
132
|
+
*
|
|
133
|
+
* Api.create().then(async (api) => {
|
|
134
|
+
* const timestamp = await api.query.timestamp.now();
|
|
135
|
+
*
|
|
136
|
+
* console.log(`lastest block timestamp ${timestamp}`);
|
|
137
|
+
* });
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
static create(options) {
|
|
141
|
+
const instance = new ApiPromise(options);
|
|
142
|
+
if (options && options.throwOnConnect) {
|
|
143
|
+
return instance.isReadyOrError;
|
|
144
|
+
}
|
|
145
|
+
// Swallow any rejections on isReadyOrError
|
|
146
|
+
// (in Node 15.x this creates issues, when not being looked at)
|
|
147
|
+
instance.isReadyOrError.catch(util_1.noop);
|
|
148
|
+
return instance.isReady;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @description Promise that resolves the first time we are connected and loaded
|
|
152
|
+
*/
|
|
153
|
+
get isReady() {
|
|
154
|
+
return this.#isReadyPromise;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* @description Promise that resolves if we can connect, or reject if there is an error
|
|
158
|
+
*/
|
|
159
|
+
get isReadyOrError() {
|
|
160
|
+
return this.#isReadyOrErrorPromise;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* @description Returns a clone of this ApiPromise instance (new underlying provider connection)
|
|
164
|
+
*/
|
|
165
|
+
clone() {
|
|
166
|
+
return new ApiPromise((0, util_1.objectSpread)({}, this._options, { source: this }));
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* @description Creates a combinator that can be used to combine the latest results from multiple subscriptions
|
|
170
|
+
* @param fns An array of function to combine, each in the form of `(cb: (value: void)) => void`
|
|
171
|
+
* @param callback A callback that will return an Array of all the values this combinator has been applied to
|
|
172
|
+
* @example
|
|
173
|
+
* <BR>
|
|
174
|
+
*
|
|
175
|
+
* ```javascript
|
|
176
|
+
* const address = '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFacT7';
|
|
177
|
+
*
|
|
178
|
+
* // combines values from balance & nonce as it updates
|
|
179
|
+
* api.combineLatest([
|
|
180
|
+
* api.rpc.chain.subscribeNewHeads,
|
|
181
|
+
* (cb) => api.query.system.account(address, cb)
|
|
182
|
+
* ], ([head, [balance, nonce]]) => {
|
|
183
|
+
* console.log(`#${head.number}: You have ${balance.free} units, with ${nonce} transactions sent`);
|
|
184
|
+
* });
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
188
|
+
async combineLatest(fns, callback) {
|
|
189
|
+
const combinator = new Combinator_js_1.Combinator(fns, callback);
|
|
190
|
+
return () => {
|
|
191
|
+
combinator.unsubscribe();
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
exports.ApiPromise = ApiPromise;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Combinator = void 0;
|
|
4
|
+
const util_1 = require("@pezkuwi/util");
|
|
5
|
+
class Combinator {
|
|
6
|
+
#allHasFired = false;
|
|
7
|
+
#callback;
|
|
8
|
+
#fired = [];
|
|
9
|
+
#fns = [];
|
|
10
|
+
#isActive = true;
|
|
11
|
+
#results = [];
|
|
12
|
+
#subscriptions = [];
|
|
13
|
+
constructor(fns, callback) {
|
|
14
|
+
this.#callback = callback;
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises, @typescript-eslint/require-await
|
|
16
|
+
this.#subscriptions = fns.map(async (input, index) => {
|
|
17
|
+
const [fn, ...args] = Array.isArray(input)
|
|
18
|
+
? input
|
|
19
|
+
: [input];
|
|
20
|
+
this.#fired.push(false);
|
|
21
|
+
this.#fns.push(fn);
|
|
22
|
+
// Not quite 100% how to have a variable number at the front here
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/ban-types
|
|
24
|
+
return fn(...args, this._createCallback(index));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
_allHasFired() {
|
|
28
|
+
this.#allHasFired ||= this.#fired.filter((hasFired) => !hasFired).length === 0;
|
|
29
|
+
return this.#allHasFired;
|
|
30
|
+
}
|
|
31
|
+
_createCallback(index) {
|
|
32
|
+
return (value) => {
|
|
33
|
+
this.#fired[index] = true;
|
|
34
|
+
this.#results[index] = value;
|
|
35
|
+
this._triggerUpdate();
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
_triggerUpdate() {
|
|
39
|
+
if (!this.#isActive || !(0, util_1.isFunction)(this.#callback) || !this._allHasFired()) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
Promise
|
|
44
|
+
.resolve(this.#callback(this.#results))
|
|
45
|
+
.catch(util_1.noop);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// swallow, we don't want the handler to trip us up
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
unsubscribe() {
|
|
52
|
+
if (!this.#isActive) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
this.#isActive = false;
|
|
56
|
+
Promise
|
|
57
|
+
.all(this.#subscriptions.map(async (subscription) => {
|
|
58
|
+
try {
|
|
59
|
+
const unsubscribe = await subscription;
|
|
60
|
+
if ((0, util_1.isFunction)(unsubscribe)) {
|
|
61
|
+
unsubscribe();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
// ignore
|
|
66
|
+
}
|
|
67
|
+
})).catch(() => {
|
|
68
|
+
// ignore, already ignored above, should never throw
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.Combinator = Combinator;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promiseTracker = promiseTracker;
|
|
4
|
+
exports.toPromiseMethod = toPromiseMethod;
|
|
5
|
+
const rxjs_1 = require("rxjs");
|
|
6
|
+
const util_1 = require("@pezkuwi/util");
|
|
7
|
+
function promiseTracker(resolve, reject) {
|
|
8
|
+
let isCompleted = false;
|
|
9
|
+
return {
|
|
10
|
+
reject: (error) => {
|
|
11
|
+
if (!isCompleted) {
|
|
12
|
+
isCompleted = true;
|
|
13
|
+
reject(error);
|
|
14
|
+
}
|
|
15
|
+
return rxjs_1.EMPTY;
|
|
16
|
+
},
|
|
17
|
+
resolve: (value) => {
|
|
18
|
+
if (!isCompleted) {
|
|
19
|
+
isCompleted = true;
|
|
20
|
+
resolve(value);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function extractArgs(args, needsCallback) {
|
|
26
|
+
const actualArgs = args.slice();
|
|
27
|
+
// If the last arg is a function, we pop it, put it into callback.
|
|
28
|
+
// actualArgs will then hold the actual arguments to be passed to `method`
|
|
29
|
+
const callback = (args.length && (0, util_1.isFunction)(args[args.length - 1]))
|
|
30
|
+
? actualArgs.pop()
|
|
31
|
+
: undefined;
|
|
32
|
+
// When we need a subscription, ensure that a valid callback is actually passed
|
|
33
|
+
if (needsCallback && !(0, util_1.isFunction)(callback)) {
|
|
34
|
+
throw new Error('Expected a callback to be passed with subscriptions');
|
|
35
|
+
}
|
|
36
|
+
return [actualArgs, callback];
|
|
37
|
+
}
|
|
38
|
+
function decorateCall(method, args) {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
// single result tracker - either reject with Error or resolve with Codec result
|
|
41
|
+
const tracker = promiseTracker(resolve, reject);
|
|
42
|
+
// encoding errors reject immediately, any result unsubscribes and resolves
|
|
43
|
+
const subscription = method(...args)
|
|
44
|
+
.pipe((0, rxjs_1.catchError)((error) => tracker.reject(error)))
|
|
45
|
+
.subscribe((result) => {
|
|
46
|
+
tracker.resolve(result);
|
|
47
|
+
(0, util_1.nextTick)(() => subscription.unsubscribe());
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function decorateSubscribe(method, args, resultCb) {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
// either reject with error or resolve with unsubscribe callback
|
|
54
|
+
const tracker = promiseTracker(resolve, reject);
|
|
55
|
+
// errors reject immediately, the first result resolves with an unsubscribe promise, all results via callback
|
|
56
|
+
const subscription = method(...args)
|
|
57
|
+
.pipe((0, rxjs_1.catchError)((error) => tracker.reject(error)), (0, rxjs_1.tap)(() => tracker.resolve(() => subscription.unsubscribe())))
|
|
58
|
+
.subscribe((result) => {
|
|
59
|
+
// queue result (back of queue to clear current)
|
|
60
|
+
(0, util_1.nextTick)(() => resultCb(result));
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @description Decorate method for ApiPromise, where the results are converted to the Promise equivalent
|
|
66
|
+
*/
|
|
67
|
+
function toPromiseMethod(method, options) {
|
|
68
|
+
const needsCallback = !!(options?.methodName && options.methodName.includes('subscribe'));
|
|
69
|
+
return function (...args) {
|
|
70
|
+
const [actualArgs, resultCb] = extractArgs(args, needsCallback);
|
|
71
|
+
return resultCb
|
|
72
|
+
? decorateSubscribe(method, actualArgs, resultCb)
|
|
73
|
+
: decorateCall(options?.overrideNoSub || method, actualArgs);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toPromiseMethod = exports.ApiPromise = void 0;
|
|
4
|
+
var Api_js_1 = require("./Api.js");
|
|
5
|
+
Object.defineProperty(exports, "ApiPromise", { enumerable: true, get: function () { return Api_js_1.ApiPromise; } });
|
|
6
|
+
var decorateMethod_js_1 = require("./decorateMethod.js");
|
|
7
|
+
Object.defineProperty(exports, "toPromiseMethod", { enumerable: true, get: function () { return decorateMethod_js_1.toPromiseMethod; } });
|
|
@@ -2,7 +2,7 @@ import type { Observable } from 'rxjs';
|
|
|
2
2
|
import type { ApiOptions } from '../types/index.js';
|
|
3
3
|
import { ApiBase } from '../base/index.js';
|
|
4
4
|
/**
|
|
5
|
-
* # @
|
|
5
|
+
* # @pezkuwi/api/rx
|
|
6
6
|
*
|
|
7
7
|
* ## Overview
|
|
8
8
|
*
|
|
@@ -11,7 +11,7 @@ import { ApiBase } from '../base/index.js';
|
|
|
11
11
|
* @description
|
|
12
12
|
* ApiRx is a powerful RxJS Observable wrapper around the RPC and interfaces on the Pezkuwi network. As a full Observable API, all interface calls return RxJS Observables, including the static `.create(...)`. In the same fashion and subscription-based methods return long-running Observables that update with the latest values.
|
|
13
13
|
*
|
|
14
|
-
* The API is well suited to real-time applications where the latest state is needed, unlocking the subscription-based features of
|
|
14
|
+
* The API is well suited to real-time applications where the latest state is needed, unlocking the subscription-based features of Pezkuwi (and Bizinikiwi) clients. Some familiarity with RxJS is a requirement to use the API, however just understanding `.subscribe` and `.pipe` on Observables will unlock full-scale use thereof.
|
|
15
15
|
*
|
|
16
16
|
* @see [[ApiPromise]]
|
|
17
17
|
*
|
package/cjs/rx/Api.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiRx = void 0;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
const util_1 = require("@pezkuwi/util");
|
|
6
|
+
const index_js_1 = require("../base/index.js");
|
|
7
|
+
const decorateMethod_js_1 = require("./decorateMethod.js");
|
|
8
|
+
/**
|
|
9
|
+
* # @pezkuwi/api/rx
|
|
10
|
+
*
|
|
11
|
+
* ## Overview
|
|
12
|
+
*
|
|
13
|
+
* @name ApiRx
|
|
14
|
+
*
|
|
15
|
+
* @description
|
|
16
|
+
* ApiRx is a powerful RxJS Observable wrapper around the RPC and interfaces on the Pezkuwi network. As a full Observable API, all interface calls return RxJS Observables, including the static `.create(...)`. In the same fashion and subscription-based methods return long-running Observables that update with the latest values.
|
|
17
|
+
*
|
|
18
|
+
* The API is well suited to real-time applications where the latest state is needed, unlocking the subscription-based features of Pezkuwi (and Bizinikiwi) clients. Some familiarity with RxJS is a requirement to use the API, however just understanding `.subscribe` and `.pipe` on Observables will unlock full-scale use thereof.
|
|
19
|
+
*
|
|
20
|
+
* @see [[ApiPromise]]
|
|
21
|
+
*
|
|
22
|
+
* ## Usage
|
|
23
|
+
*
|
|
24
|
+
* Making rpc calls -
|
|
25
|
+
* <BR>
|
|
26
|
+
*
|
|
27
|
+
* ```javascript
|
|
28
|
+
* import ApiRx from '@pezkuwi/api/rx';
|
|
29
|
+
*
|
|
30
|
+
* // initialize via Promise & static create
|
|
31
|
+
* const api = await ApiRx.create().toPromise();
|
|
32
|
+
*
|
|
33
|
+
* // make a call to retrieve the current network head
|
|
34
|
+
* api.rpc.chain.subscribeNewHeads().subscribe((header) => {
|
|
35
|
+
* console.log(`Chain is at #${header.number}`);
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
* <BR>
|
|
39
|
+
*
|
|
40
|
+
* Subscribing to chain state -
|
|
41
|
+
* <BR>
|
|
42
|
+
*
|
|
43
|
+
* ```javascript
|
|
44
|
+
* import { combineLatest, pairwise, switchMap } from 'rxjs';
|
|
45
|
+
* import { ApiRx, WsProvider } from '@pezkuwi/api';
|
|
46
|
+
*
|
|
47
|
+
*
|
|
48
|
+
* // initialize a provider with a specific endpoint
|
|
49
|
+
* const provider = new WsProvider('wss://example.com:9944')
|
|
50
|
+
*
|
|
51
|
+
* // initialize via isReady & new with specific provider
|
|
52
|
+
* new ApiRx({ provider })
|
|
53
|
+
* .isReady
|
|
54
|
+
* .pipe(
|
|
55
|
+
* switchMap((api) =>
|
|
56
|
+
* combineLatest([
|
|
57
|
+
* api.query.timestamp.blockPeriod(),
|
|
58
|
+
* api.query.timestamp.now().pipe(pairwise())
|
|
59
|
+
* ])
|
|
60
|
+
* )
|
|
61
|
+
* )
|
|
62
|
+
* .subscribe(([blockPeriod, timestamp]) => {
|
|
63
|
+
* const elapsed = timestamp[1].toNumber() - timestamp[0].toNumber();
|
|
64
|
+
* console.log(`timestamp ${timestamp[1]} \nelapsed ${elapsed} \n(${blockPeriod}s target)`);
|
|
65
|
+
* });
|
|
66
|
+
* ```
|
|
67
|
+
* <BR>
|
|
68
|
+
*
|
|
69
|
+
* Submitting a transaction -
|
|
70
|
+
* <BR>
|
|
71
|
+
*
|
|
72
|
+
* ```javascript
|
|
73
|
+
* import { first, switchMap } from 'rxjs';
|
|
74
|
+
* import ApiRx from '@pezkuwi/api/rx';
|
|
75
|
+
*
|
|
76
|
+
* // import the test keyring (already has dev keys for Alice, Bob, Charlie, Eve & Ferdie)
|
|
77
|
+
* import testingPairs from '@pezkuwi/keyring/testingPairs';
|
|
78
|
+
* const keyring = testingPairs();
|
|
79
|
+
*
|
|
80
|
+
* // get api via Promise
|
|
81
|
+
* const api = await ApiRx.create().toPromise();
|
|
82
|
+
*
|
|
83
|
+
* // retrieve nonce for the account
|
|
84
|
+
* api.query.system
|
|
85
|
+
* .account(keyring.alice.address)
|
|
86
|
+
* .pipe(
|
|
87
|
+
* first(),
|
|
88
|
+
* // pipe nonce into transfer
|
|
89
|
+
* switchMap(([nonce]) =>
|
|
90
|
+
* api.tx.balances
|
|
91
|
+
* // create transfer
|
|
92
|
+
* .transferAllowDeath(keyring.bob.address, 12345)
|
|
93
|
+
* // sign the transaction
|
|
94
|
+
* .sign(keyring.alice, { nonce })
|
|
95
|
+
* // send the transaction
|
|
96
|
+
* .send()
|
|
97
|
+
* )
|
|
98
|
+
* )
|
|
99
|
+
* // subscribe to overall result
|
|
100
|
+
* .subscribe(({ status }) => {
|
|
101
|
+
* if (status.isInBlock) {
|
|
102
|
+
* console.log('Completed at block hash', status.asFinalized.toHex());
|
|
103
|
+
* }
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
class ApiRx extends index_js_1.ApiBase {
|
|
108
|
+
#isReadyRx;
|
|
109
|
+
/**
|
|
110
|
+
* @description Create an instance of the ApiRx class
|
|
111
|
+
* @param options Options to create an instance. Can be either [[ApiOptions]] or [[WsProvider]]
|
|
112
|
+
* @example
|
|
113
|
+
* <BR>
|
|
114
|
+
*
|
|
115
|
+
* ```javascript
|
|
116
|
+
* import { switchMap } from 'rxjs';
|
|
117
|
+
* import Api from '@pezkuwi/api/rx';
|
|
118
|
+
*
|
|
119
|
+
* new Api().isReady
|
|
120
|
+
* .pipe(
|
|
121
|
+
* switchMap((api) =>
|
|
122
|
+
* api.rpc.chain.subscribeNewHeads()
|
|
123
|
+
* ))
|
|
124
|
+
* .subscribe((header) => {
|
|
125
|
+
* console.log(`new block #${header.number.toNumber()}`);
|
|
126
|
+
* });
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
constructor(options) {
|
|
130
|
+
super(options, 'rxjs', decorateMethod_js_1.toRxMethod);
|
|
131
|
+
this.#isReadyRx = (0, rxjs_1.from)(
|
|
132
|
+
// You can create an observable from an event, however my mind groks this form better
|
|
133
|
+
new Promise((resolve) => {
|
|
134
|
+
super.on('ready', () => resolve(this));
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* @description Creates an ApiRx instance using the supplied provider. Returns an Observable containing the actual Api instance.
|
|
139
|
+
* @param options options that is passed to the class constructor. Can be either [[ApiOptions]] or [[WsProvider]]
|
|
140
|
+
* @example
|
|
141
|
+
* <BR>
|
|
142
|
+
*
|
|
143
|
+
* ```javascript
|
|
144
|
+
* import { switchMap } from 'rxjs';
|
|
145
|
+
* import Api from '@pezkuwi/api/rx';
|
|
146
|
+
*
|
|
147
|
+
* Api.create()
|
|
148
|
+
* .pipe(
|
|
149
|
+
* switchMap((api) =>
|
|
150
|
+
* api.rpc.chain.subscribeNewHeads()
|
|
151
|
+
* ))
|
|
152
|
+
* .subscribe((header) => {
|
|
153
|
+
* console.log(`new block #${header.number.toNumber()}`);
|
|
154
|
+
* });
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
static create(options) {
|
|
158
|
+
return new ApiRx(options).isReady;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* @description Observable that returns the first time we are connected and loaded
|
|
162
|
+
*/
|
|
163
|
+
get isReady() {
|
|
164
|
+
return this.#isReadyRx;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @description Returns a clone of this ApiRx instance (new underlying provider connection)
|
|
168
|
+
*/
|
|
169
|
+
clone() {
|
|
170
|
+
return new ApiRx((0, util_1.objectSpread)({}, this._options, { source: this }));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
exports.ApiRx = ApiRx;
|
package/cjs/rx/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toRxMethod = exports.ApiRx = void 0;
|
|
4
|
+
var Api_js_1 = require("./Api.js");
|
|
5
|
+
Object.defineProperty(exports, "ApiRx", { enumerable: true, get: function () { return Api_js_1.ApiRx; } });
|
|
6
|
+
var decorateMethod_js_1 = require("./decorateMethod.js");
|
|
7
|
+
Object.defineProperty(exports, "toRxMethod", { enumerable: true, get: function () { return decorateMethod_js_1.toRxMethod; } });
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SubmittableResult = void 0;
|
|
4
|
+
const recordIdentity = (record) => record;
|
|
5
|
+
function filterAndApply(events, section, methods, onFound) {
|
|
6
|
+
return events
|
|
7
|
+
.filter(({ event }) => section === event.section &&
|
|
8
|
+
methods.includes(event.method))
|
|
9
|
+
.map((record) => onFound(record));
|
|
10
|
+
}
|
|
11
|
+
function getDispatchError({ event: { data: [dispatchError] } }) {
|
|
12
|
+
return dispatchError;
|
|
13
|
+
}
|
|
14
|
+
function getDispatchInfo({ event: { data, method } }) {
|
|
15
|
+
return method === 'ExtrinsicSuccess'
|
|
16
|
+
? data[0]
|
|
17
|
+
: data[1];
|
|
18
|
+
}
|
|
19
|
+
function extractError(events = []) {
|
|
20
|
+
return filterAndApply(events, 'system', ['ExtrinsicFailed'], getDispatchError)[0];
|
|
21
|
+
}
|
|
22
|
+
function extractInfo(events = []) {
|
|
23
|
+
return filterAndApply(events, 'system', ['ExtrinsicFailed', 'ExtrinsicSuccess'], getDispatchInfo)[0];
|
|
24
|
+
}
|
|
25
|
+
class SubmittableResult {
|
|
26
|
+
dispatchError;
|
|
27
|
+
dispatchInfo;
|
|
28
|
+
internalError;
|
|
29
|
+
events;
|
|
30
|
+
status;
|
|
31
|
+
txHash;
|
|
32
|
+
txIndex;
|
|
33
|
+
blockNumber;
|
|
34
|
+
constructor({ blockNumber, dispatchError, dispatchInfo, events, internalError, status, txHash, txIndex }) {
|
|
35
|
+
this.dispatchError = dispatchError || extractError(events);
|
|
36
|
+
this.dispatchInfo = dispatchInfo || extractInfo(events);
|
|
37
|
+
this.events = events || [];
|
|
38
|
+
this.internalError = internalError;
|
|
39
|
+
this.status = status;
|
|
40
|
+
this.txHash = txHash;
|
|
41
|
+
this.txIndex = txIndex;
|
|
42
|
+
this.blockNumber = blockNumber;
|
|
43
|
+
}
|
|
44
|
+
get isCompleted() {
|
|
45
|
+
return this.isError || this.status.isInBlock || this.status.isFinalized;
|
|
46
|
+
}
|
|
47
|
+
get isError() {
|
|
48
|
+
return this.status.isDropped || this.status.isFinalityTimeout || this.status.isInvalid || this.status.isUsurped;
|
|
49
|
+
}
|
|
50
|
+
get isFinalized() {
|
|
51
|
+
return this.status.isFinalized;
|
|
52
|
+
}
|
|
53
|
+
get isInBlock() {
|
|
54
|
+
return this.status.isInBlock;
|
|
55
|
+
}
|
|
56
|
+
get isWarning() {
|
|
57
|
+
return this.status.isRetracted;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @description Filters EventRecords for the specified method & section (there could be multiple)
|
|
61
|
+
*/
|
|
62
|
+
filterRecords(section, method) {
|
|
63
|
+
return filterAndApply(this.events, section, Array.isArray(method) ? method : [method], recordIdentity);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* @description Finds an EventRecord for the specified method & section
|
|
67
|
+
*/
|
|
68
|
+
findRecord(section, method) {
|
|
69
|
+
return this.filterRecords(section, method)[0];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @description Creates a human representation of the output
|
|
73
|
+
*/
|
|
74
|
+
toHuman(isExtended) {
|
|
75
|
+
return {
|
|
76
|
+
dispatchError: this.dispatchError?.toHuman(),
|
|
77
|
+
dispatchInfo: this.dispatchInfo?.toHuman(),
|
|
78
|
+
events: this.events.map((e) => e.toHuman(isExtended)),
|
|
79
|
+
internalError: this.internalError?.message.toString(),
|
|
80
|
+
status: this.status.toHuman(isExtended)
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.SubmittableResult = SubmittableResult;
|