JSPM

@polkadot/rpc-core

3.7.4-6
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 154082
  • Score
    100M100P100Q167957F
  • License Apache-2.0

A JavaScript wrapper for the Polkadot JsonRPC interface

Package Exports

  • @polkadot/rpc-core
  • @polkadot/rpc-core/README.md
  • @polkadot/rpc-core/index.d.ts
  • @polkadot/rpc-core/package-info.json
  • @polkadot/rpc-core/package.json
  • @polkadot/rpc-core/types
  • @polkadot/rpc-core/types.d.ts
  • @polkadot/rpc-core/types.jsonrpc.d.ts
  • @polkadot/rpc-core/typesonrpc.js
  • @polkadot/rpc-core/util
  • @polkadot/rpc-core/util/drr
  • @polkadot/rpc-core/util/drr.d.ts
  • @polkadot/rpc-core/util/index.d.ts
  • @polkadot/rpc-core/util/refCountDelay
  • @polkadot/rpc-core/util/refCountDelay.d.ts

Readme

@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

Initialization -

import Rpc from '@polkadot/rpc-core';
import { WsProvider } from '@polkadot/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) -

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) -

rpc.chain
  .getHead()
  .pipe(
    switchMap((headerHash) => {
      return rpc.chain.getHeader(headerHash);
    })
  )
  .subscribe(
    (header) => {
      console.log(`best #${header.number.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()}`);
    },
    (error) => {
      console.error('error:', error);
    }
  );

Retrieving best header via subscription -

api.chain
rpc
  .subscribeNewHeads()
  .subscribe(
    (header) => {
      console.log(`best #${header.number}`);
    },
    (error) => {
      console.error('error subscribing:', error);
    }
  );