Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Challenges 1 from shogun #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
target
/target
25 changes: 25 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Basic
edition = "2021"
hard_tabs = true
max_width = 100
use_small_heuristics = "Max"
# Imports
imports_granularity = "Crate"
reorder_imports = true
# Consistency
newline_style = "Unix"
# Misc
chain_width = 80
spaces_around_ranges = false
binop_separator = "Back"
reorder_impl_items = false
match_arm_leading_pipes = "Preserve"
match_arm_blocks = false
match_block_trailing_comma = true
trailing_comma = "Vertical"
trailing_semicolon = false
use_field_init_shorthand = true
# Format comments
comment_width = 100
wrap_comments = true

119 changes: 64 additions & 55 deletions src/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,88 @@ use num::traits::{CheckedAdd, CheckedSub, Zero};
use crate::system::SystemConfig;

pub trait BalanceConfig: SystemConfig {
// Định nghĩa kiểu dữ liệu Balance có khả năng thực hiện phép tính +/-
type Balance: Zero + CheckedSub + CheckedAdd + Copy;
// Định nghĩa kiểu dữ liệu Balance có khả năng thực hiện phép tính +/-
type Balance: Zero + CheckedSub + CheckedAdd + Copy;
}

pub struct BalancePallet<T: BalanceConfig> {
pub balances: HashMap<T::AccountId, T::Balance>,
pub balances: HashMap<T::AccountId, T::Balance>,
}

impl<T: BalanceConfig> BalancePallet<T> {
pub fn new() -> Self {
todo!()
}

// set coins cho 1 account
pub fn set_balance(&mut self, who: T::AccountId, amount: T::Balance) {
todo!()
}

// transfer coins
pub fn transfer(
&mut self,
from: T::AccountId,
to: T::AccountId,
amount: T::Balance,
) -> Result<(), &'static str> {
todo!()
}

pub fn get_balance(&self, who: T::AccountId) -> T::Balance {
todo!()
}
pub fn new() -> Self {
Self { balances: HashMap::new() }
}

// set coins cho 1 account
pub fn set_balance(&mut self, who: T::AccountId, amount: T::Balance) {
self.balances.insert(who.clone(), amount);
}

// transfer coins
pub fn transfer(
&mut self,
from: T::AccountId,
to: T::AccountId,
amount: T::Balance,
) -> Result<(), &'static str> {
let caller_balance = self.get_balance(from.clone());
let to_balance = self.get_balance(to.clone());

let new_caller_balance = caller_balance.checked_sub(&amount).ok_or("Not enough funds.")?;
let new_to_balance = to_balance.checked_add(&amount).ok_or("Overflow")?;

self.balances.insert(from, new_caller_balance);
self.balances.insert(to, new_to_balance);

Ok(())
}

pub fn get_balance(&self, who: T::AccountId) -> T::Balance {
*self.balances.get(&who).unwrap_or(&T::Balance::zero())
}
}

#[cfg(test)]
mod tests {
use crate::Runtime;
use crate::Runtime;

use super::*;
#[test]
fn test_set_balance_should_work() {
let alice = 1u64;
let mut balance = BalancePallet::<Runtime>::new();
//set balance
balance.set_balance(alice, 1000);
use super::*;
#[test]
fn test_set_balance_should_work() {
let alice = 1u64;
let mut balance = BalancePallet::<Runtime>::new();
//set balance
balance.set_balance(alice, 1000);

// check balance
assert_eq!(balance.get_balance(alice), 1000u64);
}
// check balance
assert_eq!(balance.get_balance(alice), 1000u64);
}

#[test]
fn test_set_transfer_should_work() {
let alice = 1u64;
let bob = 2u64;
let mut balance = BalancePallet::<Runtime>::new();
//set balance
balance.set_balance(alice, 1000);
#[test]
fn test_set_transfer_should_work() {
let alice = 1u64;
let bob = 2u64;
let mut balance = BalancePallet::<Runtime>::new();
//set balance
balance.set_balance(alice, 1000);

// check balance alice before transfer
assert_eq!(balance.get_balance(alice), 1000u64);
// check balance alice before transfer
assert_eq!(balance.get_balance(alice), 1000u64);

// check balance bob before transfer
assert_eq!(balance.get_balance(bob), 0u64);
// check balance bob before transfer
assert_eq!(balance.get_balance(bob), 0u64);

// Thực hiện transfer
// Thực hiện transfer

let result = balance.transfer(alice, bob, 100);
let result = balance.transfer(alice, bob, 100);

assert!(result.is_ok());
assert!(result.is_ok());

// check balance alice after transfer
assert_eq!(balance.get_balance(alice), 900u64);
// check balance alice after transfer
assert_eq!(balance.get_balance(alice), 900u64);

// check balance bob after transfer
assert_eq!(balance.get_balance(bob), 100u64);
}
// check balance bob after transfer
assert_eq!(balance.get_balance(bob), 100u64);
}
}
19 changes: 7 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ pub use system::SystemConfig;
pub use vote::VoteConfig;

