@5ire/api 中文文档教程
@polkadot/api
Polkadot-JS API 围绕从应用程序流向节点的 JSONRPC 调用提供易于使用的包装器。 它处理所有编码和解码或参数,提供对 RPC 函数的访问,并允许查询链状态和提交交易。
API 包装器提供了一个标准接口供使用 - 提供了
- A static
.create(<optional ApiOptions>)
that returns an API instance when connected, decorated and ready-to use. ApiOptions can include an optional WsProvider and optional custom type definitions{ provider: <Optional WsProvider>, types: <Optional RegistryTypes> }
. - The above is just a wrapper for
new Api(<optional ApiOptions>)
, exposing theisReady
getter api.rpc.<section>.<method>
provides access to actual RPC calls, be it for queries, submission or retrieving chain information- RPC (node interface)
api.query.<section>.<method>
provides access to chain state queries. These are dynamically populated based on what the runtime provides- Storage chain state (runtime node interface)
api.tx.<section>.<method>
provides the ability to create a transaction, like chain state, this list is populated from a runtime query- Extrinsics (runtime node interface)
api.consts.<section>.<constant>
provides access to the module constants (parameter types).- Constants (runtime node interface)
API Selection
两种类型的 API,一种允许通过 JavaScript Promises 的标准接口,第二种使用 RxJS. 根据您的用例和熟悉程度,您可以为您的应用程序选择其中之一(甚至两者)。
- [[ApiPromise]] All interface calls returns Promises, including the static
.create(...)
. Additionally any subscription method uses(value) => {}
callbacks, returning the value as the subscription is updated. - [[ApiRx]] All interface calls return RxJS Observables, including the static
.create(...)
. In the same fashion subscription-based methods return long-running Observables that update with the latest values.
Dynamic by default
Substrate(Polkadot 建立在其上)使用链上 WASM 运行时,允许升级。 每个运行时定义实际的链外部(提交的事务和块内部)以及链状态中的可用条目。 因此,查询和交易的 API 端点是从正在运行的链中动态填充的。
由于这种动态特性,此 API 不同于仅具有固定端点的传统 API,驱动运行时可用内容的使用。 作为开始,这种通用性质有一个学习曲线,尽管提供的文档、示例和链接文档试图使这种体验尽可能无缝。
Installation & import
安装 -
npm install --save @polkadot/api
通过基于 Promise 的 API
import { ApiPromise } from '@5ire/api';
// initialise via static create
const api = await ApiPromise.create();
// make a call to retrieve the current network head
api.rpc.chain.subscribeNewHeads((header) => {
console.log(`Chain is at #${header.number}`);
});
订阅块 - 通过基于 RxJS 的 API 订阅块 -
import { ApiRx } from '@5ire/api';
// initialise via static create
const api = await ApiRx.create().toPromise();
// make a call to retrieve the current network head
api.rpc.chain.subscribeNewHeads().subscribe((header) => {
console.log(`Chain is at #${header.number}`);
});
Registering custom types
当创建 API 的新实例时,可以添加运行时模块使用的其他类型。 如果运行时模块使用基本 Substrate 运行时中不可用的类型,则这是必需的。
import { ApiPromise } from '@5ire/api';
// initialise via static create and register custom types
const api = await ApiPromise.create({
types: {
CustomTypesExample: {
"id": "u32",
"data": "Vec<u8>",
"deposit": "Balance",
"owner": "AccountId",
"application_expiry": "Moment",
"whitelisted": "bool",
"challenge_id": "u32"
}
}
});
Users
API 的一些用户(如果您不在列表中,请告诉我们)包括 -
- Polkadot-JS UI A user-interface that allows you to make transactions, query the network or participate in actions on the network such as referendums and staking
- KodaDot (twitter) - Vue.js web wallet, governance dashboard and aspiring performance (lightweight) alternative to original apps, mobile-first.
- Polkabot Polkabot is a Matrix chatbot that keeps an eye on the Polkadot network. You can see Polkabot in action in https://matrix.to/#/#polkadot-network-status:matrix.org
- Polkawallet.io and Polkawallet (Github) A mobile wallet for the Polkadot network to manage funds and make transactions, available on both Androind and iOS
- PolkaStats.io, PolkaStats frontend GitHub repository and PolkaStats backend GitHub repository Polkadot network statistics (currently Kusama and Alexander). Shows network information and staking details from validators and intentions.
- Polkadot API Server (GitHub) A lightweight server for querying Polkadot nodes from any language, built primarily as a backend for PANIC for Polkadot (GitHub), a validator monitoring and alerting tool.
- Identity Registrar #1 from Chevdor on Westend, Kusama and Polkadot
@polkadot/api
The Polkadot-JS API provides easy-to-use wrappers around JSONRPC calls that flow from an application to a node. It handles all the encoding and decoding or parameters, provides access to RPC functions and allows for the query of chain state and the submission of transactions.
The API wrappers provide a standard interface for use -
- A static
.create(<optional ApiOptions>)
that returns an API instance when connected, decorated and ready-to use. ApiOptions can include an optional WsProvider and optional custom type definitions{ provider: <Optional WsProvider>, types: <Optional RegistryTypes> }
. - The above is just a wrapper for
new Api(<optional ApiOptions>)
, exposing theisReady
getter api.rpc.<section>.<method>
provides access to actual RPC calls, be it for queries, submission or retrieving chain information- RPC (node interface)
api.query.<section>.<method>
provides access to chain state queries. These are dynamically populated based on what the runtime provides- Storage chain state (runtime node interface)
api.tx.<section>.<method>
provides the ability to create a transaction, like chain state, this list is populated from a runtime query- Extrinsics (runtime node interface)
api.consts.<section>.<constant>
provides access to the module constants (parameter types).- Constants (runtime node interface)
API Selection
There are two flavours of the API provided, one allowing a standard interface via JavaScript Promises and the second provides an Observable wrapper using RxJS. Depending on your use-case and familiarity, you can choose either (or even both) for your application.
- [[ApiPromise]] All interface calls returns Promises, including the static
.create(...)
. Additionally any subscription method uses(value) => {}
callbacks, returning the value as the subscription is updated. - [[ApiRx]] All interface calls return RxJS Observables, including the static
.create(...)
. In the same fashion subscription-based methods return long-running Observables that update with the latest values.
Dynamic by default
Substrate (upon which Polkadot is built) uses on-chain WASM runtimes, allowing for upgradability. Each runtime defining the actual chain extrinsics (submitted transactions and block intrinsics) as well as available entries in the chain state. Due to this, the API endpoints for queries and transactions are dynamically populated from the running chain.
Due to this dynamic nature, this API departs from traditional APIs which only has fixed endpoints, driving use by what is available by the runtime. As a start, this generic nature has a learning curve, although the provided documentation, examples and linked documentation tries to make that experience as seamless as possible.
Installation & import
Installation -
npm install --save @polkadot/api
Subscribing to blocks via Promise-based API -
import { ApiPromise } from '@5ire/api';
// initialise via static create
const api = await ApiPromise.create();
// make a call to retrieve the current network head
api.rpc.chain.subscribeNewHeads((header) => {
console.log(`Chain is at #${header.number}`);
});
Subscribing to blocks via RxJS-based API -
import { ApiRx } from '@5ire/api';
// initialise via static create
const api = await ApiRx.create().toPromise();
// make a call to retrieve the current network head
api.rpc.chain.subscribeNewHeads().subscribe((header) => {
console.log(`Chain is at #${header.number}`);
});
Registering custom types
Additional types used by runtime modules can be added when a new instance of the API is created. This is necessary if the runtime modules use types which are not available in the base Substrate runtime.
import { ApiPromise } from '@5ire/api';
// initialise via static create and register custom types
const api = await ApiPromise.create({
types: {
CustomTypesExample: {
"id": "u32",
"data": "Vec<u8>",
"deposit": "Balance",
"owner": "AccountId",
"application_expiry": "Moment",
"whitelisted": "bool",
"challenge_id": "u32"
}
}
});
Users
Some of the users of the API (let us know if you are missing from the list), include -
- Polkadot-JS UI A user-interface that allows you to make transactions, query the network or participate in actions on the network such as referendums and staking
- KodaDot (twitter) - Vue.js web wallet, governance dashboard and aspiring performance (lightweight) alternative to original apps, mobile-first.
- Polkabot Polkabot is a Matrix chatbot that keeps an eye on the Polkadot network. You can see Polkabot in action in https://matrix.to/#/#polkadot-network-status:matrix.org
- Polkawallet.io and Polkawallet (Github) A mobile wallet for the Polkadot network to manage funds and make transactions, available on both Androind and iOS
- PolkaStats.io, PolkaStats frontend GitHub repository and PolkaStats backend GitHub repository Polkadot network statistics (currently Kusama and Alexander). Shows network information and staking details from validators and intentions.
- Polkadot API Server (GitHub) A lightweight server for querying Polkadot nodes from any language, built primarily as a backend for PANIC for Polkadot (GitHub), a validator monitoring and alerting tool.
- Identity Registrar #1 from Chevdor on Westend, Kusama and Polkadot
你可能也喜欢
- @01ht/ht-elements-item-youtube-preview 中文文档教程
- @0xcert/ethereum-zxc 中文文档教程
- @168-cli/command 中文文档教程
- @2012mjm/telegram.link 中文文档教程
- @4react/store 中文文档教程
- @6edesign/express-restify-mongoose 中文文档教程
- @aamodtgroup/block-processors 中文文档教程
- @abraaobarros/react-native-bynd-chat 中文文档教程
- @ac-moore-inc/oracledb-wrapper 中文文档教程
- @acai/collections 中文文档教程