How to use @polkadot/rpc-core - 10 common examples

To help you get started, we’ve selected a few @polkadot/rpc-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github polkadot-js / api / docs / examples / deprecated-rpc / 06_craft_extrinsic / index.js View on Github external
const BN = require('bn.js');
const Rpc = require('@polkadot/rpc-core').default;
const WsProvider = require('@polkadot/rpc-provider/ws').default;
const extrinsics = require('@polkadot/extrinsics').default;
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode').default;
const encodeLength = require('@polkadot/extrinsics/codec/encode/length').default;
const Keyring = require('@polkadot/keyring').default;
const storage = require('@polkadot/storage').default;

const Encoder = new TextEncoder();
const keyring = new Keyring();
const provider = new WsProvider('ws://127.0.0.1:9944');
const api = new Rpc(provider);

async function getAccountIndex (address) {
  return api.state.getStorage([storage.system.public.accountIndexOf, address]);
}

async function transfer (keyRingFrom, addressTo, amount) {
  const accountIndex = await getAccountIndex(keyRingFrom.address());

  console.log(`Current accountIndex: ${accountIndex}`);

  // encode the call for signing
  const message = encodeExtrinsic(
    keyRingFrom.publicKey(),
    accountIndex,
    extrinsics.staking.public.transfer,
    [addressTo, amount]
github polkadot-js / api / docs / examples / deprecated-rpc / 02_listen_to_blocks / index.js View on Github external
const Rpc = require('@polkadot/rpc-core').default;
const WsProvider = require('@polkadot/rpc-provider/ws').default;
const provider = new WsProvider('ws://127.0.0.1:9944');
const api = new Rpc(provider);

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);
github polkadot-js / api / docs / examples / deprecated-rpc / 07_transfer_dots / index.js View on Github external
const BN = require('bn.js');
const Rpc = require('@polkadot/rpc-core').default;
const WsProvider = require('@polkadot/rpc-provider/ws').default;
const extrinsics = require('@polkadot/extrinsics').default;
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode/uncheckedLength').default;
const Keyring = require('@polkadot/keyring').default;
const storage = require('@polkadot/storage').default;

const Encoder = new TextEncoder();
const keyring = new Keyring();
const provider = new WsProvider('ws://127.0.0.1:9944');
const api = new Rpc(provider);

async function getAccountIndex (address) {
  return api.state.getStorage([storage.system.public.accountIndexOf, address]);
}

async function transfer (keyRingFrom, addressTo, amount) {
  const nextAccountIndex = await getAccountIndex(keyRingFrom.address());

  // encode the call for signing
  const encoded = encodeExtrinsic(
    keyRingFrom,
    nextAccountIndex,
    extrinsics.staking.public.transfer,
    [addressTo, amount]
  );
github polkadot-js / api / docs / examples / deprecated-rpc / 01_simple_connect / index.js View on Github external
const Rpc = require('@polkadot/rpc-core').default;
const WsProvider = require('@polkadot/rpc-provider/ws').default;
const provider = new WsProvider('ws://127.0.0.1:9944');
const api = new Rpc(provider);

async function getChain () {
  return api.system.chain();
}

async function main () {
  const chain = await getChain();

  console.log('You are connected to chain:', chain);
}

main().finally(_ => process.exit());
github polkadot-js / api / docs / examples / deprecated-rpc / 03_listen_to_balance_change / index.js View on Github external
const BN = require('bn.js');
const Rpc = require('@polkadot/rpc-core').default;
const WsProvider = require('@polkadot/rpc-provider/ws').default;
const storage = require('@polkadot/storage').default;
const provider = new WsProvider('ws://127.0.0.1:9944');
const api = new Rpc(provider);

const Alice = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ';

console.log('You may leave this example running and start example 06 or send DOTs to ' + Alice);

api.state
  .storage([[storage.staking.public.freeBalanceOf, Alice]], (_, values) => {
    console.log(`Balance of ${Alice}: ${new BN(values[0]).toString(10)} DOTs`);
  })
  .then((subscriptionId) => {
    console.log('Balance changes subscription id:', subscriptionId);
  });
github polkadot-js / api / docs / examples / deprecated-rpc / 05_read_storage / index.js View on Github external
const BN = require('bn.js');
const Rpc = require('@polkadot/rpc-core').default;
const WsProvider = require('@polkadot/rpc-provider/ws').default;
const storage = require('@polkadot/storage').default;

const provider = new WsProvider('ws://127.0.0.1:9944');
const api = new Rpc(provider);

async function getAccountIndex (address) {
  return api.state.getStorage([storage.system.public.accountIndexOf, address]);
}

async function getValidators () {
  return api.state.getStorage([storage.session.public.validators]);
}

async function getBlockPeriod () {
  return api.state.getStorage([storage.timestamp.public.blockPeriod]);
}

async function main () {
  const address = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ';
  const validators = await getValidators();
github polkadot-js / api / packages / rpc-rx / src / index.ts View on Github external
constructor (providerOrRpc?: Rpc | ProviderInterface) {
    this._api = providerOrRpc instanceof Rpc
      ? providerOrRpc
      : new Rpc(providerOrRpc);
    this._eventemitter = new EventEmitter();
    this._isConnected = new BehaviorSubject(this._api._provider.isConnected());

    this.initEmitters(this._api._provider);

    this.author = this.createInterface(this._api.author);
    this.chain = this.createInterface(this._api.chain);
    this.state = this.createInterface(this._api.state);
    this.system = this.createInterface(this._api.system);
  }
github polkadot-js / api / packages / api / src / base / Decorate.ts View on Github external
constructor (options: ApiOptions, type: ApiTypes, decorateMethod: DecorateMethod) {
    super();

    this.registry = options.registry || new TypeRegistry();

    const thisProvider = options.source
      ? options.source._rpcCore.provider.clone()
      : (options.provider || new WsProvider());

    this.decorateMethod = decorateMethod;
    this._options = options;
    this._type = type;
    this._rpcCore = new RpcCore(this.registry, thisProvider, this._options.rpc);
    this._isConnected = new BehaviorSubject(this._rpcCore.provider.isConnected());
    this._rx.hasSubscriptions = this._rpcCore.provider.hasSubscriptions;
    this._rx.registry = this.registry;
  }
github polkadot-js / api / packages / rpc-rx / src / index.ts View on Github external
constructor (providerOrRpc?: Rpc | ProviderInterface) {
    this._api = providerOrRpc instanceof Rpc
      ? providerOrRpc
      : new Rpc(providerOrRpc);
    this._cacheMap = {};
    this._eventemitter = new EventEmitter();
    this._isConnected = new BehaviorSubject(this._api._provider.isConnected());

    this.initEmitters(this._api._provider);

    this.author = this.createInterface('author', this._api.author);
    this.chain = this.createInterface('chain', this._api.chain);
    this.state = this.createInterface('state', this._api.state);
    this.system = this.createInterface('system', this._api.system);
  }
github polkadot-js / api / packages / api-derive / src / util / memo.ts View on Github external
(...params: any[]): Observable =>
      Observable.create((observer: Observer): VoidCallback => {
        const sub = inner(...params).subscribe(observer);

        return (): void => {
          cached.delete(...params);
          sub.unsubscribe();
        };
      }).pipe(drr()),
    { normalizer }

@polkadot/rpc-core

A JavaScript wrapper for the Polkadot JsonRPC interface

Apache-2.0
Latest version published 7 days ago

Package Health Score

90 / 100
Full package analysis

Similar packages