A JavaScript wrapper for the Plugnet JsonRPC interface
npm install @plugnet/rpc-coreThis library provides a clean wrapper around all the methods exposed by a Plugnet network client.
Installation -
```
yarn add @plugnet/rpc-core
Initialisation -
`js
import Rpc from '@plugnet/rpc-core';
import WsProvider from '@plugnet/rpc-provider/ws';
const provider = new WsProvider('http://127.0.0.1:9944');
const rpc = new Rpc(provider);
`
Retrieving the block header object for a given block header hash (a 0x-prefixed hex string with length of 64) -
`js`
rpc.chain
.getHeader('0x1234567890')
.subscribe(
(header) => console.log(header),
(error) => console.error('error:', error)
);
Retrieving the best block number, parent hash, state root hash, extrinsics root hash, and digest (once-off) -
`jsbest #${header.blockNumber.toString()}
rpc.chain
.getHead()
.pipe(
switchMap((headerHash) => {
return rpc.chain.getHeader(headerHash);
})
)
.subscribe(
(header) => {
console.log();parentHash: ${header.parentHash.toString()}
console.log();stateRoot: ${header.stateRoot.toString()}
console.log();extrinsicsRoot: ${header.extrinsicsRoot.toString()}
console.log();digest: ${header.digest.toString()}
console.log();`
},
(error) => {
console.error('error:', error);
}
);
Retrieving best header via subscription -
`jsbest #${header.blockNumber}
api.chain
rpc
.subscribeNewHeads()
.subscribe(
(header) => {
console.log();``
},
(error) => {
console.error('error subscribing:', error);
}
);