Skip to main content

Compute Block Metrics

Updated on
Jun 29, 2024

Overview

Blockchain data is extensive, and having enhanced information about a block can provide comprehensive insights into blockchain activity. This enables developers, analysts, and businesses to perform better analysis and make more informed decisions.

In this Function example, we will query block, transactions, and transaction receipt data to get vital block analytics data like active addresses, gas price, ETH transferred, biggest transactions, etc.

Sample Function

The following is the code for the Function to get the vital block analytics data in JavaScript Node.js v20 runtime using the block with receipts dataset:

// Initialize the main function
function main(params) {

// Extract dataset and network from metadata in params
const block = params.data[0];
const dataset = params.metadata.dataset;
const network = params.metadata.network;

const transactions = block.transactions; // Get transactions of the block
const blockNumber = parseInt(block.number, 16); // Convert block number from hex to int

// Initialize variables for enhanced stats
let totalGasPrice = BigInt(0);
let totalEthTransferred = BigInt(0);
let contractCreations = 0;
let largestTxValue = BigInt(0);
let largestTxHash = null;
const activeAddresses = new Set();
const uniqueTokens = new Set();

// Process all transactions
for (const tx of transactions) {
// Gas price
const gasPriceInWei = BigInt(tx.gasPrice);
totalGasPrice += gasPriceInWei;

// ETH transferred
const valueInWei = BigInt(tx.value);
totalEthTransferred += valueInWei;

// Largest transaction
if (valueInWei > largestTxValue) {
largestTxValue = valueInWei;
largestTxHash = tx.hash;
}

// Active addresses
if (tx.from) activeAddresses.add(tx.from.toLowerCase());
if (tx.to) activeAddresses.add(tx.to.toLowerCase());

// Contract creations
if (!tx.to) contractCreations++;

// Unique tokens (approximation based on 'to' address)
if (tx.to) uniqueTokens.add(tx.to.toLowerCase());
}

// Calculate average gas price
const averageGasPriceInWei = totalGasPrice / BigInt(transactions.length);
const averageGasPriceInGwei = Number(averageGasPriceInWei) / 1e9; // Convert Wei to Gwei

// Return data
return {
blockNumber,
dataset,
network,
transactionCount: transactions.length,
averageGasPriceInWei: averageGasPriceInWei.toString(),
averageGasPriceInGwei,
totalEthTransferred: (Number(totalEthTransferred) / 1e18).toString(), // Convert Wei to Ether
activeAddressCount: activeAddresses.size,
contractCreations,
uniqueTokensInteracted: uniqueTokens.size,
largestTx: {
value: (Number(largestTxValue) / 1e18).toString(), // Convert Wei to Ether
hash: largestTxHash
}
};
}

Request

We will invoke the function with the following cURL command. Be sure to replace the YOUR_API_KEY with your own QuickNode API key and the POST URL with the URL of your Function.

curl -X POST "https://api.quicknode.com/functions/rest/v1/namespaces/0f6812dd-a17f-4cbc-9ab4-7a529eb33940/functions/block-analysis/call" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_API_KEY" \
-d '{
"network": "ethereum-mainnet",
"dataset": "block",
"block": "latest"
}'

Response

Resulting in the following response:

{
"response" : {
"activationId" : "6397a527b82649e497a527b82689e46c",
"annotations" : [
{
"key" : "path",
"value" : "ca43f206-1786-4d85-8521-952f2a528f49/block-analysis"
},
{
"key" : "waitTime",
"value" : 88
},
{
"key" : "kind",
"value" : "nodejs:20"
},
{
"key" : "timeout",
"value" : false
},
{
"key" : "limits",
"value" : {
"concurrency" : 1,
"logs" : 10,
"memory" : 256,
"timeout" : 60000
}
},
{
"key" : "initTime",
"value" : 11
}
],
"duration" : 16,
"end" : 1719590061927,
"logs" : [],
"name" : "block-analysis",
"namespace" : "ca43f206-1786-4d85-8521-952f2a528f49",
"publish" : false,
"response" : {
"result" : {
"activeAddressCount" : 297,
"averageGasPriceInGwei" : 9.438903833,
"averageGasPriceInWei" : "9438903833",
"blockNumber" : 20191211,
"contractCreations" : 0,
"dataset" : "block",
"largestTx" : {
"hash" : "0x58d707d0c238821d17c18fa716912ad05e851bfb199f620bbf3386ff58aabafa",
"value" : "29.2"
},
"network" : "ethereum-mainnet",
"totalEthTransferred" : "118.54106589039715",
"transactionCount" : 187,
"uniqueTokensInteracted" : 132
},
"size" : 390,
"status" : "success",
"success" : true
},
"start" : 1719590061911,
"subject" : "0f6812dd-a17f-4cbc-9ab4-7a529eb33940",
"version" : "0.0.11"
}
}

Learn more about QuickNode Functions.

We ❤️ Feedback!

If you have any feedback or questions about this documentation, let us know. We'd love to hear from you!

Share this doc