diff --git a/subgraphs/venus-governance/schema.graphql b/subgraphs/venus-governance/schema.graphql index 2c9d76b1..6d2d57ef 100644 --- a/subgraphs/venus-governance/schema.graphql +++ b/subgraphs/venus-governance/schema.graphql @@ -10,13 +10,17 @@ 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! - "Amount of votes delegated to this delegate to be used on proposal votings expressed in the smallest unit of the XVS Token" - delegatedVotes: BigInt! + "Amount of XVS this user has staked" + stakedXvsMantissa: BigInt! + + "Total amount of votes via delegation and staking" + totalVotesMantissa: BigInt! - tokenHoldersRepresentedAmount: Int! + "Accounts delegating to this voter" + delegateCount: Int! - "Token holders that this delegate represents" - tokenHoldersRepresented: [TokenHolder!]! @derivedFrom(field: "delegate") + "Accounts delegating to this voter" + delegates: [Delegate!]! "Votes that a delegate has made in different proposals" votes: [Vote!]! @derivedFrom(field: "voter") diff --git a/subgraphs/venus-governance/src/operations/getOrCreate.ts b/subgraphs/venus-governance/src/operations/getOrCreate.ts index 585d7c00..054711a7 100644 --- a/subgraphs/venus-governance/src/operations/getOrCreate.ts +++ b/subgraphs/venus-governance/src/operations/getOrCreate.ts @@ -11,11 +11,12 @@ export class GetOrCreateDelegateReturn { export const getOrCreateDelegate = (id: string): GetOrCreateDelegateReturn => { let created = false; let delegate = Delegate.load(id); - if (!delegate) { delegate = new Delegate(id); - delegate.delegatedVotes = BIGINT_ZERO; - delegate.tokenHoldersRepresentedAmount = 0; + delegate.stakedXvsMantissa = BIGINT_ZERO; + delegate.totalVotesMantissa = BIGINT_ZERO; + delegate.delegateCount = 0; + delegate.delegates = []; if (id != nullAddress.toString()) { const governance = getGovernanceEntity(); diff --git a/subgraphs/venus-governance/src/operations/update.ts b/subgraphs/venus-governance/src/operations/update.ts index 15a20beb..0b4e1a3e 100644 --- a/subgraphs/venus-governance/src/operations/update.ts +++ b/subgraphs/venus-governance/src/operations/update.ts @@ -49,9 +49,9 @@ export function updateDelegateChanged(event: E): void { const newDelegateResult = getOrCreateDelegate(params.toDelegate.toHexString()); const newDelegate = newDelegateResult.entity; - oldDelegate.tokenHoldersRepresentedAmount = oldDelegate.tokenHoldersRepresentedAmount - 1; - newDelegate.tokenHoldersRepresentedAmount = newDelegate.tokenHoldersRepresentedAmount + 1; + oldDelegate.delegateCount = oldDelegate.delegateCount - 1; oldDelegate.save(); + newDelegate.delegateCount = newDelegate.delegateCount + 1; newDelegate.save(); } @@ -65,7 +65,7 @@ export function updateDelegateVoteChanged(event: E): void { const newBalance = params.newBalance; const votesDifference = newBalance.minus(previousBalance); - delegate.delegatedVotes = newBalance; + delegate.totalVotesMantissa = newBalance; delegate.save(); if (previousBalance == BIGINT_ZERO && newBalance > BIGINT_ZERO) { diff --git a/subgraphs/venus-governance/tests/Alpha/index.test.ts b/subgraphs/venus-governance/tests/Alpha/index.test.ts index 2eb0726a..66c45fca 100644 --- a/subgraphs/venus-governance/tests/Alpha/index.test.ts +++ b/subgraphs/venus-governance/tests/Alpha/index.test.ts @@ -45,7 +45,7 @@ const description = 'Very creative Proposal'; beforeAll(() => { createGovernorBravoMocks(); -}) +}); beforeEach(() => { getOrCreateDelegate(user1.toHexString()); @@ -74,8 +74,10 @@ describe('Alpha', () => { assert.fieldEquals('Delegate', user1.toHex(), key, value); }; assertDelegateDocument('id', user1.toHexString()); - assertDelegateDocument('delegatedVotes', '0'); - assertDelegateDocument('tokenHoldersRepresentedAmount', '0'); + assertDelegateDocument('totalVotesMantissa', '0'); + assertDelegateDocument('delegateCount', '0'); + assertDelegateDocument('proposals', '[1]'); + assertDelegateDocument('delegates', '[]'); // Proposal const assertProposalDocument = (key: string, value: string): void => { diff --git a/subgraphs/venus-governance/tests/Bravo/index.test.ts b/subgraphs/venus-governance/tests/Bravo/index.test.ts index 55b8ef0c..00736301 100644 --- a/subgraphs/venus-governance/tests/Bravo/index.test.ts +++ b/subgraphs/venus-governance/tests/Bravo/index.test.ts @@ -44,6 +44,7 @@ import { createProposalQueuedEvent, createVoteCastBravoEvent, } from '../common/events'; +import { createGovernorBravoMocks } from '../common/mocks'; import { createNewAdminEvent, createNewGuardianEvent, @@ -54,7 +55,6 @@ import { createNewVotingDelayEvent, createNewVotingPeriodEvent, } from './events'; -import { createGovernorBravoMocks } from '../common/mocks'; const startBlock = 4563820; const endBlock = 4593820; @@ -67,7 +67,7 @@ const cleanup = (): void => { beforeAll(() => { createGovernorBravoMocks(); -}) +}); beforeEach(() => { /** setup test */ @@ -113,8 +113,10 @@ describe('Bravo', () => { assert.fieldEquals('Delegate', user1.toHex(), key, value); }; assertDelegateDocument('id', user1.toHexString()); - assertDelegateDocument('delegatedVotes', '0'); - assertDelegateDocument('tokenHoldersRepresentedAmount', '0'); + assertDelegateDocument('totalVotesMantissa', '0'); + assertDelegateDocument('delegateCount', '0'); + assertDelegateDocument('proposals', '[1]'); + assertDelegateDocument('delegates', '[]'); // Proposal const assertProposalDocument = (key: string, value: string): void => { @@ -155,8 +157,10 @@ describe('Bravo', () => { assert.fieldEquals('Delegate', user1.toHex(), key, value); }; assertDelegateDocument('id', user1.toHexString()); - assertDelegateDocument('delegatedVotes', '0'); - assertDelegateDocument('tokenHoldersRepresentedAmount', '0'); + assertDelegateDocument('totalVotesMantissa', '0'); + assertDelegateDocument('delegateCount', '0'); + assertDelegateDocument('proposals', '[1]'); + assertDelegateDocument('delegates', '[]'); // Proposal const assertProposalDocument = (key: string, value: string): void => { @@ -279,7 +283,12 @@ describe('Bravo', () => { ); handleVotingDelaySet(votingDelayEvent); - assert.fieldEquals('Governance', governorBravoDelegateAddress.toHex(), 'votingDelay', newVotingDelay.toString()); + assert.fieldEquals( + 'Governance', + governorBravoDelegateAddress.toHex(), + 'votingDelay', + newVotingDelay.toString(), + ); }); test('registers new voting period', () => { @@ -292,7 +301,12 @@ describe('Bravo', () => { ); handleVotingPeriodSet(votingPeriodEvent); - assert.fieldEquals('Governance', governorBravoDelegateAddress.toHex(), 'votingPeriod', newVotingPeriod.toString()); + assert.fieldEquals( + 'Governance', + governorBravoDelegateAddress.toHex(), + 'votingPeriod', + newVotingPeriod.toString(), + ); }); test('registers new implementation', () => { @@ -305,7 +319,12 @@ describe('Bravo', () => { ); handleNewImplementation(newImplementationEvent); - assert.fieldEquals('Governance', governorBravoDelegateAddress.toHex(), 'implementation', newImplementation.toHexString()); + assert.fieldEquals( + 'Governance', + governorBravoDelegateAddress.toHex(), + 'implementation', + newImplementation.toHexString(), + ); }); test('registers new proposal threshold', () => { @@ -336,7 +355,12 @@ describe('Bravo', () => { ); handleNewPendingAdmin(pendingAdminEvent); - assert.fieldEquals('Governance', governorBravoDelegateAddress.toHex(), 'pendingAdmin', newPendingAdmin.toHexString()); + assert.fieldEquals( + 'Governance', + governorBravoDelegateAddress.toHex(), + 'pendingAdmin', + newPendingAdmin.toHexString(), + ); }); test('registers new admin', () => { @@ -345,7 +369,12 @@ describe('Bravo', () => { const newAdminEvent = createNewAdminEvent(governanceAddress, oldAdmin, newAdmin); handleNewAdmin(newAdminEvent); - assert.fieldEquals('Governance', governorBravoDelegateAddress.toHex(), 'admin', newAdmin.toHexString()); + assert.fieldEquals( + 'Governance', + governorBravoDelegateAddress.toHex(), + 'admin', + newAdmin.toHexString(), + ); assert.fieldEquals('Governance', governorBravoDelegateAddress.toHex(), 'pendingAdmin', 'null'); }); @@ -355,7 +384,12 @@ describe('Bravo', () => { const newGuardianEvent = createNewGuardianEvent(governanceAddress, oldGuardian, newGuardian); handleNewGuardian(newGuardianEvent); - assert.fieldEquals('Governance', governorBravoDelegateAddress.toHex(), 'guardian', newGuardian.toHexString()); + assert.fieldEquals( + 'Governance', + governorBravoDelegateAddress.toHex(), + 'guardian', + newGuardian.toHexString(), + ); }); test('registers new proposal max operations', () => { diff --git a/subgraphs/venus-governance/tests/Bravo/xvsVault.test.ts b/subgraphs/venus-governance/tests/Bravo/xvsVault.test.ts index fa2631a1..6f6fe554 100644 --- a/subgraphs/venus-governance/tests/Bravo/xvsVault.test.ts +++ b/subgraphs/venus-governance/tests/Bravo/xvsVault.test.ts @@ -2,6 +2,7 @@ import { BigInt } from '@graphprotocol/graph-ts'; import { afterEach, assert, + beforeAll, beforeEach, clearStore, describe, @@ -15,6 +16,7 @@ import { handleDelegateChanged } from '../../src/mappings/xvsVault'; import { getOrCreateDelegate } from '../../src/operations/getOrCreate'; import { user1, user2, user3 } from '../common/constants'; import { createDelegateChangedEvent, createProposalCreatedEvent } from '../common/events'; +import { createGovernorBravoMocks } from '../common/mocks'; const cleanup = (): void => { clearStore(); @@ -24,6 +26,10 @@ const startBlock = 4563820; const endBlock = 4593820; const description = 'Very creative Proposal'; +beforeAll(() => { + createGovernorBravoMocks(); +}); + beforeEach(() => { /** setup test */ getOrCreateDelegate(user1.toHexString()); @@ -61,12 +67,12 @@ describe('XVS Vault', () => { const assertOldDelegateDocument = (key: string, value: string): void => { assert.fieldEquals('Delegate', user2.toHex(), key, value); }; - assertOldDelegateDocument('tokenHoldersRepresentedAmount', '-1'); + assertOldDelegateDocument('delegateCount', '-1'); // New Delegate const assertNewDelegateDocument = (key: string, value: string): void => { assert.fieldEquals('Delegate', user3.toHex(), key, value); }; - assertNewDelegateDocument('tokenHoldersRepresentedAmount', '1'); + assertNewDelegateDocument('delegateCount', '1'); }); }); diff --git a/subgraphs/venus-governance/tests/integration/queries/delegateByIdQuery.graphql b/subgraphs/venus-governance/tests/integration/queries/delegateByIdQuery.graphql index 7a6c9685..3ba02acc 100644 --- a/subgraphs/venus-governance/tests/integration/queries/delegateByIdQuery.graphql +++ b/subgraphs/venus-governance/tests/integration/queries/delegateByIdQuery.graphql @@ -1,8 +1,10 @@ query DelegateById($id: ID!) { delegate(id: $id) { id - delegatedVotes - tokenHoldersRepresented { + stakedXvsMantissa + totalVotesMantissa + delegateCount + delegates { id } votes {