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 | * # @polkadot/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 Polkadot 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 Polkadot (and Substrate) clients.
|
14 | *
|
15 | * @see [[ApiRx]]
|
16 | *
|
17 | * ## Usage
|
18 | *
|
19 | * Making rpc calls -
|
20 | * <BR>
|
21 | *
|
22 | * ```javascript
|
23 | * import ApiPromise from '@polkadot/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 '@polkadot/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 '@polkadot/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 '@polkadot/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 '@polkadot/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 | }
|