Connect to a Wit/RPC provider
Initialize and verify connection to a Wit/RPC provider using the Witnet SDK
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.
const { Witnet } = require('@witnet/sdk')
main()
async function main() {
const provider = await Witnet.JsonRpcProvider.fromEnv()
console.log(provider.network)
console.log(provider.networkId)
// ...
}import Witnet from '@witnet/sdk'
main()
async function main() {
const provider = await Witnet.JsonRpcProvider.fromEnv() // reads actual URL from environment
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// ...
let syncStatus: Witnet.SyncStatus = await provider.syncStatus()
syncStatus.node_state
syncStatus.current_epochLast updated
Was this helpful?
