Skip to content

Commit

Permalink
refactor: refactor Delegates entity
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Oct 4, 2023
1 parent 784bb76 commit 488eba9
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 30 deletions.
14 changes: 9 additions & 5 deletions subgraphs/venus-governance/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 4 additions & 3 deletions subgraphs/venus-governance/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions subgraphs/venus-governance/src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export function updateDelegateChanged<E>(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();
}

Expand All @@ -65,7 +65,7 @@ export function updateDelegateVoteChanged<E>(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) {
Expand Down
8 changes: 5 additions & 3 deletions subgraphs/venus-governance/tests/Alpha/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const description = 'Very creative Proposal';

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

beforeEach(() => {
getOrCreateDelegate(user1.toHexString());
Expand Down Expand Up @@ -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 => {
Expand Down
58 changes: 46 additions & 12 deletions subgraphs/venus-governance/tests/Bravo/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
createProposalQueuedEvent,
createVoteCastBravoEvent,
} from '../common/events';
import { createGovernorBravoMocks } from '../common/mocks';
import {
createNewAdminEvent,
createNewGuardianEvent,
Expand All @@ -54,7 +55,6 @@ import {
createNewVotingDelayEvent,
createNewVotingPeriodEvent,
} from './events';
import { createGovernorBravoMocks } from '../common/mocks';

const startBlock = 4563820;
const endBlock = 4593820;
Expand All @@ -67,7 +67,7 @@ const cleanup = (): void => {

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

beforeEach(() => {
/** setup test */
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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');
});

Expand All @@ -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', () => {
Expand Down
10 changes: 8 additions & 2 deletions subgraphs/venus-governance/tests/Bravo/xvsVault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BigInt } from '@graphprotocol/graph-ts';
import {
afterEach,
assert,
beforeAll,
beforeEach,
clearStore,
describe,
Expand All @@ -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();
Expand All @@ -24,6 +26,10 @@ const startBlock = 4563820;
const endBlock = 4593820;
const description = 'Very creative Proposal';

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

beforeEach(() => {
/** setup test */
getOrCreateDelegate(user1.toHexString());
Expand Down Expand Up @@ -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');
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
query DelegateById($id: ID!) {
delegate(id: $id) {
id
delegatedVotes
tokenHoldersRepresented {
stakedXvsMantissa
totalVotesMantissa
delegateCount
delegates {
id
}
votes {
Expand Down

0 comments on commit 488eba9

Please sign in to comment.