Connect to a Wit/RPC provider

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

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.

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

main()

async function main() {
    const provider = await Witnet.Provider.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.

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

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

Last updated

Was this helpful?