docs ⦿ dedot 🧑‍💻
TypinkXTelegramGithub
dedot 🧑‍💻
dedot 🧑‍💻
  • Welcome to Dedot
  • Why Dedot?
  • Getting started
    • Installation
    • Connect to network
    • @polkadot/api -> dedot
    • Packages structure
  • Clients & Providers
    • Providers
    • Clients
  • Client API
    • ChainApi
    • Constants
    • Runtime APIs
    • Storage Queries
    • Transactions
    • Events
    • Errors
  • ink! Smart Contracts
    • Introduction
    • Generate Types & APIs
    • Deploy contracts
    • Queries
    • Transactions
    • Events
    • Handle errors
  • CLI
  • Keyring & Signer
  • Runtime upgrades
  • Type system
  • Utilities
    • HexString
    • Uint8Array (U8a)
    • String
    • Hash functions
    • Address
    • BigInt & number
    • Balances
    • Merkleized Metadata
  • Help & FAQ
    • Tutorials
      • Develop ink! dApp using Typink
    • Built with Dedot
    • Forum Posts
    • Telegram
    • Github
    • API Reference
Powered by GitBook
On this page
  • WsProvider
  • SmoldotProvider
  • Add your own custom provider?

Was this helpful?

Edit on GitHub
  1. Clients & Providers

Providers

PreviousClients & ProvidersNextClients

Last updated 8 months ago

Was this helpful?

Providers are means to provide connection to the network, Dedot comes by default with providers for connection via (wss://) and light client. But you can implement your own provider for your own needs.

WsProvider

import { WsProvider } from 'dedot';

// Initialize the provider & connect to the network
const provider = new WsProvider('wss://rpc.polkadot.io');
await provider.connect();  

// Fetch the genesis hash 
const genesisHash = await provider.send('chain_getBlockHash', [0]); 
console.log(genesisHash);  

// Subscribe to runtimeVersion changes 
await provider.subscribe({
  subname: 'chain_newHead', // subscription name for notification
  subscribe: 'chain_subscribeNewHeads', // subscribe method
  params: [], // params for subscribe method
  unsubscribe: 'chain_unsubscribeNewHeads', // unsubscribe method
}, (error, newHead, subscription) => { 
  console.log('newHead', newHead);   
});  

// Disconnect from the network
await provider.disconnect();

SmoldotProvider

  1. Install smoldot

npm i smoldot
  1. Initialize SmoldotProvider

import { SmoldotProvider } from 'dedot';
import * as smoldot from 'smoldot';
import chainSpec from './polkadot-chainspec.json';

// Start smoldot instance & initialize a chain
const client = smoldot.start();
const chain = await client.addChain({ chainSpec });

// Initialize providers & connect to the network
const provider = new SmoldotProvider(chain);

await provider.connect();  

// Fetch the genesis hash 
const genesisHash = await provider.send('chain_getBlockHash', [0]); 
console.log(genesisHash);  

// Subscribe to runtimeVersion changes 
await provider.subscribe({
  subname: 'chain_newHead', // subscription name for notification
  subscribe: 'chain_subscribeNewHeads', // subscribe method
  params: [], // params for subscribe method
  unsubscribe: 'chain_unsubscribeNewHeads', // unsubscribe method
}, (error, newHead, subscription) => { 
  console.log('newHead', newHead);   
});  

// Disconnect from the network
await provider.disconnect();

Add your own custom provider?

Every provider must implement the JsonRpcProvider interface, defined as below:

type ConnectionStatus = 'connected' | 'disconnected' | 'reconnecting';
type ProviderEvent = ConnectionStatus | 'error'; // | 'timeout';

interface JsonRpcProvider extends IEventEmitter<ProviderEvent> {
  /**
   * The current connection status
   */
  status: ConnectionStatus;

  /**
   * Send a JSON-RPC request,
   * make sure to connect to the provider first before sending requests
   *
   * @param method
   * @param params
   */
  send<T = any>(method: string, params: any[]): Promise<T>;

  /**
   * Make a subscription request,
   * make sure to connect to the provider first before sending requests
   *
   * @param input
   * @param callback
   */
  subscribe<T = any>(
    input: JsonRpcSubscriptionInput,
    callback: JsonRpcSubscriptionCallback<T>,
  ): Promise<JsonRpcSubscription>;

  /**
   * Connect to the provider
   */
  connect(): Promise<this>;

  /**
   * Disconnect from the provider
   */
  disconnect(): Promise<void>;
}

One can easily add a custom provider by implementing this interface:

// custom-provider.ts
import { JsonRpcProvider } from 'dedot';

export class MyCustomProvider implements JsonRpcProvider {
  // ... implementation details
}

// main.ts
import { DedotClient } from 'dedot';
import { MyCustomProvider } from './custom-provider';

const client = await DedotClient(new MyCustomProvider());
const chain: string = await client.rpc.system_chain();

SmoldotProvider take in a parameter of type from smoldot, so before initialize a SmoldotProvider, one should install smoldot package and following the to instanciate a Chain connection to the network.

If you're building a browser dapp, it's highly recommended to setup a for smoldot

More detailed information about the JsonRpcProvider and related types can be found in the .

WebSocket
smoldot
Chain
instruction
worker
source code