Skip to content

Commit

Permalink
feat: add hash, blockNumber, and timestamp of proposal action transac…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
coreyar committed Mar 13, 2024
1 parent 6b2bd94 commit a348169
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 31 deletions.
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ services:
condition: service_started
environment:
ETHEREUM_REORG_THRESHOLD: 1
GRAPH_ALLOW_NON_DETERMINISTIC_FULLTEXT_SEARCH: "true"
postgres_host: postgres
postgres_user: graph-node
postgres_pass: let-me-in
Expand Down
23 changes: 20 additions & 3 deletions subgraphs/venus-governance/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ type _Schema_
include: [{ entity: "Proposal", fields: [{ name: "description" }] }]
)

type ProposalAction @entity {
"Transaction hash used as the ID"
id: ID!

"Transaction hash for the proposal action"
txHash: Bytes!

"Block number of the proposal action"
blockNumber: BigInt!

"Timestamp of the transaction block"
timestamp: BigInt!
}

type Proposal @entity {
"Internal proposal ID, in this implementation it seems to be a autoincremental id"
id: ID!
Expand Down Expand Up @@ -70,13 +84,13 @@ type Proposal @entity {
executionEta: BigInt

"Whether a proposal has been queued"
queued: Boolean
queued: ProposalAction

"Whether a proposal has been canceled"
canceled: Boolean
canceled: ProposalAction

"Whether a proposal has been executed"
executed: Boolean
executed: ProposalAction

"Total of for votes on the proposal"
forVotes: BigInt!
Expand All @@ -87,6 +101,9 @@ type Proposal @entity {
"Total of abstain votes on the proposal"
abstainVotes: BigInt!

"Difference between for and against"
passing: Boolean!

"Votes associated to this proposal"
votes: [Vote!]! @derivedFrom(field: "proposal")

Expand Down
4 changes: 1 addition & 3 deletions subgraphs/venus-governance/src/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ export function createProposal<E>(event: E): Proposal {
proposal.startBlock = event.params.startBlock;
proposal.endBlock = event.params.endBlock;
proposal.description = event.params.description;
proposal.queued = false;
proposal.canceled = false;
proposal.executed = false;
proposal.type = NORMAL;
proposal.forVotes = BIGINT_ZERO;
proposal.againstVotes = BIGINT_ZERO;
proposal.abstainVotes = BIGINT_ZERO;
proposal.passing = false;

proposal.save();

Expand Down
28 changes: 24 additions & 4 deletions subgraphs/venus-governance/src/operations/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BigInt } from '@graphprotocol/graph-ts';

import { GovernorBravoDelegate2 } from '../../generated/GovernorBravoDelegate2/GovernorBravoDelegate2';
import { Governance } from '../../generated/schema';
import { Governance, ProposalAction } from '../../generated/schema';
import { BIGINT_ONE } from '../constants';
import { governorBravoDelegatorAddress, nullAddress } from '../constants/addresses';
import { getGovernanceId } from '../utilities/ids';
Expand All @@ -12,15 +12,27 @@ export function updateProposalCanceled<E>(event: E): void {
const params = event.params;
const proposal = getProposal(params.id.toString());

proposal.canceled = true;
const canceledAction = new ProposalAction(event.transaction.hash.toHexString());
canceledAction.blockNumber = event.block.number;
canceledAction.timestamp = event.block.timestamp;
canceledAction.txHash = event.transaction.hash;
canceledAction.save();

proposal.canceled = canceledAction.id;
proposal.save();
}

export function updateProposalQueued<E>(event: E): void {
const params = event.params;
const proposal = getProposal(params.id.toString());

proposal.queued = true;
const queuedAction = new ProposalAction(event.transaction.hash.toHexString());
queuedAction.blockNumber = event.block.number;
queuedAction.timestamp = event.block.timestamp;
queuedAction.txHash = event.transaction.hash;
queuedAction.save();

proposal.queued = queuedAction.id;
proposal.executionEta = params.eta;
proposal.save();
}
Expand All @@ -29,7 +41,13 @@ export function updateProposalExecuted<E>(event: E): void {
const params = event.params;
const proposal = getProposal(params.id.toString());

proposal.executed = true;
const executedAction = new ProposalAction(event.transaction.hash.toHexString());
executedAction.blockNumber = event.block.number;
executedAction.timestamp = event.block.timestamp;
executedAction.txHash = event.transaction.hash;
executedAction.save();

proposal.executed = executedAction.id;
proposal.save();
}

Expand Down Expand Up @@ -104,6 +122,7 @@ export function updateAlphaProposalVotes(id: BigInt, votes: BigInt, support: boo
} else {
proposal.againstVotes = proposal.againstVotes.plus(votes);
}
proposal.passing = proposal.forVotes > proposal.againstVotes;
proposal.save();
}

Expand All @@ -116,5 +135,6 @@ export function updateBravoProposalVotes(id: BigInt, votes: BigInt, support: i32
} else {
proposal.abstainVotes = proposal.abstainVotes.plus(votes);
}
proposal.passing = proposal.forVotes > proposal.againstVotes;
proposal.save();
}
15 changes: 12 additions & 3 deletions subgraphs/venus-governance/tests/integration/alpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('GovernorAlpha', function () {
expect(proposal.forVotes).to.be.equal(scaleValue(1000000, 18).toFixed());
expect(proposal.againstVotes).to.be.equal(scaleValue(100000, 18).toFixed());
expect(proposal.abstainVotes).to.be.equal('0');
expect(proposal.passing).to.be.equal(true);

const {
data: { delegate: delegate1 },
Expand Down Expand Up @@ -119,7 +120,9 @@ describe('GovernorAlpha', function () {
data: { proposal },
} = await subgraphClient.getProposalById('1');

expect(proposal.canceled).to.equal(true);
expect(typeof proposal.canceled.blockNumber).to.equal('string');
expect(typeof proposal.canceled.txHash).to.equal('string');
expect(typeof proposal.canceled.timestamp).to.equal('string');
});
});

Expand Down Expand Up @@ -165,6 +168,7 @@ describe('GovernorAlpha', function () {
expect(proposal.forVotes).to.be.equal(scaleValue(800000, 18).toFixed());
expect(proposal.againstVotes).to.be.equal('0');
expect(proposal.abstainVotes).to.be.equal('0');
expect(proposal.passing).to.be.equal(true);
});

it('should transition to queued', async () => {
Expand All @@ -188,7 +192,10 @@ describe('GovernorAlpha', function () {
data: { proposal },
} = await subgraphClient.getProposalById('21');

expect(proposal.queued).to.equal(true);
expect(typeof proposal.queued.blockNumber).to.equal('string');
expect(typeof proposal.queued.txHash).to.equal('string');
expect(typeof proposal.queued.timestamp).to.equal('string');

expect(proposal.executionEta).to.equal(eta.toString());
await mine(1);

Expand All @@ -204,7 +211,9 @@ describe('GovernorAlpha', function () {
data: { proposal },
} = await subgraphClient.getProposalById('21');

expect(proposal.executed).to.equal(true);
expect(typeof proposal.executed.blockNumber).to.equal('string');
expect(typeof proposal.executed.txHash).to.equal('string');
expect(typeof proposal.executed.timestamp).to.equal('string');
});
});
});
29 changes: 23 additions & 6 deletions subgraphs/venus-governance/tests/integration/bravo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ describe('GovernorBravo', function () {
expect(proposal.againstVotes).to.be.equal(scaleValue(700000, 18).toFixed());
expect(proposal.forVotes).to.be.equal(scaleValue(200000, 18).toFixed());
expect(proposal.abstainVotes).to.be.equal('0');
expect(proposal.passing).to.be.equal(false);

const {
data: { delegate: delegate1 },
Expand Down Expand Up @@ -135,7 +136,9 @@ describe('GovernorBravo', function () {
data: { proposal },
} = await subgraphClient.getProposalById('22');

expect(proposal.canceled).to.equal(true);
expect(typeof proposal.canceled.blockNumber).to.equal('string');
expect(typeof proposal.canceled.txHash).to.equal('string');
expect(typeof proposal.canceled.timestamp).to.equal('string');
});

it('should index queued proposal event', async function () {
Expand Down Expand Up @@ -175,12 +178,15 @@ describe('GovernorBravo', function () {
data: { proposal },
} = await subgraphClient.getProposalById('23');

expect(proposal.queued).to.equal(true);
expect(typeof proposal.queued.blockNumber).to.equal('string');
expect(typeof proposal.queued.txHash).to.equal('string');
expect(typeof proposal.queued.timestamp).to.equal('string');
expect(proposal.executionEta).to.equal(eta.toString());

expect(proposal.againstVotes).to.be.equal('0');
expect(proposal.forVotes).to.be.equal(scaleValue(800000, 18).toFixed());
expect(proposal.abstainVotes).to.be.equal('0');
expect(proposal.passing).to.be.equal(true);

await mine(1);
await ethers.provider.send('evm_setNextBlockTimestamp', [eta]);
Expand All @@ -195,7 +201,9 @@ describe('GovernorBravo', function () {
data: { proposal },
} = await subgraphClient.getProposalById('23');

expect(proposal.executed).to.equal(true);
expect(typeof proposal.executed.blockNumber).to.equal('string');
expect(typeof proposal.executed.txHash).to.equal('string');
expect(typeof proposal.executed.timestamp).to.equal('string');
});
});

Expand Down Expand Up @@ -317,6 +325,7 @@ describe('GovernorBravo', function () {
expect(proposal.againstVotes).to.be.equal(scaleValue(100000, 18).toFixed());
expect(proposal.forVotes).to.be.equal(scaleValue(1000000, 18).toFixed());
expect(proposal.abstainVotes).to.be.equal('0');
expect(proposal.passing).to.be.equal(true);

const {
data: { delegate: delegate1 },
Expand Down Expand Up @@ -352,7 +361,9 @@ describe('GovernorBravo', function () {
data: { proposal },
} = await subgraphClient.getProposalById('24');

expect(proposal.canceled).to.equal(true);
expect(typeof proposal.canceled.blockNumber).to.equal('string');
expect(typeof proposal.canceled.txHash).to.equal('string');
expect(typeof proposal.canceled.timestamp).to.equal('string');
});

it('should index queued proposal event', async function () {
Expand Down Expand Up @@ -399,12 +410,16 @@ describe('GovernorBravo', function () {
data: { proposal },
} = await subgraphClient.getProposalById('25');

expect(proposal.queued).to.equal(true);
expect(typeof proposal.queued.blockNumber).to.equal('string');
expect(typeof proposal.queued.txHash).to.equal('string');
expect(typeof proposal.queued.timestamp).to.equal('string');

expect(proposal.executionEta).to.equal(eta.toString());

expect(proposal.againstVotes).to.be.equal('0');
expect(proposal.forVotes).to.be.equal(scaleValue(800000, 18).toFixed());
expect(proposal.abstainVotes).to.be.equal('0');
expect(proposal.passing).to.be.equal(true);

await mine(1);
await ethers.provider.send('evm_setNextBlockTimestamp', [eta]);
Expand All @@ -419,7 +434,9 @@ describe('GovernorBravo', function () {
data: { proposal },
} = await subgraphClient.getProposalById('25');

expect(proposal.executed).to.equal(true);
expect(typeof proposal.executed.blockNumber).to.equal('string');
expect(typeof proposal.executed.txHash).to.equal('string');
expect(typeof proposal.executed.timestamp).to.equal('string');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,26 @@ query ProposalById($id: ID!) {
endBlock
description
executionEta
queued
canceled
executed
queued {
blockNumber
timestamp
txHash
}
canceled {
blockNumber
timestamp
txHash
}
executed {
blockNumber
timestamp
txHash
}
type
forVotes
againstVotes
abstainVotes
passing
votes {
id
votes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,26 @@ query Proposals {
endBlock
description
executionEta
queued
canceled
executed
queued {
blockNumber
timestamp
txHash
}
canceled {
blockNumber
timestamp
txHash
}
executed {
blockNumber
timestamp
txHash
}
type
forVotes
againstVotes
abstainVotes
passing
votes {
id
votes
Expand Down
6 changes: 3 additions & 3 deletions subgraphs/venus-governance/tests/unit/Alpha/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('Alpha', () => {
const assertProposalDocument = (key: string, value: string): void => {
assert.fieldEquals('Proposal', '1', key, value);
};
assertProposalDocument('canceled', 'true');
assertProposalDocument('canceled', proposalCanceledEvent.transaction.hash.toHexString());
});

test('queue proposal', () => {
Expand All @@ -120,7 +120,7 @@ describe('Alpha', () => {
assert.fieldEquals('Proposal', '1', key, value);
};

assertProposalDocument('queued', 'true');
assertProposalDocument('queued', proposalQueuedEvent.transaction.hash.toHexString());
assertProposalDocument('executionEta', eta.toString());
});

Expand All @@ -138,7 +138,7 @@ describe('Alpha', () => {
assert.fieldEquals('Proposal', '1', key, value);
};

assertProposalDocument('executed', 'true');
assertProposalDocument('executed', proposalExecutedEvent.transaction.hash.toHexString());
});

test('vote cast', () => {
Expand Down
6 changes: 3 additions & 3 deletions subgraphs/venus-governance/tests/unit/Bravo/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('Bravo', () => {
const assertProposalDocument = (key: string, value: string): void => {
assert.fieldEquals('Proposal', '1', key, value);
};
assertProposalDocument('canceled', 'true');
assertProposalDocument('canceled', proposalCanceledEvent.transaction.hash.toHexString());
});

test('queue proposal', () => {
Expand All @@ -215,7 +215,7 @@ describe('Bravo', () => {
assert.fieldEquals('Proposal', '1', key, value);
};

assertProposalDocument('queued', 'true');
assertProposalDocument('queued', proposalQueuedEvent.transaction.hash.toHexString());
assertProposalDocument('executionEta', eta.toString());
});

Expand All @@ -233,7 +233,7 @@ describe('Bravo', () => {
assert.fieldEquals('Proposal', '1', key, value);
};

assertProposalDocument('executed', 'true');
assertProposalDocument('executed', proposalExecutedEvent.transaction.hash.toHexString());
});

test('vote cast', () => {
Expand Down

0 comments on commit a348169

Please sign in to comment.