* fix: Remove docs/api since replaced with docs/rpc-core * fix: Remove docs/api-provider since replaced with docs/rpc-provider * fix: Remove docs/api-rx since replaced with docs/rpc-rx * fix: Remove the guide file that serves no purpose * fix: Change license from ui-observable to api-observable * fix: Remove disused mockHttp and mockWs that are duplicates of those in rpc-provider * fix: Clarifies difference between PropIndex and ProposalIndex from Issue #226 * fix: Update api-observable Readme to refer to rpc-core, previously called api * misc: Update Readme of rpc-core. See commit message details. * Update name to be @polkadot/rpc-core instead of @polkadot/api * Use Yarn instead of NPM for instructions adding dependencies * Refer to @polkadot/rpc-core as `Rpc` * Highlight that given block header hash should be a 0x-prefixed hex string with length of 64 (not only length of 10 as shown in the example) * Showcase more information that you can obtain from the header object (i.e. best block number, parentHash, stateRoot, extrinsicsRoot, and digest) * Use `header.raw` instead of just `header` to access properties of the header object * Update the example to console.log the subscription ID * Add missing semicolon * fix: Fix name in license header and rename Api to Rpc for rpc-core e2e tests * fix: Update rpc-provider Readme to use Yarn instead of NPM install * fix: Fix minor typo * fix: Rename Api to Rpc for rpc-provider ws * fix: Refer to rpc-core instead of api in the rpc-rx Readme * fix: Update rpc-rx files to refer to it as RpcRx instead of RxApi or just Api * fix: Refer to rpc-core as Rpc instead of Api * fix: Update type-jsonrpc Readme. Fix typo. Use Yarn not NPM. Refer to TSLint not Flow * refactor: Move non-type dependency import with non-type dependencies * docs: Add link to parity-codec repo * fix: Avoid use of .raw. Fix listen to blocks example * fix: Add missing error to newHead in listen to blocks example
2.9 KiB
2.9 KiB
@polkadot/rpc-core
This library provides a clean wrapper around all the methods exposed by a Polkadot network client.
Usage
Installation -
yarn add @polkadot/rpc-core
Initialisation -
import Rpc from '@polkadot/rpc-core';
import WsProvider from '@polkadot/rpc-provider/ws';
const provider = new WsProvider('http://127.0.0.1:9944');
const api = new Rpc(provider);
Retrieving the block header object for a given block header hash (a 0x-prefixed hex string with length of 64) -
api.chain
.getHeader('0x1234567890')
.then((header) => console.log(header))
.catch((error) => console.error(error));
Retrieving the best block number, parent hash, state root hash, extrinsics root hash, and digest (once-off) -
api.chain
.getHead()
.then((headerHash) => {
return api.chain.getHeader(headerHash);
})
.then((header) => {
console.log(`best #${header.blockNumber.toString()}`);
console.log(`parentHash: ${header.parentHash.toString()}`);
console.log(`stateRoot: ${header.stateRoot.toString()}`);
console.log(`extrinsicsRoot: ${header.extrinsicsRoot.toString()}`);
console.log(`digest: ${header.digest.toString()}`);
})
.catch((error) => {
console.error('error:', error);
});
Retrieving best header via subscription -
api.chain
.newHead((error, header) => {
if (error) {
console.error('error:', error);
}
console.log(`best #${header.blockNumber.toString()}`);
})
.then((subscriptionId) => {
console.log(`subscriptionId: ${subscriptionId}`);
// id for the subscription, can unsubscribe via
// api.chain.newHead.unsubscribe(subscriptionId);
})
.catch((error) => {
console.error('error subscribing:', error);
});