Skip to content

Commit

Permalink
Index Governor.VoteCastWithParams (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Jun 14, 2022
1 parent 7bf974b commit 8bb9e7f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 11 deletions.
11 changes: 7 additions & 4 deletions CHANGELOG.md
@@ -1,22 +1,25 @@
# Changelog

### Unreleased
* `Governor`: add handler for `VoteCastWithParams` events ([#30](https://github.com/OpenZeppelin/openzeppelin-subgraphs/pull/30))

### 0.1.8-1 (2022-07-11)
* `Governor`: improve indexing of proposal queeing
* `Governor`: improve indexing of proposal queeing ([#29](https://github.com/OpenZeppelin/openzeppelin-subgraphs/pull/29))

### 0.1.8 (2022-05-18)
* Update dependency to @graphprotocol/graph-cli version 0.29.x
* Update dependency to @graphprotocol/graph-ts version 0.26.x
* Update dependency to @amxx/graphprotocol-utils version 1.1.0
* Use Bytes for some entities ID
* Use Bytes for some entities ID ([#25](https://github.com/OpenZeppelin/openzeppelin-subgraphs/pull/25))
* Make events and some other entites immutable
* `Governor`: index the "counting mode" for OZ governors

### 0.1.7-1 (2022-01-27)
* `Governor`: fix vulnerability to ill-formed ProposalCreated events

### 0.1.7 (2022-01-27)
* `ERC1155`: fetch token uri on minting.
* `ERC1155`: fix vulnerability to ill-formed TransferBatch events
* `ERC1155`: fetch token uri on minting. ([#14](https://github.com/OpenZeppelin/openzeppelin-subgraphs/pull/14))
* `ERC1155`: fix vulnerability to ill-formed TransferBatch events ([#16](https://github.com/OpenZeppelin/openzeppelin-subgraphs/pull/16))

### 0.1.6 (2021-11-23)
* Include emmiter address (Account) to all Event objects
Expand Down
2 changes: 2 additions & 0 deletions generated/all.schema.graphql
Expand Up @@ -301,6 +301,7 @@ type ProposalSupport @entity(immutable: true) {
id: ID!
proposal: Proposal!
support: Int!
weight: BigInt!
votes: [VoteReceipt!]! @derivedFrom(field: "support")
}
type VoteReceipt @entity(immutable: true) {
Expand All @@ -310,6 +311,7 @@ type VoteReceipt @entity(immutable: true) {
support: ProposalSupport!
weight: BigInt!
reason: String!
params: Bytes
}
type ProposalCreated implements Event @entity(immutable: true) {
id: ID!
Expand Down
2 changes: 2 additions & 0 deletions generated/governor.schema.graphql
Expand Up @@ -51,6 +51,7 @@ type ProposalSupport @entity(immutable: true) {
id: ID!
proposal: Proposal!
support: Int!
weight: BigInt!
votes: [VoteReceipt!]! @derivedFrom(field: "support")
}
type VoteReceipt @entity(immutable: true) {
Expand All @@ -60,6 +61,7 @@ type VoteReceipt @entity(immutable: true) {
support: ProposalSupport!
weight: BigInt!
reason: String!
params: Bytes
}
type ProposalCreated implements Event @entity(immutable: true) {
id: ID!
Expand Down
4 changes: 3 additions & 1 deletion src/datasources/governor.gql.json
Expand Up @@ -60,6 +60,7 @@
"fields": [
{ "name": "proposal", "type": "Proposal!" },
{ "name": "support", "type": "Int!" },
{ "name": "weight", "type": "BigInt!" },
{ "name": "votes", "type": "VoteReceipt!", "derived": "support" }
]
},{
Expand All @@ -70,7 +71,8 @@
{ "name": "voter", "type": "Account!" },
{ "name": "support", "type": "ProposalSupport!" },
{ "name": "weight", "type": "BigInt!" },
{ "name": "reason", "type": "String!" }
{ "name": "reason", "type": "String!" },
{ "name": "params", "type": "Bytes" }
]
},{
"name": "ProposalCreated",
Expand Down
41 changes: 35 additions & 6 deletions src/datasources/governor.ts
Expand Up @@ -11,12 +11,13 @@ import {
} from '../../generated/schema'

import {
Governor as GovernorContract,
ProposalCreated as ProposalCreatedEvent,
ProposalQueued as ProposalQueuedEvent,
ProposalExecuted as ProposalExecutedEvent,
ProposalCanceled as ProposalCanceledEvent,
VoteCast as VoteCastEvent,
Governor as GovernorContract,
ProposalCreated as ProposalCreatedEvent,
ProposalQueued as ProposalQueuedEvent,
ProposalExecuted as ProposalExecutedEvent,
ProposalCanceled as ProposalCanceledEvent,
VoteCast as VoteCastEvent,
VoteCastWithParams as VoteCastWithParamsEvent
} from '../../generated/governor/Governor'

import {
Expand Down Expand Up @@ -146,3 +147,31 @@ export function handleVoteCast(event: VoteCastEvent): void {
ev.voter = receipt.voter
ev.save()
}

export function handleVoteCastWithParams(event: VoteCastWithParamsEvent): void {
let governor = fetchGovernor(event.address)

let proposal = fetchProposal(governor, event.params.proposalId)

let support = fetchProposalSupport(proposal, event.params.support)
support.weight += event.params.weight
support.save()

let receipt = fetchVoteReceipt(proposal, event.params.voter)
receipt.support = support.id
receipt.weight = event.params.weight
receipt.reason = event.params.reason
receipt.params = event.params.params
receipt.save()

let ev = new VoteCast(events.id(event))
ev.emitter = governor.id
ev.transaction = transactions.log(event).id
ev.timestamp = event.block.timestamp
ev.governor = governor.id
ev.proposal = receipt.proposal
ev.support = receipt.support
ev.receipt = receipt.id
ev.voter = receipt.voter
ev.save()
}
2 changes: 2 additions & 0 deletions src/datasources/governor.yaml
Expand Up @@ -25,4 +25,6 @@
handler: handleProposalQueued
- event: VoteCast(indexed address,uint256,uint8,uint256,string)
handler: handleVoteCast
- event: VoteCastWithParams(indexed address,uint256,uint8,uint256,string,bytes)
handler: handleVoteCastWithParams
file: {file}
5 changes: 5 additions & 0 deletions src/fetch/governor.ts
Expand Up @@ -20,6 +20,10 @@ import {
fetchAccount
} from './account'

import {
constants,
} from '@amxx/graphprotocol-utils'

export function fetchGovernor(address: Address): Governor {
let contract = Governor.load(address)

Expand Down Expand Up @@ -78,6 +82,7 @@ export function fetchProposalSupport(proposal: Proposal, support: i32): Proposal
proposalSupport = new ProposalSupport(id)
proposalSupport.proposal = proposal.id
proposalSupport.support = support
proposalSupport.weight = constants.BIGINT_ZERO
}

return proposalSupport as ProposalSupport
Expand Down

0 comments on commit 8bb9e7f

Please sign in to comment.