Skip to content

Commit

Permalink
department funding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Aug 13, 2024
1 parent a5e0c8d commit bf57cf4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 10 deletions.
36 changes: 27 additions & 9 deletions custom-pallets/department-funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ pub mod pallet {
PhaseData = PhaseData<Self>,
JurorGameResult = JurorGameResult,
>;
type DepartmentsSource: DepartmentsLink<DepartmentId = DepartmentId>;
type DepartmentsSource: DepartmentsLink<
DepartmentId = DepartmentId,
AccountId = AccountIdOf<Self>,
>;
type Currency: ReservableCurrency<Self::AccountId>;
type Reward: OnUnbalanced<PositiveImbalanceOf<Self>>;
}
Expand Down Expand Up @@ -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<T>,
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<T>,
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::<T>::insert(
department_id,
Expand Down Expand Up @@ -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<T>,
Expand Down Expand Up @@ -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<T>,
Expand All @@ -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<T>,
Expand All @@ -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<T>,
Expand All @@ -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<T>,
Expand All @@ -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<T>,
Expand Down
56 changes: 56 additions & 0 deletions custom-pallets/department-funding/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
});
}
10 changes: 9 additions & 1 deletion custom-pallets/departments/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ use trait_departments::DepartmentsLink;

impl<T: Config> DepartmentsLink for Pallet<T> {
type DepartmentId = DepartmentId;
type AccountId = AccountIdOf<T>;

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<T: Config> Pallet<T> {
Expand Down
2 changes: 2 additions & 0 deletions custom-pallets/departments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ use pallet_support::{

use sp_std::vec;
use sp_std::vec::Vec;
type AccountIdOf<T> = <T as frame_system::Config>::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 {
Expand Down
5 changes: 5 additions & 0 deletions traits/trait-departments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit bf57cf4

Please sign in to comment.