Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ 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

| Emoji | Name | Github Username | Current Occupation |
|-------|----------------|------------------------------------------------------|----------------------------------|
| 🤖 | Putu Tio Lovan | [TioLovan07](https://github.com/TioLovan07) | Information Technology Student |
53 changes: 46 additions & 7 deletions src/governance.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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,
status: ProposalStatus,
}

#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub enum ProposalStatus {
Active,
Approved,
Expand All @@ -26,16 +28,31 @@ pub struct GovernancePallet<T: GovernanceConfig> {

impl<T: GovernanceConfig> GovernancePallet<T> {
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<u32, &'static str> {
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)
Expand All @@ -45,17 +62,39 @@ impl<T: GovernanceConfig> GovernancePallet<T> {
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<ProposalStatus, &'static str> {
todo!()
if let Some(proposal) = self.proposals.get_mut(&proposal_id) {
if proposal.status != ProposalStatus::Active {
return Err("Proposal is already finalized");
}
proposal.status = if proposal.yes_votes > proposal.no_votes {
ProposalStatus::Approved
} else {
ProposalStatus::Rejected
};
Ok(proposal.status.clone())
} else {
Err("Proposal not found")
}
}
}

Expand Down
33 changes: 27 additions & 6 deletions src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,53 @@ pub struct StakingPallet<T: StakingConfig> {

impl<T: StakingConfig> StakingPallet<T> {
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).copied().unwrap_or(T::Balance::zero());
if let Some(new_free_balance) = free_balance.checked_sub(&amount) {
let staked_balance = self.staked_balances.get(&who).copied().unwrap_or(T::Balance::zero());
if let Some(new_staked_balance) = staked_balance.checked_add(&amount) {
self.free_balances.insert(who.clone(), new_free_balance);
self.staked_balances.insert(who, new_staked_balance);
return Ok(())
}
}
Err("Insufficient free 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).copied().unwrap_or(T::Balance::zero());
if let Some(new_staked_balance) = staked_balance.checked_sub(&amount) {
let free_balance = self.free_balances.get(&who).copied().unwrap_or(T::Balance::zero());
if let Some(new_free_balance) = free_balance.checked_add(&amount) {
self.staked_balances.insert(who.clone(), new_staked_balance);
self.free_balances.insert(who, new_free_balance);
return Ok(())
}
}
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).copied().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).copied().unwrap_or(T::Balance::zero())
}
}

Expand Down