diff --git a/README.md b/README.md index c0ca209..dedfdfb 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Go to **Participant Registration** section and register to be the workshop parti ``` | 🦄 | Name | Github username | Your current occupation | +| 1 | Made Pranajaya Dibyacita | mdprana | Computer Science Student | ``` - Step 5: `Commit` your code and push to the forked Github repository @@ -94,3 +95,8 @@ OpenGuild is a builder-driven community centered around Polkadot. OpenGuild is b - **Website:** [OpenGuild Website](https://openguild.wtf/) - **Github:** [OpenGuild Labs](https://github.com/openguild-labs) - **Discord**: [Openguild Discord Channel](https://discord.gg/bcjMzxqtD7) + +## Participant Registration + +| 🦄 | Name | Github username | Your current occupation | +| 1 | Made Pranajaya Dibyacita | [mdprana](https://github.com/mdprana) | Computer Science Student | \ No newline at end of file diff --git a/src/governance.rs b/src/governance.rs index f71f10b..ace0e6a 100644 --- a/src/governance.rs +++ b/src/governance.rs @@ -2,16 +2,17 @@ use crate::staking::StakingConfig; use crate::system::SystemConfig; use std::collections::HashMap; -pub trait GovernanceConfig: StakingConfig {} +pub trait GovernanceConfig: StakingConfig + SystemConfig {} -pub struct Proposal { +pub struct Proposal { description: String, yes_votes: u32, no_votes: u32, status: ProposalStatus, + creator: T::AccountId, // Store the creator of the proposal } -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub enum ProposalStatus { Active, Approved, @@ -19,14 +20,18 @@ pub enum ProposalStatus { } pub struct GovernancePallet { - pub proposals: HashMap, + pub proposals: HashMap>, pub votes: HashMap<(T::AccountId, u32), bool>, // (voter, proposal_id) -> vote_type next_proposal_id: u32, } impl GovernancePallet { pub fn new() -> Self { - todo!() + Self { + proposals: HashMap::new(), + votes: HashMap::new(), + next_proposal_id: 0, + } } // Create a new proposal @@ -35,7 +40,20 @@ impl GovernancePallet { creator: T::AccountId, description: String, ) -> Result { - todo!() + let current_id = self.next_proposal_id; + + let new_proposal = Proposal { + description, + yes_votes: 0, + no_votes: 0, + status: ProposalStatus::Active, + creator, + }; + + self.proposals.insert(current_id, new_proposal); + self.next_proposal_id += 1; + + Ok(current_id) } // Vote on a proposal (true = yes, false = no) @@ -45,17 +63,69 @@ impl GovernancePallet { proposal_id: u32, vote_type: bool, ) -> Result<(), &'static str> { - todo!() + let vote_key = (voter.clone(), proposal_id); + + match self.proposals.get_mut(&proposal_id) { + Some(proposal) => { + if proposal.status != ProposalStatus::Active { + return Err("Cannot vote on inactive proposal"); + } + + if self.votes.contains_key(&vote_key) { + return Err("Voter has already cast a vote for this proposal"); + } + + self.votes.insert(vote_key, vote_type); + + match vote_type { + true => proposal.yes_votes += 1, // Yes vote + false => proposal.no_votes += 1, // No vote + } + + Ok(()) + }, + None => Err("No proposal found with the given ID"), + } } // Get proposal details - pub fn get_proposal(&self, proposal_id: u32) -> Option<&Proposal> { - todo!() + pub fn get_proposal(&self, proposal_id: u32) -> Option<&Proposal> { + self.proposals.get(&proposal_id) } // Finalize a proposal (changes status based on votes) pub fn finalize_proposal(&mut self, proposal_id: u32) -> Result { - todo!() + match self.proposals.get_mut(&proposal_id) { + Some(proposal) => { + if proposal.status != ProposalStatus::Active { + return Err("Cannot finalize an already finalized proposal"); + } + + let new_status = if proposal.yes_votes > proposal.no_votes { + ProposalStatus::Approved + } else { + ProposalStatus::Rejected + }; + + proposal.status = new_status.clone(); + + Ok(new_status) + }, + None => Err("No proposal found with the given ID"), + } + } + + // Get full proposal details including description and creator + pub fn get_proposal_details( + &self, + proposal_id: u32, + ) -> Result<(String, T::AccountId), &'static str> { + match self.proposals.get(&proposal_id) { + Some(proposal) => { + Ok((proposal.description.clone(), proposal.creator.clone())) + }, + None => Err("No proposal found with the given ID"), + } } } diff --git a/src/staking.rs b/src/staking.rs index 7bd646e..1df8037 100644 --- a/src/staking.rs +++ b/src/staking.rs @@ -16,32 +16,65 @@ 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, 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()); + + if let Some(new_free_balance) = free_balance.checked_sub(&amount) { + self.free_balances.insert(who.clone(), new_free_balance); + + let staked_balance = self.get_staked_balance(who.clone()); + if let Some(new_staked_balance) = staked_balance.checked_add(&amount) { + self.staked_balances.insert(who, new_staked_balance); + Ok(()) + } else { + self.free_balances.insert(who, free_balance); + Err("Staked balance would overflow") + } + } else { + Err("Not enough free balance to stake") + } } // 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()); + + if let Some(new_staked_balance) = staked_balance.checked_sub(&amount) { + self.staked_balances.insert(who.clone(), new_staked_balance); + + let free_balance = self.get_free_balance(who.clone()); + if let Some(new_free_balance) = free_balance.checked_add(&amount) { + self.free_balances.insert(who, new_free_balance); + Ok(()) + } else { + self.staked_balances.insert(who, staked_balance); + Err("Free balance would overflow") + } + } else { + Err("Not enough staked balance to unstake") + } } // 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()) } }