Skip to content

Commit 5544648

Browse files
committed
feat: add block-stats subgraph
1 parent 8fdf799 commit 5544648

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

block-stats-subgraph/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build/
2+
generated/
3+
node_modules/
4+
5+
package-lock.json

block-stats-subgraph/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "block-stats-subgraph",
3+
"license": "MIT",
4+
"scripts": {
5+
"codegen": "graph codegen --ipfs http://localhost:5001/",
6+
"build": "graph build",
7+
"create-local": "graph create --node http://localhost:8020/ block-stats-subgraph",
8+
"remove-local": "graph remove --node http://localhost:8020/ block-stats-subgraph",
9+
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001/ block-stats-subgraph"
10+
},
11+
"devDependencies": {
12+
"@graphprotocol/graph-cli": "0.95.0",
13+
"@graphprotocol/graph-ts": "0.37.0"
14+
}
15+
}

block-stats-subgraph/schema.graphql

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
type BlockDataSource @entity {
2+
id: ID!
3+
number: BigInt!
4+
blockTime: BigInt
5+
gasUsed: BigInt
6+
size: BigInt
7+
}
8+
9+
type Block @entity(timeseries: true) {
10+
id: Int8!
11+
timestamp: Timestamp!
12+
13+
hash: Bytes!
14+
number: BigInt!
15+
blockTime: BigInt!
16+
gasUsed: BigInt!
17+
size: BigInt!
18+
}
19+
20+
type Stats @aggregation(intervals: ["hour", "day"], source: "Block") {
21+
id: Int8!
22+
timestamp: Timestamp!
23+
24+
count: Int! @aggregate(fn: "count")
25+
26+
minBlockTime: BigInt! @aggregate(fn: "min", arg: "blockTime")
27+
maxBlockTime: BigInt! @aggregate(fn: "max", arg: "blockTime")
28+
sumBlockTime: BigInt! @aggregate(fn: "sum", arg: "blockTime")
29+
firstBlockTime: BigInt! @aggregate(fn: "first", arg: "blockTime")
30+
lastBlockTime: BigInt! @aggregate(fn: "last", arg: "blockTime")
31+
allTimeMinBlockTime: BigInt! @aggregate(fn: "min", arg: "blockTime", cumulative: true)
32+
allTimeMaxBlockTime: BigInt! @aggregate(fn: "max", arg: "blockTime", cumulative: true)
33+
34+
minGasUsed: BigInt! @aggregate(fn: "min", arg: "gasUsed")
35+
maxGasUsed: BigInt! @aggregate(fn: "max", arg: "gasUsed")
36+
sumGasUsed: BigInt! @aggregate(fn: "sum", arg: "gasUsed")
37+
firstGasUsed: BigInt! @aggregate(fn: "first", arg: "gasUsed")
38+
lastGasUsed: BigInt! @aggregate(fn: "last", arg: "gasUsed")
39+
totalGasUsed: BigInt! @aggregate(fn: "sum", arg: "gasUsed", cumulative: true)
40+
allTimeMinGasUsed: BigInt! @aggregate(fn: "min", arg: "gasUsed", cumulative: true)
41+
allTimeMaxGasUsed: BigInt! @aggregate(fn: "max", arg: "gasUsed", cumulative: true)
42+
43+
minSize: BigInt! @aggregate(fn: "min", arg: "size")
44+
maxSize: BigInt! @aggregate(fn: "max", arg: "size")
45+
sumSize: BigInt! @aggregate(fn: "sum", arg: "size")
46+
firstSize: BigInt! @aggregate(fn: "first", arg: "size")
47+
lastSize: BigInt! @aggregate(fn: "last", arg: "size")
48+
totalSize: BigInt! @aggregate(fn: "sum", arg: "size", cumulative: true)
49+
allTimeMinSize: BigInt! @aggregate(fn: "min", arg: "size", cumulative: true)
50+
allTimeMaxSize: BigInt! @aggregate(fn: "max", arg: "size", cumulative: true)
51+
}

