UNPKG

6.34 kBJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/api authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { assertReturn } from '@polkadot/util';
4import { packageInfo } from "../packageInfo.js";
5import { findCall, findError } from "./find.js";
6import { Init } from "./Init.js";
7
8function assertResult(value) {
9 return assertReturn(value, 'Api needs to be initialized before using, listen on \'ready\'');
10}
11
12export class Getters extends Init {
13 /**
14 * @description Contains the parameter types (constants) of all modules.
15 *
16 * The values are instances of the appropriate type and are accessible using `section`.`constantName`,
17 *
18 * @example
19 * <BR>
20 *
21 * ```javascript
22 * console.log(api.consts.democracy.enactmentPeriod.toString())
23 * ```
24 */
25 get consts() {
26 return assertResult(this._consts);
27 }
28 /**
29 * @description Derived results that are injected into the API, allowing for combinations of various query results.
30 *
31 * @example
32 * <BR>
33 *
34 * ```javascript
35 * api.derive.chain.bestNumber((number) => {
36 * console.log('best number', number);
37 * });
38 * ```
39 */
40
41
42 get derive() {
43 return assertResult(this._derive);
44 }
45 /**
46 * @description Errors from metadata
47 */
48
49
50 get errors() {
51 return assertResult(this._errors);
52 }
53 /**
54 * @description Events from metadata
55 */
56
57
58 get events() {
59 return assertResult(this._events);
60 }
61 /**
62 * @description Returns the version of extrinsics in-use on this chain
63 */
64
65
66 get extrinsicVersion() {
67 return this._extrinsicType;
68 }
69 /**
70 * @description Contains the genesis Hash of the attached chain. Apart from being useful to determine the actual chain, it can also be used to sign immortal transactions.
71 */
72
73
74 get genesisHash() {
75 return assertResult(this._genesisHash);
76 }
77 /**
78 * @description true is the underlying provider is connected
79 */
80
81
82 get isConnected() {
83 return this._isConnected.getValue();
84 }
85 /**
86 * @description The library information name & version (from package.json)
87 */
88
89
90 get libraryInfo() {
91 return `${packageInfo.name} v${packageInfo.version}`;
92 }
93 /**
94 * @description Contains all the chain state modules and their subsequent methods in the API. These are attached dynamically from the runtime metadata.
95 *
96 * All calls inside the namespace, is denoted by `section`.`method` and may take an optional query parameter. As an example, `api.query.timestamp.now()` (current block timestamp) does not take parameters, while `api.query.system.account(<accountId>)` (retrieving the associated nonce & balances for an account), takes the `AccountId` as a parameter.
97 *
98 * @example
99 * <BR>
100 *
101 * ```javascript
102 * api.query.system.account(<accountId>, ([nonce, balance]) => {
103 * console.log('new free balance', balance.free, 'new nonce', nonce);
104 * });
105 * ```
106 */
107
108
109 get query() {
110 return assertResult(this._query);
111 }
112 /**
113 * @description Allows for the querying of multiple storage entries and the combination thereof into a single result. This is a very optimal way to make multiple queries since it only makes a single connection to the node and retrieves the data over one subscription.
114 *
115 * @example
116 * <BR>
117 *
118 * ```javascript
119 * const unsub = await api.queryMulti(
120 * [
121 * // you can include the storage without any parameters
122 * api.query.balances.totalIssuance,
123 * // or you can pass parameters to the storage query
124 * [api.query.system.account, '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY']
125 * ],
126 * ([existential, [, { free }]]) => {
127 * console.log(`You have ${free.sub(existential)} more than the existential deposit`);
128 *
129 * unsub();
130 * }
131 * );
132 * ```
133 */
134
135
136 get queryMulti() {
137 return assertResult(this._queryMulti);
138 }
139 /**
140 * @description Contains all the raw rpc sections and their subsequent methods in the API as defined by the jsonrpc interface definitions. Unlike the dynamic `api.query` and `api.tx` sections, these methods are fixed (although extensible with node upgrades) and not determined by the runtime.
141 *
142 * RPC endpoints available here allow for the query of chain, node and system information, in addition to providing interfaces for the raw queries of state (using known keys) and the submission of transactions.
143 *
144 * @example
145 * <BR>
146 *
147 * ```javascript
148 * api.rpc.chain.subscribeNewHeads((header) => {
149 * console.log('new header', header);
150 * });
151 * ```
152 */
153
154
155 get rpc() {
156 return assertResult(this._rpc);
157 }
158 /**
159 * @description Contains the chain information for the current node.
160 */
161
162
163 get runtimeChain() {
164 return assertResult(this._runtimeChain);
165 }
166 /**
167 * @description Yields the current attached runtime metadata. Generally this is only used to construct extrinsics & storage, but is useful for current runtime inspection.
168 */
169
170
171 get runtimeMetadata() {
172 return assertResult(this._runtimeMetadata);
173 }
174 /**
175 * @description Contains the version information for the current runtime.
176 */
177
178
179 get runtimeVersion() {
180 return assertResult(this._runtimeVersion);
181 }
182 /**
183 * @description The underlying Rx API interface
184 */
185
186
187 get rx() {
188 return assertResult(this._rx);
189 }
190 /**
191 * @description The type of this API instance, either 'rxjs' or 'promise'
192 */
193
194
195 get type() {
196 return this._type;
197 }
198 /**
199 * @description Contains all the extrinsic modules and their subsequent methods in the API. It allows for the construction of transactions and the submission thereof. These are attached dynamically from the runtime metadata.
200 *
201 * @example
202 * <BR>
203 *
204 * ```javascript
205 * api.tx.balances
206 * .transfer(<recipientId>, <balance>)
207 * .signAndSend(<keyPair>, ({status}) => {
208 * console.log('tx status', status.asFinalized.toHex());
209 * });
210 * ```
211 */
212
213
214 get tx() {
215 return assertResult(this._extrinsics);
216 }
217 /**
218 * @description Finds the definition for a specific [[CallFunction]] based on the index supplied
219 */
220
221
222 findCall(callIndex) {
223 return findCall(this.registry, callIndex);
224 }
225 /**
226 * @description Finds the definition for a specific [[RegistryError]] based on the index supplied
227 */
228
229
230 findError(errorIndex) {
231 return findError(this.registry, errorIndex);
232 }
233
234}
\No newline at end of file