Skip to content

Commit

Permalink
👷 change isFinalized to status enum
Browse files Browse the repository at this point in the history
  • Loading branch information
cmontecoding committed Sep 26, 2023
1 parent a2f34d7 commit 46e045f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
18 changes: 11 additions & 7 deletions src/AutomatedVoting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract AutomatedVoting is IAutomatedVoting {
struct Election {
uint256 startTime;
uint256 totalVote; ///@dev for quorum calculation
bool isFinalized; //todo: enum status, ongoing, invalid, finalized //todo: set elections to ongoing
Enums.status status;
Enums.electionType theElectionType;
address[] candidateAddresses;
mapping(address => uint256) voteCounts;
Expand Down Expand Up @@ -230,7 +230,11 @@ contract AutomatedVoting is IAutomatedVoting {
function isElectionFinalized(
uint256 _election
) public view override returns (bool) {
return elections[_election].isFinalized;
if (elections[_election].status == Enums.status.finalized || elections[_election].status == Enums.status.invalid) {
return true;
} else {
return false;
}
}

/// @notice starts the scheduled election
Expand Down Expand Up @@ -322,7 +326,7 @@ contract AutomatedVoting is IAutomatedVoting {
/// @notice finalizes an election
/// @param _election the election to finalize
function finalizeElection(uint256 _election) public override {
if (elections[_election].isFinalized) {
if (isElectionFinalized(_election)) {
revert ElectionAlreadyFinalized();
} else if (block.timestamp >= electionEndTime(_election)) {
_finalizeElection(_election);
Expand Down Expand Up @@ -424,7 +428,7 @@ contract AutomatedVoting is IAutomatedVoting {
uint256 electionNumber = electionCounter;
electionCounter++;
elections[electionNumber].startTime = block.timestamp;
elections[electionNumber].isFinalized = false;
elections[electionNumber].status = Enums.status.ongoing;
elections[electionNumber].theElectionType = electionType;
}

Expand Down Expand Up @@ -472,7 +476,7 @@ contract AutomatedVoting is IAutomatedVoting {

/// @dev internal function to finalize elections depending on type
function _finalizeElection(uint256 _election) internal {
elections[_election].isFinalized = true;
elections[_election].status = Enums.status.finalized;
if (
elections[_election].theElectionType == Enums.electionType.scheduled
) {
Expand Down Expand Up @@ -559,8 +563,8 @@ contract AutomatedVoting is IAutomatedVoting {
/// otherwise previous elections will be nullified
function _cancelOngoingElections() internal {
for (uint i = lastFinalizedElection; i < electionCounter; i++) {
if (elections[i].isFinalized == false) {
elections[i].isFinalized = true;
if (!isElectionFinalized(i)) {
elections[i].status = Enums.status.invalid;
}
}
lastFinalizedElection = electionCounter;
Expand Down
5 changes: 5 additions & 0 deletions src/Enums.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ contract Enums {
community,
replacement
}
enum status {
ongoing,
invalid,
finalized
}
}
15 changes: 9 additions & 6 deletions test/AutomatedVoting.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ contract AutomatedVotingTest is DefaultStakingV2Setup {
(
uint256 electionStartTime,
uint256 stakedAmountsForQuorum,
bool isFinalized,
Enums.status status,
Enums.electionType theElectionType
) = automatedVoting.elections(1);
assertEq(electionStartTime, block.timestamp);
assertEq(isFinalized, false);
assertTrue(status == Enums.status.ongoing);
assertTrue(theElectionType == Enums.electionType.scheduled);
assertEq(stakedAmountsForQuorum, 0);
}
Expand Down Expand Up @@ -400,11 +400,11 @@ contract AutomatedVotingTest is DefaultStakingV2Setup {
(
uint256 electionStartTime,
uint256 stakedAmountsForQuorum,
bool isFinalized,
Enums.status status,
Enums.electionType theElectionType
) = automatedVoting.elections(1);
assertEq(electionStartTime, block.timestamp);
assertEq(isFinalized, false);
assertTrue(status == Enums.status.ongoing);
assertTrue(theElectionType == Enums.electionType.community);
assertEq(stakedAmountsForQuorum, 0);
}
Expand Down Expand Up @@ -504,11 +504,11 @@ contract AutomatedVotingTest is DefaultStakingV2Setup {
(
uint256 electionStartTime,
uint256 stakedAmountsForQuorum,
bool isFinalized,
Enums.status status,
Enums.electionType theElectionType
) = automatedVoting.elections(1);
assertEq(electionStartTime, block.timestamp);
assertEq(isFinalized, false);
assertTrue(status == Enums.status.ongoing);
assertTrue(theElectionType == Enums.electionType.replacement);
assertEq(stakedAmountsForQuorum, 0);

Expand Down Expand Up @@ -1286,6 +1286,9 @@ contract AutomatedVotingTest is DefaultStakingV2Setup {
.elections(1);
assertTrue(theElectionType == Enums.electionType.replacement);
automatedVotingInternals.cancelOngoingElectionsInternal();
(, , Enums.status status, ) = automatedVotingInternals
.elections(1);
assertTrue(status == Enums.status.invalid);
assertEq(automatedVotingInternals.isElectionFinalized(1), true);
assertEq(automatedVotingInternals.isElectionFinalized(2), true);
}
Expand Down

0 comments on commit 46e045f

Please sign in to comment.