DedotClient

Dedot provides a unified client interface for interacting with Substrate-based blockchains. The main entry point is DedotClient, which offers powerful APIs that abstract over the complexities of on-chain interactions (queries, transactions, subscriptions) and supports both modern and legacy JSON-RPC specifications.

DedotClient

DedotClient is the unified client interface for interacting with Substrate-based blockchains. It provides a consistent API regardless of whether you're using the new JSON-RPC v2 specification or the legacy JSON-RPC specification.

Interface Overview

interface DedotClient<ChainApi> {
  // Connection
  status: ConnectionStatus;              // Current connection status
  connect(): Promise<this>;              // Connect to the network
  disconnect(): Promise<void>;           // Disconnect from the network

  // Chain Information
  genesisHash: Hash;                     // Genesis hash of the connected chain
  runtimeVersion: SubstrateRuntimeVersion; // Current runtime version
  metadata: Metadata;                    // Chain metadata
  registry: PortableRegistry;            // Type registry for encoding/decoding

  // Chain Spec
  chainSpec: {
    chainName(): Promise<string>;        // Get chain name
    genesisHash(): Promise<HexString>;   // Get genesis hash
    properties(): Promise<Properties>;   // Get chain properties (token symbol, decimals, etc.)
  };

  // Block Explorer
  block: {
    best(): Promise<BlockInfo>;                            // Get current best block
    best(callback: (block: BlockInfo) => void): () => void; // Subscribe to best blocks
    finalized(): Promise<BlockInfo>;                       // Get current finalized block
    finalized(callback: (block: BlockInfo) => void): () => void; // Subscribe to finalized blocks
    header(hash: BlockHash): Promise<Header>;              // Get block header
    body(hash: BlockHash): Promise<HexString[]>;           // Get block body (extrinsics)
  };

  // On-chain Interactions
  rpc: ChainApi['rpc'];                  // Raw JSON-RPC method access
  query: ChainApi['query'];              // Storage queries
  consts: ChainApi['consts'];            // Pallet constants
  call: ChainApi['call'];                // Runtime API calls
  tx: ChainApi['tx'];                    // Transaction builder
  events: ChainApi['events'];            // Events API
  errors: ChainApi['errors'];            // Errors API
  view: ChainApi['view'];                // View functions (Metadata V16+)

  // Utilities
  at(hash: BlockHash): Promise<ISubstrateClientAt>;        // Get an instance at a specific block for querying historical state
  getRuntimeVersion(): Promise<SubstrateRuntimeVersion>;   // Get runtime version with metadata sync
  setSigner(signer?: InjectedSigner): void;                // Set transaction signer
  sendTx(tx: HexString | Extrinsic, callback?: Callback): TxUnsub; // Broadcast transaction
  queryMulti(queries: Query[], callback?: Callback): Promise<any[]>; // Query multiple storage items

  // Events
  on(event: ApiEvent, handler: EventHandlerFn): () => void;   // Subscribe to client events
  once(event: ApiEvent, handler: EventHandlerFn): () => void; // Subscribe once
  off(event: ApiEvent, handler?: EventHandlerFn): this;       // Unsubscribe
}

Usage Example

Connect to network via legacy JSON-RPC APIs

If the JSON-RPC server doesn't support the new JSON-RPC v2 specification yet (nodes using Polkadot-SDK < 1.11.0), you can connect using legacy JSON-RPC APIs. The DedotClient provides a unified interface for both:

The same API is available regardless of which JSON-RPC version you use. The only difference is the underlying RPC calls made to the node.

For more details on when to use JSON-RPC v2 or legacy, see the Connect to network page.

JsonRpcClient (Advanced)

JsonRpcClient is a low-level JSON-RPC client for advanced use cases where you need direct access to raw JSON-RPC methods without the high-level abstractions. DedotClient extends JsonRpcClient, so all JsonRpcClient methods are also available on DedotClient.


Connection Status

Clients track the connection status, accessible via client.status:

Available statuses:

Status
Description

connected

The client is connected to the network

disconnected

The client is disconnected from the network

reconnecting

The client is attempting to reconnect to the network

Block Explorer API (client.block)

The Block Explorer API provides access to block data, allowing you to query and subscribe to best and finalized blocks.

The BlockInfo object returned contains:

Property
Type
Description

hash

BlockHash

The block hash

number

number

The block number

parent

BlockHash

The parent block hash

runtimeUpgraded

boolean

Whether a runtime upgrade occurred in this block

Chain Spec API (client.chainSpec)

The Chain Spec API provides access to chain information such as the chain name, genesis hash, and chain properties.

Broadcast Transaction API (client.sendTx)

The sendTx method broadcasts a signed transaction to the network and provides utilities to track its status.

For building and signing transactions, use client.tx.<pallet>.<method>() which provides a higher-level API with .signAndSend() method. The sendTx method is useful when you already have a signed transaction hex string.

Client Events

Clients emit events that you can listen to for handling connection state changes, errors, and runtime upgrades.

Event Handlers

Each event has a typed handler signature:

Event Methods

DedotClientOptions

When creating a DedotClient, you can pass configuration options to customize the client behavior.

Option
Type
Default
Description

provider

JsonRpcProvider

required

The JSON-RPC provider (WsProvider or SmoldotProvider)

rpcVersion

'v2' | 'legacy'

'v2'

The JSON-RPC specification version to use

cacheMetadata

boolean

false

Enable metadata caching to localStorage (browser)

cacheStorage

IStorage

localStorage

Custom storage implementation for metadata caching

metadata

Record<string, HexString>

-

Pre-loaded metadata to skip download step

signedExtensions

Record<string, AnySignedExtension>

-

Custom signed extensions for the chain

runtimeApis

Record<string, RuntimeApiSpec[]>

-

Runtime API specifications (for Metadata V14 chains)

throwOnUnknownApi

boolean

true

Throw error when accessing unknown APIs

hasher

HashFn

blake2_256

Custom hashing function

signer

InjectedSigner

-

Transaction signer instance

stalingDetectionTimeout

number

30000

Stale connection detection timeout in ms (0 to disable)

Example

Last updated

Was this helpful?