From bf57cf475031b7690d1fa23cbbabba474bad915d Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Tue, 13 Aug 2024 20:36:05 +0530 Subject: [PATCH] department funding tests --- custom-pallets/department-funding/src/lib.rs | 36 +++++++++--- .../department-funding/src/tests.rs | 56 +++++++++++++++++++ custom-pallets/departments/src/extras.rs | 10 +++- custom-pallets/departments/src/lib.rs | 2 + traits/trait-departments/src/lib.rs | 5 ++ 5 files changed, 99 insertions(+), 10 deletions(-) diff --git a/custom-pallets/department-funding/src/lib.rs b/custom-pallets/department-funding/src/lib.rs index 70a3fb0..f12be23 100644 --- a/custom-pallets/department-funding/src/lib.rs +++ b/custom-pallets/department-funding/src/lib.rs @@ -92,7 +92,10 @@ pub mod pallet { PhaseData = PhaseData, JurorGameResult = JurorGameResult, >; - type DepartmentsSource: DepartmentsLink; + type DepartmentsSource: DepartmentsLink< + DepartmentId = DepartmentId, + AccountId = AccountIdOf, + >; type Currency: ReservableCurrency; type Reward: OnUnbalanced>; } @@ -254,18 +257,33 @@ pub mod pallet { Ok(()) } - // Check update and discussion time over, only project creator can apply staking period #[pallet::call_index(1)] #[pallet::weight(0)] + pub fn allow_validation( + origin: OriginFor, + department_required_fund_id: DepartmentRequiredFundId, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + T::DepartmentsSource::check_member_is_admin(who, department_required_fund_id)?; + + Ok(()) + } + + // Check update and discussion time over, only project creator can apply staking period + #[pallet::call_index(2)] + #[pallet::weight(0)] pub fn apply_staking_period( origin: OriginFor, department_required_fund_id: DepartmentRequiredFundId, ) -> DispatchResult { let _who = ensure_signed(origin)?; - Self::ensure_validation_to_do(department_required_fund_id)?; + // Self::ensure_validation_to_do(department_required_fund_id)?; let department_id = Self::get_department_id_from_department_required_fund_id( department_required_fund_id, )?; + + // These two steps can be removed to eliminate the time limit for department funding applications. let department_funding_status = Self::ensure_can_stake_using_status(department_id)?; DepartmentFundingStatusForDepartmentId::::insert( department_id, @@ -311,7 +329,7 @@ pub mod pallet { // Ok(()) // } - #[pallet::call_index(2)] + #[pallet::call_index(3)] #[pallet::weight(0)] pub fn apply_jurors( origin: OriginFor, @@ -345,7 +363,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(3)] + #[pallet::call_index(4)] #[pallet::weight(0)] pub fn pass_period( origin: OriginFor, @@ -367,7 +385,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(4)] + #[pallet::call_index(5)] #[pallet::weight(0)] pub fn draw_jurors( origin: OriginFor, @@ -393,7 +411,7 @@ pub mod pallet { // Unstaking // Stop drawn juror to unstake ✔️ - #[pallet::call_index(5)] + #[pallet::call_index(6)] #[pallet::weight(0)] pub fn unstaking( origin: OriginFor, @@ -411,7 +429,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(6)] + #[pallet::call_index(7)] #[pallet::weight(0)] pub fn commit_vote( origin: OriginFor, @@ -430,7 +448,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(7)] + #[pallet::call_index(8)] #[pallet::weight(0)] pub fn reveal_vote( origin: OriginFor, diff --git a/custom-pallets/department-funding/src/tests.rs b/custom-pallets/department-funding/src/tests.rs index 400b369..f11db79 100644 --- a/custom-pallets/department-funding/src/tests.rs +++ b/custom-pallets/department-funding/src/tests.rs @@ -127,3 +127,59 @@ fn create_department_required_fund_fails_if_department_does_not_exist() { ); }); } + +#[test] +fn apply_staking_period_works() { + new_test_ext().execute_with(|| { + System::set_block_number(1); + + let account_id = 1; + let department_id = 1; + let tipping_name = TippingName::SmallTipper; + let funding_needed = 10_000u64.into(); + // Dispatch a signed extrinsic. + let department_account_id = 5; + let content_department: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); + assert_ok!(Departments::create_department( + RuntimeOrigin::signed(department_account_id), + content_department.clone() + )); + + let content: Content = Content::IPFS( + "bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy" + .as_bytes() + .to_vec(), + ); + + assert_ok!(DepartmentFunding::create_department_required_fund( + RuntimeOrigin::signed(account_id), + department_id, + content.clone(), + tipping_name, + funding_needed + )); + + System::set_block_number(50); + + // Dispatch the extrinsic + assert_ok!(DepartmentFunding::apply_staking_period( + RuntimeOrigin::signed(account_id), + department_id + )); + + // Verify that the correct event was emitted + let block_number = System::block_number(); + + System::assert_last_event( + Event::StakingPeriodStarted { + department_required_fund_id: department_id, + block_number, + } + .into(), + ); + }); +} diff --git a/custom-pallets/departments/src/extras.rs b/custom-pallets/departments/src/extras.rs index baa6642..8696ac9 100644 --- a/custom-pallets/departments/src/extras.rs +++ b/custom-pallets/departments/src/extras.rs @@ -3,10 +3,18 @@ use trait_departments::DepartmentsLink; impl DepartmentsLink for Pallet { type DepartmentId = DepartmentId; + type AccountId = AccountIdOf; - fn check_department_exists(department_id: DepartmentId) -> DispatchResult { + fn check_department_exists(department_id: Self::DepartmentId) -> DispatchResult { Self::check_department_exists(department_id) } + + fn check_member_is_admin( + who: Self::AccountId, + department_id: Self::DepartmentId, + ) -> DispatchResult { + Self::check_member_is_admin(who, department_id) + } } impl Pallet { diff --git a/custom-pallets/departments/src/lib.rs b/custom-pallets/departments/src/lib.rs index 0b716c1..f4eb410 100644 --- a/custom-pallets/departments/src/lib.rs +++ b/custom-pallets/departments/src/lib.rs @@ -72,6 +72,8 @@ use pallet_support::{ use sp_std::vec; use sp_std::vec::Vec; +type AccountIdOf = ::AccountId; + // All pallet logic is defined in its own module and must be annotated by the `pallet` attribute. #[frame_support::pallet(dev_mode)] pub mod pallet { diff --git a/traits/trait-departments/src/lib.rs b/traits/trait-departments/src/lib.rs index 5ce4022..07128cb 100644 --- a/traits/trait-departments/src/lib.rs +++ b/traits/trait-departments/src/lib.rs @@ -5,5 +5,10 @@ use sp_std::prelude::*; pub trait DepartmentsLink { type DepartmentId; + type AccountId; fn check_department_exists(department_id: Self::DepartmentId) -> DispatchResult; + fn check_member_is_admin( + who: Self::AccountId, + department_id: Self::DepartmentId, + ) -> DispatchResult; }