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

Governance-v2: Fix GovernanceProposal default #784

Merged
merged 8 commits into from
Nov 13, 2023
47 changes: 46 additions & 1 deletion energy-integration/governance-v2/src/proposal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use multiversx_sc::codec::{DecodeDefault, EncodeDefault};

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

Expand Down Expand Up @@ -50,7 +52,9 @@ impl<M: ManagedTypeApi> From<GovernanceActionAsMultiArg<M>> for GovernanceAction
}
}

#[derive(TypeAbi, TopEncode, TopDecode, PartialEq, Debug)]
#[derive(
TypeAbi, NestedEncode, NestedDecode, PartialEq, Debug, TopEncodeOrDefault, TopDecodeOrDefault,
)]
pub struct GovernanceProposal<M: ManagedTypeApi> {
pub proposal_id: usize,
pub proposer: ManagedAddress<M>,
Expand All @@ -65,3 +69,44 @@ pub struct GovernanceProposal<M: ManagedTypeApi> {
pub proposal_start_block: u64,
pub fee_withdrawn: bool,
}

impl<M: ManagedTypeApi> EncodeDefault for GovernanceProposal<M> {
fn is_default(&self) -> bool {
self.proposal_id == 0
}
}

impl<M: ManagedTypeApi> DecodeDefault for GovernanceProposal<M> {
fn default() -> Self {
Self::new()
}
}

impl<M: ManagedTypeApi> Default for GovernanceProposal<M> {
fn default() -> Self {
Self::new()
}
}

impl<M: ManagedTypeApi> GovernanceProposal<M> {
pub fn new() -> Self {
GovernanceProposal {
proposal_id: 0,
proposer: ManagedAddress::default(),
actions: ArrayVec::default(),
description: ManagedBuffer::default(),
fee_payment: EsdtTokenPayment {
token_identifier: TokenIdentifier::from(""),
token_nonce: 0,
amount: BigUint::zero(),
},
minimum_quorum: BigUint::default(),
voting_delay_in_blocks: 0,
voting_period_in_blocks: 0,
withdraw_percentage_defeated: 0,
total_quorum: BigUint::default(),
proposal_start_block: 0,
fee_withdrawn: false,
}
}
}
30 changes: 30 additions & 0 deletions energy-integration/governance-v2/tests/gov_rust_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,19 @@ fn gov_propose_cancel_proposal_id_test() {

gov_setup.cancel_proposal(&first_user_addr, 2).assert_ok();

// Try to retrieve the cancelled proposal
gov_setup
.b_mock
.execute_tx(
&gov_setup.first_user.clone(),
&gov_setup.gov_wrapper,
&rust_biguint!(0),
|sc| {
sc.proposals().get(2);
},
)
.assert_ok();

// Check proposer balance (fee should be refunded)
gov_setup.b_mock.check_nft_balance::<Empty>(
&first_user_addr,
Expand All @@ -730,6 +743,10 @@ fn gov_propose_cancel_proposal_id_test() {
&min_fee,
None,
);
assert_eq!(proposal_id, 3);
gov_setup
.check_proposal_id_consistency(&first_user_addr, proposal_id)
.assert_ok();

// Proposal ID = 4
let (result, proposal_id) = gov_setup.propose(
Expand All @@ -747,6 +764,19 @@ fn gov_propose_cancel_proposal_id_test() {

gov_setup.cancel_proposal(&first_user_addr, 4).assert_ok();

// Try to retrieve the cancelled proposal
gov_setup
.b_mock
.execute_tx(
&gov_setup.first_user.clone(),
&gov_setup.gov_wrapper,
&rust_biguint!(0),
|sc| {
sc.proposals().get(4);
},
)
.assert_ok();

// Proposal ID = 5
let (result, proposal_id) = gov_setup.propose(
&first_user_addr,
Expand Down