Skip to content

Commit

Permalink
refactor: instiate updated governor including governance routes
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Oct 3, 2023
1 parent 1d43a36 commit c59fea8
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 62 deletions.
2 changes: 0 additions & 2 deletions subgraphs/venus-governance/config/bsc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"governorAlpha2StartBlock": "11934064",
"governorBravoDelegateAddress": "0x2d56dC077072B53571b8252008C60e945108c75a",
"governorBravoDelegateStartBlock": "13729317",
"governorBravoDelegate2Address": "",
"governorBravoDelegate2StartBlock": "",
"xvsVaultAddress": "0x6eF49b4e0772Fe78128F981d42D54172b55eCF9F",
"xvsVaultStartBlock": "13018718"
}
4 changes: 1 addition & 3 deletions subgraphs/venus-governance/config/chapel.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
"governorAlpha2Address": "0x7116894ed34FC4B27D5b84f46B70Af48397a6C24",
"governorAlpha2StartBlock": "13584539",
"governorBravoDelegateAddress": "0x5573422a1a59385c247ec3a66b93b7c08ec2f8f2",
"governorBravoDelegateStartBlock": "16002994 ",
"governorBravoDelegate2Address": "",
"governorBravoDelegate2StartBlock": "",
"governorBravoDelegateStartBlock": "16002994",
"xvsVaultAddress": "0xa4Fd54cACdA379FB7CaA783B83Cc846f8ac0Faa6",
"xvsVaultStartBlock": "13937802"
}
2 changes: 0 additions & 2 deletions subgraphs/venus-governance/config/local.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"governorAlpha2StartBlock": "0",
"governorBravoDelegateAddress": "0x1c9fD50dF7a4f066884b58A05D91e4b55005876A",
"governorBravoDelegateStartBlock": "0",
"governorBravoDelegate2Address": "0x1c9fD50dF7a4f066884b58A05D91e4b55005876A",
"governorBravoDelegate2StartBlock": "0",
"xvsVaultAddress": "0xe1708FA6bb2844D5384613ef0846F9Bc1e8eC55E",
"xvsVaultStartBlock": "0"
}
39 changes: 26 additions & 13 deletions subgraphs/venus-governance/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,14 @@ type Governance @entity {
"Number of proposals created"
proposals: BigInt!

"Total number of token holders currently"
currentTokenHolders: BigInt!

"Total number of delegates participating on the governance currently"
currentDelegates: BigInt!

"Total number of token holders"
totalTokenHolders: BigInt!

"Total number of delegates that held delegated votes"
"Total number of accounts delegates that can participate in governance by voting or creating proposals"
totalDelegates: BigInt!

"Total number of accounts participating in governance as delegates or by delegating"
totalVoters: BigInt!

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

"Number of proposals currently queued for execution"
proposalsQueued: BigInt!
Expand All @@ -140,11 +134,14 @@ type Governance @entity {
"The duration of voting on a proposal, in blocks"
votingPeriod: BigInt!

"The number of votes required to reach quorum"
quorumVotesMantissa: BigInt!

"Active brains of Governor"
implementation: Bytes!

"The number of votes required in order for a voter to become a proposer"
proposalThreshold: BigInt!
proposalThresholdMantissa: BigInt!

"Pending administrator for this contract"
pendingAdmin: Bytes
Expand All @@ -157,7 +154,23 @@ type Governance @entity {

"The maximum number of actions that can be included in a proposal"
proposalMaxOperations: BigInt!
}

type GovernanceRoute @entity {
"Index of the governance route"
id: ID!
"Governor implementation the route belongs to"
governor: Bytes!
"Address of timelock contract for route"
timelock: Bytes!
"Que execution delay in blocks"
queDelay: BigInt!
"The delay before voting on a proposal may take place, once proposed, in blocks"
votingDelay: BigInt!
"The duration of voting on a proposal, in blocks"
votingPeriod: BigInt!
"The number of votes required in order for a voter to become a proposer"
proposalThreshold: BigInt!
}

enum PermissionStatus {
Expand Down
9 changes: 9 additions & 0 deletions subgraphs/venus-governance/src/constants/addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Address } from '@graphprotocol/graph-ts';

import {
governorBravoDelegateAddress as governorBravoDelegateAddressString,
} from './config';

export const governorBravoDelegateAddress = Address.fromString(governorBravoDelegateAddressString);

export const nullAddress = Address.fromString('0x0000000000000000000000000000000000000000');
3 changes: 3 additions & 0 deletions subgraphs/venus-governance/src/constants/config-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Use yarn prepare commands to generate config typescript file per env

export const governorBravoDelegateAddress = '{{ governorBravoDelegateAddress }}';
5 changes: 1 addition & 4 deletions subgraphs/venus-governance/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigDecimal, BigInt } from '@graphprotocol/graph-ts';

export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';

export const BIGINT_ZERO = BigInt.fromI32(0);
export const BIGINT_ONE = BigInt.fromI32(1);
export const BIGDECIMAL_ZERO = new BigDecimal(BIGINT_ZERO);
Expand All @@ -10,9 +10,6 @@ export const EXECUTED = 'EXECUTED';
export const QUEUED = 'QUEUED';
export const ACTIVE = 'ACTIVE';

// Ids
export const GOVERNANCE = 'GOVERNANCE';

// Vote support
export const FOR = 'FOR';
export const AGAINST = 'AGAINST';
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/venus-governance/src/mappings/bravo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function handleNewImplementation(event: NewImplementation): void {

export function handleProposalThresholdSet(event: ProposalThresholdSet): void {
const governance = getGovernanceEntity();
governance.proposalThreshold = event.params.newProposalThreshold;
governance.proposalThresholdMantissa = event.params.newProposalThreshold;
governance.save();
}

Expand Down
77 changes: 59 additions & 18 deletions subgraphs/venus-governance/src/operations/get.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,72 @@
import { Address, BigInt, log } from '@graphprotocol/graph-ts';
import { BigInt, log } from '@graphprotocol/graph-ts';

import { Delegate, Governance, Proposal } from '../../generated/schema';
import { BIGINT_ONE, BIGINT_ZERO, GOVERNANCE } from '../constants';
import { GovernorBravoDelegate2 } from '../../generated/GovernorBravoDelegate2/GovernorBravoDelegate2';
import { Timelock } from '../../generated/GovernorBravoDelegate2/Timelock';
import { Delegate, Governance, GovernanceRoute, Proposal } from '../../generated/schema';
import { BIGINT_ZERO } from '../constants';
import { governorBravoDelegateAddress } from '../constants/addresses';

/**
* While techinically this function does also create, we don't care because it only happens once as the id is a constant.
* While technically this function does also create, we don't care because it only happens once as the id is a constant.
* @returns Governance
*/
export const getGovernanceEntity = (): Governance => {
let governance = Governance.load(GOVERNANCE);
let governance = Governance.load(governorBravoDelegateAddress.toHex());
if (!governance) {
governance = new Governance(GOVERNANCE);
const governorBravoDelegate2 = GovernorBravoDelegate2.bind(governorBravoDelegateAddress);
governance = new Governance(governorBravoDelegateAddress.toHex());
governance.proposals = BIGINT_ZERO;
governance.totalTokenHolders = BIGINT_ZERO;
governance.currentTokenHolders = BIGINT_ZERO;
governance.currentDelegates = BIGINT_ZERO;
governance.totalDelegates = BIGINT_ZERO;
governance.delegatedVotes = BIGINT_ZERO;
governance.totalVoters = BIGINT_ZERO;
governance.totalVotesMantissa = BIGINT_ZERO;
governance.proposalsQueued = BIGINT_ZERO;
// defaulting to Governor Bravo constructor defaults
governance.votingDelay = BIGINT_ONE;
governance.votingPeriod = BigInt.fromI64(86400);
governance.implementation = Address.fromString('0x18df46ec843e79d9351b57f85af7d69aec0d7eff');
governance.proposalThreshold = BigInt.fromI64(300000000000000000000000);
governance.admin = Address.fromString('0x1c2cac6ec528c20800b2fe734820d87b581eaa6b');
governance.guardian = Address.fromString('0x1c2cac6ec528c20800b2fe734820d87b581eaa6b');
governance.proposalMaxOperations = BigInt.fromI32(10);

governance.votingDelay = governorBravoDelegate2.votingDelay();

governance.votingPeriod = governorBravoDelegate2.votingPeriod();
governance.proposalThresholdMantissa = governorBravoDelegate2.proposalThreshold();
governance.admin = governorBravoDelegate2.admin();
governance.implementation = governorBravoDelegate2.implementation();
governance.guardian = governorBravoDelegate2.guardian();
governance.quorumVotesMantissa = governorBravoDelegate2.quorumVotes();
governance.proposalMaxOperations = governorBravoDelegate2.proposalMaxOperations();
// Governance Routes are set in initialization
// Normal
const normalProposalConfig = governorBravoDelegate2.proposalConfigs(new BigInt(0));
const normalTimelockAddress = governorBravoDelegate2.proposalTimelocks(new BigInt(0));
const normalTimelock = Timelock.bind(normalTimelockAddress);
const normalGovernanceRoute = new GovernanceRoute('0');
normalGovernanceRoute.governor = governorBravoDelegateAddress;
normalGovernanceRoute.timelock = normalTimelockAddress;
normalGovernanceRoute.queDelay = normalTimelock.delay();
normalGovernanceRoute.votingDelay = normalProposalConfig.getVotingDelay();
normalGovernanceRoute.votingPeriod = normalProposalConfig.getVotingPeriod();
normalGovernanceRoute.proposalThreshold = normalProposalConfig.getProposalThreshold();
normalGovernanceRoute.save();
// Fast track
const fastTrackProposalConfig = governorBravoDelegate2.proposalConfigs(new BigInt(1));
const fastTrackTimelockAddress = governorBravoDelegate2.proposalTimelocks(new BigInt(1));
const fastTrackTimelock = Timelock.bind(normalTimelockAddress);
const fastTrackGovernanceRoute = new GovernanceRoute('1');
fastTrackGovernanceRoute.governor = governorBravoDelegateAddress;
fastTrackGovernanceRoute.timelock = fastTrackTimelockAddress;
fastTrackGovernanceRoute.queDelay = fastTrackTimelock.delay();
fastTrackGovernanceRoute.votingDelay = fastTrackProposalConfig.getVotingDelay();
fastTrackGovernanceRoute.votingPeriod = fastTrackProposalConfig.getVotingPeriod();
fastTrackGovernanceRoute.proposalThreshold = fastTrackProposalConfig.getProposalThreshold();
fastTrackGovernanceRoute.save();
// Critical
const criticalProposalConfig = governorBravoDelegate2.proposalConfigs(new BigInt(2));
const criticalTimelockAddress = governorBravoDelegate2.proposalTimelocks(new BigInt(2));
const criticalTimelock = Timelock.bind(normalTimelockAddress);
const criticalGovernanceRoute = new GovernanceRoute('2');
criticalGovernanceRoute.governor = governorBravoDelegateAddress;
criticalGovernanceRoute.timelock = criticalTimelockAddress;
criticalGovernanceRoute.queDelay = criticalTimelock.delay();
criticalGovernanceRoute.votingDelay = criticalProposalConfig.getVotingDelay();
criticalGovernanceRoute.votingPeriod = criticalProposalConfig.getVotingPeriod();
criticalGovernanceRoute.proposalThreshold = criticalProposalConfig.getProposalThreshold();
criticalGovernanceRoute.save();
}

return governance as Governance;
Expand Down
1 change: 1 addition & 0 deletions subgraphs/venus-governance/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const getOrCreateDelegate = (id: string): GetOrCreateDelegateReturn => {
if (id != ZERO_ADDRESS) {
const governance = getGovernanceEntity();
governance.totalDelegates = governance.totalDelegates.plus(BIGINT_ONE);
governance.totalVoters = governance.totalVoters.plus(BIGINT_ONE);
governance.save();
}

Expand Down
4 changes: 2 additions & 2 deletions subgraphs/venus-governance/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ export function updateDelegateVoteChanged<E>(event: E): void {
delegate.save();

if (previousBalance == BIGINT_ZERO && newBalance > BIGINT_ZERO) {
governance.currentDelegates = governance.currentDelegates.plus(BIGINT_ONE);
governance.totalDelegates = governance.totalDelegates.plus(BIGINT_ONE);
}
if (newBalance == BIGINT_ZERO) {
governance.currentDelegates = governance.currentDelegates.minus(BIGINT_ONE);
governance.totalDelegates = governance.totalDelegates.minus(BIGINT_ONE);
}
governance.delegatedVotes = governance.delegatedVotes.plus(votesDifference);
governance.save();
Expand Down
6 changes: 5 additions & 1 deletion subgraphs/venus-governance/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ dataSources:
name: GovernorBravoDelegate2
network: {{ network }}
source:
address: "{{ governorBravoDelegate2Address }}"
address: "{{ governorBravoDelegateAddress }}"
abi: GovernorBravoDelegate2
startBlock: {{ governorBravoDelegate2StartBlock }}
mapping:
Expand All @@ -137,6 +137,8 @@ dataSources:
abis:
- name: GovernorBravoDelegate2
file: ../../packages/venus-governance-abis/GovernorBravoDelegate2.json
- name: Timelock
file: ../../node_modules/@venusprotocol/governance-contracts/artifacts/contracts/Governance/Timelock.sol/Timelock.json
eventHandlers:
- event: ProposalCreated(uint256,address,address[],uint256[],string[],bytes[],uint256,uint256,string,uint8)
handler: handleProposalCreatedV2
Expand Down Expand Up @@ -183,6 +185,8 @@ dataSources:
- name: XVSVault
file: ../../packages/venus-governance-abis/XVSVault.json
eventHandlers:
- event: Deposit(indexed address,indexed address,indexed uint256,uint256)
handler: handleDeposit
- event: DelegateChangedV2(indexed address,indexed address,indexed address)
handler: handleDelegateChanged
- event: DelegateVotesChangedV2(indexed address,uint256,uint256)
Expand Down
12 changes: 9 additions & 3 deletions subgraphs/venus-governance/tests/Alpha/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address, BigInt, Bytes } from '@graphprotocol/graph-ts';
import {
afterEach,
assert,
beforeAll,
beforeEach,
clearStore,
describe,
Expand All @@ -14,7 +15,7 @@ import {
ProposalExecuted,
ProposalQueued,
} from '../../generated/GovernorAlpha/GovernorAlpha';
import { GOVERNANCE } from '../../src/constants';
import { governorBravoDelegateAddress } from '../../src/constants/addresses';
import {
handleProposalCanceled,
handleProposalCreated,
Expand All @@ -32,6 +33,7 @@ import {
createProposalQueuedEvent,
createVoteCastAlphaEvent,
} from '../common/events';
import { createGovernorBravoMocks } from '../common/mocks';

const cleanup = (): void => {
clearStore();
Expand All @@ -41,6 +43,10 @@ const startBlock = 4563820;
const endBlock = 4593820;
const description = 'Very creative Proposal';

beforeAll(() => {
createGovernorBravoMocks();
})

beforeEach(() => {
getOrCreateDelegate(user1.toHexString());
const proposalCreatedEvent = createProposalCreatedEvent<ProposalCreated>(
Expand Down Expand Up @@ -111,7 +117,7 @@ describe('Alpha', () => {
};

const assertGovernanceDocument = (key: string, value: string): void => {
assert.fieldEquals('Governance', GOVERNANCE, key, value);
assert.fieldEquals('Governance', governorBravoDelegateAddress.toHex(), key, value);
};

assertProposalDocument('status', 'QUEUED');
Expand All @@ -134,7 +140,7 @@ describe('Alpha', () => {
};

const assertGovernanceDocument = (key: string, value: string): void => {
assert.fieldEquals('Governance', GOVERNANCE, key, value);
assert.fieldEquals('Governance', governorBravoDelegateAddress.toHex(), key, value);
};

assertProposalDocument('status', 'EXECUTED');
Expand Down
Loading

0 comments on commit c59fea8

Please sign in to comment.