Providers are means to provide connection to the network, Dedot comes by default with providers for connection via WebSocket (wss://) and smoldot light client. But you can implement your own provider for your own needs.
WsProvider
import { WsProvider } from'dedot';// Initialize the provider & connect to the networkconstprovider=newWsProvider('wss://rpc.polkadot.io');awaitprovider.connect(); // Fetch the genesis hash constgenesisHash=awaitprovider.send('chain_getBlockHash', [0]); console.log(genesisHash); // Subscribe to runtimeVersion changes awaitprovider.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 networkawaitprovider.disconnect();
SmoldotProvider
SmoldotProvider take in a parameter of type Chain from smoldot, so before initialize a SmoldotProvider, one should install smoldot package and following the instruction to instanciate a Chain connection to the network.
If you're building a browser dapp, it's highly recommended to setup a worker for smoldot
Install smoldot
npmismoldot
Initialize SmoldotProvider
import { SmoldotProvider } from'dedot';import*as smoldot from'smoldot';import chainSpec from'./polkadot-chainspec.json';// Start smoldot instance & initialize a chainconstclient=smoldot.start();constchain=awaitclient.addChain({ chainSpec });// Initialize providers & connect to the networkconstprovider=newSmoldotProvider(chain);awaitprovider.connect(); // Fetch the genesis hash constgenesisHash=awaitprovider.send('chain_getBlockHash', [0]); console.log(genesisHash); // Subscribe to runtimeVersion changes awaitprovider.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 networkawaitprovider.disconnect();
Add your own custom provider?
Every provider must implement the JsonRpcProvider interface, defined as below:
typeConnectionStatus='connected'|'disconnected'|'reconnecting';typeProviderEvent=ConnectionStatus|'error'; // | 'timeout';interfaceJsonRpcProviderextendsIEventEmitter<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: