Class LegacyClient<ChainApi>

LegacyClient

Promised-based API Client for Polkadot & Substrate

Initialize API instance and interact with substrate-based network

import { Dedot } from 'dedot';
import type { PolkadotApi } from '@dedot/chaintypes/polkadot';

const run = async () => {
const api = await LegacyClient.new<PolkadotApi>('wss://rpc.polkadot.io');

// Call rpc `state_getMetadata` to fetch raw scale-encoded metadata and decode it.
const metadata = await api.rpc.state.getMetadata();
console.log('Metadata:', metadata);

// Query on-chain storage
const address = '14...';
const balance = await api.query.system.account(address);
console.log('Balance:', balance);


// Subscribe to on-chain storage changes
const unsub = await api.query.system.number((blockNumber) => {
console.log(`Current block number: ${blockNumber}`);
});

// Get pallet constants
const ss58Prefix = api.consts.system.ss58Prefix;
console.log('Polkadot ss58Prefix:', ss58Prefix)

// await unsub();
// await api.disconnect();
}

run().catch(console.error);

Type Parameters

Hierarchy

Constructors

Properties

_genesisHash?: `0x${string}`
_localCache?: IStorage
_metadata?: Metadata
_options: ApiOptions
_registry?: PortableRegistry
_runtimeUpgrading?: Deferred<void>
_runtimeVersion?: SubstrateRuntimeVersion
onConnected: (() => Promise<void>)
onDisconnected: (() => Promise<void>)
rpcVersion: RpcVersion

Accessors

  • get call(): ChainApi["legacy"]["call"]
  • Returns ChainApi["legacy"]["call"]

    Entry-point for executing runtime api

    // Get account nonce
    const nonce = await api.call.accountNonceApi.accountNonce(<address>);

    // Query transaction payment info
    const tx = api.tx.balances.transferKeepAlive(<address>, 2_000_000_000_000n);
    const queryInfo = await api.call.transactionPaymentApi.queryInfo(tx.toU8a(), tx.length);
  • get consts(): ChainApi["legacy"]["consts"]
  • Returns ChainApi["legacy"]["consts"]

    Entry-point for inspecting constants (parameter types) for all pallets (modules).

    const ss58Prefix = api.consts.system.ss58Prefix;
    console.log('ss58Prefix:', ss58Prefix)
  • get currentMetadataKey(): string
  • Returns string

  • get errors(): ChainApi["legacy"]["errors"]
  • Returns ChainApi["legacy"]["errors"]

    Entry-point for inspecting errors from metadata

  • get events(): ChainApi["legacy"]["events"]
  • Returns ChainApi["legacy"]["events"]

    Entry-point for inspecting events from metadata

  • get genesisHash(): `0x${string}`
  • Returns `0x${string}`

  • get metadata(): Metadata
  • Returns Metadata

  • get options(): ApiOptions
  • Returns ApiOptions

  • get provider(): JsonRpcProvider
  • Returns JsonRpcProvider

    Get the JSON-RPC provider instance

  • get query(): ChainApi["legacy"]["query"]
  • Returns ChainApi["legacy"]["query"]

    Entry-point for executing query to on-chain storage.

    const balance = await api.query.system.account(<address>);
    console.log('Balance:', balance);
  • get registry(): PortableRegistry
  • Returns PortableRegistry

  • get rpc(): ChainApi[RpcVersion]["rpc"]
  • Returns ChainApi[RpcVersion]["rpc"]

    Entry-point for executing JSON-RPCs to blockchain node.

    const client = new JsonRpcClient('wss://rpc.polkadot.io');
    await client.connect();

    // Subscribe to new heads
    client.rpc.chain_subscribeNewHeads((header) => {
    console.log(header);
    });

    // Execute arbitrary rpc method: `module_rpc_name`
    const result = await client.rpc.module_rpc_name();
  • get status(): ConnectionStatus
  • Returns ConnectionStatus

    Check connection status of the api instance

  • get tx(): ChainApi["legacy"]["tx"]
  • Returns ChainApi["legacy"]["tx"]

    Entry-point for executing on-chain transactions

    // Make a transfer balance transaction
    api.tx.balances.transferKeepAlive(<address>, <amount>)
    .signAndSend(<keyPair|address>, { signer }, ({ status }) => {
    console.log('Transaction status', status.type);
    });

Methods

  • Returns Promise<void>

  • Returns Promise<void>

  • Parameters

    • Optionalhash: `0x${string}`

    Returns ChainApi["legacy"]["call"]

  • Returns void

  • Returns Promise<void>

    Clear local cache

  • Returns void

  • Returns Promise<LegacyClient<ChainApi>>

    Connect to blockchain node

  • Returns Promise<void>

    Disconnect to blockchain node

  • Initialize APIs before usage

    Returns Promise<void>

  • Returns void

  • Parameters

    Returns boolean

  • Returns Promise<void>

  • Parameters

    Returns Promise<Metadata>

  • Parameters

    Returns undefined | Metadata

  • Parameters

    Returns `RAW_META/${string}`

  • Get current version of the runtime This is similar to .runtimeVersion but also ensure the corresponding metadata of this runtime version is downloaded & setup.

    This is helpful when you want to check runtime version to prepare for runtime upgrade

    Returns Promise<SubstrateRuntimeVersion>

  • Returns Promise<void>

  • Returns Promise<void>

  • Parameters

    • event: ApiEvent
    • Optionalhandler: HandlerFn

    Returns this

  • Parameters

    Returns Unsub

  • Parameters

    Returns Unsub

  • Parameters

    Returns void

  • Update the signer instance for signing transactions

    Parameters

    • Optionalsigner: Signer

    Returns void

  • Parameters

    Returns Promise<void>

  • Returns Promise<boolean>

  • Returns void

  • Parameters

    • runtimeVersion: {
          apis: (readonly [`0x${string}`, number])[];
          authoringVersion: number;
          implName: string;
          implVersion: number;
          specName: string;
          specVersion: number;
          stateVersion: number;
          transactionVersion: number;
      }
      • apis: (readonly [`0x${string}`, number])[]
      • authoringVersion: number
      • implName: string
      • implVersion: number
      • specName: string
      • specVersion: number
      • stateVersion: number
      • transactionVersion: number

    Returns SubstrateRuntimeVersion