Skip to content

Commit

Permalink
Merge pull request #753 from multiversx/governance-v2-fixes
Browse files Browse the repository at this point in the history
Governance-v2: Fix div by 0 error + unit test
  • Loading branch information
dragos-rebegea authored Jul 27, 2023
2 parents 08f3a4f + 456d52d commit f6cb945
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
3 changes: 0 additions & 3 deletions energy-integration/governance-v2/elrond.json

This file was deleted.

4 changes: 4 additions & 0 deletions energy-integration/governance-v2/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ pub trait ViewsModule:
let proposal = self.proposals().get(proposal_id);
let total_energy_for_proposal = proposal.total_energy;

if total_energy_for_proposal == 0u64 {
return false
}

let required_minimum_percentage = proposal.minimum_quorum;

let current_quorum = self.proposal_votes(proposal_id).get().quorum;
Expand Down
38 changes: 38 additions & 0 deletions energy-integration/governance-v2/tests/gov_rust_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,44 @@ fn gov_propose_test() {
.assert_ok();
}

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

let first_user_addr = gov_setup.first_user.clone();
let sc_addr = gov_setup.gov_wrapper.address_ref().clone();
let min_fee = rust_biguint!(MIN_FEE_FOR_PROPOSE) * DECIMALS_CONST;
// Give proposer the minimum fee
gov_setup
.b_mock
.set_nft_balance(&first_user_addr, WXMEX_TOKEN_ID, 1, &min_fee, &Empty);

let (result, proposal_id) = gov_setup.propose(
&first_user_addr,
&min_fee,
&sc_addr,
b"changeTODO",
vec![1_000u64.to_be_bytes().to_vec()],
);
result.assert_ok();
assert_eq!(proposal_id, 1);
gov_setup.increment_block_nonce(VOTING_PERIOD_BLOCKS + VOTING_DELAY_BLOCKS);

gov_setup
.b_mock
.execute_query(&gov_setup.gov_wrapper, |sc| {
let mut proposal = sc.proposals().get(1);
proposal.total_energy = managed_biguint!(0);
sc.proposals().set(1, &proposal);
assert!(
sc.get_proposal_status(1) == GovernanceProposalStatus::Defeated,
"Action should have been Defeated"
);
})
.assert_ok();
}


#[test]
fn gov_no_veto_vote_test() {
let mut gov_setup = GovSetup::new(governance_v2::contract_obj);
Expand Down
8 changes: 2 additions & 6 deletions energy-integration/governance-v2/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 31
// Endpoints: 27
// Async Callback (empty): 1
// Total number of exported functions: 33
// Total number of exported functions: 29

#![no_std]
#![feature(alloc_error_handler, lang_items)]
Expand Down Expand Up @@ -38,10 +38,6 @@ multiversx_sc_wasm_adapter::endpoints! {
getUserVotedProposals
getProposalVotes
getProposalStatus
getCurrentQuorum
getProposer
getProposalDescription
getProposalActions
getFeesCollectorAddress
setEnergyFactoryAddress
getEnergyFactoryAddress
Expand Down

0 comments on commit f6cb945

Please sign in to comment.