Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cast voting mode when undefined #19

Merged
merged 26 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
671071c
feat: manage the voting mode indexes to set an Undefined mode when th…
clauBv23 Apr 10, 2024
2c1f777
feat: remove VOTING_MODE_INDEXES because is not longer needed and add…
clauBv23 Apr 10, 2024
1c456aa
feat: update the extended method classes to properly manage with the …
clauBv23 Apr 10, 2024
e088648
feat: modify the extender to also generate the class extended properties
clauBv23 Apr 10, 2024
99ea005
feat: add test for the checking it works when an undefined voting mod…
clauBv23 Apr 10, 2024
7b64a78
feat: create a constant for the default index of undefined voting mode
clauBv23 Apr 11, 2024
977937d
ci: rename votingMode that comes from solidity event to avoid confusion
clauBv23 Apr 12, 2024
6e55c9f
ci: rename constant to avoid confusion
clauBv23 Apr 12, 2024
2ad7f75
ci: remove voting mode one for more explicit one
clauBv23 Apr 12, 2024
302514d
fix: remove the properties definition from the extender
clauBv23 Apr 12, 2024
e3b51ab
fix: remove the votingModeIndex from the extended class because is al…
clauBv23 Apr 12, 2024
ea62699
feat: add voting mode index to the plugin and proposal entity
clauBv23 Apr 12, 2024
34400d6
feat: store the voting mode index manager in the handlers
clauBv23 Apr 12, 2024
ee8a0f7
ci: undo the voting mode constant rename
clauBv23 Apr 12, 2024
ddd2761
ci: rename the constant name undo
clauBv23 Apr 12, 2024
7d0c0dd
ci: change voting mode index type to int8
clauBv23 Apr 12, 2024
bc2c91d
feat: modify the extended method class to cast the voting mode index …
clauBv23 Apr 12, 2024
e800940
feat: update the tests
clauBv23 Apr 12, 2024
c27e91a
Merge branch 'develop' into OS-1217/cast-voting-mode-when-undefined
clauBv23 Apr 12, 2024
c7dd2b7
fix: define the received index, not hte fixed one
clauBv23 Apr 12, 2024
111979d
ci: update the test comment
clauBv23 Apr 12, 2024
7d54b83
feat: add mock call to correctly simulate the test
clauBv23 Apr 12, 2024
6752ce9
Update packages/subgraph/tests/helpers/method-classes.ts
clauBv23 Apr 15, 2024
6dc2a3e
Update packages/subgraph/tests/plugin/governance-erc20.test.ts
clauBv23 Apr 15, 2024
4e34a5f
feat: define constants to remove magic numbers in subgraph tests
clauBv23 Apr 15, 2024
3ef486d
ci: remove not needed imports
clauBv23 Apr 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
novaknole marked this conversation as resolved.
Show resolved Hide resolved
// 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 for simulate the event
clauBv23 marked this conversation as resolved.
Show resolved Hide resolved
(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 has any", () => {
clauBv23 marked this conversation as resolved.
Show resolved Hide resolved
let memberOne = new ExtendedTokenVotingMember().withDefaultValues(
fromAddress,
pluginAddress
Expand Down
Loading
Loading