Skip to content

Commit

Permalink
refactor: remove token holder
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Oct 3, 2023
1 parent c59fea8 commit 01ea876
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 90 deletions.
14 changes: 0 additions & 14 deletions subgraphs/venus-governance/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ enum ProposalStatus {
EXECUTED
}

type TokenHolder @entity {
"A TokenHolder is any address that holds any amount of Venus Tokens, the id used is the blockchain address."
id: ID!

"Delegate address of the token holder which will participate in votings. Delegates don't need to hold any tokens and can even be the token holder itself."
delegate: Delegate

"Venus Token balance of this address expressed in the smallest unit of the XVS Token"
tokenBalance: BigInt!

"Total amount of Venus Token ever held by this address expressed in the smallest unit of the XVS Token"
totalTokensHeld: BigInt!
}

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
34 changes: 4 additions & 30 deletions subgraphs/venus-governance/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
import { Delegate, TokenHolder } from '../../generated/schema';
import { BIGINT_ONE, BIGINT_ZERO, ZERO_ADDRESS } from '../constants';
import { Delegate } from '../../generated/schema';
import { BIGINT_ONE, BIGINT_ZERO } from '../constants';
import { nullAddress } from '../constants/addresses';
import { getGovernanceEntity } from './get';

export class GetOrCreateTokenHolderReturn {
entity: TokenHolder;
created: boolean;
}

export const getOrCreateTokenHolder = (id: string): GetOrCreateTokenHolderReturn => {
let created = false;
let tokenHolder = TokenHolder.load(id);

if (!tokenHolder) {
tokenHolder = new TokenHolder(id);
tokenHolder.tokenBalance = BIGINT_ZERO;
tokenHolder.totalTokensHeld = BIGINT_ZERO;

if (id != ZERO_ADDRESS) {
const governance = getGovernanceEntity();
governance.totalTokenHolders = governance.totalTokenHolders.plus(BIGINT_ONE);
governance.save();
}

tokenHolder.save();
created = true;
}

return { entity: tokenHolder as TokenHolder, created };
};

export class GetOrCreateDelegateReturn {
entity: Delegate;
created: boolean;
Expand All @@ -43,7 +17,7 @@ export const getOrCreateDelegate = (id: string): GetOrCreateDelegateReturn => {
delegate.delegatedVotes = BIGINT_ZERO;
delegate.tokenHoldersRepresentedAmount = 0;

if (id != ZERO_ADDRESS) {
if (id != nullAddress.toString()) {
const governance = getGovernanceEntity();
governance.totalDelegates = governance.totalDelegates.plus(BIGINT_ONE);
governance.totalVoters = governance.totalVoters.plus(BIGINT_ONE);
Expand Down
6 changes: 1 addition & 5 deletions subgraphs/venus-governance/src/operations/update.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BIGINT_ONE, BIGINT_ZERO, CANCELLED, EXECUTED, QUEUED } from '../constants';
import { getGovernanceEntity, getProposal } from './get';
import { getOrCreateDelegate, getOrCreateTokenHolder } from './getOrCreate';
import { getOrCreateDelegate } from './getOrCreate';

export const updateProposalStatus = (id: string, status: string): void => {
const proposal = getProposal(id);
Expand Down Expand Up @@ -44,14 +44,10 @@ export function updateProposalExecuted<E>(event: E): void {

export function updateDelegateChanged<E>(event: E): void {
const params = event.params;
const tokenHolderResult = getOrCreateTokenHolder(params.delegator.toHexString());
const tokenHolder = tokenHolderResult.entity;
const oldDelegateResult = getOrCreateDelegate(params.fromDelegate.toHexString());
const oldDelegate = oldDelegateResult.entity;
const newDelegateResult = getOrCreateDelegate(params.toDelegate.toHexString());
const newDelegate = newDelegateResult.entity;
tokenHolder.delegate = newDelegate.id;
tokenHolder.save();

oldDelegate.tokenHoldersRepresentedAmount = oldDelegate.tokenHoldersRepresentedAmount - 1;
newDelegate.tokenHoldersRepresentedAmount = newDelegate.tokenHoldersRepresentedAmount + 1;
Expand Down
5 changes: 0 additions & 5 deletions subgraphs/venus-governance/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ dataSources:
file: ./src/mappings/alpha.ts
entities:
- Delegate
- TokenHolder
- Proposal
- Vote
- Governance
Expand Down Expand Up @@ -50,7 +49,6 @@ dataSources:
file: ./src/mappings/alpha.ts
entities:
- Delegate
- TokenHolder
- Proposal
- Vote
- Governance
Expand Down Expand Up @@ -82,7 +80,6 @@ dataSources:
file: ./src/mappings/bravo.ts
entities:
- Delegate
- TokenHolder
- Proposal
- Vote
- Governance
Expand Down Expand Up @@ -130,7 +127,6 @@ dataSources:
file: ./src/mappings/bravo.ts
entities:
- Delegate
- TokenHolder
- Proposal
- Vote
- Governance
Expand Down Expand Up @@ -180,7 +176,6 @@ dataSources:
file: ./src/mappings/xvsVault.ts
entities:
- Delegate
- TokenHolder
abis:
- name: XVSVault
file: ../../packages/venus-governance-abis/XVSVault.json
Expand Down
36 changes: 0 additions & 36 deletions subgraphs/venus-governance/tests/Bravo/xvsVault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,41 +68,5 @@ describe('XVS Vault', () => {
assert.fieldEquals('Delegate', user3.toHex(), key, value);
};
assertNewDelegateDocument('tokenHoldersRepresentedAmount', '1');

// TokenHolder
const assertTokenHolderDocument = (key: string, value: string): void => {
assert.fieldEquals('TokenHolder', user1.toHex(), key, value);
};
assertTokenHolderDocument('delegate', user3.toHexString());
});

test('delegate changed', () => {
const delegator = user1;
const fromDelegate = user2;
const toDelegate = user3;
/** run handler */
const delegateChangedEvent = createDelegateChangedEvent<DelegateChangedV2>(
delegator,
fromDelegate,
toDelegate,
);
handleDelegateChanged(delegateChangedEvent);
// OldDelegate
const assertOldDelegateDocument = (key: string, value: string): void => {
assert.fieldEquals('Delegate', user2.toHex(), key, value);
};
assertOldDelegateDocument('tokenHoldersRepresentedAmount', '-1');

// New Delegate
const assertNewDelegateDocument = (key: string, value: string): void => {
assert.fieldEquals('Delegate', user3.toHex(), key, value);
};
assertNewDelegateDocument('tokenHoldersRepresentedAmount', '1');

// TokenHolder
const assertTokenHolderDocument = (key: string, value: string): void => {
assert.fieldEquals('TokenHolder', user1.toHex(), key, value);
};
assertTokenHolderDocument('delegate', user3.toHexString());
});
});

0 comments on commit 01ea876

Please sign in to comment.