Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
55 changes: 27 additions & 28 deletions energy-integration/governance-v2/src/configurable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ multiversx_sc::imports!();
///
/// Voting is done through energy.
///
/// The module provides the following configurable parameters:
/// The module provides the following configurable parameters:
/// - `minEnergyForPropose` - the minimum energy required for submitting a proposal
/// - `quorum` - the minimum number of (`votes` minus `downvotes`) at the end of voting period
/// - `maxActionsPerProposal` - Maximum number of actions (transfers and/or smart contract calls) that a proposal may have
/// - `votingDelayInBlocks` - Number of blocks to wait after a block is proposed before being able to vote/downvote that proposal
/// - `votingPeriodInBlocks` - Number of blocks the voting period lasts (voting delay does not count towards this)
/// - `lockTimeAfterVotingEndsInBlocks` - Number of blocks to wait before a successful proposal can be executed
/// - `quorum` - the minimum number of (`votes` minus `downvotes`) at the end of voting period
/// - `maxActionsPerProposal` - Maximum number of actions (transfers and/or smart contract calls) that a proposal may have
/// - `votingDelayInSeconds` - Number of seconds to wait after a proposal is created before being able to vote/downvote that proposal
/// - `votingPeriodInSeconds` - Number of seconds the voting period lasts (voting delay does not count towards this)
///
/// The module also provides events for most actions that happen:
/// - `proposalCreated` - triggers when a proposal is created. Also provoides all the relevant information, like proposer, actions etc.
Expand All @@ -28,10 +27,10 @@ multiversx_sc::imports!();
///
/// Please note that although the main contract can modify the module's storage directly, it is not recommended to do so,
/// as that defeats the whole purpose of having governance. These parameters should only be modified through actions.
const MIN_VOTING_DELAY: u64 = 1;
const MAX_VOTING_DELAY: u64 = 100_800; // 1 Week
const MIN_VOTING_PERIOD: u64 = 14_400; // 24 Hours
const MAX_VOTING_PERIOD: u64 = 201_600; // 2 Weeks
const MIN_VOTING_DELAY: DurationSeconds = DurationSeconds::new(1);
const MAX_VOTING_DELAY: DurationSeconds = DurationSeconds::new(100_800);
const MIN_VOTING_PERIOD: DurationSeconds = DurationSeconds::new(14_400);
const MAX_VOTING_PERIOD: DurationSeconds = DurationSeconds::new(201_600);
const MIN_QUORUM: u64 = 1_000; // 10%
const MAX_QUORUM: u64 = 6_000; // 60%
const MIN_MIN_FEE_FOR_PROPOSE: u64 = 2_000_000;
Expand Down Expand Up @@ -72,15 +71,15 @@ pub trait ConfigurablePropertiesModule:
}

