1 | import type { Observable } from 'rxjs';
|
2 | import type { ApiOptions } from '../types/index.js';
|
3 | import { ApiBase } from '../base/index.js';
|
4 | /**
|
5 | * # @polkadot/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 Polkadot 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 Polkadot (and Substrate) 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 '@polkadot/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 '@polkadot/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 '@polkadot/api/rx';
|
71 | *
|
72 | * // import the test keyring (already has dev keys for Alice, Bob, Charlie, Eve & Ferdie)
|
73 | * import testingPairs from '@polkadot/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 '@polkadot/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 '@polkadot/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 | }
|