Skip to content

Commit

Permalink
Feature: Update subgraph membership definition (#373)
Browse files Browse the repository at this point in the history
* update membership definition

* remove unused log

* remove unused log

* fix comments

* fix comment
  • Loading branch information
josemarinas authored May 18, 2023
1 parent 7b4e183 commit 2dc178a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
15 changes: 11 additions & 4 deletions packages/subgraph/src/packages/token/governance-erc20.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
}
}
}
51 changes: 47 additions & 4 deletions packages/subgraph/tests/token/governance-erc20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
clearStore,
dataSourceMock,
test,
describe,
logStore
describe
} from 'matchstick-as';
import {
ADDRESS_ONE,
Expand Down Expand Up @@ -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);

Expand All @@ -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);
});
});
});

0 comments on commit 2dc178a

Please sign in to comment.