#[only_owner]
#[endpoint(changeVotingDelayInBlocks)]
fn change_voting_delay_in_blocks(&self, new_value: u64) {
self.try_change_voting_delay_in_blocks(new_value);
#[endpoint(changeVotingDelayInSeconds)]
fn change_voting_delay_in_seconds(&self, new_value: DurationSeconds) {
self.try_change_voting_delay_in_seconds(&new_value);
}

#[only_owner]
#[endpoint(changeVotingPeriodInBlocks)]
fn change_voting_period_in_blocks(&self, new_value: u64) {
self.try_change_voting_period_in_blocks(new_value);
#[endpoint(changeVotingPeriodInSeconds)]
fn change_voting_period_in_seconds(&self, new_value: DurationSeconds) {
self.try_change_voting_period_in_seconds(&new_value);
}

fn try_change_min_energy_for_propose(&self, new_value: BigUint) {
Expand Down Expand Up @@ -109,22 +108,22 @@ pub trait ConfigurablePropertiesModule:
self.quorum_percentage().set(new_quorum_percentage);
}

fn try_change_voting_delay_in_blocks(&self, new_voting_delay: u64) {
fn try_change_voting_delay_in_seconds(&self, new_voting_delay: &DurationSeconds) {
require!(
(MIN_VOTING_DELAY..MAX_VOTING_DELAY).contains(&new_voting_delay),
(MIN_VOTING_DELAY..MAX_VOTING_DELAY).contains(new_voting_delay),
"Not valid value for voting delay!"
);

self.voting_delay_in_blocks().set(new_voting_delay);
self.voting_delay_in_seconds().set(new_voting_delay);
}

fn try_change_voting_period_in_blocks(&self, new_voting_period: u64) {
fn try_change_voting_period_in_seconds(&self, new_voting_period: &DurationSeconds) {
require!(
(MIN_VOTING_PERIOD..MAX_VOTING_PERIOD).contains(&new_voting_period),
(MIN_VOTING_PERIOD..MAX_VOTING_PERIOD).contains(new_voting_period),
"Not valid value for voting period!"
);

self.voting_period_in_blocks().set(new_voting_period);
self.voting_period_in_seconds().set(new_voting_period);
}

fn try_change_withdraw_percentage_defeated(&self, new_withdraw_percentage: u64) {
Expand Down Expand Up @@ -158,13 +157,13 @@ pub trait ConfigurablePropertiesModule:
#[storage_mapper("quorumPercentage")]
fn quorum_percentage(&self) -> SingleValueMapper<u64>;

#[view(getVotingDelayInBlocks)]
#[storage_mapper("votingDelayInBlocks")]
fn voting_delay_in_blocks(&self) -> SingleValueMapper<u64>;
#[view(getVotingDelayInSeconds)]
#[storage_mapper("votingDelayInSeconds")]
fn voting_delay_in_seconds(&self) -> SingleValueMapper<DurationSeconds>;

#[view(getVotingPeriodInBlocks)]
#[storage_mapper("votingPeriodInBlocks")]
fn voting_period_in_blocks(&self) -> SingleValueMapper<u64>;
#[view(getVotingPeriodInSeconds)]
#[storage_mapper("votingPeriodInSeconds")]
fn voting_period_in_seconds(&self) -> SingleValueMapper<DurationSeconds>;

#[view(getFeeTokenId)]
#[storage_mapper("feeTokenId")]
Expand Down
2 changes: 1 addition & 1 deletion energy-integration/governance-v2/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait EventsModule {
&self,
#[indexed] proposal_id: usize,
#[indexed] proposer: &ManagedAddress,
#[indexed] start_block: u64,
#[indexed] start_timestamp: TimestampSeconds,
#[indexed] proposal: &GovernanceProposal<Self::Api>,
);

Expand Down
30 changes: 15 additions & 15 deletions energy-integration/governance-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ pub trait GovernanceV2:
{
/// - `min_energy_for_propose` - the minimum energy required for submitting a proposal
/// - `min_fee_for_propose` - the minimum fee required for submitting a proposal
/// - `quorum_percentage` - the minimum number of (`votes` minus `downvotes`) at the end of voting period
/// - `votingDelayInBlocks` - Number of blocks to wait after a block is proposed before being able to vote/downvote that proposal
/// - `votingPeriodInBlocks` - Number of blocks the voting period lasts (voting delay does not count towards this)
/// - `withdraw_percentage_defeated` - Percetange of the fee to be returned if proposal defetead
/// - `quorum_percentage` - the minimum number of (`votes` minus `downvotes`) at the end of voting period
/// - `voting_delay_in_seconds` - Number of seconds to wait after a proposal is created before being able to vote/downvote that proposal
/// - `voting_period_in_seconds` - Number of seconds the voting period lasts (voting delay does not count towards this)
/// - `withdraw_percentage_defeated` - Percentage of the fee to be returned if proposal defeated
/// - `energy_factory_address`
/// - `fees_collector_address`
/// - `fee_token` - The token used to pay the fee
Expand All @@ -43,8 +43,8 @@ pub trait GovernanceV2:
min_energy_for_propose: BigUint,
min_fee_for_propose: BigUint,
quorum_percentage: u64,
voting_delay_in_blocks: u64,
voting_period_in_blocks: u64,
voting_delay_in_seconds: DurationSeconds,
voting_period_in_seconds: DurationSeconds,
withdraw_percentage_defeated: u64,
energy_factory_address: ManagedAddress,
fees_collector_address: ManagedAddress,
Expand All @@ -53,8 +53,8 @@ pub trait GovernanceV2:
self.try_change_min_energy_for_propose(min_energy_for_propose);
self.try_change_min_fee_for_propose(min_fee_for_propose);
self.try_change_quorum_percentage(quorum_percentage);
self.try_change_voting_delay_in_blocks(voting_delay_in_blocks);
self.try_change_voting_period_in_blocks(voting_period_in_blocks);
self.try_change_voting_delay_in_seconds(&voting_delay_in_seconds);
self.try_change_voting_period_in_seconds(&voting_period_in_seconds);
self.try_change_withdraw_percentage_defeated(withdraw_percentage_defeated);
self.set_energy_factory_address(energy_factory_address);
self.fees_collector_address().set(&fees_collector_address);
Expand Down Expand Up @@ -121,10 +121,10 @@ pub trait GovernanceV2:
);

let minimum_quorum = self.quorum_percentage().get();
let voting_delay_in_blocks = self.voting_delay_in_blocks().get();
let voting_period_in_blocks = self.voting_period_in_blocks().get();
let voting_delay_in_seconds = self.voting_delay_in_seconds().get();
let voting_period_in_seconds = self.voting_period_in_seconds().get();
let withdraw_percentage_defeated = self.withdraw_percentage_defeated().get();
let current_block = self.blockchain().get_block_nonce();
let current_timestamp = self.blockchain().get_block_timestamp_seconds();

let proposal = GovernanceProposal {
proposal_id: self.proposals().len() + 1,
Expand All @@ -133,18 +133,18 @@ pub trait GovernanceV2:
actions: gov_actions,
fee_payment: user_fee,
minimum_quorum,
voting_delay_in_blocks,
voting_period_in_blocks,
voting_delay_in_seconds,
voting_period_in_seconds,
withdraw_percentage_defeated,
total_quorum: BigUint::zero(),
proposal_start_block: current_block,
proposal_start_timestamp: current_timestamp,
fee_withdrawn: false,
};
let proposal_id = self.proposals().push(&proposal);

self.proposal_votes(proposal_id)
.set(ProposalVotes::default());
self.proposal_created_event(proposal_id, &proposer, current_block, &proposal);
self.proposal_created_event(proposal_id, &proposer, current_timestamp, &proposal);

proposal_id
}
Expand Down
14 changes: 7 additions & 7 deletions energy-integration/governance-v2/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ pub struct GovernanceProposal<M: ManagedTypeApi> {
pub description: ManagedBuffer<M>,
pub fee_payment: EsdtTokenPayment<M>,
pub minimum_quorum: u64,
pub voting_delay_in_blocks: u64,
pub voting_period_in_blocks: u64,
pub voting_delay_in_seconds: DurationSeconds,
pub voting_period_in_seconds: DurationSeconds,
pub withdraw_percentage_defeated: u64,
pub total_quorum: BigUint<M>,
pub proposal_start_block: u64,
pub proposal_start_timestamp: TimestampSeconds,
pub fee_withdrawn: bool,
}

Expand Down Expand Up @@ -97,16 +97,16 @@ impl<M: ManagedTypeApi> GovernanceProposal<M> {
actions: ArrayVec::default(),
description: ManagedBuffer::default(),
fee_payment: EsdtTokenPayment {
token_identifier: TokenIdentifier::from(""),
token_identifier: TokenIdentifier::from("EGLD-123456"),
token_nonce: 0,
amount: BigUint::zero(),
},
minimum_quorum: 0,
voting_delay_in_blocks: 0,
voting_period_in_blocks: 0,
voting_delay_in_seconds: DurationSeconds::zero(),
voting_period_in_seconds: DurationSeconds::zero(),
withdraw_percentage_defeated: 0,
total_quorum: BigUint::default(),
proposal_start_block: 0,
proposal_start_timestamp: TimestampSeconds::zero(),
fee_withdrawn: false,
}
}
Expand Down
14 changes: 7 additions & 7 deletions energy-integration/governance-v2/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ pub trait ViewsModule:
return GovernanceProposalStatus::None;
}

let current_block = self.blockchain().get_block_nonce();
let current_timestamp = self.blockchain().get_block_timestamp_seconds();
let proposal = self.proposals().get(proposal_id);
let proposal_block = proposal.proposal_start_block;
let proposal_timestamp = proposal.proposal_start_timestamp;

let voting_delay = proposal.voting_delay_in_blocks;
let voting_period = proposal.voting_period_in_blocks;
let voting_delay = proposal.voting_delay_in_seconds;
let voting_period = proposal.voting_period_in_seconds;

let voting_start = proposal_block + voting_delay;
let voting_start = proposal_timestamp + voting_delay;
let voting_end = voting_start + voting_period;

if current_block < voting_start {
if current_timestamp < voting_start {
return GovernanceProposalStatus::Pending;
}
if current_block >= voting_start && current_block < voting_end {
if current_timestamp >= voting_start && current_timestamp < voting_end {
return GovernanceProposalStatus::Active;
}

Expand Down
Loading