Skip to content

Commit

Permalink
refactor: remove proposal status in favor of event flags
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Oct 18, 2023
1 parent 6510461 commit 5b1a3ba
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 102 deletions.
25 changes: 10 additions & 15 deletions subgraphs/venus-governance/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
enum ProposalStatus {
PENDING
ACTIVE
CANCELLED
QUEUED
EXECUTED
}

type Delegate @entity {
"A Delegate is any address that has been delegated with voting tokens by a token holder, id is the blockchain address of said delegate"
id: ID!
Expand Down Expand Up @@ -66,11 +58,17 @@ type Proposal @entity {
"String description of the change"
description: String!

"Status of the proposal"
status: ProposalStatus!

"Once the proposal is queued for execution it will have an ETA of the execution"
executionETA: BigInt
executionEta: BigInt

"Whether a proposal has been queued"
queued: Boolean

"Whether a proposal has been canceled"
canceled: Boolean

"Whether a proposal has been executed"
executed: Boolean

"Votes associated to this proposal"
votes: [Vote!]! @derivedFrom(field: "proposal")
Expand Down Expand Up @@ -117,9 +115,6 @@ type Governance @entity {

"Total number of votes delegated expressed in the smallest unit of XVS"
totalVotesMantissa: BigInt!

"Number of proposals currently queued for execution"
proposalsQueued: BigInt!

"The number of votes required to reach quorum"
quorumVotesMantissa: BigInt!
Expand Down
5 changes: 0 additions & 5 deletions subgraphs/venus-governance/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import { BigDecimal, BigInt } from '@graphprotocol/graph-ts';
export const BIGINT_ZERO = BigInt.fromI32(0);
export const BIGINT_ONE = BigInt.fromI32(1);
export const BIGDECIMAL_ZERO = new BigDecimal(BIGINT_ZERO);
export const PENDING = 'PENDING';
export const CANCELLED = 'CANCELLED';
export const EXECUTED = 'EXECUTED';
export const QUEUED = 'QUEUED';
export const ACTIVE = 'ACTIVE';

// Vote support
export const FOR = 'FOR';
Expand Down
17 changes: 2 additions & 15 deletions subgraphs/venus-governance/src/mappings/alpha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import {
ProposalQueued,
VoteCast,
} from '../../generated/GovernorAlpha/GovernorAlpha';
import { ACTIVE, CANCELLED, PENDING } from '../constants';
import { createProposal, createVoteAlpha } from '../operations/create';
import { getProposal } from '../operations/get';
import { getOrCreateDelegate } from '../operations/getOrCreate';
import {
updateProposalCanceled,
updateProposalExecuted,
updateProposalQueued,
updateProposalStatus,
} from '../operations/update';

// - event: ProposalCreated(uint256,address,address[],uint256[],string[],bytes[],uint256,uint256,string)
Expand All @@ -39,8 +37,7 @@ export function handleProposalCreated(event: ProposalCreated): void {
// handler: handleProposalCanceled

export function handleProposalCanceled(event: ProposalCanceled): void {
const proposalId = event.params.id.toString();
updateProposalStatus(proposalId, CANCELLED);
updateProposalCanceled<ProposalCanceled>(event);
}

// - event: ProposalQueued(uint256,uint256)
Expand All @@ -64,18 +61,8 @@ 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.toHexString());
createVoteAlpha(event);
const proposalId = event.params.proposalId.toString();
const proposal = getProposal(proposalId);
if (proposal.status == PENDING) {
updateProposalStatus(proposalId, ACTIVE);
}
}

export function handleVoteCastV2(event: VoteCast): void {
createVoteAlpha(event);
const proposalId = event.params.proposalId.toString();
const proposal = getProposal(proposalId);
if (proposal.status == PENDING) {
updateProposalStatus(proposalId, ACTIVE);
}
}
14 changes: 4 additions & 10 deletions subgraphs/venus-governance/src/mappings/bravo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
ProposalQueued,
VoteCast,
} from '../../generated/GovernorBravoDelegate/GovernorBravoDelegate';
import { ACTIVE, CANCELLED, CRITICAL, FAST_TRACK, NORMAL, PENDING } from '../constants';
import { CRITICAL, FAST_TRACK, NORMAL } from '../constants';
import { createProposal, createVoteBravo } from '../operations/create';
import { getGovernanceEntity, getProposal } from '../operations/get';
import { getGovernanceEntity } from '../operations/get';
import { getOrCreateDelegate } from '../operations/getOrCreate';
import {
updateProposalCanceled,
updateProposalExecuted,
updateProposalQueued,
updateProposalStatus,
} from '../operations/update';

export function handleProposalCreated(event: ProposalCreated): void {
Expand All @@ -35,8 +35,7 @@ export function handleProposalCreatedV2(event: ProposalCreatedV2): void {
}

export function handleProposalCanceled(event: ProposalCanceled): void {
const proposalId = event.params.id.toString();
updateProposalStatus(proposalId, CANCELLED);
updateProposalCanceled(event);
}

export function handleProposalQueued(event: ProposalQueued): void {
Expand All @@ -49,11 +48,6 @@ export function handleProposalExecuted(event: ProposalExecuted): void {

export function handleBravoVoteCast(event: VoteCast): void {
createVoteBravo(event);
const proposalId = event.params.proposalId.toString();
const proposal = getProposal(proposalId);
if (proposal.status == PENDING) {
updateProposalStatus(proposalId, ACTIVE);
}
}

export function handleNewImplementation(event: NewImplementation): void {
Expand Down
6 changes: 4 additions & 2 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, ACTIVE, AGAINST, BIGINT_ONE, FOR, NORMAL, PENDING } from '../constants';
import { ABSTAIN, AGAINST, BIGINT_ONE, FOR, NORMAL } from '../constants';
import { getVoteId } from '../utilities/ids';
import { getDelegate, getGovernanceEntity, getProposal } from './get';

Expand All @@ -26,7 +26,9 @@ export function createProposal<E>(event: E): Proposal {
proposal.startBlock = event.params.startBlock;
proposal.endBlock = event.params.endBlock;
proposal.description = event.params.description;
proposal.status = event.block.number >= proposal.startBlock ? ACTIVE : PENDING;
proposal.queued = false;
proposal.canceled = false;
proposal.executed = false;
proposal.type = NORMAL;

proposal.save();
Expand Down
32 changes: 12 additions & 20 deletions subgraphs/venus-governance/src/operations/update.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,35 @@
import { BIGINT_ONE, BIGINT_ZERO, CANCELLED, EXECUTED, QUEUED } from '../constants';
import { BIGINT_ONE } from '../constants';
import { nullAddress } from '../constants/addresses';
import { getGovernanceEntity, getProposal } from './get';
import { getOrCreateDelegate } from './getOrCreate';

export const updateProposalStatus = (id: string, status: string): void => {
const proposal = getProposal(id);
proposal.status = status;
proposal.save();
};

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

proposal.status = CANCELLED;
proposal.canceled = true;
proposal.save();
}

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

proposal.status = QUEUED;
proposal.executionETA = params.eta;
proposal.queued = true;
proposal.executionEta = params.eta;
proposal.save();

governance.proposalsQueued = governance.proposalsQueued.plus(BIGINT_ONE);
governance.save();
}

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

proposal.status = EXECUTED;
proposal.executionETA = null;
proposal.executed = true;
proposal.save();

governance.proposalsQueued = governance.proposalsQueued.minus(BIGINT_ONE);
governance.save();
}

export function updateDelegateChanged<E>(event: E): void {
const governance = getGovernanceEntity();
const params = event.params;
const fromDelegate = params.fromDelegate.toHexString();
const toDelegate = params.toDelegate.toHexString();
Expand All @@ -59,13 +45,19 @@ export function updateDelegateChanged<E>(event: E): void {
const oldDelegate = oldDelegateResult.entity;
oldDelegate.delegateCount = oldDelegate.delegateCount - 1;
oldDelegate.save();

governance.totalDelegates = governance.totalDelegates.minus(BIGINT_ONE);
governance.save();
}

if (toDelegate != nullAddress.toHexString()) {
const newDelegateResult = getOrCreateDelegate(toDelegate);
const newDelegate = newDelegateResult.entity;
newDelegate.delegateCount = newDelegate.delegateCount + 1;
newDelegate.save();

governance.totalDelegates = governance.totalDelegates.plus(BIGINT_ONE);
governance.save();
}
}

Expand Down
19 changes: 14 additions & 5 deletions subgraphs/venus-governance/subgraph-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DocumentNode } from 'graphql';
import { Client as UrqlClient, createClient } from 'urql/core';

import {
Expand All @@ -18,28 +19,36 @@ class SubgraphClient {
});
}

async query(document: DocumentNode, args: Record<string, string>) {
const result = await this.urqlClient.query(document, args).toPromise();
if (result.error) {
console.error(result.error);
}
return result;
}

async getProposalById(id: string) {
const result = await this.urqlClient.query(ProposalByIdDocument, { id: id }).toPromise();
const result = await this.query(ProposalByIdDocument, { id: id });
return result;
}

async getDelegateById(id: string) {
const result = await this.urqlClient.query(DelegateByIdDocument, { id: id }).toPromise();
const result = await this.query(DelegateByIdDocument, { id: id });
return result;
}

async getDelegates() {
const result = await this.urqlClient.query(DelegatesDocument, {}).toPromise();
const result = await this.query(DelegatesDocument, {});
return result;
}

async getProposals() {
const result = await this.urqlClient.query(ProposalsDocument, {}).toPromise();
const result = await this.query(ProposalsDocument, {});
return result;
}

async getPermissions() {
const result = await this.urqlClient.query(PermissionsDocument, {}).toPromise();
const result = await this.query(PermissionsDocument, {});
return result;
}
}
Expand Down
Loading

0 comments on commit 5b1a3ba

Please sign in to comment.