From e3b3a517cf3f27fa912b92f5a01d2d5c126cc40d Mon Sep 17 00:00:00 2001 From: Syamsudin Date: Thu, 13 Mar 2025 14:08:33 +0700 Subject: [PATCH 1/3] move to new repo --- src/governance.rs | 68 ++++++++++++++++++++++++++++++++++++++++++----- src/staking.rs | 33 ++++++++++++++++++----- 2 files changed, 89 insertions(+), 12 deletions(-) diff --git a/src/governance.rs b/src/governance.rs index f71f10b..0a237f5 100644 --- a/src/governance.rs +++ b/src/governance.rs @@ -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,60 @@ 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"); + + 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()) } } From d738a0e16225bc1fcc449c6231d5aa3180faad5e Mon Sep 17 00:00:00 2001 From: Syamsudin Date: Thu, 13 Mar 2025 14:16:29 +0700 Subject: [PATCH 2/3] make litle fix --- src/governance.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/governance.rs b/src/governance.rs index 0a237f5..1b14a09 100644 --- a/src/governance.rs +++ b/src/governance.rs @@ -1,5 +1,5 @@ use crate::staking::StakingConfig; -use crate::system::SystemConfig; +//use crate::system::SystemConfig; use std::collections::HashMap; pub trait GovernanceConfig: StakingConfig {} @@ -11,7 +11,7 @@ pub struct Proposal { status: ProposalStatus, } -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub enum ProposalStatus { Active, Approved, @@ -69,6 +69,9 @@ impl GovernancePallet { 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}; From dfe3a69df54ea84c930655b08ac687b37f0b71ae Mon Sep 17 00:00:00 2001 From: Syamsudin Date: Thu, 13 Mar 2025 14:20:09 +0700 Subject: [PATCH 3/3] make litle fix --- src/governance.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/governance.rs b/src/governance.rs index 1b14a09..bb3876d 100644 --- a/src/governance.rs +++ b/src/governance.rs @@ -1,8 +1,8 @@ use crate::staking::StakingConfig; -//use crate::system::SystemConfig; +use crate::system::SystemConfig; use std::collections::HashMap; -pub trait GovernanceConfig: StakingConfig {} +pub trait GovernanceConfig: StakingConfig + SystemConfig {} pub struct Proposal { description: String,