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/functions/{functionId}/call?result_only=true" \
-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:
{
"blockNumber": 21194268,
"dataset": "block",
"network": "ethereum-mainnet",
"transactionCount": 204,
"averageGasPriceInWei": "31088390223",
"averageGasPriceInGwei": 31.088390223,
"totalEthTransferred": "2156.1978496173274",
"activeAddressCount": 317,
"contractCreations": 2,
"uniqueTokensInteracted": 132,
"largestTx": {
"value": "2100",
"hash": "0x908f3b5fd5f84b9c751ec6453c4112af41f0e87e6e96b64a403a5ad40be5a8ec"
}
}
Learn more about QuickNode Functions.
We ❤️ Feedback!
Let us know if you have any feedback or requests for new topics. We'd love to hear from you.