diff --git a/packages/subgraph/src/packages/token/governance-erc20.ts b/packages/subgraph/src/packages/token/governance-erc20.ts index c8ecc65bd..3e7f61e05 100644 --- a/packages/subgraph/src/packages/token/governance-erc20.ts +++ b/packages/subgraph/src/packages/token/governance-erc20.ts @@ -1,4 +1,4 @@ -import {Address, BigInt, dataSource} from '@graphprotocol/graph-ts'; +import {Address, BigInt, dataSource, store} from '@graphprotocol/graph-ts'; import {TokenVotingMember} from '../../../generated/schema'; import {Transfer} from '../../../generated/templates/TokenVoting/ERC20'; @@ -79,8 +79,15 @@ export function handleDelegateVotesChanged(event: DelegateVotesChanged): void { if (event.params.delegate != Address.zero()) { let member = getOrCreateMember(event.params.delegate, pluginId); - // Assign the cumulative delegated votes to this member from all their delegators. - member.votingPower = event.params.newBalance; - member.save(); + if ( + member.balance.equals(BigInt.zero()) && + event.params.newBalance.equals(BigInt.zero()) + ) { + store.remove('TokenVotingMember', member.id); + } else { + // Assign the cumulative delegated votes to this member from all their delegators. + member.votingPower = event.params.newBalance; + member.save(); + } } } diff --git a/packages/subgraph/tests/token/governance-erc20.test.ts b/packages/subgraph/tests/token/governance-erc20.test.ts index c55835dde..38e8aabed 100644 --- a/packages/subgraph/tests/token/governance-erc20.test.ts +++ b/packages/subgraph/tests/token/governance-erc20.test.ts @@ -5,8 +5,7 @@ import { clearStore, dataSourceMock, test, - describe, - logStore + describe } from 'matchstick-as'; import { ADDRESS_ONE, @@ -251,8 +250,8 @@ describe('Governance ERC20', () => { memberAddress, pluginAddress ); - - let event = member.createEvent_DelegateVotesChanged(); + member.votingPower = BigInt.fromString('100'); + let event = member.createEvent_DelegateVotesChanged('100', '0'); handleDelegateVotesChanged(event); @@ -279,5 +278,49 @@ describe('Governance ERC20', () => { member.assertEntity(); assert.entityCount('TokenVotingMember', 1); }); + + test('it should delete a member without voting power or balance', () => { + let memberOneAddress = ADDRESS_ONE; + let memberTwoAddress = ADDRESS_TWO; + let pluginAddress = ADDRESS_SIX; + let memberOne = new ExtendedTokenVotingMember().withDefaultValues( + memberOneAddress, + pluginAddress + ); + let memberTwo = new ExtendedTokenVotingMember().withDefaultValues( + memberTwoAddress, + pluginAddress + ); + /* member one has 100s token delegated to member two*/ + memberOne.balance = BigInt.fromString('100'); + memberOne.votingPower = BigInt.fromString('0'); + /* member two balance is 0 but has 100 voting power from the delegation of member one */ + memberTwo.balance = BigInt.fromString('0'); + memberTwo.votingPower = BigInt.fromString('100'); + /* member three has 100 tokens and none delegated */ + + memberOne.buildOrUpdate(); + memberTwo.buildOrUpdate(); + + assert.entityCount('TokenVotingMember', 2); + + // member one undelegates from member two + let eventOne = memberOne.createEvent_DelegateVotesChanged('100'); + let eventTwo = memberTwo.createEvent_DelegateVotesChanged('0'); + + handleDelegateVotesChanged(eventOne); + handleDelegateVotesChanged(eventTwo); + + // assert + // expected changes + memberOne.votingPower = BigInt.fromString('100'); + memberOne.assertEntity(); + // member two should be deleted because it has no balance or voting power + assert.notInStore( + 'TokenVotingMember', + memberTwo.id + ); + assert.entityCount('TokenVotingMember', 1); + }); }); });