Skip to content

Commit

Permalink
Merge pull request #19 from aragon/OS-1217/cast-voting-mode-when-unde…
Browse files Browse the repository at this point in the history
…fined

feat: cast voting mode when undefined
  • Loading branch information
clauBv23 authored Apr 15, 2024
2 parents 198f491 + 3ef486d commit 43901f1
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 99 deletions.
2 changes: 2 additions & 0 deletions packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type TokenVotingPlugin implements IPlugin @entity {

proposals: [TokenVotingProposal!]! @derivedFrom(field: "plugin")
votingMode: VotingMode
votingModeIndex: Int8
supportThreshold: BigInt
minParticipation: BigInt
minDuration: BigInt
Expand Down Expand Up @@ -111,6 +112,7 @@ type TokenVotingProposal implements IProposal @entity {
metadata: String

votingMode: VotingMode!
votingModeIndex: Int8!
supportThreshold: BigInt!
minVotingPower: BigInt!
snapshotBlock: BigInt!
Expand Down
26 changes: 20 additions & 6 deletions packages/subgraph/src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import {
MembershipContractAnnounced,
TokenVoting,
} from '../../generated/templates/TokenVoting/TokenVoting';
import {RATIO_BASE, VOTER_OPTIONS, VOTING_MODES} from '../utils/constants';
import {
RATIO_BASE,
VOTER_OPTIONS,
VOTING_MODES,
VOTING_MODE_UNDEFINED,
} from '../utils/constants';
import {identifyAndFetchOrCreateERC20TokenEntity} from '../utils/erc20';
import {generateMemberEntityId, generateVoteEntityId} from '../utils/ids';
import {
Expand Down Expand Up @@ -70,9 +75,13 @@ export function _handleProposalCreated(

// ProposalParameters
const parameters = proposal.value.value2;
const votingMode = VOTING_MODES.get(parameters.votingMode);

proposalEntity.votingMode = votingMode as string;
let votingModeIndex = parameters.votingMode;
proposalEntity.votingModeIndex = votingModeIndex;
if (!VOTING_MODES.has(votingModeIndex)) {
// if the voting mode is not defined, set it to 'Undefined'
votingModeIndex = VOTING_MODE_UNDEFINED;
}
proposalEntity.votingMode = VOTING_MODES.get(votingModeIndex) as string;
proposalEntity.supportThreshold = parameters.supportThreshold;
proposalEntity.snapshotBlock = parameters.snapshotBlock;
proposalEntity.minVotingPower = parameters.minVotingPower;
Expand Down Expand Up @@ -264,9 +273,14 @@ export function handleVotingSettingsUpdated(
generatePluginEntityId(event.address)
);
if (pluginEntity) {
const votingMode = VOTING_MODES.get(event.params.votingMode);
let votingModeIndex = event.params.votingMode;
pluginEntity.votingModeIndex = votingModeIndex;
if (!VOTING_MODES.has(votingModeIndex)) {
// if the voting mode is not defined, set it to 'Undefined'
votingModeIndex = VOTING_MODE_UNDEFINED;
}

pluginEntity.votingMode = votingMode as string;
pluginEntity.votingMode = VOTING_MODES.get(votingModeIndex) as string;
pluginEntity.supportThreshold = event.params.supportThreshold;
pluginEntity.minParticipation = event.params.minParticipation;
pluginEntity.minDuration = event.params.minDuration;
Expand Down
10 changes: 4 additions & 6 deletions packages/subgraph/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000';

export const VOTING_MODE_UNDEFINED = 10;

// AS does not support initializing Map with data, a chain of sets is used instead
export const VOTER_OPTIONS = new Map<number, string>()
.set(0, 'None')
Expand All @@ -16,12 +18,8 @@ export const VOTE_OPTIONS = new Map<string, string>()
export const VOTING_MODES = new Map<number, string>()
.set(0, 'Standard')
.set(1, 'EarlyExecution')
.set(2, 'VoteReplacement');

export const VOTING_MODE_INDEXES = new Map<string, string>()
.set('Standard', '0')
.set('EarlyExecution', '1')
.set('VoteReplacement', '2');
.set(2, 'VoteReplacement')
.set(VOTING_MODE_UNDEFINED, 'Undefined');

export const TOKEN_VOTING_INTERFACE_ID = '0x50eb001e';
export const GOVERNANCE_WRAPPED_ERC20_INTERFACE_ID = '0x0f13099a';
Expand Down
60 changes: 27 additions & 33 deletions packages/subgraph/tests/helpers/method-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
VOTER_OPTIONS,
VOTE_OPTIONS,
VOTING_MODES,
VOTING_MODE_INDEXES,
VOTING_MODE_UNDEFINED,
} from '../../src/utils/constants';
import {generateMemberEntityId} from '../../src/utils/ids';
import {
Expand Down Expand Up @@ -85,7 +85,7 @@ import {
createDummyAction,
generateActionEntityId,
} from '@aragon/osx-commons-subgraph';
import {Address, BigInt, Bytes, ethereum} from '@graphprotocol/graph-ts';
import {Address, BigInt, Bytes, ethereum, Int8} from '@graphprotocol/graph-ts';

class ERC20WrapperContractMethods extends ERC20WrapperContract {
withDefaultValues(): ERC20WrapperContractMethods {
Expand Down Expand Up @@ -187,7 +187,9 @@ class TokenVotingVoterMethods extends TokenVotingVoter {
}

class TokenVotingProposalMethods extends TokenVotingProposal {
withDefaultValues(): TokenVotingProposalMethods {
withDefaultValues(
votingModeIndex: number = parseInt(VOTING_MODE)
): TokenVotingProposalMethods {
this.id = PROPOSAL_ENTITY_ID;

this.daoAddress = Bytes.fromHexString(DAO_ADDRESS);
Expand All @@ -198,7 +200,12 @@ class TokenVotingProposalMethods extends TokenVotingProposal {
this.open = true;
this.executed = false;

this.votingMode = VOTING_MODE;
// for event we need the index of the mapping to simulate the contract event
this.votingModeIndex = votingModeIndex as Int8;
this.votingMode = VOTING_MODES.has(votingModeIndex)
? (VOTING_MODES.get(votingModeIndex) as string)
: (VOTING_MODES.get(VOTING_MODE_UNDEFINED) as string);

this.supportThreshold = BigInt.fromString(SUPPORT_THRESHOLD);
this.minVotingPower = BigInt.fromString(MIN_VOTING_POWER);
this.startDate = BigInt.fromString(START_DATE);
Expand Down Expand Up @@ -231,7 +238,7 @@ class TokenVotingProposalMethods extends TokenVotingProposal {
this.pluginProposalId.toString(),
this.open,
this.executed,
this.votingMode,
this.votingModeIndex.toString(), // we need the index for this mocked call
this.supportThreshold.toString(),
this.minVotingPower.toString(),
this.startDate.toString(),
Expand Down Expand Up @@ -362,22 +369,21 @@ class TokenVotingVoteMethods extends TokenVotingVote {
class TokenVotingPluginMethods extends TokenVotingPlugin {
// build entity
// if id not changed it will update
withDefaultValues(): TokenVotingPluginMethods {
let votingModeIndex = parseInt(VOTING_MODE);
if (!VOTING_MODES.has(votingModeIndex)) {
throw new Error('voting mode is not valid.');
}

// we use casting here to remove autocompletion complaint
// since we know it will be captured by the previous check
let votingMode = VOTING_MODES.get(votingModeIndex) as string;
withDefaultValues(
votingModeIndex: number = parseInt(VOTING_MODE)
): TokenVotingPluginMethods {
const pluginAddress = Address.fromString(CONTRACT_ADDRESS);
const pluginEntityId = generatePluginEntityId(pluginAddress);

this.id = pluginEntityId;
this.daoAddress = Bytes.fromHexString(DAO_ADDRESS);
this.pluginAddress = pluginAddress;
this.votingMode = votingMode;

this.votingModeIndex = votingModeIndex as Int8; // for event we need the index of the mapping to simulate the contract event
this.votingMode = VOTING_MODES.has(votingModeIndex)
? (VOTING_MODES.get(votingModeIndex) as string)
: (VOTING_MODES.get(VOTING_MODE_UNDEFINED) as string);

this.supportThreshold = BigInt.fromString(SUPPORT_THRESHOLD);
this.minParticipation = BigInt.fromString(MIN_PARTICIPATION);
this.minDuration = BigInt.fromString(MIN_DURATION);
Expand All @@ -396,22 +402,8 @@ class TokenVotingPluginMethods extends TokenVotingPlugin {
}

createEvent_VotingSettingsUpdated(): VotingSettingsUpdated {
if (this.votingMode === null) {
throw new Error('Voting mode is null.');
}

// we cast to string only for stoping rust compiler complaints.
let votingMode: string = this.votingMode as string;
if (!VOTING_MODE_INDEXES.has(votingMode)) {
throw new Error('Voting mode index is not valid.');
}

// we use casting here to remove autocompletion complaint
// since we know it will be captured by the previous check
let votingModeIndex = VOTING_MODE_INDEXES.get(votingMode) as string;

let event = createNewVotingSettingsUpdatedEvent(
votingModeIndex, // for event we need the index of the mapping to simulate the contract event
this.votingModeIndex.toString(), // we need the index to simulate the event
(this.supportThreshold as BigInt).toString(),
(this.minParticipation as BigInt).toString(),
(this.minDuration as BigInt).toString(),
Expand All @@ -435,16 +427,18 @@ class TokenVotingPluginMethods extends TokenVotingPlugin {
}

setNewPluginSetting(
newVotingMode: string = VOTING_MODES.get(parseInt(TWO)) as string,
votingModeIndex: number = parseInt(TWO),
newSupportThreshold: BigInt = BigInt.fromString(NEW_SUPPORT_THRESHOLD),
newMinParticipation: BigInt = BigInt.fromString(NEW_MIN_PARTICIPATION),
newMinDuration: BigInt = BigInt.fromString(NEW_MIN_DURATION),
newMinProposerVotingPower: BigInt = BigInt.fromString(
NEW_MIN_PROPOSER_VOTING_POWER
)
): TokenVotingPluginMethods {
let votingMode = newVotingMode;
this.votingMode = votingMode;
this.votingModeIndex = votingModeIndex as Int8;
this.votingMode = VOTING_MODES.has(votingModeIndex)
? (VOTING_MODES.get(votingModeIndex) as string)
: (VOTING_MODES.get(VOTING_MODE_UNDEFINED) as string);
this.supportThreshold = newSupportThreshold;
this.minParticipation = newMinParticipation;
this.minDuration = newMinDuration;
Expand Down
2 changes: 1 addition & 1 deletion packages/subgraph/tests/plugin/governance-erc20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ describe('Governance ERC20', () => {
assert.entityCount('TokenVotingMember', 2);
});

test("It should initialize with the user's existing voting power and delegation, if she has any", () => {
test("it should initialize with the user's existing voting power and delegation, if they have any", () => {
let memberOne = new ExtendedTokenVotingMember().withDefaultValues(
fromAddress,
pluginAddress
Expand Down
Loading

0 comments on commit 43901f1

Please sign in to comment.