pub mod balances;
pub mod vote;
pub mod system;


pub mod vote;

pub struct Runtime;

// implement kiểu dữ liệu cụ thể System cho runtime
// implement kiểu dữ liệu cụ thể System cho runtime
impl SystemConfig for Runtime {
type AccountId = u64;
type AccountId = u64;
}


// implement kiểu dữ liệu cụ thể Balance cho runtime
// implement kiểu dữ liệu cụ thể Balance cho runtime
impl BalanceConfig for Runtime {
type Balance = u64;
type Balance = u64;
}

// implement kiểu dữ liệu cụ thể Vote cho runtime
impl VoteConfig for Runtime {

}
// implement kiểu dữ liệu cụ thể Vote cho runtime
impl VoteConfig for Runtime {}
11 changes: 2 additions & 9 deletions src/system.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@


use std::hash::Hash;



pub trait SystemConfig {
// Định nghĩa AccountId
type AccountId: Eq + Hash + Clone;

// Định nghĩa AccountId
type AccountId: Eq + Hash + Clone;
}


74 changes: 39 additions & 35 deletions src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,62 @@ use crate::system::SystemConfig;
pub trait VoteConfig: SystemConfig {}

pub struct VotePallet<T: VoteConfig> {
pub votes: HashMap<(T::AccountId, T::AccountId), bool>,
pub votes: HashMap<(T::AccountId, T::AccountId), bool>,
}

impl<T: VoteConfig> VotePallet<T> {
pub fn new() -> Self {
todo!()
}
pub fn new() -> Self {
Self { votes: HashMap::new() }
}

// Vote Yes
// Vote Yes

pub fn vote(&mut self, who: T::AccountId, voter: T::AccountId) -> Result<(), &'static str> {
todo!()
}
pub fn vote(&mut self, who: T::AccountId, voter: T::AccountId) -> Result<(), &'static str> {
self.votes.insert((who.clone(), voter.clone()), true);

// Vote No
Ok(())
}

pub fn revoke(&mut self, who: T::AccountId, voter: T::AccountId) -> Result<(), &'static str> {
todo!()
}
// Vote No

pub fn get_vote(&self, who: T::AccountId, voter: T::AccountId) -> bool {
todo!()
}
pub fn revoke(&mut self, who: T::AccountId, voter: T::AccountId) -> Result<(), &'static str> {
self.votes.insert((who.clone(), voter.clone()), false);

Ok(())
}

pub fn get_vote(&self, who: T::AccountId, voter: T::AccountId) -> bool {
*self.votes.get(&(who, voter)).unwrap_or(&false)
}
}

#[cfg(test)]
mod tests {
use crate::Runtime;
use crate::Runtime;

use super::*;
#[test]
fn test_vote_should_work() {
let alice = 1u64;
let bob = 2u64;
let mut vote = VotePallet::<Runtime>::new();
use super::*;
#[test]
fn test_vote_should_work() {
let alice = 1u64;
let bob = 2u64;
let mut vote = VotePallet::<Runtime>::new();

// alice vote cho bob
// alice vote cho bob

let result = vote.vote(alice, bob);
assert!(result.is_ok());
let result = vote.vote(alice, bob);
assert!(result.is_ok());

// kiểm tra vote
let yes = vote.get_vote(alice, bob);
assert_eq!(yes, true);
// kiểm tra vote
let yes = vote.get_vote(alice, bob);
assert_eq!(yes, true);

// alice revoke bob
// alice revoke bob

let result = vote.revoke(alice, bob);
assert!(result.is_ok());
let result = vote.revoke(alice, bob);
assert!(result.is_ok());

// kiểm tra vote
let no = vote.get_vote(alice, bob);
assert_eq!(no, false);
}
// kiểm tra vote
let no = vote.get_vote(alice, bob);
assert_eq!(no, false);
}
}