Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
ensure_staking_period_set_once_project_id
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Nov 14, 2023
1 parent d99242e commit a044c55
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
8 changes: 8 additions & 0 deletions pallets/project-tips/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub fn ensure_staking_period_set_once_project_id(project_id: ProjectId) -> DispatchResult {
let block_number_option = <ValidationProjectBlock<T>>::get(project_id);
match block_number_option {
Some(_block) => Err(Error::<T>::ProjectIdStakingPeriodAlreadySet)?,
None => Ok(()),
}
}

pub(super) fn u64_to_balance_saturated(input: u64) -> BalanceOf<T> {
input.saturated_into::<BalanceOf<T>>()
}
Expand Down
10 changes: 6 additions & 4 deletions pallets/project-tips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn validation_project_block_number)]
pub type ValidationProjectBlock<T: Config> =
StorageMap<_, Blake2_128Concat, ProjectId, BlockNumberOf<T>, ValueQuery>;
StorageMap<_, Blake2_128Concat, ProjectId, BlockNumberOf<T>>;

// Pallets use events to inform users when important changes are made.
// https://docs.substrate.io/main-docs/build/events-errors/
Expand Down Expand Up @@ -156,6 +156,7 @@ pub mod pallet {
FundingMoreThanTippingValue,
ProjectDontExists,
ProjectCreatorDontMatch,
ProjectIdStakingPeriodAlreadySet,
}

// Check deparment exists, it will done using loose coupling
Expand Down Expand Up @@ -201,18 +202,19 @@ pub mod pallet {
// 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<T>, project_id: ProjectId) -> DispatchResult {
pub fn apply_staking_period(origin: OriginFor<T>, project_id: ProjectId) -> DispatchResult {

let who = ensure_signed(origin)?;

// Self::ensure_validation_on_positive_externality(user_to_calculate.clone())?;
Self::ensure_user_is_project_creator_and_project_exists(project_id, who)?;
Self::ensure_staking_period_set_once_project_id(project_id)?;

let now = <frame_system::Pallet<T>>::block_number();

let key = SumTreeName::ProjectTips { project_id, block_number: now.clone() };

<ValidationProjectBlock<T>>::insert(project_id, now.clone());
// check what if called again
// check what if called again, its done with `ensure_staking_period_set_once_project_id`
T::SchellingGameSharedSource::set_to_staking_period_pe_link(key.clone(), now.clone())?;
T::SchellingGameSharedSource::create_tree_helper_link(key, 3)?;

Expand Down
5 changes: 5 additions & 0 deletions pallets/project-tips/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,10 @@ fn check_apply_staking_period_function() {
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),
Error::<Test>::ProjectIdStakingPeriodAlreadySet
);
});
}

0 comments on commit a044c55

Please sign in to comment.