Skip to content

Connect to a Wit/RPC provider

Initialize and verify connection to a Wit/RPC provider using the Witnet SDK

TIP

The code examples below assume that the environment is properly set up as described in Getting Started. You can easily adapt the examples to pass the URL of the Wit/RPC provider to use, and/or the wallet master key (and the password, if encrypted).

Step 1: Initialize connection to the Wit/RPC provider

A connection is considered to be initialized once information about the actual Witnet network being served by the provider is retrieved. Namely, an initialized connection lets you know the network identifier, and whether you are connected to the Witnet Mainnet, or perhaps to some Testnet.

js
const { Witnet } = require('@witnet/sdk')

main()

async function main() {
    const provider = await Witnet.JsonRpcProvider.fromEnv() 
    console.log(provider.network)
    console.log(provider.networkId)
    // ...
}

Step 2: Check synchronization status and current epoch

In order to serve up-to-date information, the provider needs to keep synchronized with the Witnet blockchain. The following code shows how to check the provider's node_state, and the current_epoch of the Witnet network as well as.

javascript
// ...
let syncStatus = await provider.syncStatus()

syncStatus.node_state // "Synced" | "AlmostSynced" | "WaitingConsensus" | "Synchronizing"
syncStatus.current_epoch