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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| ----- | --------------- | ----------------------------------------------------- | ------------------------ |
| 🦞 | Akhmad Syarifudin | [gethook]([text](https://github.com/gethook)) | Web Developer |
68 changes: 63 additions & 5 deletions src/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ pub struct GovernancePallet<T: GovernanceConfig> {

impl<T: GovernanceConfig> GovernancePallet<T> {
pub fn new() -> Self {
todo!()
// todo!()
Self {
proposals: HashMap::new(),
votes: HashMap::new(),
next_proposal_id: 0,
}
}

// Create a new proposal
Expand All @@ -35,7 +40,16 @@ impl<T: GovernanceConfig> GovernancePallet<T> {
creator: T::AccountId,
description: String,
) -> Result<u32, &'static str> {
todo!()
// todo!()
self.next_proposal_id += 1;
let proposal = Proposal {
description,
yes_votes: 0,
no_votes: 0,
status: ProposalStatus::Active,
};
self.proposals.insert(self.next_proposal_id, proposal);
Ok(self.next_proposal_id)
}

// Vote on a proposal (true = yes, false = no)
Expand All @@ -45,17 +59,61 @@ impl<T: GovernanceConfig> GovernancePallet<T> {
proposal_id: u32,
vote_type: bool,
) -> Result<(), &'static str> {
todo!()
// todo!()
let voted = self.votes.contains_key(&(voter.clone(), proposal_id));
if voted {
return Err("Already vote");
}
self.votes.insert((voter, proposal_id), vote_type);
let proposal = self.get_proposal(proposal_id).ok_or("Proposal not found")?;
match proposal.status {
ProposalStatus::Approved => return Err("Proposal finalized"),
ProposalStatus::Rejected => return Err("Proposal finalized"),
ProposalStatus::Active => (),
}

let mut yes_votes = proposal.yes_votes;
let mut no_votes = proposal.no_votes;

if vote_type {
yes_votes += 1;
} else {
no_votes += 1;
}

self.proposals.insert(proposal_id, Proposal {
description: proposal.description.clone(),
yes_votes,
no_votes,
status: ProposalStatus::Active,
});

Ok(())
}

// Get proposal details
pub fn get_proposal(&self, proposal_id: u32) -> Option<&Proposal> {
todo!()
// 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!()
// todo!()
let proposal = self.get_proposal(proposal_id).ok_or("Proposal not found")?;
let mut status = ProposalStatus::Rejected;
if proposal.yes_votes > proposal.no_votes {
status = ProposalStatus::Approved;
}

self.proposals.insert(proposal_id, Proposal {
description: proposal.description.clone(),
yes_votes: proposal.yes_votes,
no_votes: proposal.no_votes,
status: status.clone(),
});

Ok(status)
}
}

Expand Down
45 changes: 39 additions & 6 deletions src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,65 @@ 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!()
// 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!()
// todo!()
let curr_free_balances = self.get_free_balance(who.clone());
let curr_staked_balances = self.get_staked_balance(who.clone());

let new_free_balances = curr_free_balances
.checked_sub(&amount)
.ok_or("No enough funds")?;
let new_staked_balances = curr_staked_balances
.checked_add(&amount)
.ok_or("Overflow")?;
self.free_balances.insert(who.clone(), new_free_balances);
self.staked_balances.insert(who.clone(), new_staked_balances);

Ok(())
}

// Unstake tokens (move from staked to free)
pub fn unstake(&mut self, who: T::AccountId, amount: T::Balance) -> Result<(), &'static str> {
todo!()
// todo!()
let curr_free_balances = self.get_free_balance(who.clone());
let curr_staked_balances = self.get_staked_balance(who.clone());

let new_free_balances = curr_free_balances
.checked_add(&amount)
.ok_or("Overflow")?;
let new_staked_balances = curr_staked_balances
.checked_sub(&amount)
.ok_or("No enough funds")?;
self.free_balances.insert(who.clone(), new_free_balances);
self.staked_balances.insert(who.clone(), new_staked_balances);

Ok(())

}

// Get free balance for an account
pub fn get_free_balance(&self, who: T::AccountId) -> T::Balance {
todo!()
// 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!()
// todo!()
self.staked_balances.get(&who).copied().unwrap_or(T::Balance::zero())
}
}

Expand Down