Dedot exposes 3 high-level clients offering powerful API that abstracting over the complexities of on-making chain interactions (queries, transactions...) or sending raw JSON-RPC request to a blockchain nodes.
DedotClient: High-level Substrate client built on top of the new JSON-RPC API
LegacyClient: High-level Substrate client built on top of the legacy JSON-RPC API
JsonRpcClient: High-level JSON-RPC client to help making raw JSON-RPC requests or subscriptions seamlessly. Both DedotClient and LegacyClient are also a JsonRpcClient.
Both DedotClient and LegacyClient are both implementing theISubstrateClient<ChainApi, ApiEvent> interface, so they are sharing a list of common APIs of a client to interact with Subtrated-based blockchains.
DedotClient
DedotClient's high-level APIs are built on top of the new JSON-RPC API, beside implementing the common client APIs, DedotClient are also exposing access to JsonRpcGroup instances like ChainHead or ChainSpec.
import { DedotClient, WsProvider, PinnedBlock } from'dedot';importtype { PolkadotApi } from'@dedot/chaintypes';// Initialize providers & clientsconstprovider=newWsProvider('wss://rpc.polkadot.io');constclient=awaitDedotClient.new<PolkadotApi>(provider);// Get current best runtime versionconstcurrentBestRuntimeVersion=awaitclient.getRuntimeVersion();// Execute JSON-RPC methodsconstmetadata=awaitclient.rpc.state_getMetadata();// On-chain interactionsconstbalance=client.query.system.balance('<address>');consttransferTx=client.tx.balances.transferKeepAlive(<address>,2_000_000_000_000n);// Fetch ChainSpec informationconstgenesisHash=awaitclient.chainSpec.genesisHash();constchainProps=awaitclient.chainSpec.properties();// Access ChainHead dataconstcurrentBestBlock=awaitclient.chainHead.bestBlock();constcurrentFinalizedBlock=awaitclient.chainHead.finalizedBlock();constfinalizedRuntimeVersion=awaitclient.chainHead.runtimeVersion();constbestRuntimeVersion=awaitclient.chainHead.bestRuntimeVersion();// ...// Listen to ChainHead events (best blocks, finalized block, ...)constunsub=client.chainHead.on('bestBlock', (block:PinnedBlock, bestChainChanged:boolean) => {console.log(`Current best block: ${block.number}, hash: ${block.hash}, bestChainChanged: ${bestChainChanged}`);});// Other ChainHead events available like: // 'finalizedBlock': new best finalized block// 'bestChainChanged': new best chain changed, a fork happened// Or broadcast a raw transactionconstrawTxHex='0x...';constunsub=awaitclient.chainHead.txBroadcaster.broadcastTx(rawTxHex);
LegacyClient
Similarly to DedotClient, LegacyClient is also another client that Dedot offers, but its high-level APIs are built on of the legacy JSON-RPC API.
import { LegacyClient, WsProvider } from'dedot';importtype { PolkadotApi } from'@dedot/chaintypes';// Initialize providers & clientsconstprovider=newWsProvider('wss://rpc.polkadot.io');constclient=awaitLegacyClient.new<PolkadotApi>(provider);// Get current best runtime versionconstcurrentBestRuntimeVersion=awaitclient.getRuntimeVersion();// Execute JSON-RPC methodsconstmetadata=awaitclient.rpc.state_getMetadata();// On-chain interactionsconstbalance=client.query.system.balance('<address>');consttransferTx=client.tx.balances.transferKeepAlive(<address>,2_000_000_000_000n);// Fetch ChainSpec informationconstgenesisHash=awaitclient.rpc.chain_getBlockHash(0);constchainProps=awaitclient.rpc.system_properties();// Access ChainHead dataconstcurrentBestBlock=awaitclient.rpc.chain_getBlock();constcurrentFinalizedHash=awaitclient.rpc.chain_getFinalizedHead();constfinalizedBlock=awaitclient.rpc.chain_getBlock(currentFinalizedHash);// ...// Or broadcast a raw transactionconstrawTxHex='0x...';consttxHash=awaitclient.rpc.author_submitExtrinsic(rawTxHex);// orconstunsub=awaitclient.rpc.author_submitAndWatchExtrinsic(rawTxHex, (txStatus) => {});
JsonRpcClient
JsonRpcClient is a light-weight JSON-RPC client. Both DedotClient and LegacyClient are also extending JsonRpcClient, so one can access all the methods and props of a JsonRpcClient on DedotClient or LegacyClient. So unless for advanced interaction intensively via raw JSON-RPC, we recommend using high-level abstraction APIs on DedotClient or LegacyClient.
import { JsonRpcClient, WsProvider } from'dedot';importtype { PolkadotApi } from'@dedot/chaintypes';// Initialize providers & clientsconstprovider=newWsProvider('wss://rpc.polkadot.io');constclient=awaitJsonRpcClient.new<PolkadotApi>(provider);// Get current best runtime versionconsole.log('Connection status:',client.status);// Execute JSON-RPC methodsconstmetadata=awaitclient.rpc.state_getMetadata();constchain=awaitclient.rpc.system_chain();constnodeVersion=awaitclient.rpc.system_version();// Submit raw txconsttxHash=awaitclient.rpc.author_submitExtrinsic('0x...');
Connection Status
Clients keep track of connection status, and it can be access via client.status with type ConnectionStatus
import { ConnectionStatus } from'dedot';// client: DedotClient, LegacyClient or JsonRpcClientconststatus:ConnectionStatus=client.status;
There are 3 available statues:
connected: The client is connected to the network
disconnected: The client is disconnected from the network
reconnecting: The client is trying to reconnect to the network
Client Events
Clients will emit the following events in specific situations: