Overview
Measuring the amount of developer activity happening on a given blockchain is not straightforward. A combination of methodologies is the best way to get a comprehensive view of the blockchain's activity. One of the most important metrics is the number of smart contracts being deployed.
To get this value, many rely on data found within a block's receipts. While a good estimate and the only option for blockchains that don't support debug traces, it misses contracts deployed through other smart contracts, known as factories.
In this Function example, we will extract the number of contracts deployed through a block's traces.
Sample Function
function main(params) {
// Validate we have expected dataset
if (params.metadata?.dataset) {
if (params.metadata.dataset !== 'debug_traces') {
return {
error: 'Unexpected dataset, expected debug_traces',
}
}
}
// Extract data from params
const traces = params.data ? params.data[0] : params[0]
const blockNumber = params.metadata.batch_start_range
let numContractDeployments = 0
// Recursive function to process traces and count where type is CREATE or CREATE2
const processTrace = trace => {
const item = trace.result ? trace.result : trace
if (item.type && ['CREATE', 'CREATE2'].includes(item.type)) {
numContractDeployments++
}
if (item.calls) {
item.calls.forEach(processTrace)
}
}
traces.forEach(processTrace)
return {
network: params.metadata.network,
blockNumber,
numContractDeployments,
}
}
Request
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": "debug_traces",
"block_number": 21503215
}'
Response
Resulting in the following response:
{
"network": "ethereum-mainnet",
"blockNumber": 21503215,
"numContractDeployments": 5
}
Looking to learn more about Functions? Get started here.
We ❤️ Feedback!
Let us know if you have any feedback or requests for new topics. We'd love to hear from you.