web3_clientVersion | Avalanche

Avalanche API method that returns the client type and version running on the Avalanche node. This information can be useful to developers to verify that a node they are connecting to is compatible with their needs.

👍

Get you own node endpoint today

Start for free and get your app to production levels immediately. No credit card required.

You can sign up with your GitHub, X, Google, or Microsoft account.

Parameters

  • none

Response

  • string — a string identifying the type of client, version, operating system, and language version running on the node

web3_clientVersion code examples

const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function getClient() {
  const client = await web3.eth.getNodeInfo();
  console.log(client);
}

getClient();
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

const clientVersion = async () => {
  const version = await provider.send("web3_clientVersion");
  console.log(`Client version: ${version}`);
};

clientVersion();
from web3 import Web3  
node_url = "CHAINSTACK_NODE_URL"
web3 = Web3.HTTPProvider(node_url)

client_version = web3.provider.make_request('web3_clientVersion', [])
print(client_version)

Use case

A use case for the web3_clientVersion method can be to verify which client version is running to then decide which function to call.

Let's say you have a DApp that needs to call a different function based on the client's version. You can use web3_clientVersion to build this logic.

Here is an implementation of this use case using web3.js:

const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

// Get client version
async function getClientVersion() {
  web3.extend({
      property: 'eth',
      methods: [{
          name: 'getClientVersion',
          call: 'web3_clientVersion',
          params: 0,
          inputFormatter: [],
          outputFormatter: null
      }]
  });

  const clientVersion = await web3.eth.getClientVersion();
  return clientVersion
}

// define a function to be run if the client version is 'v0.11.5'
function runFunction1() {
  console.log('Running function 1...');
}

// define a function to be run if the client version is 'v0.11.4'
function runFunction2() {
  console.log('Running function 2...');
}

// define an async function to retrieve the client version and run the appropriate function
async function runBasedOnClientVersion() {
  try {
    const clientVersion = await getClientVersion();
    console.log(`Client version: ${clientVersion}`);

    if (clientVersion === 'v0.11.5') {
      runFunction1();
    }

    else if (clientVersion === 'v0.11.4') {
      runFunction2();
    }
    // log an error if the client version is not recognized
    else {
      console.error('Unrecognized client version.');
    }
  } catch (error) {
    console.error(error);
  }
}

// call the async function to retrieve the client version and run the appropriate function
runBasedOnClientVersion();

This code uses the web3.js library to connect to a blockchain node specified by the NODE_URL variable and retrieves the client version using the web3_clientVersion method. It then runs different functions based on the client version by defining two functions runFunction1 and runFunction2, and using an if statement to check the client version and call the appropriate function.

The getClientVersion function extends the Web3 library with a custom method getClientVersion, which calls the web3_clientVersion method and returns the result. This function is called within the runBasedOnClientVersion function to retrieve the client version.

Overall, this code demonstrates how to retrieve the client version on a blockchain node using the web3.js library and how to use the client version to run different functions based on the version. The code can be modified to add additional functions and version checks or to perform other actions based on the client version.

Try the web3_clientVersion RPC method yourself

Language
Click Try It! to start a request and see the response here!