const { Connection } = require('@solana/web3.js');
async function getBlockCommitmentDetails() {
const rpcUrl = 'https://mainnet.helius-rpc.com/?api-key=YOUR_API_KEY'; // Replace YOUR_API_KEY
const connection = new Connection(rpcUrl, 'confirmed');
// Replace with a valid, recent slot number on your target network
const slotToQuery = 250000000;
try {
// Note: getBlockCommitment is not directly available in @solana/web3.js Connection object.
// You typically need to make a direct RPC call for this method.
// The example below shows how to construct and send such a raw request.
const response = await fetch(rpcUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'getBlockCommitment',
params: [slotToQuery],
}),
});
const result = await response.json();
if (result.error) {
console.error(`Error fetching block commitment for slot ${slotToQuery}:`, result.error.message);
return;
}
const blockCommitment = result.result;
if (blockCommitment) {
console.log(`Block Commitment for Slot ${slotToQuery}:`);
console.log(` Total Stake (Lamports): ${blockCommitment.totalStake}`);
console.log(` Commitment Array:`, blockCommitment.commitment ? blockCommitment.commitment : 'Not available/Unknown block');
// The commitment array shows lamports committed at different depths.
// A null commitment array usually means the block is not found or too old.
// A non-null array where later entries are higher indicates increasing finality.
} else {
console.log(`Block commitment data for slot ${slotToQuery} not found.`);
}
} catch (error) {
console.error(`Error fetching block commitment for slot ${slotToQuery}:`, error);
}
}
getBlockCommitmentDetails();