The script below calculates the current balance of a given cluster, through two actions, mainly:
query the subgraph for the relevant data (see the Subgraph Examples page for more information)
use these values to compute the current cluster balance for the specified cluster, outputting the amount in SSV
Details on the formulas used can be found in the documentation page related to Cluster Balance.
// Import the fetch function from node-fetchimport fetch from"node-fetch";// Define the GraphQL endpoint URLconsturl="https://api.studio.thegraph.com/query/71118/ssv-network-holesky/version/latest";// Define the GraphQL query - Update operators and account address to query different clustersconstquery=`{ _meta { block { number } } daovalues(id: "0x38A4794cCEd47d3baf7370CcC43B560D3a1beEFA") { networkFee networkFeeIndex networkFeeIndexBlockNumber } # Operator IDS are hardcoded in this example operators(where: {id_in: ["11", "13", "24", "30"]}) { fee feeIndex feeIndexBlockNumber } # The cluster ID is hardcoded in this example cluster(id: "0xaa184b86b4cdb747f4a3bf6e6fcd5e27c1d92c5c-11-13-24-30") { validatorCount networkFeeIndex index balance }}`// Function to fetch dataasyncfunctionfetchData() {try {// Make the HTTP request to the GraphQL endpointconstresponse=awaitfetch(url, { method:'POST', headers: {'Content-Type':'application/json', }, body:JSON.stringify({ query }) });// Parse the response dataconstresponseData=awaitresponse.json();// networkFee = nfip + (bN - nfbp) * nf - nfic * 10000000 // note: the 10^7 multiplier is due to the networkFeeIndex being "shrunk" to a uint64 in the smart contract to save space
const networkFee = parseInt(responseData.data.daovalues.networkFeeIndex) + (responseData.data._meta.block.number - parseInt(responseData.data.daovalues.networkFeeIndexBlockNumber)) * parseInt(responseData.data.daovalues.networkFee) - responseData.data.cluster.networkFeeIndex * 10000000
console.log("Network Fee:", networkFee)// operatorFee = (ofin + (bn - ofbn) * ofn) - ofc * 10000000 // note: the 10^7 multiplier is due to the operator fee index being "shrunk" to a uint64 in the smart contract to save space
// start with negative cluster index, instead of 0, to avoid initialize it as 0 and subtracting it at the endlet operatorFee =-responseData.data.cluster.index *10000000;for (let operator ofresponseData.data.operators) { operatorFee += parseInt(operator.feeIndex) + (responseData.data._meta.block.number - parseInt(operator.feeIndexBlockNumber)) * parseInt(operator.fee)
}console.log("Operator Fee:", operatorFee)// balance = cluster balance - (networkfee + operatorfee) * nv const clusterBalance = responseData.data.cluster.balance - (networkFee + operatorFee) * responseData.data.cluster.validatorCount
console.log("Cluster Balance: ", clusterBalance) } catch (error) {// Log any errors that occur during the fetchconsole.error("Error fetching data:", error); }}// Run the fetchData functionfetchData();
To run the script:
Prerequisite: Node.js must be installed on the system
Create a file called fetchData.js
Copy the code above into this file
Install the fetch module:
npminode-fetch
Run the script with node :
nodefetchData.js
If you get a warning, or an error similar to this:
(node:232985) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
Just make sure to edit the package.json file and a line with"type": "module" to it, like in the example below:```json