From 44094f4b0d3e250786b1a21e4f0f2daa596167f1 Mon Sep 17 00:00:00 2001 From: Anton <14254374+0xmad@users.noreply.github.com> Date: Thu, 13 Jun 2024 12:30:01 -0500 Subject: [PATCH] feat(subgraph): optimize subgraph schema --- subgraph/.gitignore | 2 ++ subgraph/config/optimism-sepolia.json | 4 ++-- subgraph/schemas/schema.v1.graphql | 31 ++++++++---------------- subgraph/src/maci.ts | 34 +++++++++++++-------------- subgraph/src/poll.ts | 12 ---------- subgraph/src/utils/entity.ts | 11 ++------- 6 files changed, 32 insertions(+), 62 deletions(-) diff --git a/subgraph/.gitignore b/subgraph/.gitignore index 477b9b7972..fb74381a00 100644 --- a/subgraph/.gitignore +++ b/subgraph/.gitignore @@ -6,4 +6,6 @@ subgraph.yaml schema.graphql tests/.bin/ tests/.latest.json +config/*.json +!config/[network].json diff --git a/subgraph/config/optimism-sepolia.json b/subgraph/config/optimism-sepolia.json index a36409b375..35c469d7ba 100644 --- a/subgraph/config/optimism-sepolia.json +++ b/subgraph/config/optimism-sepolia.json @@ -1,5 +1,5 @@ { "network": "optimism-sepolia", - "maciContractAddress": "0xD18Ca45b6cC1f409380731C40551BD66932046c3", - "maciContractStartBlock": 11052407 + "maciContractAddress": "0xbE6e250bf8B5F689c65Cc79667589A9EBF6Fe8E3", + "maciContractStartBlock": 13160483 } diff --git a/subgraph/schemas/schema.v1.graphql b/subgraph/schemas/schema.v1.graphql index fd1c3f29fc..462d591943 100644 --- a/subgraph/schemas/schema.v1.graphql +++ b/subgraph/schemas/schema.v1.graphql @@ -2,8 +2,6 @@ type MACI @entity { id: ID! stateTreeDepth: BigInt! # uint8 updatedAt: BigInt! - blockNumber: BigInt! - txHash: Bytes! "state" numSignUps: BigInt! @@ -13,10 +11,7 @@ type MACI @entity { type User @entity(immutable: true) { id: ID! # pubkey - timestamp: BigInt! # uint256 - blockNumber: BigInt! - txHash: Bytes! - + createdAt: BigInt! # uint256 "relations" accounts: [Account!]! @derivedFrom(field: "owner") } @@ -24,23 +19,23 @@ type User @entity(immutable: true) { type Account @entity { id: ID! # stateIndex voiceCreditBalance: BigInt! # uint256 - timestamp: BigInt! # uint256 - blockNumber: BigInt! - txHash: Bytes! - + createdAt: BigInt! # uint256 "relations" owner: User! } type Poll @entity { id: Bytes! # poll address - pollId: BigInt # uint256 + pollId: BigInt! # uint256 duration: BigInt! # uint256 treeDepth: BigInt! # uint8 - maxMessages: BigInt - maxVoteOption: BigInt - messageProcessor: Bytes # address - tally: Bytes # address + maxMessages: BigInt! + maxVoteOption: BigInt! + messageProcessor: Bytes! # address + tally: Bytes! # address + createdAt: BigInt! + updatedAt: BigInt! + "merge state after ended" stateRoot: BigInt # uint256 numSignups: BigInt! # uint256 @@ -48,10 +43,6 @@ type Poll @entity { "merge message tree after ended" numSrQueueOps: BigInt # uint256 messageRoot: BigInt - createdAt: BigInt! # startedAt - updatedAt: BigInt! - blockNumber: BigInt! - txHash: Bytes! "relations" owner: Bytes! @@ -62,8 +53,6 @@ type Vote @entity(immutable: true) { id: Bytes! data: [BigInt!]! # uint256[10] timestamp: BigInt! - blockNumber: BigInt! - txHash: Bytes! "relations" poll: Poll! diff --git a/subgraph/src/maci.ts b/subgraph/src/maci.ts index 2b3d834958..0c5633937b 100644 --- a/subgraph/src/maci.ts +++ b/subgraph/src/maci.ts @@ -12,38 +12,36 @@ import { createOrLoadMACI, createOrLoadUser, createOrLoadAccount } from "./utils export function handleDeployPoll(event: DeployPollEvent): void { const maci = createOrLoadMACI(event); - const entity = new Poll(event.params.pollAddr.poll); + const poll = new Poll(event.params.pollAddr.poll); const contract = PollContract.bind(event.params.pollAddr.poll); const maxValues = contract.maxValues(); const treeDepths = contract.treeDepths(); const durations = contract.getDeployTimeAndDuration(); - entity.pollId = event.params._pollId; - entity.messageProcessor = event.params.pollAddr.messageProcessor; - entity.tally = event.params.pollAddr.tally; - entity.maxMessages = maxValues.value0; - entity.maxVoteOption = maxValues.value1; - entity.treeDepth = GraphBN.fromI32(treeDepths.value0); - entity.duration = durations.value1; + poll.pollId = event.params._pollId; + poll.messageProcessor = event.params.pollAddr.messageProcessor; + poll.tally = event.params.pollAddr.tally; + poll.maxMessages = maxValues.value0; + poll.maxVoteOption = maxValues.value1; + poll.treeDepth = GraphBN.fromI32(treeDepths.value0); + poll.duration = durations.value1; - entity.blockNumber = event.block.number; - entity.createdAt = event.block.timestamp; - entity.updatedAt = event.block.timestamp; - entity.txHash = event.transaction.hash; - entity.owner = event.transaction.from; + poll.createdAt = event.block.timestamp; + poll.updatedAt = event.block.timestamp; + poll.owner = event.transaction.from; - entity.numSignups = maci.numSignUps; - entity.numMessages = GraphBN.zero(); - entity.save(); + poll.numSignups = maci.numSignUps; + poll.numMessages = GraphBN.zero(); + poll.save(); maci.numPoll = maci.numPoll.plus(ONE_BIG_INT); - maci.latestPoll = entity.id; + maci.latestPoll = poll.id; maci.updatedAt = event.block.timestamp; maci.save(); // Start indexing the poll; `event.params.pollAddr.poll` is the // address of the new poll contract - PollTemplate.create(Address.fromBytes(entity.id)); + PollTemplate.create(Address.fromBytes(poll.id)); } export function handleSignUp(event: SignUpEvent): void { diff --git a/subgraph/src/poll.ts b/subgraph/src/poll.ts index 9b76d469e9..f768761aff 100644 --- a/subgraph/src/poll.ts +++ b/subgraph/src/poll.ts @@ -17,10 +17,7 @@ export function handleMergeMaciState(event: MergeMaciStateEvent): void { if (poll) { poll.stateRoot = event.params._stateRoot; poll.numSignups = event.params._numSignups; - - poll.blockNumber = event.block.number; poll.updatedAt = event.block.timestamp; - poll.txHash = event.transaction.hash; poll.save(); } @@ -35,10 +32,7 @@ export function handleMergeMessageAq(event: MergeMessageAqEvent): void { if (poll) { poll.messageRoot = event.params._messageRoot; - - poll.blockNumber = event.block.number; poll.updatedAt = event.block.timestamp; - poll.txHash = event.transaction.hash; poll.save(); } } @@ -48,10 +42,7 @@ export function handleMergeMessageAqSubRoots(event: MergeMessageAqSubRootsEvent) if (poll) { poll.numSrQueueOps = event.params._numSrQueueOps; - - poll.blockNumber = event.block.number; poll.updatedAt = event.block.timestamp; - poll.txHash = event.transaction.hash; poll.save(); } } @@ -60,10 +51,7 @@ export function handlePublishMessage(event: PublishMessageEvent): void { const vote = new Vote(event.transaction.hash.concatI32(event.logIndex.toI32())); vote.data = event.params._message.data; vote.poll = event.address; - - vote.blockNumber = event.block.number; vote.timestamp = event.block.timestamp; - vote.txHash = event.transaction.hash; vote.save(); const poll = Poll.load(event.address); diff --git a/subgraph/src/utils/entity.ts b/subgraph/src/utils/entity.ts index 916694709f..2e2d60d3b6 100644 --- a/subgraph/src/utils/entity.ts +++ b/subgraph/src/utils/entity.ts @@ -12,9 +12,6 @@ export const createOrLoadMACI = (event: ethereum.Event, stateTreeDepth: GraphBN maci = new MACI(DEFAULT_MACI_ID); maci.stateTreeDepth = stateTreeDepth; maci.updatedAt = event.block.timestamp; - maci.blockNumber = event.block.number; - maci.txHash = event.transaction.hash; - maci.numPoll = GraphBN.zero(); maci.numSignUps = GraphBN.zero(); maci.latestPoll = Bytes.empty(); @@ -30,9 +27,7 @@ export const createOrLoadUser = (publicKeyX: GraphBN, publicKeyY: GraphBN, event if (!user) { user = new User(publicKey); - user.timestamp = event.block.timestamp; - user.blockNumber = event.block.number; - user.txHash = event.transaction.hash; + user.createdAt = event.block.timestamp; user.save(); } @@ -52,9 +47,7 @@ export const createOrLoadAccount = ( account = new Account(id); account.owner = owner; account.voiceCreditBalance = voiceCreditBalance; - account.timestamp = event.block.timestamp; - account.blockNumber = event.block.number; - account.txHash = event.transaction.hash; + account.createdAt = event.block.timestamp; account.save(); }