Skip to content

Commit

Permalink
Merge pull request #755 from multiversx/governance-v2-energy-refactor
Browse files Browse the repository at this point in the history
Governance v2 energy refactor
  • Loading branch information
dragos-rebegea authored Jul 27, 2023
2 parents f6cb945 + 06a3235 commit d49b05d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
16 changes: 8 additions & 8 deletions energy-integration/governance-v2/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@ pub trait EventsModule {
&self,
#[indexed] up_voter: &ManagedAddress,
#[indexed] proposal_id: ProposalId,
#[indexed] nr_votes: &BigUint,
#[indexed] energy: &BigUint,
#[indexed] voting_power: &BigUint,
#[indexed] user_quorum: &BigUint,
);

#[event("downVoteCast")]
fn down_vote_cast_event(
&self,
#[indexed] down_voter: &ManagedAddress,
#[indexed] proposal_id: ProposalId,
#[indexed] nr_votes: &BigUint,
#[indexed] energy: &BigUint,
#[indexed] voting_power: &BigUint,
#[indexed] user_quorum: &BigUint,
);

#[event("downVetoVoteCast")]
fn down_veto_vote_cast_event(
&self,
#[indexed] down_veto_voter: &ManagedAddress,
#[indexed] proposal_id: ProposalId,
#[indexed] nr_votes: &BigUint,
#[indexed] energy: &BigUint,
#[indexed] voting_power: &BigUint,
#[indexed] user_quorum: &BigUint,
);

#[event("abstainVoteCast")]
fn abstain_vote_cast_event(
&self,
#[indexed] abstain_voter: &ManagedAddress,
#[indexed] proposal_id: ProposalId,
#[indexed] nr_votes: &BigUint,
#[indexed] energy: &BigUint,
#[indexed] voting_power: &BigUint,
#[indexed] user_quorum: &BigUint,
);

#[event("proposalCanceled")]
Expand Down
26 changes: 13 additions & 13 deletions energy-integration/governance-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub trait GovernanceV2:
voting_delay_in_blocks,
voting_period_in_blocks,
withdraw_percentage_defeated,
total_energy: BigUint::zero(),
total_quorum: BigUint::zero(),
proposal_start_block: current_block,
};
let proposal_id = self.proposals().push(&proposal);
Expand Down Expand Up @@ -176,47 +176,47 @@ pub trait GovernanceV2:
.last_global_update_week()
.execute_on_dest_context();

let total_energy: BigUint = self
let total_quorum: BigUint = self
.fees_collector_proxy(fees_collector_addr)
.total_energy_for_week(last_global_update_week)
.execute_on_dest_context();

let mut proposal = self.proposals().get(proposal_id);
proposal.total_energy = total_energy;
proposal.total_quorum = total_quorum;
self.proposals().set(proposal_id, &proposal);
}

let user_energy = self.get_energy_amount_non_zero(&voter);
let voting_power = user_energy.sqrt();
let user_quorum = self.get_energy_amount_non_zero(&voter);
let voting_power = user_quorum.sqrt();

match vote {
VoteType::UpVote => {
self.proposal_votes(proposal_id).update(|proposal_votes| {
proposal_votes.up_votes += &voting_power.clone();
proposal_votes.quorum += &user_energy.clone();
proposal_votes.quorum += &user_quorum.clone();
});
self.up_vote_cast_event(&voter, proposal_id, &voting_power, &user_energy);
self.up_vote_cast_event(&voter, proposal_id, &voting_power, &user_quorum);
}
VoteType::DownVote => {
self.proposal_votes(proposal_id).update(|proposal_votes| {
proposal_votes.down_votes += &voting_power.clone();
proposal_votes.quorum += &user_energy.clone();
proposal_votes.quorum += &user_quorum.clone();
});
self.down_vote_cast_event(&voter, proposal_id, &voting_power, &user_energy);
self.down_vote_cast_event(&voter, proposal_id, &voting_power, &user_quorum);
}
VoteType::DownVetoVote => {
self.proposal_votes(proposal_id).update(|proposal_votes| {
proposal_votes.down_veto_votes += &voting_power.clone();
proposal_votes.quorum += &user_energy.clone();
proposal_votes.quorum += &user_quorum.clone();
});
self.down_veto_vote_cast_event(&voter, proposal_id, &voting_power, &user_energy);
self.down_veto_vote_cast_event(&voter, proposal_id, &voting_power, &user_quorum);
}
VoteType::AbstainVote => {
self.proposal_votes(proposal_id).update(|proposal_votes| {
proposal_votes.abstain_votes += &voting_power.clone();
proposal_votes.quorum += &user_energy.clone();
proposal_votes.quorum += &user_quorum.clone();
});
self.abstain_vote_cast_event(&voter, proposal_id, &voting_power, &user_energy);
self.abstain_vote_cast_event(&voter, proposal_id, &voting_power, &user_quorum);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion energy-integration/governance-v2/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ pub struct GovernanceProposal<M: ManagedTypeApi> {
pub voting_delay_in_blocks: u64,
pub voting_period_in_blocks: u64,
pub withdraw_percentage_defeated: u64,
pub total_energy: BigUint<M>,
pub total_quorum: BigUint<M>,
pub proposal_start_block: u64,
}
8 changes: 4 additions & 4 deletions energy-integration/governance-v2/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ pub trait ViewsModule:

fn quorum_reached(&self, proposal_id: ProposalId) -> bool {
let proposal = self.proposals().get(proposal_id);
let total_energy_for_proposal = proposal.total_energy;
let total_quorum_for_proposal = proposal.total_quorum;

if total_energy_for_proposal == 0u64 {
return false
if total_quorum_for_proposal == 0u64 {
return false;
}

let required_minimum_percentage = proposal.minimum_quorum;

let current_quorum = self.proposal_votes(proposal_id).get().quorum;
let current_quorum_percentage =
current_quorum * FULL_PERCENTAGE / total_energy_for_proposal;
current_quorum * FULL_PERCENTAGE / total_quorum_for_proposal;

current_quorum_percentage >= required_minimum_percentage
}
Expand Down
3 changes: 1 addition & 2 deletions energy-integration/governance-v2/tests/gov_rust_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn gov_propose_total_energy_0_test() {
.b_mock
.execute_query(&gov_setup.gov_wrapper, |sc| {
let mut proposal = sc.proposals().get(1);
proposal.total_energy = managed_biguint!(0);
proposal.total_quorum = managed_biguint!(0);
sc.proposals().set(1, &proposal);
assert!(
sc.get_proposal_status(1) == GovernanceProposalStatus::Defeated,
Expand All @@ -119,7 +119,6 @@ fn gov_propose_total_energy_0_test() {
.assert_ok();
}


#[test]
fn gov_no_veto_vote_test() {
let mut gov_setup = GovSetup::new(governance_v2::contract_obj);
Expand Down

0 comments on commit d49b05d

Please sign in to comment.