Skip to content

Commit

Permalink
Merge pull request #145 from VenusProtocol/add-search-schema-to-propo…
Browse files Browse the repository at this point in the history
…sal-entity

feat: add search schema to filter proposals by description
  • Loading branch information
coreyar authored Mar 27, 2024
2 parents 2edc35f + a348169 commit e8741f2
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 33 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
40 changes: 37 additions & 3 deletions subgraphs/venus-governance/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ enum PROPOSAL_TYPE {
CRITICAL
}

type _Schema_
@fulltext(
name: "proposalSearch"
language: en
algorithm: rank
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 @@ -62,13 +84,25 @@ 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!

"Total of against votes on the proposal"
againstVotes: BigInt!

"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
3 changes: 3 additions & 0 deletions subgraphs/venus-governance/src/mappings/alpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { createProposal, createVoteAlpha } from '../operations/create';
import { getOrCreateDelegate } from '../operations/getOrCreate';
import {
updateAlphaProposalVotes,
updateProposalCanceled,
updateProposalExecuted,
updateProposalQueued,
Expand Down Expand Up @@ -61,8 +62,10 @@ export function handleVoteCast(event: VoteCast): void {
// Alpha V1 doesn't require staking in the vault so we need to create delegates when casting a vote
getOrCreateDelegate(event.params.voter);
createVoteAlpha(event);
updateAlphaProposalVotes(event.params.proposalId, event.params.votes, event.params.support);
}

export function handleVoteCastV2(event: VoteCast): void {
createVoteAlpha(event);
updateAlphaProposalVotes(event.params.proposalId, event.params.votes, event.params.support);
}
2 changes: 2 additions & 0 deletions subgraphs/venus-governance/src/mappings/bravo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { createProposal, createVoteBravo } from '../operations/create';
import { getGovernanceEntity } from '../operations/get';
import { getOrCreateDelegate } from '../operations/getOrCreate';
import {
updateBravoProposalVotes,
updateGovernanceEntity,
updateProposalCanceled,
updateProposalExecuted,
Expand Down Expand Up @@ -49,6 +50,7 @@ export function handleProposalExecuted(event: ProposalExecuted): void {

export function handleBravoVoteCast(event: VoteCast): void {
createVoteBravo(event);
updateBravoProposalVotes(event.params.proposalId, event.params.votes, event.params.support);
}

export function handleNewImplementation(event: NewImplementation): void {
Expand Down
9 changes: 5 additions & 4 deletions subgraphs/venus-governance/src/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Address, Bytes } from '@graphprotocol/graph-ts';
import { VoteCast as VoteCastAlpha } from '../../generated/GovernorAlpha/GovernorAlpha';
import { VoteCast as VoteCastBravo } from '../../generated/GovernorBravoDelegate/GovernorBravoDelegate';
import { Proposal, Vote } from '../../generated/schema';
import { ABSTAIN, AGAINST, BIGINT_ONE, FOR, NORMAL } from '../constants';
import { ABSTAIN, AGAINST, BIGINT_ONE, BIGINT_ZERO, FOR, NORMAL } from '../constants';
import { getVoteId } from '../utilities/ids';
import { getGovernanceEntity, getProposal } from './get';
import { getOrCreateDelegate } from './getOrCreate';
Expand All @@ -27,10 +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
52 changes: 48 additions & 4 deletions subgraphs/venus-governance/src/operations/update.ts
Original file line number Diff line number Diff line change
@@ -1,5 +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 @@ -10,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 @@ -27,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 @@ -94,3 +114,27 @@ export function updateGovernanceEntity(): void {
governance.proposalMaxOperations = governorBravoDelegate2.proposalMaxOperations();
governance.save();
}

export function updateAlphaProposalVotes(id: BigInt, votes: BigInt, support: boolean): void {
const proposal = getProposal(id.toString());
if (support) {
proposal.forVotes = proposal.forVotes.plus(votes);
} else {
proposal.againstVotes = proposal.againstVotes.plus(votes);
}
proposal.passing = proposal.forVotes > proposal.againstVotes;
proposal.save();
}

export function updateBravoProposalVotes(id: BigInt, votes: BigInt, support: i32): void {
const proposal = getProposal(id.toString());
if (support == 0) {
proposal.againstVotes = proposal.againstVotes.plus(votes);
} else if (support == 1) {
proposal.forVotes = proposal.forVotes.plus(votes);
} else {
proposal.abstainVotes = proposal.abstainVotes.plus(votes);
}
proposal.passing = proposal.forVotes > proposal.againstVotes;
proposal.save();
}
2 changes: 2 additions & 0 deletions subgraphs/venus-governance/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ description: Venus Governance Subgraph
repository: https://github.com/protofire/venus-governance-subgraph
schema:
file: ./schema.graphql
features:
- fullTextSearch
dataSources:
- kind: ethereum/contract
name: GovernorAlpha
Expand Down
23 changes: 20 additions & 3 deletions subgraphs/venus-governance/tests/integration/alpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ describe('GovernorAlpha', function () {
} = await subgraphClient.getProposalById('1');
expect(proposal.votes.length).to.be.equal(4);

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 },
} = await subgraphClient.getDelegateById(user1.address.toLowerCase());
Expand Down Expand Up @@ -115,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 @@ -157,6 +164,11 @@ describe('GovernorAlpha', function () {
expect(proposal.values).to.deep.equal(['0']);
expect(proposal.signatures).to.deep.equal(['setPendingAdmin(address)']);
expect(proposal.calldatas).to.deep.equal([callData]);

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 @@ -180,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 @@ -196,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');
});
});
});
48 changes: 41 additions & 7 deletions subgraphs/venus-governance/tests/integration/bravo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { expect } from 'chai';
import { Contract } from 'ethers';
import { ethers, network } from 'hardhat';
import { waitForSubgraphToBeSynced } from 'venus-subgraph-utils';
import { scaleValue, waitForSubgraphToBeSynced } from 'venus-subgraph-utils';

import subgraphClient from '../../subgraph-client/index';
import { SYNC_DELAY, mockAddress } from './utils/constants';
Expand Down Expand Up @@ -102,6 +102,11 @@ describe('GovernorBravo', function () {
} = await subgraphClient.getProposalById('22');
expect(proposal.votes.length).to.be.equal(2);

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 },
} = await subgraphClient.getDelegateById(user1.address.toLowerCase());
Expand Down Expand Up @@ -131,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 @@ -171,8 +178,16 @@ 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 @@ -186,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 @@ -305,6 +322,11 @@ describe('GovernorBravo', function () {
} = await subgraphClient.getProposalById('1');
expect(proposal.votes.length).to.be.equal(4);

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 },
} = await subgraphClient.getDelegateById(user1.address.toLowerCase());
Expand Down Expand Up @@ -339,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 @@ -386,9 +410,17 @@ 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 @@ -402,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');
});
});
});
Loading

0 comments on commit e8741f2

Please sign in to comment.