Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add search schema to filter proposals by description #145

Merged
merged 16 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@typescript-eslint/eslint-plugin": "^5.40.1",
"@typescript-eslint/parser": "^5.40.1",
"@venusprotocol/governance-contracts": "1.4.0",
"@venusprotocol/isolated-pools": "2.5.0",
"@venusprotocol/isolated-pools": "2.6.0",
"@venusprotocol/oracle": "^1.9.0",
"@venusprotocol/protocol-reserve": "1.4.0",
"@venusprotocol/venus-protocol": "7.2.0",
Expand Down
16 changes: 15 additions & 1 deletion subgraphs/isolated-pools/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import mainnetDeployments from '@venusprotocol/isolated-pools/deployments/bscmainnet.json';
import chapelDeployments from '@venusprotocol/isolated-pools/deployments/bsctestnet.json';
import ethereumDeployments from '@venusprotocol/isolated-pools/deployments/ethereum_addresses.json';
import opBnbMainnetDeployments from '@venusprotocol/isolated-pools/deployments/opbnbmainnet_addresses.json';
import sepoliaDeployments from '@venusprotocol/isolated-pools/deployments/sepolia.json';
import fs from 'fs';
import Mustache from 'mustache';

export const getNetwork = () => {
const supportedNetworks = ['mainnet', 'sepolia', 'chapel', 'bsc', 'docker'] as const;
const supportedNetworks = [
'mainnet',
'sepolia',
'chapel',
'bsc',
'docker',
'opbnbMainnet',
] as const;
const network = process.env.NETWORK;
// @ts-expect-error network env var is unknown here
if (!supportedNetworks.includes(network)) {
Expand Down Expand Up @@ -48,6 +56,12 @@ const main = () => {
poolLensAddress: mainnetDeployments.contracts.PoolLens.address,
startBlock: '29300000',
},
opbnbMainnet: {
network: 'opbnb-mainnet',
poolRegistryAddress: opBnbMainnetDeployments.addresses.PoolRegistry,
poolLensAddress: opBnbMainnetDeployments.addresses.PoolLens,
startBlock: '16232873',
},
};

const yamlTemplate = fs.readFileSync('template.yaml', 'utf8');
Expand Down
2 changes: 2 additions & 0 deletions subgraphs/isolated-pools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
"deploy:bsc": "yarn prepare:bsc && graph deploy venusprotocol/venus-isolated-pools --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy:ethereum": "yarn prepare:ethereum && npx graph deploy venusprotocol/venus-isolated-pools-ethereum --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy:sepolia": "yarn prepare:sepolia && npx graph deploy venusprotocol/venus-isolated-pools-sepolia --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/",
"deploy:opbnbMainnet": "yarn prepare:opbnbMainnet && npx graph deploy venusprotocol/venus-isolated-pools-opbnb --ipfs https://api.thegraph.com/ipfs/ --node https://open-platform-ap.nodereal.io/5c42a03458b64b33af7cf9ff0c70c088/opbnb-mainnet-graph-indexer/",
"prepare:docker": "NETWORK=docker npx ts-node config/index.ts",
"prepare:chapel": "NETWORK=chapel npx ts-node config/index.ts",
"prepare:bsc": "NETWORK=bsc npx ts-node config/index.ts",
"prepare:ethereum": "NETWORK=mainnet npx ts-node config/index.ts",
"prepare:sepolia": "NETWORK=sepolia npx ts-node config/index.ts",
"prepare:opbnbMainnet": "NETWORK=opbnbMainnet npx ts-node config/index.ts",
"generate-subgraph-types": "rm -rf /subgraph-client/.graphclient && npx graphclient build --dir ./subgraph-client",
"pretty": "prettier —-write '**/*.ts'",
"test": "yarn prepare:docker && graph test",
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');
});
});
});
Loading
Loading