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
75 changes: 67 additions & 8 deletions src/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -11,7 +11,7 @@ pub struct Proposal {
status: ProposalStatus,
}

#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub enum ProposalStatus {
Active,
Approved,
Expand All @@ -26,16 +26,29 @@ 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!()
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)
Expand All @@ -45,17 +58,63 @@ impl<T: GovernanceConfig> GovernancePallet<T> {
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<ProposalStatus, &'static str> {
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)
}
}

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.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())
}
}

Expand Down