@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
package/promise/Api.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { ApiOptions, UnsubscribePromise } from '../types/index.js';
|
|
2
|
+
import type { CombinatorCallback, CombinatorFunction } from './Combinator.js';
|
|
3
|
+
import { ApiBase } from '../base/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* # @pezkuwi/api/promise
|
|
6
|
+
*
|
|
7
|
+
* ## Overview
|
|
8
|
+
*
|
|
9
|
+
* @name ApiPromise
|
|
10
|
+
* @description
|
|
11
|
+
* 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.
|
|
12
|
+
*
|
|
13
|
+
* 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.
|
|
14
|
+
*
|
|
15
|
+
* @see [[ApiRx]]
|
|
16
|
+
*
|
|
17
|
+
* ## Usage
|
|
18
|
+
*
|
|
19
|
+
* Making rpc calls -
|
|
20
|
+
* <BR>
|
|
21
|
+
*
|
|
22
|
+
* ```javascript
|
|
23
|
+
* import ApiPromise from '@pezkuwi/api/promise';
|
|
24
|
+
*
|
|
25
|
+
* // initialise via static create
|
|
26
|
+
* const api = await ApiPromise.create();
|
|
27
|
+
*
|
|
28
|
+
* // make a subscription to the network head
|
|
29
|
+
* api.rpc.chain.subscribeNewHeads((header) => {
|
|
30
|
+
* console.log(`Chain is at #${header.number}`);
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
* <BR>
|
|
34
|
+
*
|
|
35
|
+
* Subscribing to chain state -
|
|
36
|
+
* <BR>
|
|
37
|
+
*
|
|
38
|
+
* ```javascript
|
|
39
|
+
* import { ApiPromise, WsProvider } from '@pezkuwi/api';
|
|
40
|
+
*
|
|
41
|
+
* // initialise a provider with a specific endpoint
|
|
42
|
+
* const provider = new WsProvider('wss://example.com:9944')
|
|
43
|
+
*
|
|
44
|
+
* // initialise via isReady & new with specific provider
|
|
45
|
+
* const api = await new ApiPromise({ provider }).isReady;
|
|
46
|
+
*
|
|
47
|
+
* // retrieve the block target time
|
|
48
|
+
* const blockPeriod = await api.query.timestamp.blockPeriod().toNumber();
|
|
49
|
+
* let last = 0;
|
|
50
|
+
*
|
|
51
|
+
* // subscribe to the current block timestamp, updates automatically (callback provided)
|
|
52
|
+
* api.query.timestamp.now((timestamp) => {
|
|
53
|
+
* const elapsed = last
|
|
54
|
+
* ? `, ${timestamp.toNumber() - last}s since last`
|
|
55
|
+
* : '';
|
|
56
|
+
*
|
|
57
|
+
* last = timestamp.toNumber();
|
|
58
|
+
* console.log(`timestamp ${timestamp}${elapsed} (${blockPeriod}s target)`);
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
* <BR>
|
|
62
|
+
*
|
|
63
|
+
* Submitting a transaction -
|
|
64
|
+
* <BR>
|
|
65
|
+
*
|
|
66
|
+
* ```javascript
|
|
67
|
+
* import ApiPromise from '@pezkuwi/api/promise';
|
|
68
|
+
*
|
|
69
|
+
* ApiPromise.create().then((api) => {
|
|
70
|
+
* const [nonce] = await api.query.system.account(keyring.alice.address);
|
|
71
|
+
*
|
|
72
|
+
* api.tx.balances
|
|
73
|
+
* // create transfer
|
|
74
|
+
* transfer(keyring.bob.address, 12345)
|
|
75
|
+
* // sign the transcation
|
|
76
|
+
* .sign(keyring.alice, { nonce })
|
|
77
|
+
* // send the transaction (optional status callback)
|
|
78
|
+
* .send((status) => {
|
|
79
|
+
* console.log(`current status ${status.type}`);
|
|
80
|
+
* })
|
|
81
|
+
* // retrieve the submitted extrinsic hash
|
|
82
|
+
* .then((hash) => {
|
|
83
|
+
* console.log(`submitted with hash ${hash}`);
|
|
84
|
+
* });
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export declare class ApiPromise extends ApiBase<'promise'> {
|
|
89
|
+
#private;
|
|
90
|
+
/**
|
|
91
|
+
* @description Creates an instance of the ApiPromise class
|
|
92
|
+
* @param options Options to create an instance. This can be either [[ApiOptions]] or
|
|
93
|
+
* an [[WsProvider]].
|
|
94
|
+
* @example
|
|
95
|
+
* <BR>
|
|
96
|
+
*
|
|
97
|
+
* ```javascript
|
|
98
|
+
* import Api from '@pezkuwi/api/promise';
|
|
99
|
+
*
|
|
100
|
+
* new Api().isReady.then((api) => {
|
|
101
|
+
* api.rpc.subscribeNewHeads((header) => {
|
|
102
|
+
* console.log(`new block #${header.number.toNumber()}`);
|
|
103
|
+
* });
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
constructor(options?: ApiOptions);
|
|
108
|
+
/**
|
|
109
|
+
* @description Creates an ApiPromise instance using the supplied provider. Returns an Promise containing the actual Api instance.
|
|
110
|
+
* @param options options that is passed to the class contructor. Can be either [[ApiOptions]] or a
|
|
111
|
+
* provider (see the constructor arguments)
|
|
112
|
+
* @example
|
|
113
|
+
* <BR>
|
|
114
|
+
*
|
|
115
|
+
* ```javascript
|
|
116
|
+
* import Api from '@pezkuwi/api/promise';
|
|
117
|
+
*
|
|
118
|
+
* Api.create().then(async (api) => {
|
|
119
|
+
* const timestamp = await api.query.timestamp.now();
|
|
120
|
+
*
|
|
121
|
+
* console.log(`lastest block timestamp ${timestamp}`);
|
|
122
|
+
* });
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
static create(options?: ApiOptions): Promise<ApiPromise>;
|
|
126
|
+
/**
|
|
127
|
+
* @description Promise that resolves the first time we are connected and loaded
|
|
128
|
+
*/
|
|
129
|
+
get isReady(): Promise<ApiPromise>;
|
|
130
|
+
/**
|
|
131
|
+
* @description Promise that resolves if we can connect, or reject if there is an error
|
|
132
|
+
*/
|
|
133
|
+
get isReadyOrError(): Promise<ApiPromise>;
|
|
134
|
+
/**
|
|
135
|
+
* @description Returns a clone of this ApiPromise instance (new underlying provider connection)
|
|
136
|
+
*/
|
|
137
|
+
clone(): ApiPromise;
|
|
138
|
+
/**
|
|
139
|
+
* @description Creates a combinator that can be used to combine the latest results from multiple subscriptions
|
|
140
|
+
* @param fns An array of function to combine, each in the form of `(cb: (value: void)) => void`
|
|
141
|
+
* @param callback A callback that will return an Array of all the values this combinator has been applied to
|
|
142
|
+
* @example
|
|
143
|
+
* <BR>
|
|
144
|
+
*
|
|
145
|
+
* ```javascript
|
|
146
|
+
* const address = '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFacT7';
|
|
147
|
+
*
|
|
148
|
+
* // combines values from balance & nonce as it updates
|
|
149
|
+
* api.combineLatest([
|
|
150
|
+
* api.rpc.chain.subscribeNewHeads,
|
|
151
|
+
* (cb) => api.query.system.account(address, cb)
|
|
152
|
+
* ], ([head, [balance, nonce]]) => {
|
|
153
|
+
* console.log(`#${head.number}: You have ${balance.free} units, with ${nonce} transactions sent`);
|
|
154
|
+
* });
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
combineLatest<T extends any[] = any[]>(fns: (CombinatorFunction | [CombinatorFunction, ...any[]])[], callback: CombinatorCallback<T>): UnsubscribePromise;
|
|
158
|
+
}
|
package/promise/Api.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { noop, objectSpread } from '@pezkuwi/util';
|
|
2
|
+
import { ApiBase } from '../base/index.js';
|
|
3
|
+
import { Combinator } from './Combinator.js';
|
|
4
|
+
import { promiseTracker, toPromiseMethod } from './decorateMethod.js';
|
|
5
|
+
/**
|
|
6
|
+
* # @pezkuwi/api/promise
|
|
7
|
+
*
|
|
8
|
+
* ## Overview
|
|
9
|
+
*
|
|
10
|
+
* @name ApiPromise
|
|
11
|
+
* @description
|
|
12
|
+
* 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.
|
|
13
|
+
*
|
|
14
|
+
* 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.
|
|
15
|
+
*
|
|
16
|
+
* @see [[ApiRx]]
|
|
17
|
+
*
|
|
18
|
+
* ## Usage
|
|
19
|
+
*
|
|
20
|
+
* Making rpc calls -
|
|
21
|
+
* <BR>
|
|
22
|
+
*
|
|
23
|
+
* ```javascript
|
|
24
|
+
* import ApiPromise from '@pezkuwi/api/promise';
|
|
25
|
+
*
|
|
26
|
+
* // initialise via static create
|
|
27
|
+
* const api = await ApiPromise.create();
|
|
28
|
+
*
|
|
29
|
+
* // make a subscription to the network head
|
|
30
|
+
* api.rpc.chain.subscribeNewHeads((header) => {
|
|
31
|
+
* console.log(`Chain is at #${header.number}`);
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
34
|
+
* <BR>
|
|
35
|
+
*
|
|
36
|
+
* Subscribing to chain state -
|
|
37
|
+
* <BR>
|
|
38
|
+
*
|
|
39
|
+
* ```javascript
|
|
40
|
+
* import { ApiPromise, WsProvider } from '@pezkuwi/api';
|
|
41
|
+
*
|
|
42
|
+
* // initialise a provider with a specific endpoint
|
|
43
|
+
* const provider = new WsProvider('wss://example.com:9944')
|
|
44
|
+
*
|
|
45
|
+
* // initialise via isReady & new with specific provider
|
|
46
|
+
* const api = await new ApiPromise({ provider }).isReady;
|
|
47
|
+
*
|
|
48
|
+
* // retrieve the block target time
|
|
49
|
+
* const blockPeriod = await api.query.timestamp.blockPeriod().toNumber();
|
|
50
|
+
* let last = 0;
|
|
51
|
+
*
|
|
52
|
+
* // subscribe to the current block timestamp, updates automatically (callback provided)
|
|
53
|
+
* api.query.timestamp.now((timestamp) => {
|
|
54
|
+
* const elapsed = last
|
|
55
|
+
* ? `, ${timestamp.toNumber() - last}s since last`
|
|
56
|
+
* : '';
|
|
57
|
+
*
|
|
58
|
+
* last = timestamp.toNumber();
|
|
59
|
+
* console.log(`timestamp ${timestamp}${elapsed} (${blockPeriod}s target)`);
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
* <BR>
|
|
63
|
+
*
|
|
64
|
+
* Submitting a transaction -
|
|
65
|
+
* <BR>
|
|
66
|
+
*
|
|
67
|
+
* ```javascript
|
|
68
|
+
* import ApiPromise from '@pezkuwi/api/promise';
|
|
69
|
+
*
|
|
70
|
+
* ApiPromise.create().then((api) => {
|
|
71
|
+
* const [nonce] = await api.query.system.account(keyring.alice.address);
|
|
72
|
+
*
|
|
73
|
+
* api.tx.balances
|
|
74
|
+
* // create transfer
|
|
75
|
+
* transfer(keyring.bob.address, 12345)
|
|
76
|
+
* // sign the transcation
|
|
77
|
+
* .sign(keyring.alice, { nonce })
|
|
78
|
+
* // send the transaction (optional status callback)
|
|
79
|
+
* .send((status) => {
|
|
80
|
+
* console.log(`current status ${status.type}`);
|
|
81
|
+
* })
|
|
82
|
+
* // retrieve the submitted extrinsic hash
|
|
83
|
+
* .then((hash) => {
|
|
84
|
+
* console.log(`submitted with hash ${hash}`);
|
|
85
|
+
* });
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export class ApiPromise extends ApiBase {
|
|
90
|
+
#isReadyPromise;
|
|
91
|
+
#isReadyOrErrorPromise;
|
|
92
|
+
/**
|
|
93
|
+
* @description Creates an instance of the ApiPromise class
|
|
94
|
+
* @param options Options to create an instance. This can be either [[ApiOptions]] or
|
|
95
|
+
* an [[WsProvider]].
|
|
96
|
+
* @example
|
|
97
|
+
* <BR>
|
|
98
|
+
*
|
|
99
|
+
* ```javascript
|
|
100
|
+
* import Api from '@pezkuwi/api/promise';
|
|
101
|
+
*
|
|
102
|
+
* new Api().isReady.then((api) => {
|
|
103
|
+
* api.rpc.subscribeNewHeads((header) => {
|
|
104
|
+
* console.log(`new block #${header.number.toNumber()}`);
|
|
105
|
+
* });
|
|
106
|
+
* });
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
constructor(options) {
|
|
110
|
+
super(options, 'promise', toPromiseMethod);
|
|
111
|
+
this.#isReadyPromise = new Promise((resolve) => {
|
|
112
|
+
super.once('ready', () => resolve(this));
|
|
113
|
+
});
|
|
114
|
+
this.#isReadyOrErrorPromise = new Promise((resolve, reject) => {
|
|
115
|
+
const tracker = promiseTracker(resolve, reject);
|
|
116
|
+
super.once('ready', () => tracker.resolve(this));
|
|
117
|
+
super.once('error', (error) => tracker.reject(error));
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* @description Creates an ApiPromise instance using the supplied provider. Returns an Promise containing the actual Api instance.
|
|
122
|
+
* @param options options that is passed to the class contructor. Can be either [[ApiOptions]] or a
|
|
123
|
+
* provider (see the constructor arguments)
|
|
124
|
+
* @example
|
|
125
|
+
* <BR>
|
|
126
|
+
*
|
|
127
|
+
* ```javascript
|
|
128
|
+
* import Api from '@pezkuwi/api/promise';
|
|
129
|
+
*
|
|
130
|
+
* Api.create().then(async (api) => {
|
|
131
|
+
* const timestamp = await api.query.timestamp.now();
|
|
132
|
+
*
|
|
133
|
+
* console.log(`lastest block timestamp ${timestamp}`);
|
|
134
|
+
* });
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
static create(options) {
|
|
138
|
+
const instance = new ApiPromise(options);
|
|
139
|
+
if (options && options.throwOnConnect) {
|
|
140
|
+
return instance.isReadyOrError;
|
|
141
|
+
}
|
|
142
|
+
// Swallow any rejections on isReadyOrError
|
|
143
|
+
// (in Node 15.x this creates issues, when not being looked at)
|
|
144
|
+
instance.isReadyOrError.catch(noop);
|
|
145
|
+
return instance.isReady;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* @description Promise that resolves the first time we are connected and loaded
|
|
149
|
+
*/
|
|
150
|
+
get isReady() {
|
|
151
|
+
return this.#isReadyPromise;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* @description Promise that resolves if we can connect, or reject if there is an error
|
|
155
|
+
*/
|
|
156
|
+
get isReadyOrError() {
|
|
157
|
+
return this.#isReadyOrErrorPromise;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @description Returns a clone of this ApiPromise instance (new underlying provider connection)
|
|
161
|
+
*/
|
|
162
|
+
clone() {
|
|
163
|
+
return new ApiPromise(objectSpread({}, this._options, { source: this }));
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* @description Creates a combinator that can be used to combine the latest results from multiple subscriptions
|
|
167
|
+
* @param fns An array of function to combine, each in the form of `(cb: (value: void)) => void`
|
|
168
|
+
* @param callback A callback that will return an Array of all the values this combinator has been applied to
|
|
169
|
+
* @example
|
|
170
|
+
* <BR>
|
|
171
|
+
*
|
|
172
|
+
* ```javascript
|
|
173
|
+
* const address = '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFacT7';
|
|
174
|
+
*
|
|
175
|
+
* // combines values from balance & nonce as it updates
|
|
176
|
+
* api.combineLatest([
|
|
177
|
+
* api.rpc.chain.subscribeNewHeads,
|
|
178
|
+
* (cb) => api.query.system.account(address, cb)
|
|
179
|
+
* ], ([head, [balance, nonce]]) => {
|
|
180
|
+
* console.log(`#${head.number}: You have ${balance.free} units, with ${nonce} transactions sent`);
|
|
181
|
+
* });
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
185
|
+
async combineLatest(fns, callback) {
|
|
186
|
+
const combinator = new Combinator(fns, callback);
|
|
187
|
+
return () => {
|
|
188
|
+
combinator.unsubscribe();
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Callback } from '@pezkuwi/types/types';
|
|
2
|
+
import type { UnsubscribePromise } from '../types/index.js';
|
|
3
|
+
export type CombinatorCallback<T extends unknown[]> = Callback<T>;
|
|
4
|
+
export type CombinatorFunction = (cb: Callback<any>) => UnsubscribePromise;
|
|
5
|
+
export declare class Combinator<T extends unknown[] = unknown[]> {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(fns: (CombinatorFunction | [CombinatorFunction, ...unknown[]])[], callback: CombinatorCallback<T>);
|
|
8
|
+
protected _allHasFired(): boolean;
|
|
9
|
+
protected _createCallback(index: number): (value: any) => void;
|
|
10
|
+
protected _triggerUpdate(): void;
|
|
11
|
+
unsubscribe(): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { isFunction, noop } from '@pezkuwi/util';
|
|
2
|
+
export class Combinator {
|
|
3
|
+
#allHasFired = false;
|
|
4
|
+
#callback;
|
|
5
|
+
#fired = [];
|
|
6
|
+
#fns = [];
|
|
7
|
+
#isActive = true;
|
|
8
|
+
#results = [];
|
|
9
|
+
#subscriptions = [];
|
|
10
|
+
constructor(fns, callback) {
|
|
11
|
+
this.#callback = callback;
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises, @typescript-eslint/require-await
|
|
13
|
+
this.#subscriptions = fns.map(async (input, index) => {
|
|
14
|
+
const [fn, ...args] = Array.isArray(input)
|
|
15
|
+
? input
|
|
16
|
+
: [input];
|
|
17
|
+
this.#fired.push(false);
|
|
18
|
+
this.#fns.push(fn);
|
|
19
|
+
// Not quite 100% how to have a variable number at the front here
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/ban-types
|
|
21
|
+
return fn(...args, this._createCallback(index));
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
_allHasFired() {
|
|
25
|
+
this.#allHasFired ||= this.#fired.filter((hasFired) => !hasFired).length === 0;
|
|
26
|
+
return this.#allHasFired;
|
|
27
|
+
}
|
|
28
|
+
_createCallback(index) {
|
|
29
|
+
return (value) => {
|
|
30
|
+
this.#fired[index] = true;
|
|
31
|
+
this.#results[index] = value;
|
|
32
|
+
this._triggerUpdate();
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
_triggerUpdate() {
|
|
36
|
+
if (!this.#isActive || !isFunction(this.#callback) || !this._allHasFired()) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
Promise
|
|
41
|
+
.resolve(this.#callback(this.#results))
|
|
42
|
+
.catch(noop);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// swallow, we don't want the handler to trip us up
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
unsubscribe() {
|
|
49
|
+
if (!this.#isActive) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
this.#isActive = false;
|
|
53
|
+
Promise
|
|
54
|
+
.all(this.#subscriptions.map(async (subscription) => {
|
|
55
|
+
try {
|
|
56
|
+
const unsubscribe = await subscription;
|
|
57
|
+
if (isFunction(unsubscribe)) {
|
|
58
|
+
unsubscribe();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// ignore
|
|
63
|
+
}
|
|
64
|
+
})).catch(() => {
|
|
65
|
+
// ignore, already ignored above, should never throw
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Observable } from 'rxjs';
|
|
2
|
+
import type { Codec } from '@pezkuwi/types/types';
|
|
3
|
+
import type { DecorateFn, DecorateMethodOptions, ObsInnerType, StorageEntryPromiseOverloads } from '../types/index.js';
|
|
4
|
+
interface Tracker<T> {
|
|
5
|
+
reject: (value: Error) => Observable<never>;
|
|
6
|
+
resolve: (value: T) => void;
|
|
7
|
+
}
|
|
8
|
+
type CodecReturnType<T extends (...args: unknown[]) => Observable<Codec>> = T extends (...args: any) => infer R ? R extends Observable<Codec> ? ObsInnerType<R> : never : never;
|
|
9
|
+
export declare function promiseTracker<T>(resolve: (value: T) => void, reject: (value: Error) => void): Tracker<T>;
|
|
10
|
+
/**
|
|
11
|
+
* @description Decorate method for ApiPromise, where the results are converted to the Promise equivalent
|
|
12
|
+
*/
|
|
13
|
+
export declare function toPromiseMethod<M extends DecorateFn<CodecReturnType<M>>>(method: M, options?: DecorateMethodOptions): StorageEntryPromiseOverloads;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { catchError, EMPTY, tap } from 'rxjs';
|
|
2
|
+
import { isFunction, nextTick } from '@pezkuwi/util';
|
|
3
|
+
export function promiseTracker(resolve, reject) {
|
|
4
|
+
let isCompleted = false;
|
|
5
|
+
return {
|
|
6
|
+
reject: (error) => {
|
|
7
|
+
if (!isCompleted) {
|
|
8
|
+
isCompleted = true;
|
|
9
|
+
reject(error);
|
|
10
|
+
}
|
|
11
|
+
return EMPTY;
|
|
12
|
+
},
|
|
13
|
+
resolve: (value) => {
|
|
14
|
+
if (!isCompleted) {
|
|
15
|
+
isCompleted = true;
|
|
16
|
+
resolve(value);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function extractArgs(args, needsCallback) {
|
|
22
|
+
const actualArgs = args.slice();
|
|
23
|
+
// If the last arg is a function, we pop it, put it into callback.
|
|
24
|
+
// actualArgs will then hold the actual arguments to be passed to `method`
|
|
25
|
+
const callback = (args.length && isFunction(args[args.length - 1]))
|
|
26
|
+
? actualArgs.pop()
|
|
27
|
+
: undefined;
|
|
28
|
+
// When we need a subscription, ensure that a valid callback is actually passed
|
|
29
|
+
if (needsCallback && !isFunction(callback)) {
|
|
30
|
+
throw new Error('Expected a callback to be passed with subscriptions');
|
|
31
|
+
}
|
|
32
|
+
return [actualArgs, callback];
|
|
33
|
+
}
|
|
34
|
+
function decorateCall(method, args) {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
// single result tracker - either reject with Error or resolve with Codec result
|
|
37
|
+
const tracker = promiseTracker(resolve, reject);
|
|
38
|
+
// encoding errors reject immediately, any result unsubscribes and resolves
|
|
39
|
+
const subscription = method(...args)
|
|
40
|
+
.pipe(catchError((error) => tracker.reject(error)))
|
|
41
|
+
.subscribe((result) => {
|
|
42
|
+
tracker.resolve(result);
|
|
43
|
+
nextTick(() => subscription.unsubscribe());
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function decorateSubscribe(method, args, resultCb) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
// either reject with error or resolve with unsubscribe callback
|
|
50
|
+
const tracker = promiseTracker(resolve, reject);
|
|
51
|
+
// errors reject immediately, the first result resolves with an unsubscribe promise, all results via callback
|
|
52
|
+
const subscription = method(...args)
|
|
53
|
+
.pipe(catchError((error) => tracker.reject(error)), tap(() => tracker.resolve(() => subscription.unsubscribe())))
|
|
54
|
+
.subscribe((result) => {
|
|
55
|
+
// queue result (back of queue to clear current)
|
|
56
|
+
nextTick(() => resultCb(result));
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @description Decorate method for ApiPromise, where the results are converted to the Promise equivalent
|
|
62
|
+
*/
|
|
63
|
+
export function toPromiseMethod(method, options) {
|
|
64
|
+
const needsCallback = !!(options?.methodName && options.methodName.includes('subscribe'));
|
|
65
|
+
return function (...args) {
|
|
66
|
+
const [actualArgs, resultCb] = extractArgs(args, needsCallback);
|
|
67
|
+
return resultCb
|
|
68
|
+
? decorateSubscribe(method, actualArgs, resultCb)
|
|
69
|
+
: decorateCall(options?.overrideNoSub || method, actualArgs);
|
|
70
|
+
};
|
|
71
|
+
}
|
package/promise/index.js
ADDED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
// Copyright 2017-2025 @polkadot/api authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
1
|
import type { SubmittableExtrinsic as SubmittableExtrinsicBase } from '../submittable/types.js';
|
|
5
2
|
import type { QueryableStorageEntry as QueryableStorageEntryBase, SubmittableExtrinsicFunction as SubmittableExtrinsicFunctionBase } from '../types/index.js';
|
|
6
|
-
|
|
7
3
|
export type QueryableStorageEntry = QueryableStorageEntryBase<'promise'>;
|
|
8
4
|
export type SubmittableExtrinsic = SubmittableExtrinsicBase<'promise'>;
|
|
9
5
|
export type SubmittableExtrinsicFunction = SubmittableExtrinsicFunctionBase<'promise'>;
|
package/promise/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/rx/Api.d.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { Observable } from 'rxjs';
|
|
2
|
+
import type { ApiOptions } from '../types/index.js';
|
|
3
|
+
import { ApiBase } from '../base/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* # @pezkuwi/api/rx
|
|
6
|
+
*
|
|
7
|
+
* ## Overview
|
|
8
|
+
*
|
|
9
|
+
* @name ApiRx
|
|
10
|
+
*
|
|
11
|
+
* @description
|
|
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
|
+
*
|
|
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
|
+
*
|
|
16
|
+
* @see [[ApiPromise]]
|
|
17
|
+
*
|
|
18
|
+
* ## Usage
|
|
19
|
+
*
|
|
20
|
+
* Making rpc calls -
|
|
21
|
+
* <BR>
|
|
22
|
+
*
|
|
23
|
+
* ```javascript
|
|
24
|
+
* import ApiRx from '@pezkuwi/api/rx';
|
|
25
|
+
*
|
|
26
|
+
* // initialize via Promise & static create
|
|
27
|
+
* const api = await ApiRx.create().toPromise();
|
|
28
|
+
*
|
|
29
|
+
* // make a call to retrieve the current network head
|
|
30
|
+
* api.rpc.chain.subscribeNewHeads().subscribe((header) => {
|
|
31
|
+
* console.log(`Chain is at #${header.number}`);
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
34
|
+
* <BR>
|
|
35
|
+
*
|
|
36
|
+
* Subscribing to chain state -
|
|
37
|
+
* <BR>
|
|
38
|
+
*
|
|
39
|
+
* ```javascript
|
|
40
|
+
* import { combineLatest, pairwise, switchMap } from 'rxjs';
|
|
41
|
+
* import { ApiRx, WsProvider } from '@pezkuwi/api';
|
|
42
|
+
*
|
|
43
|
+
*
|
|
44
|
+
* // initialize a provider with a specific endpoint
|
|
45
|
+
* const provider = new WsProvider('wss://example.com:9944')
|
|
46
|
+
*
|
|
47
|
+
* // initialize via isReady & new with specific provider
|
|
48
|
+
* new ApiRx({ provider })
|
|
49
|
+
* .isReady
|
|
50
|
+
* .pipe(
|
|
51
|
+
* switchMap((api) =>
|
|
52
|
+
* combineLatest([
|
|
53
|
+
* api.query.timestamp.blockPeriod(),
|
|
54
|
+
* api.query.timestamp.now().pipe(pairwise())
|
|
55
|
+
* ])
|
|
56
|
+
* )
|
|
57
|
+
* )
|
|
58
|
+
* .subscribe(([blockPeriod, timestamp]) => {
|
|
59
|
+
* const elapsed = timestamp[1].toNumber() - timestamp[0].toNumber();
|
|
60
|
+
* console.log(`timestamp ${timestamp[1]} \nelapsed ${elapsed} \n(${blockPeriod}s target)`);
|
|
61
|
+
* });
|
|
62
|
+
* ```
|
|
63
|
+
* <BR>
|
|
64
|
+
*
|
|
65
|
+
* Submitting a transaction -
|
|
66
|
+
* <BR>
|
|
67
|
+
*
|
|
68
|
+
* ```javascript
|
|
69
|
+
* import { first, switchMap } from 'rxjs';
|
|
70
|
+
* import ApiRx from '@pezkuwi/api/rx';
|
|
71
|
+
*
|
|
72
|
+
* // import the test keyring (already has dev keys for Alice, Bob, Charlie, Eve & Ferdie)
|
|
73
|
+
* import testingPairs from '@pezkuwi/keyring/testingPairs';
|
|
74
|
+
* const keyring = testingPairs();
|
|
75
|
+
*
|
|
76
|
+
* // get api via Promise
|
|
77
|
+
* const api = await ApiRx.create().toPromise();
|
|
78
|
+
*
|
|
79
|
+
* // retrieve nonce for the account
|
|
80
|
+
* api.query.system
|
|
81
|
+
* .account(keyring.alice.address)
|
|
82
|
+
* .pipe(
|
|
83
|
+
* first(),
|
|
84
|
+
* // pipe nonce into transfer
|
|
85
|
+
* switchMap(([nonce]) =>
|
|
86
|
+
* api.tx.balances
|
|
87
|
+
* // create transfer
|
|
88
|
+
* .transferAllowDeath(keyring.bob.address, 12345)
|
|
89
|
+
* // sign the transaction
|
|
90
|
+
* .sign(keyring.alice, { nonce })
|
|
91
|
+
* // send the transaction
|
|
92
|
+
* .send()
|
|
93
|
+
* )
|
|
94
|
+
* )
|
|
95
|
+
* // subscribe to overall result
|
|
96
|
+
* .subscribe(({ status }) => {
|
|
97
|
+
* if (status.isInBlock) {
|
|
98
|
+
* console.log('Completed at block hash', status.asFinalized.toHex());
|
|
99
|
+
* }
|
|
100
|
+
* });
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export declare class ApiRx extends ApiBase<'rxjs'> {
|
|
104
|
+
#private;
|
|
105
|
+
/**
|
|
106
|
+
* @description Create an instance of the ApiRx class
|
|
107
|
+
* @param options Options to create an instance. Can be either [[ApiOptions]] or [[WsProvider]]
|
|
108
|
+
* @example
|
|
109
|
+
* <BR>
|
|
110
|
+
*
|
|
111
|
+
* ```javascript
|
|
112
|
+
* import { switchMap } from 'rxjs';
|
|
113
|
+
* import Api from '@pezkuwi/api/rx';
|
|
114
|
+
*
|
|
115
|
+
* new Api().isReady
|
|
116
|
+
* .pipe(
|
|
117
|
+
* switchMap((api) =>
|
|
118
|
+
* api.rpc.chain.subscribeNewHeads()
|
|
119
|
+
* ))
|
|
120
|
+
* .subscribe((header) => {
|
|
121
|
+
* console.log(`new block #${header.number.toNumber()}`);
|
|
122
|
+
* });
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
constructor(options?: ApiOptions);
|
|
126
|
+
/**
|
|
127
|
+
* @description Creates an ApiRx instance using the supplied provider. Returns an Observable containing the actual Api instance.
|
|
128
|
+
* @param options options that is passed to the class constructor. Can be either [[ApiOptions]] or [[WsProvider]]
|
|
129
|
+
* @example
|
|
130
|
+
* <BR>
|
|
131
|
+
*
|
|
132
|
+
* ```javascript
|
|
133
|
+
* import { switchMap } from 'rxjs';
|
|
134
|
+
* import Api from '@pezkuwi/api/rx';
|
|
135
|
+
*
|
|
136
|
+
* Api.create()
|
|
137
|
+
* .pipe(
|
|
138
|
+
* switchMap((api) =>
|
|
139
|
+
* api.rpc.chain.subscribeNewHeads()
|
|
140
|
+
* ))
|
|
141
|
+
* .subscribe((header) => {
|
|
142
|
+
* console.log(`new block #${header.number.toNumber()}`);
|
|
143
|
+
* });
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
static create(options?: ApiOptions): Observable<ApiRx>;
|
|
147
|
+
/**
|
|
148
|
+
* @description Observable that returns the first time we are connected and loaded
|
|
149
|
+
*/
|
|
150
|
+
get isReady(): Observable<ApiRx>;
|
|
151
|
+
/**
|
|
152
|
+
* @description Returns a clone of this ApiRx instance (new underlying provider connection)
|
|
153
|
+
*/
|
|
154
|
+
clone(): ApiRx;
|
|
155
|
+
}
|