From d7673247b6348a0e365281c3631da8c62bf5916e Mon Sep 17 00:00:00 2001 From: arkaadiana Date: Thu, 13 Mar 2025 17:05:49 +0800 Subject: [PATCH 1/4] Register for OpenGuild FRAME Challenges --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c0ca209..4b87967 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,9 @@ git clone https://github.com/openguild-labs/frame-challenges.git Go to **Participant Registration** section and register to be the workshop participants. Add the below to the list, replace any placeholder with your personal information. -``` -| 🦄 | Name | Github username | Your current occupation | -``` +| Emoji | Name | Github Username | Current Occupation | +| ----- | --------------- | ----------------------------------------------------- | ------------------------ | +| 🐺 | Putu Arka Adiana | [arkaadiana](https://github.com/arkaadiana) | Information Technology Student | - Step 5: `Commit` your code and push to the forked Github repository From d87e5b456fe40ce0d84ebc70fcbc53dd77ab8e1d Mon Sep 17 00:00:00 2001 From: arkaadiana Date: Thu, 13 Mar 2025 17:07:10 +0800 Subject: [PATCH 2/4] feat(governance): Complete implementation of GovernancePallet --- src/governance.rs | 50 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/governance.rs b/src/governance.rs index f71f10b..75e01e0 100644 --- a/src/governance.rs +++ b/src/governance.rs @@ -1,10 +1,12 @@ use crate::staking::StakingConfig; +#[allow(unused_imports)] use crate::system::SystemConfig; use std::collections::HashMap; pub trait GovernanceConfig: StakingConfig {} pub struct Proposal { + #[allow(dead_code)] description: String, yes_votes: u32, no_votes: u32, @@ -26,16 +28,31 @@ 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!() + let proposal_id = self.next_proposal_id; + self.proposals.insert( + proposal_id, + Proposal { + description, + yes_votes: 0, + no_votes: 0, + status: ProposalStatus::Active, + }, + ); + self.next_proposal_id += 1; + Ok(proposal_id) } // Vote on a proposal (true = yes, false = no) @@ -45,17 +62,36 @@ impl GovernancePallet { proposal_id: u32, vote_type: bool, ) -> Result<(), &'static str> { - todo!() + if let Some(proposal) = self.proposals.get_mut(&proposal_id) { + self.votes.insert((voter.clone(), proposal_id), vote_type); // Clone voter + if vote_type { + proposal.yes_votes += 1; + } else { + proposal.no_votes += 1; + } + Ok(()) + } else { + Err("Proposal not found") + } } // 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!() + if let Some(proposal) = self.proposals.get_mut(&proposal_id) { + if proposal.yes_votes > proposal.no_votes { + proposal.status = ProposalStatus::Approved; + } else { + proposal.status = ProposalStatus::Rejected; + } + Ok(proposal.status.clone()) + } else { + Err("Proposal not found") + } } } @@ -98,4 +134,4 @@ mod tests { ProposalStatus::Approved )); } -} +} \ No newline at end of file From fbfcdc1239863f374562887d5b7714bbd2891f4b Mon Sep 17 00:00:00 2001 From: arkaadiana Date: Thu, 13 Mar 2025 17:07:45 +0800 Subject: [PATCH 3/4] feat(staking): Complete implementation of StakingPallet --- src/staking.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/staking.rs b/src/staking.rs index 7bd646e..a4c664f 100644 --- a/src/staking.rs +++ b/src/staking.rs @@ -16,32 +16,51 @@ 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.free_balances.get(&who).unwrap_or(&T::Balance::zero()); + if let Some(new_balance) = free_balance.checked_sub(&amount) { + self.free_balances.insert(who.clone(), new_balance); // Clone who + let staked_balance = *self.staked_balances.get(&who).unwrap_or(&T::Balance::zero()); + self.staked_balances.insert(who, staked_balance.checked_add(&amount).unwrap()); + Ok(()) + } else { + Err("Insufficient balance") + } } // 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.staked_balances.get(&who).unwrap_or(&T::Balance::zero()); + if let Some(new_staked) = staked_balance.checked_sub(&amount) { + self.staked_balances.insert(who.clone(), new_staked); // Clone who + let free_balance = *self.free_balances.get(&who).unwrap_or(&T::Balance::zero()); + self.free_balances.insert(who, free_balance.checked_add(&amount).unwrap()); + Ok(()) + } else { + Err("Insufficient staked balance") + } } // 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()) } } @@ -99,4 +118,4 @@ mod tests { let result = staking.unstake(bob, 400); assert!(result.is_err()); } -} +} \ No newline at end of file From fdbf5ef13edc2cb89345b4dce676e2cff7769d39 Mon Sep 17 00:00:00 2001 From: arkaadiana Date: Thu, 13 Mar 2025 18:36:54 +0800 Subject: [PATCH 4/4] docs(README): Update participant registration entry --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4b87967..c02f242 100644 --- a/README.md +++ b/README.md @@ -27,9 +27,9 @@ git clone https://github.com/openguild-labs/frame-challenges.git Go to **Participant Registration** section and register to be the workshop participants. Add the below to the list, replace any placeholder with your personal information. -| Emoji | Name | Github Username | Current Occupation | -| ----- | --------------- | ----------------------------------------------------- | ------------------------ | -| 🐺 | Putu Arka Adiana | [arkaadiana](https://github.com/arkaadiana) | Information Technology Student | +``` +| 🦄 | Name | Github username | Your current occupation | +``` - Step 5: `Commit` your code and push to the forked Github repository @@ -94,3 +94,11 @@ 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 + +Add your information to the below list to officially participate in the workshop challenge (This is the first mission of the whole workshop) + +| Emoji | Name | Github Username | Occupations | +| ----- | --------------- | ----------------------------------------------------- | ------------------------ | +| 🐺 | Putu Arka Adiana | [arkaadiana](https://github.com/arkaadiana) | Information Technology Student | \ No newline at end of file