diff --git a/src/governance.rs b/src/governance.rs index f71f10b..bb3876d 100644 --- a/src/governance.rs +++ b/src/governance.rs @@ -2,7 +2,7 @@ use crate::staking::StakingConfig; use crate::system::SystemConfig; use std::collections::HashMap; -pub trait GovernanceConfig: StakingConfig {} +pub trait GovernanceConfig: StakingConfig + SystemConfig {} pub struct Proposal { description: String, @@ -11,7 +11,7 @@ pub struct Proposal { status: ProposalStatus, } -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub enum ProposalStatus { Active, Approved, @@ -26,16 +26,29 @@ pub struct GovernancePallet { impl GovernancePallet { pub fn new() -> Self { - todo!() + Self { + proposals: HashMap::new(), + votes: HashMap::new(), + next_proposal_id: 0 + } } // Create a new proposal pub fn create_proposal( &mut self, - creator: T::AccountId, + _creator: T::AccountId, description: String, ) -> Result { - todo!() + self.next_proposal_id += 1; + + self.proposals.insert(self.next_proposal_id, Proposal{ + description, + yes_votes: 0, + no_votes: 0, + status: ProposalStatus::Active + }); + + Ok(self.next_proposal_id) } // Vote on a proposal (true = yes, false = no) @@ -45,17 +58,63 @@ impl GovernancePallet { proposal_id: u32, vote_type: bool, ) -> Result<(), &'static str> { - todo!() + if self.votes.contains_key(&(voter.clone(), proposal_id)) { + return Err("can not vote more than once"); + } + + if !self.proposals.contains_key(&proposal_id) { + return Err("proposal does not exist"); + } + + self.votes.insert((voter, proposal_id), vote_type); + let proposal = self.get_proposal(proposal_id).expect("could not found the proposal"); + + if proposal.status != ProposalStatus::Active { + return Err("proposal was Finalized"); + } + let yes_votes = if vote_type {proposal.yes_votes + 1} else {proposal.yes_votes}; + let no_votes = if !vote_type {proposal.no_votes + 1} else {proposal.no_votes}; + + let new_proposal = Proposal { + description: proposal.description.clone(), + yes_votes, + no_votes, + status: proposal.status.clone(), + }; + + self.proposals.insert(proposal_id, new_proposal); + + Ok(()) } // Get proposal details pub fn get_proposal(&self, proposal_id: u32) -> Option<&Proposal> { - todo!() + self.proposals.get(&proposal_id) } // Finalize a proposal (changes status based on votes) pub fn finalize_proposal(&mut self, proposal_id: u32) -> Result { - todo!() + let proposal = self.get_proposal(proposal_id).expect("could not found the proposal"); + + let yes_votes = proposal.yes_votes; + let no_votes = proposal.no_votes; + + let status = if yes_votes > no_votes { + ProposalStatus::Approved + } else { + ProposalStatus::Rejected + }; + + let new_proposal = Proposal { + description: proposal.description.clone(), + yes_votes, + no_votes, + status: status.clone(), + }; + + self.proposals.insert(proposal_id, new_proposal); + + Ok(status) } } diff --git a/src/staking.rs b/src/staking.rs index 7bd646e..6d03cb1 100644 --- a/src/staking.rs +++ b/src/staking.rs @@ -16,32 +16,53 @@ pub struct StakingPallet { impl StakingPallet { pub fn new() -> Self { - todo!() + Self { + free_balances: HashMap::new(), + staked_balances: HashMap::new() + } } // Set free balance for an account pub fn set_balance(&mut self, who: T::AccountId, amount: T::Balance) { - todo!() + self.free_balances.insert(who.clone(), amount); } // Stake tokens (move from free to staked) pub fn stake(&mut self, who: T::AccountId, amount: T::Balance) -> Result<(), &'static str> { - todo!() + let free_balance = self.get_free_balance(who.clone()); + let staked_balance = self.get_staked_balance(who.clone()); + + let new_free_balance = free_balance.checked_sub(&amount).ok_or("not enough funds")?; + let new_staked_balance = staked_balance.checked_add(&amount).ok_or("overflow")?; + + self.free_balances.insert(who.clone(), new_free_balance); + self.staked_balances.insert(who.clone(), new_staked_balance); + + Ok(()) } // Unstake tokens (move from staked to free) pub fn unstake(&mut self, who: T::AccountId, amount: T::Balance) -> Result<(), &'static str> { - todo!() + let staked_balance = self.get_staked_balance(who.clone()); + let free_balance = self.get_free_balance(who.clone()); + + let new_staked_balance = staked_balance.checked_sub(&amount).ok_or("not enough funds")?; + let new_free_balance = free_balance.checked_add(&amount).ok_or("overvlow")?; + + self.free_balances.insert(who.clone(), new_free_balance); + self.staked_balances.insert(who.clone(), new_staked_balance); + + Ok(()) } // Get free balance for an account pub fn get_free_balance(&self, who: T::AccountId) -> T::Balance { - todo!() + *self.free_balances.get(&who).unwrap_or(&T::Balance::zero()) } // Get staked balance for an account pub fn get_staked_balance(&self, who: T::AccountId) -> T::Balance { - todo!() + *self.staked_balances.get(&who).unwrap_or(&T::Balance::zero()) } }