curl --request POST \
--url https://nd-500-249-268.p2pify.com/512e720763b369ed620657f84d38d2af \
--header 'Content-Type: application/json' \
--data '{
"id": 1,
"jsonrpc": "2.0",
"method": "web3_clientVersion",
"params": []
}'
{
"jsonrpc": "<string>",
"id": 123,
"result": "<string>"
}
curl --request POST \
--url https://nd-500-249-268.p2pify.com/512e720763b369ed620657f84d38d2af \
--header 'Content-Type: application/json' \
--data '{
"id": 1,
"jsonrpc": "2.0",
"method": "web3_clientVersion",
"params": []
}'
{
"jsonrpc": "<string>",
"id": 123,
"result": "<string>"
}
none
string
— a string identifying the type of client, version, operating system, and language version running on the nodeweb3_clientVersion
code examplesconst 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();
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();
const clientName = clientVersion.split('/')[1];
return clientName
}
// define a function to be run if the client version is 'vv2.0.10'
function runFunction1() {
console.log('Running function 1...');
}
// define a function to be run if the client version is 'vv2.0.11'
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 === 'v1.14.7+4fe81c6b') {
runFunction1();
}
else if (clientVersion === 'v1.14.8+4fe81c6b') {
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();
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.The client running on this node.
The response is of type object
.
Was this page helpful?