Overview

A Provider establishes a connection to the blockchain, whch can be used to query its current state, simulate execution and send transactions to update the state.

Providers are one of the most fundamental components of interacting with a blockchain application, and there are many ways to connect, such as over HTTP, WebSockets or injected providers such as Pelagus.

Usage

There are two types of network providers you can connect to:

  • Remote Node Providers: any remote node that is open to data requests
  • Browser/Injected Providers: typically wallet interfaces used in browser Applications

Remote Node Provider

To connect to a remote node provider, use the JsonRpcProvider or WebSocketProvider classes. This connects to a remote node using the JSON-RPC interface. Remote node providers will never have a signer bundled into them.

Quais providers are configured to first ask the node which shards it is currently running, and then establish a connection to each of those chains. This is done using

import { quais } from 'quais'


// configure a multi-shard JSON-RPC provider using the dev rpc and pathing
const provider = new quais.JsonRpcProvider('https://rpc.quai.network', undefined, { usePathing: true })

// configure a multi-shard WebSocket provider using the dev rpc and pathing
const provider = new quais.WebSocketProvider('wss://rpc.quai.network', undefined, { usePathing: true })

Injected Provider

There are multiple ways to configure a network provider using the Provider interface.

To connect to a browser wallet provider like Pelagus, use the BrowserProvider class. Pelagus bundles a Signer into the injected provider that can be used to sign transactions.

import { quais } from 'quais'

// configure pelagus as the provider
const provider = new quais.BrowserProvider(window.pelagus)

// get the signer
const signer = provider.getSigner()