From 60e1463b1526b7af1d6649edef5c4252acef1d5d Mon Sep 17 00:00:00 2001 From: Shogun Date: Sat, 14 Sep 2024 00:41:01 +0700 Subject: [PATCH] Init project --- .gitignore | 2 +- rustfmt.toml | 25 ++++++++++ src/balances.rs | 119 ++++++++++++++++++++++++++---------------------- src/lib.rs | 19 +++----- src/system.rs | 11 +---- src/vote.rs | 74 ++++++++++++++++-------------- 6 files changed, 138 insertions(+), 112 deletions(-) create mode 100644 rustfmt.toml diff --git a/.gitignore b/.gitignore index eb5a316..ea8c4bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -target +/target diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..b247002 --- /dev/null +++ b/rustfmt.toml @@ -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 + diff --git a/src/balances.rs b/src/balances.rs index 9873192..b3c825a 100644 --- a/src/balances.rs +++ b/src/balances.rs @@ -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 { - pub balances: HashMap, + pub balances: HashMap, } impl BalancePallet { - 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::::new(); - //set balance - balance.set_balance(alice, 1000); + use super::*; + #[test] + fn test_set_balance_should_work() { + let alice = 1u64; + let mut balance = BalancePallet::::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::::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::::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); + } } diff --git a/src/lib.rs b/src/lib.rs index ec05640..6949430 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 {} diff --git a/src/system.rs b/src/system.rs index c5dbb0a..7afb130 100644 --- a/src/system.rs +++ b/src/system.rs @@ -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; } - - diff --git a/src/vote.rs b/src/vote.rs index ab15832..dd725a7 100644 --- a/src/vote.rs +++ b/src/vote.rs @@ -5,58 +5,62 @@ use crate::system::SystemConfig; pub trait VoteConfig: SystemConfig {} pub struct VotePallet { - pub votes: HashMap<(T::AccountId, T::AccountId), bool>, + pub votes: HashMap<(T::AccountId, T::AccountId), bool>, } impl VotePallet { - 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::::new(); + use super::*; + #[test] + fn test_vote_should_work() { + let alice = 1u64; + let bob = 2u64; + let mut vote = VotePallet::::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); + } }