Skip to content

Commit

Permalink
Timestamp based Governance V2 SC
Browse files Browse the repository at this point in the history
  • Loading branch information
psorinionut committed Feb 14, 2025
1 parent 420269a commit 0af3fe6
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 86 deletions.
50 changes: 26 additions & 24 deletions energy-integration/governance-v2/src/configurable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
multiversx_sc::imports!();

use crate::errors::ERROR_NOT_AN_ESDT;

multiversx_sc::imports!();
pub type Timestamp = u64;

/// # MultiversX smart contract module - Governance
///
Expand All @@ -15,9 +17,9 @@ multiversx_sc::imports!();
/// - `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
/// - `votingDelayInSeconds` - Number of seconds to wait after a timestamp is proposed before being able to vote/downvote that proposal
/// - `votingPeriodInSeconds` - Number of seconds the voting period lasts (voting delay does not count towards this)
/// - `lockTimeAfterVotingEndsInSeconds` - Number of seconds to wait before a successful proposal can be executed
///
/// 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 @@ -30,10 +32,10 @@ multiversx_sc::imports!();
/// 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: Timestamp = 1;

Check warning on line 35 in energy-integration/governance-v2/src/configurable.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] energy-integration/governance-v2/src/configurable.rs#L35

warning: empty line after doc comment --> energy-integration/governance-v2/src/configurable.rs:33:1 | 33 | / /// 34 | | | |_^ 35 | const MIN_VOTING_DELAY: Timestamp = 1; | --------------------------------- the comment documents this constant | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments = note: `#[warn(clippy::empty_line_after_doc_comments)]` on by default = help: if the empty line is unintentional remove it
Raw output
energy-integration/governance-v2/src/configurable.rs:35:1:w:warning: empty line after doc comment
  --> energy-integration/governance-v2/src/configurable.rs:33:1
   |
33 | / ///
34 | |
   | |_^
35 |   const MIN_VOTING_DELAY: Timestamp = 1;
   |   --------------------------------- the comment documents this constant
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments
   = note: `#[warn(clippy::empty_line_after_doc_comments)]` on by default
   = help: if the empty line is unintentional remove it


__END__

Check warning on line 35 in energy-integration/governance-v2/src/configurable.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] energy-integration/governance-v2/src/configurable.rs#L35

warning: empty line after doc comment --> energy-integration/governance-v2/src/configurable.rs:33:1 | 33 | / /// 34 | | | |_^ 35 | const MIN_VOTING_DELAY: Timestamp = 1; | --------------------------------- the comment documents this constant | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments = note: `#[warn(clippy::empty_line_after_doc_comments)]` on by default = help: if the empty line is unintentional remove it
Raw output
energy-integration/governance-v2/src/configurable.rs:35:1:w:warning: empty line after doc comment
  --> energy-integration/governance-v2/src/configurable.rs:33:1
   |
33 | / ///
34 | |
   | |_^
35 |   const MIN_VOTING_DELAY: Timestamp = 1;
   |   --------------------------------- the comment documents this constant
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments
   = note: `#[warn(clippy::empty_line_after_doc_comments)]` on by default
   = help: if the empty line is unintentional remove it


__END__
const MAX_VOTING_DELAY: Timestamp = 604_800; // 1 Week
const MIN_VOTING_PERIOD: Timestamp = 86_400; // 24 Hours
const MAX_VOTING_PERIOD: Timestamp = 1_209_600; // 2 Weeks
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 @@ -74,15 +76,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: Timestamp) {
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: Timestamp) {
self.try_change_voting_period_in_seconds(new_value);
}

fn try_change_min_energy_for_propose(&self, new_value: BigUint) {
Expand Down Expand Up @@ -111,22 +113,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: Timestamp) {
require!(
(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: Timestamp) {
require!(
(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 @@ -160,13 +162,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<Timestamp>;

#[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<Timestamp>;

#[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: u64,
#[indexed] proposal: &GovernanceProposal<Self::Api>,
);

Expand Down
28 changes: 14 additions & 14 deletions energy-integration/governance-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use proposal_storage::VoteType;
use weekly_rewards_splitting::events::Week;
use weekly_rewards_splitting::global_info::ProxyTrait as _;

use crate::configurable::{FULL_PERCENTAGE, MAX_GAS_LIMIT_PER_BLOCK};
use crate::configurable::{Timestamp, FULL_PERCENTAGE, MAX_GAS_LIMIT_PER_BLOCK};
use crate::errors::*;
use crate::proposal_storage::ProposalVotes;

Expand All @@ -31,8 +31,8 @@ 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)
/// - `votingDelayInSeconds` - Number of seconds to wait after a timestamp is proposed before being able to vote/downvote that proposal
/// - `votingPeriodInSeconds` - Number of seconds the voting period lasts (voting delay does not count towards this)
/// - `withdraw_percentage_defeated` - Percetange of the fee to be returned if proposal defetead
/// - `energy_factory_address`
/// - `fees_collector_address`
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: Timestamp,
voting_period_in_seconds: Timestamp,
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();

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
12 changes: 6 additions & 6 deletions energy-integration/governance-v2/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,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: u64,
pub voting_period_in_seconds: u64,
pub withdraw_percentage_defeated: u64,
pub total_quorum: BigUint<M>,
pub proposal_start_block: u64,
pub proposal_start_timestamp: u64,
pub fee_withdrawn: bool,
}

Expand Down Expand Up @@ -101,11 +101,11 @@ impl<M: ManagedTypeApi> GovernanceProposal<M> {
amount: BigUint::zero(),
},
minimum_quorum: 0,
voting_delay_in_blocks: 0,
voting_period_in_blocks: 0,
voting_delay_in_seconds: 0,
voting_period_in_seconds: 0,
withdraw_percentage_defeated: 0,
total_quorum: BigUint::default(),
proposal_start_block: 0,
proposal_start_timestamp: 0,
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();
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

0 comments on commit 0af3fe6

Please sign in to comment.