본문으로 건너뛰기

EVM Compute Block Metrics

업데이트 날짜:
2025년 11월 12일

개요

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,
},
}
}

요청

We will invoke the function with the following cURL command. A few notes:

  • Replace the YOUR_API_KEY with your own Quicknode API key - follow this guide for creating an API key.
  • Replace the FUNCTION_ID with the ID of your Function - you can find this in the URL when viewing your Function in the Quicknode Dashboard.
  • Use the block_number parameter to specify the block number you want to analyze. You can also omit this property for the function to run against the latest block.
curl -X POST "https://api.quicknode.com/functions/rest/v1/functions/FUNCTION_ID/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_number": "21194268"
}'

답변

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.

여러분의 피드백을 ❤️ 환영합니다!

의견이나 다루었으면 하는 새로운 주제가 있다면 알려주세요. 여러분의 의견을 기다리고 있습니다.

이 문서 공유하기