diff --git a/pallets/project-tips/src/extras.rs b/pallets/project-tips/src/extras.rs index 8edd6a3..46ed49c 100644 --- a/pallets/project-tips/src/extras.rs +++ b/pallets/project-tips/src/extras.rs @@ -4,6 +4,7 @@ impl Project { pub fn new( project_id: ProjectId, department_id: DepartmentId, + content: Content, tipping_name: TippingName, funding_needed: BalanceOf, project_leader: T::AccountId, @@ -12,6 +13,7 @@ impl Project { created: new_who_and_when::(project_leader.clone()), project_id, department_id, + content, tipping_name, funding_needed, project_leader, @@ -28,7 +30,7 @@ impl Pallet { project_id: ProjectId, user: T::AccountId, ) -> DispatchResult { - let project_option: Option> = Projects::get(project_id); + let project_option: Option> = >::get(project_id); match project_option { Some(project) => { let project_leader = project.project_leader; diff --git a/pallets/project-tips/src/lib.rs b/pallets/project-tips/src/lib.rs index 03dee59..650fb01 100644 --- a/pallets/project-tips/src/lib.rs +++ b/pallets/project-tips/src/lib.rs @@ -171,46 +171,64 @@ pub mod pallet { pub fn create_project( origin: OriginFor, department_id: DepartmentId, + content: Content, tipping_name: TippingName, funding_needed: BalanceOf, ) -> DispatchResult { let who = ensure_signed(origin)?; + + let new_project_id = Self::next_project_id(); let tipping_value = Self::value_of_tipping_name(tipping_name); let max_tipping_value = tipping_value.max_tipping_value; - let stake_required = tipping_value.stake_required; - let new_project_id = Self::next_project_id(); + ensure!( + funding_needed <= max_tipping_value, + Error::::FundingMoreThanTippingValue + ); let new_project: Project = Project::new( new_project_id, department_id, + content, tipping_name, funding_needed, who.clone(), ); - ensure!(funding_needed <= max_tipping_value, Error::::FundingMoreThanTippingValue); - // Check user has done kyc - let _ = ::Currency::withdraw( - &who, - stake_required, - WithdrawReasons::TRANSFER, - ExistenceRequirement::AllowDeath, - )?; + Projects::insert(new_project_id, new_project); NextProjectId::::mutate(|n| { *n += 1; }); Self::deposit_event(Event::ProjectCreated { account: who, project_id: new_project_id }); + Ok(()) } + + // Check update and discussion time over, only project creator can apply staking period #[pallet::call_index(1)] #[pallet::weight(0)] - pub fn apply_staking_period(origin: OriginFor, project_id: ProjectId) -> DispatchResult { + pub fn apply_staking(origin: OriginFor, project_id: ProjectId) -> DispatchResult { let who = ensure_signed(origin)?; - Self::ensure_user_is_project_creator_and_project_exists(project_id, who)?; + Self::ensure_user_is_project_creator_and_project_exists(project_id, who.clone())?; Self::ensure_staking_period_set_once_project_id(project_id)?; + match >::get(project_id) { + Some(project) => { + let tipping_name = project.tipping_name; + let tipping_value = Self::value_of_tipping_name(tipping_name); + let stake_required = tipping_value.stake_required; + + let _ = ::Currency::withdraw( + &who, + stake_required, + WithdrawReasons::TRANSFER, + ExistenceRequirement::AllowDeath, + )?; + }, + + None => Err(Error::::ProjectDontExists)?, + } let now = >::block_number(); diff --git a/pallets/project-tips/src/tests.rs b/pallets/project-tips/src/tests.rs index ec44199..2d83913 100644 --- a/pallets/project-tips/src/tests.rs +++ b/pallets/project-tips/src/tests.rs @@ -1,11 +1,12 @@ use crate::types::TippingName; use crate::{mock::*, Error, Event}; use frame_support::{assert_noop, assert_ok}; +use pallet_support::Content; use schelling_game_shared::types::Period; use sortition_sum_game::types::SumTreeName; #[test] -fn check_create_project_function() { +fn check_balance_on_staking() { new_test_ext().execute_with(|| { // Go past genesis block so events get deposited System::set_block_number(1); @@ -14,17 +15,53 @@ fn check_create_project_function() { let max_tipping_value = tipping_value.max_tipping_value; let stake_required = tipping_value.stake_required; let funding_needed = max_tipping_value - 100; - let balance = Balances::free_balance(1); + let content: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); assert_ok!(ProjectTips::create_project( RuntimeOrigin::signed(1), 2, + content.clone(), tipping_name, funding_needed )); + System::assert_last_event(Event::ProjectCreated { account: 1, project_id: 1 }.into()); + + let balance = Balances::free_balance(1); + + assert_ok!(ProjectTips::apply_staking(RuntimeOrigin::signed(1), 1)); + let after_balance = Balances::free_balance(1); assert_eq!(after_balance, balance - stake_required); + }); +} + +#[test] +fn check_create_project_function() { + new_test_ext().execute_with(|| { + System::set_block_number(1); + let tipping_name = TippingName::SmallTipper; + let tipping_value = ProjectTips::value_of_tipping_name(tipping_name); + let max_tipping_value = tipping_value.max_tipping_value; + let funding_needed = max_tipping_value - 100; + let content: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); + assert_ok!(ProjectTips::create_project( + RuntimeOrigin::signed(1), + 2, + content.clone(), + tipping_name, + funding_needed + )); + + System::assert_last_event(Event::ProjectCreated { account: 1, project_id: 1 }.into()); @@ -35,7 +72,7 @@ fn check_create_project_function() { let funding_needed = max_tipping_value + 100; assert_noop!( - ProjectTips::create_project(RuntimeOrigin::signed(1), 2, tipping_name, funding_needed), + ProjectTips::create_project(RuntimeOrigin::signed(1), 2, content, tipping_name, funding_needed), Error::::FundingMoreThanTippingValue ); }); @@ -46,7 +83,7 @@ fn check_apply_staking_period_function() { new_test_ext().execute_with(|| { System::set_block_number(1); assert_noop!( - ProjectTips::apply_staking_period(RuntimeOrigin::signed(1), 2), + ProjectTips::apply_staking(RuntimeOrigin::signed(1), 2), Error::::ProjectDontExists ); @@ -54,26 +91,32 @@ fn check_apply_staking_period_function() { let tipping_value = ProjectTips::value_of_tipping_name(tipping_name); let max_tipping_value = tipping_value.max_tipping_value; let funding_needed = max_tipping_value - 100; + let content: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); assert_ok!(ProjectTips::create_project( RuntimeOrigin::signed(1), 2, + content, tipping_name, funding_needed )); assert_noop!( - ProjectTips::apply_staking_period(RuntimeOrigin::signed(3), 1), + ProjectTips::apply_staking(RuntimeOrigin::signed(3), 1), Error::::ProjectCreatorDontMatch ); - assert_ok!(ProjectTips::apply_staking_period(RuntimeOrigin::signed(1), 1)); + assert_ok!(ProjectTips::apply_staking(RuntimeOrigin::signed(1), 1)); System::assert_last_event( Event::StakinPeriodStarted { project_id: 1, block_number: 1 }.into(), ); System::set_block_number(5); assert_noop!( - ProjectTips::apply_staking_period(RuntimeOrigin::signed(1), 1), + ProjectTips::apply_staking(RuntimeOrigin::signed(1), 1), Error::::ProjectIdStakingPeriodAlreadySet ); }); @@ -88,20 +131,27 @@ fn schelling_game_test() { let max_tipping_value = tipping_value.max_tipping_value; let stake_required = tipping_value.stake_required; let funding_needed = max_tipping_value - 100; - let balance = Balances::free_balance(1); + let content: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); assert_ok!(ProjectTips::create_project( RuntimeOrigin::signed(1), 2, + content, tipping_name, funding_needed )); + let balance = Balances::free_balance(1); + + assert_ok!(ProjectTips::apply_staking(RuntimeOrigin::signed(1), 1)); + let after_balance = Balances::free_balance(1); assert_eq!(after_balance, balance - stake_required); - assert_ok!(ProjectTips::apply_staking_period(RuntimeOrigin::signed(1), 1)); - let phase_data = ProjectTips::get_phase_data(); let balance = Balances::free_balance(29); diff --git a/pallets/project-tips/src/types.rs b/pallets/project-tips/src/types.rs index f3a36e2..6e15b5c 100644 --- a/pallets/project-tips/src/types.rs +++ b/pallets/project-tips/src/types.rs @@ -26,6 +26,7 @@ pub struct Project { pub created: WhoAndWhenOf, pub project_id: ProjectId, pub department_id: DepartmentId, + pub content: Content, pub tipping_name: TippingName, pub funding_needed: BalanceOf, pub project_leader: T::AccountId,