block-stats-subgraph/src/mapping.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {Bytes, EntityOp, EntityTrigger, store} from "@graphprotocol/graph-ts";
2+
import {Block, BlockDataSource} from "../generated/schema";
3+
import {BlockTime} from "../generated/subgraph-QmcKB3XQyfNM2Uzzeyd9UmGqsw83Ysh8t9LGQD94DdfSS7";
4+
import {BlockCost} from "../generated/subgraph-QmQ2kJphSSsSUXqnSAKLvxmhPGNxjVtrTsLTUPeCszns17";
5+
import {BlockSize} from "../generated/subgraph-QmQRWu5c2EqssTHjGJyD9cUKRrArX6TGtVUWdYajdeC4My";
6+
7+
export function handleBlockTime(trigger: EntityTrigger<BlockTime>): void {
8+
if (trigger.operation !== EntityOp.Create) {
9+
return;
10+
}
11+
12+
let blockData = BlockDataSource.load(trigger.data.id);
13+
14+
if (!blockData) {
15+
blockData = new BlockDataSource(trigger.data.id);
16+
blockData.number = trigger.data.number;
17+
}
18+
19+
blockData.blockTime = trigger.data.blockTime;
20+
blockData.save();
21+
22+
maybeCreateBlock(blockData);
23+
}
24+
25+
export function handleBlockCost(trigger: EntityTrigger<BlockCost>): void {
26+
if (trigger.operation !== EntityOp.Create) {
27+
return;
28+
}
29+
30+
let blockData = BlockDataSource.load(trigger.data.id);
31+
32+
if (!blockData) {
33+
blockData = new BlockDataSource(trigger.data.id);
34+
blockData.number = trigger.data.number;
35+
}
36+
37+
blockData.gasUsed = trigger.data.gasUsed;
38+
blockData.save();
39+
40+
maybeCreateBlock(blockData);
41+
}
42+
43+
export function handleBlockSize(trigger: EntityTrigger<BlockSize>): void {
44+
if (trigger.operation !== EntityOp.Create) {
45+
return;
46+
}
47+
48+
let blockData = BlockDataSource.load(trigger.data.id);
49+
50+
if (!blockData) {
51+
blockData = new BlockDataSource(trigger.data.id);
52+
blockData.number = trigger.data.number;
53+
}
54+
55+
blockData.size = trigger.data.size;
56+
blockData.save();
57+
58+
maybeCreateBlock(blockData);
59+
}
60+
61+
function maybeCreateBlock(blockData: BlockDataSource): void {
62+
if (blockData.blockTime === null || blockData.gasUsed === null || blockData.size === null) {
63+
return;
64+
}
65+
66+
let block = new Block('auto');
67+
68+
block.hash = Bytes.fromHexString(blockData.id);
69+
block.number = blockData.number;
70+
block.blockTime = blockData.blockTime!;
71+
block.gasUsed = blockData.gasUsed!;
72+
block.size = blockData.size!;
73+
74+
block.save();
75+
76+
store.remove("BlockDataSource", blockData.id);
77+
}

block-stats-subgraph/subgraph.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
specVersion: 1.3.0
2+
description: A subgraph that collects statistics about blocks.
3+
schema:
4+
file: ./schema.graphql
5+
dataSources:
6+
- kind: subgraph
7+
name: block-time-subgraph
8+
network: mainnet
9+
source:
10+
address: "QmcKB3XQyfNM2Uzzeyd9UmGqsw83Ysh8t9LGQD94DdfSS7"
11+
startBlock: 10000000
12+
mapping:
13+
apiVersion: 0.0.7
14+
language: wasm/assemblyscript
15+
file: ./src/mapping.ts
16+
entities:
17+
- BlockData
18+
abis: []
19+
handlers:
20+
- entity: BlockTime
21+
handler: handleBlockTime
22+
- kind: subgraph
23+
name: block-cost-subgraph
24+
network: mainnet
25+
source:
26+
address: "QmQ2kJphSSsSUXqnSAKLvxmhPGNxjVtrTsLTUPeCszns17"
27+
startBlock: 10000000
28+
mapping:
29+
apiVersion: 0.0.7
30+
language: wasm/assemblyscript
31+
file: ./src/mapping.ts
32+
entities:
33+
- BlockData
34+
abis: []
35+
handlers:
36+
- entity: BlockCost
37+
handler: handleBlockCost
38+
- kind: subgraph
39+
name: block-size-subgraph
40+
network: mainnet
41+
source:
42+
address: "QmQRWu5c2EqssTHjGJyD9cUKRrArX6TGtVUWdYajdeC4My"
43+
startBlock: 10000000
44+
mapping:
45+
apiVersion: 0.0.7
46+
language: wasm/assemblyscript
47+
file: ./src/mapping.ts
48+
entities:
49+
- BlockData
50+
abis: []
51+
handlers:
52+
- entity: BlockSize
53+
handler: handleBlockSize

0 commit comments

Comments
 (0)