Ethereum API method that returns the client type and version running on the Ethereum 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
web3_clientVersion code examplesLearn more about the
ChainstackProviderinethers.js: ethers ChainstackProvider Documentation.
const ethers = require("ethers");
// Create a ChainstackProvider instance for Ethereum mainnet
const chainstack = new ethers.ChainstackProvider("mainnet");
const clientVersion = async () => {
const version = await chainstack.send("web3_clientVersion");
console.log(`Client version: ${version}`);
};
clientVersion();
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();
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 is running to then decide which method to run.
Let's say you have a DApp that needs to retrieve all of the transaction receipts in a block; the Erigon client has a method for this, eth_getBlockReceipts, but with Geth, this method is only available starting from V1.13.0. You can use the web3_clientVersion method in your logic to identify which client you are using to see if you have the method available.
Read Expanding your blockchain horizons: The eth_getBlockReceipts emulator to learn how to build a program to emulate
eth_getBlockReceiptson any EVM-compatible chain.
Here is an implementation of this use case using web3.js:
const { Web3, Web3PluginBase } = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);
// Define the ExtraMethods class
class ExtraMethods extends Web3PluginBase {
pluginNamespace = 'extra';
async getBlockReceipts(blockNumber) {
return this.requestManager.send({
method: 'eth_getBlockReceipts',
params: [blockNumber],
});
}
}
web3.registerPlugin(new ExtraMethods());
// Get client version
async function getClient() {
const client = await web3.eth.getNodeInfo();
return client;
}
// Extract the client's name and version
async function getClientName() {
const clientVersion = await getClient();
// Split the clientVersion string by '/'
let parts = clientVersion.split('/');
// Split the second part by '-' and take the first element
let version = parts[1].split('-')[0];
// Concatenate the client name and version
let clientNameWithVersion = parts[0] + '/' + version;
console.log(clientNameWithVersion);
return clientNameWithVersion;
}
// Parse version from version string
function parseVersion(versionString) {
const versionMatch = versionString.match(/(\d+\.\d+\.\d+)/);
return versionMatch ? versionMatch[1] : null;
}
// Compare versions
function isVersionGreaterThanOrEqual(v1, v2) {
const v1Parts = v1.split('.').map(Number);
const v2Parts = v2.split('.').map(Number);
for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) {
const num1 = v1Parts[i] || 0;
const num2 = v2Parts[i] || 0;
if (num1 > num2) return true;
if (num1 < num2) return false;
}
return true; // versions are equal
}
// Get the receipts based on the client
async function getAllReceipts(blockId) {
const clientNameWithVersion = await getClientName();
const [clientName, clientVersion] = clientNameWithVersion.split('/');
if (clientName === 'Geth') {
const version = parseVersion(clientVersion);
if (isVersionGreaterThanOrEqual(version, '1.13.0')) {
console.log('Client is Geth with supported version.');
const receipts = await web3.extra.getBlockReceipts(blockId);
return receipts;
} else {
console.log('Client is Geth, but version is not supported.');
}
} else if (clientName === 'erigon') {
console.log('Client is Erigon');
const receipts = await web3.extra.getBlockReceipts(blockId);
return receipts;
} else {
console.log('Client version is not Geth or Erigon');
}
}
async function main() {
try {
const receipts = await getAllReceipts('latest');
if (receipts !== undefined) {
console.log(receipts);
}
} catch (error) {
console.error(error);
}
}
main();
Let's break down the code:
-
The
getBlockReceiptsMethod:
ThegetBlockReceiptsfunction is a part of theExtraMethodsclass, which extends the functionality of theweb3object via theWeb3PluginBase. This method usesthis.requestManager.sendto call theeth_getBlockReceiptsJSON-RPC method, passing a single parameterblockNumber. The function formats the input and output values and returns the resulting receipts. This method is accessible throughweb3.extra.getBlockReceiptsdue to the registration of theExtraMethodsplugin withweb3.registerPlugin(new ExtraMethods()). -
The
getClientFunction:
This function retrieves the client version using theweb3.eth.getNodeInfocall, which internally calls theweb3_clientVersionJSON-RPC method without parameters. It formats the returned value and provides the full client version string. -
The
getClientNameFunction:
getClientNamecallsgetClientto get the complete client version string. It then extracts both the client's name and its version by splitting the version string at the first/and-characters. The function concatenates these parts to form a string in the format "ClientName/Version". -
The
getAllReceiptsFunction:
This function usesgetClientNameto retrieve the name and version of the client in a combined format. It checks if the client isGethand, if so, further verifies whether its version is greater than or equal to1.13.0using theparseVersionandisVersionGreaterThanOrEqualfunctions. If the client isGethwith a supported version orErigon, the function proceeds to fetch block receipts usingweb3.extra.getBlockReceipts. If the client is neither a supported version ofGethnorErigon, it logs a message indicating this. -
Version Parsing and Comparison:
Additional functionsparseVersionandisVersionGreaterThanOrEqualare included for parsing the version string and comparing it against a specified minimum version.parseVersionextracts the semantic version number from the client version string, andisVersionGreaterThanOrEqualcompares this version against the specified minimum version,1.13.0in this case. -
Main Function and Execution Flow:
Themainfunction serves as the entry point for execution. It attempts to callgetAllReceiptswith a specified block identifier, handling any exceptions and logging the results if available.
Try the web3_clientVersion RPC method yourself
web3_clientVersion RPC method yourself