description: ApiPromise is a standard JavaScript wrapper around the RPC and interfaces on the Polkadot network. As a full Promise-based, all interface calls return Promises, including the static .create(...). Subscription calls utilise standard JavaScript-convention (error, value) callbacks.

The API is well suited to real-time applications where either the single-shot state is needed or use is to be made of athe subscription-based features of Polkadot (and Substrate) clients.

example:

Making rpc calls -

import ApiPromise from '@polkadot/api/promise';

// initialise via static create
const api = await ApiPromise.create();

// make a subscription to the network head
api.rpc.chain.newHead((error, header) => {
  console.log(`Chain is at #${header.blockNumber}`);
});

Subscribing to chain state -

import { ApiPromise } from '@polkadot/api';
import WsProvider from '@polkadot/rpc-provider/ws';

// initialise via isReady & new with specific non-local endpoint
const api = await new ApiPromise(new WsProvider('wss://example.com:9944')).isReady;

// retrieve the block target time
const blockPeriod = await api.st.timestamp.blockPeriod().toNumber();
let last = 0;

// subscribe to the current block timestamp, updates automatically
api.st.timestamp.now((error, timestamp) => {
  const elapsed = last
    ? `, ${timestamp.toNumber() - last}s since last`
    : '';

  last = timestamp.toNumber();
  console.log(`timestamp ${timestamp}${elapsed} (${blockPeriod}s target)`);
});

Submitting a transaction -

import ApiPromise from '@polkadot/api/promise';

ApiPromise.create().then((api) => {
  const nonce = await api.st.system.accountNonce(keyring.alice.address());

  api.tx.balances
    // create transfer
    transfer(keyring.bob.address(), 12345)
    // sign the transcation
    .sign(keyring.alice, nonce)
    // send the transaction
    .send()
    // retrieve the overall result
    .then((hash) => {
      console.log(`submitted with hash ${hash}`);
    });
});

Hierarchy

ApiBase<Rpc, QueryableStorage, SubmittableExtrinsics>

↳ ApiPromise

Implements

  • ApiBaseInterface<Rpc, QueryableStorage, SubmittableExtrinsics>
  • ApiPromiseInterface

Constructors

constructor

⊕ new ApiPromise(wsProvider?: WsProvider): ApiPromise

Overrides ApiBase.constructor

Defined in promise/index.ts:113

example:

import Api from '@polkadot/api/promise';

new Api().isReady.then((api) => {
  api.rpc.newHead((error, header) => {
    console.log(`new block #${header.blockNumber.toNumber()}`);
  });
});

Parameters:

Param Type Description
Optional wsProvider WsProvider An optional WebSocket provider from rpc-provider/ws. If not specified, it will default to connecting to the localhost with the default port

Returns: ApiPromise


Accessors

genesisHash

getgenesisHash(): Hash

Inherited from ApiBase.genesisHash

Defined in Base.ts:68

description: Contains the genesis Hash of the attached chain. Apart from being useful to determine the actual chain, it can also be used to sign immortal transactions.

Returns: Hash


isReady

getisReady(): Promise<ApiPromise>

Defined in promise/index.ts:143

description: Promise that returns the first time we are connected and loaded

Returns: Promise<ApiPromise>


rpc

getrpc(): Rpc

Inherited from ApiBase.rpc

Defined in Base.ts:105

description: Contains all the raw rpc sections and their subsequent methods in the API as defined by the jsonrpc interface definitions.

example:

api.rpc.chain
  .newHead()
  .subscribe((header) => {
    console.log('new header', header);
  });

Returns: Rpc


runtimeMetadata

getruntimeMetadata(): RuntimeMetadata

Inherited from ApiBase.runtimeMetadata

Defined in Base.ts:77

description: Yields the current attached runtime metadata. Generally this is only used to construct extrinsics & storage, but is useful for current runtime inspection.

Returns: RuntimeMetadata


runtimeVersion

getruntimeVersion(): RuntimeVersion

Inherited from ApiBase.runtimeVersion

Defined in Base.ts:86

description: Contains the version information for the current runtime.

Returns: RuntimeVersion


st

getst(): QueryableStorage

Inherited from ApiBase.st

Defined in Base.ts:122

description: Contains all the chain state modules and their subsequent methods in the API. These are attached dynamically from the runtime metadata.

example:

api.st.balances
  .freeBalance(<accountId>)
  .subscribe((balance) => {
    console.log('new balance', balance);
  });

Returns: QueryableStorage


tx

gettx(): SubmittableExtrinsics

Inherited from ApiBase.tx

Defined in Base.ts:143

description: Contains all the extrinsic modules and their subsequent methods in the API. It allows for the construction of transactions and the submission thereof. These are attached dynamically from the runtime metadata.

example:

api.tx.balances
  .transfer(<recipientId>, <balance>)
  .sign(<keyPair>, <accountNonce>, <blockHash (optional)>)
  .send()
  .subscribe((status) => {
    console.log('tx status', status);
  });

Returns: SubmittableExtrinsics


Methods

on

▸ on(type: ApiInterface$Events, handler: function): void

Inherited from ApiBase.on

Defined in Base.ts:149

Parameters:

Param Type
type ApiInterface$Events
handler function

Returns: void


<Static> create

▸ create(wsProvider?: WsProvider): Promise<ApiPromise>

Defined in promise/index.ts:111

description: Creates an ApiPromise instance using the supplied provider. Returns an Promise containing the actual Api instance.

example:

import Api from '@polkadot/api/promise';

Api.create().then(async (api) => {
  const timestamp = await api.st.timestamp.now();

  console.log(`lastest block timestamp ${timestamp}`);
});

Parameters:

Param Type Description
Optional wsProvider WsProvider Optional WebSocket provider that is passed to the class contructor

Returns: Promise<ApiPromise>


results matching ""

    No results matching ""