diff --git a/pallets/department-funding/src/extras.rs b/pallets/department-funding/src/extras.rs index a110b79..e1e37f8 100644 --- a/pallets/department-funding/src/extras.rs +++ b/pallets/department-funding/src/extras.rs @@ -1,4 +1,5 @@ use crate::*; +use types::{TIME_FOR_STAKING_FUNDING_STATUS_FAILED, TIME_FOR_STAKING_FUNDING_STATUS_PASSED}; impl DepartmentRequiredFund { pub fn new( @@ -24,7 +25,7 @@ impl Pallet { T::SchellingGameSharedSource::create_phase_data(50, 5, 3, 100, (100, 100)) } - pub fn ensure_validation_on_positive_externality( + pub fn ensure_validation_to_do( department_required_fund_id: DepartmentRequiredFundId, ) -> DispatchResult { let bool_data = ValidateDepartmentRequiredFund::::get(department_required_fund_id); @@ -36,7 +37,41 @@ impl Pallet { pub fn get_department_id_from_department_required_fund_id( department_required_fund_id: DepartmentRequiredFundId, ) -> Result { - Ok(1) + let department_required_fund_option = + DepartmentRequiredFunds::::get(department_required_fund_id); + + match department_required_fund_option { + Some(department_required_fund) => Ok(department_required_fund.department_id), + None => Err(Error::::DepartmentRequiredFundDontExits)?, + } + } + + pub fn ensure_can_stake_using_status( + department_id: DepartmentId, + ) -> Result, FundingStatus>, DispatchError> { + let department_status_option = + DepartmentFundingStatusForDepartmentId::::get(department_id); + match department_status_option { + Some(department_status) => { + let funding_status = department_status.status; + if funding_status == FundingStatus::Processing { + Err(Error::::FundingStatusProcessing.into()) + } else { + // else check 3 month or 6 months passed then return new department_status + let three_month_number = TIME_FOR_STAKING_FUNDING_STATUS_FAILED; + let three_month_block = Self::u64_to_block_saturated(three_month_number); + Ok(department_status) + } + }, + None => { + let now = >::block_number(); + let department_funding_status = DepartmentFundingStatus { + block_number: now, + status: FundingStatus::Processing, + }; + Ok(department_funding_status) + }, + } } // pub fn ensure_user_is_project_creator_and_project_exists( diff --git a/pallets/department-funding/src/lib.rs b/pallets/department-funding/src/lib.rs index c90f7c7..0b715e2 100644 --- a/pallets/department-funding/src/lib.rs +++ b/pallets/department-funding/src/lib.rs @@ -40,7 +40,7 @@ use schelling_game_shared_link::SchellingGameSharedLink; use shared_storage_link::SharedStorageLink; use sortition_sum_game::types::SumTreeName; pub use types::DEPARTMENT_REQUIRED_FUND_ID; -use types::{DepartmentRequiredFund, TippingName, TippingValue}; +use types::{DepartmentFundingStatus, DepartmentRequiredFund, TippingName, TippingValue, FundingStatus}; type AccountIdOf = ::AccountId; type BalanceOf = <::Currency as Currency>>::Balance; @@ -125,6 +125,15 @@ pub mod pallet { pub type ValidationDepartmentRequiredFundsBlock = StorageMap<_, Blake2_128Concat, DepartmentRequiredFundId, BlockNumberOf>; + #[pallet::storage] + #[pallet::getter(fn department_funding_status)] + pub type DepartmentFundingStatusForDepartmentId = StorageMap< + _, + Blake2_128Concat, + DepartmentId, + DepartmentFundingStatus, FundingStatus>, + >; + // Pallets use events to inform users when important changes are made. // https://docs.substrate.io/main-docs/build/events-errors/ #[pallet::event] @@ -162,6 +171,7 @@ pub mod pallet { DepartmentRequiredFundDontExits, BlockDepartmentRequiredFundIdNotExists, ValidationForDepartmentRequiredFundIdIsOff, + FundingStatusProcessing, } // Check deparment exists, it will done using loose coupling @@ -214,8 +224,9 @@ pub mod pallet { origin: OriginFor, department_required_fund_id: DepartmentRequiredFundId, ) -> DispatchResult { + 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)?; - Ok(()) } diff --git a/pallets/department-funding/src/types.rs b/pallets/department-funding/src/types.rs index aa8e09f..806e6b5 100644 --- a/pallets/department-funding/src/types.rs +++ b/pallets/department-funding/src/types.rs @@ -5,7 +5,9 @@ use scale_info::TypeInfo; pub const DEPARTMENT_REQUIRED_FUND_ID: DepartmentRequiredFundId = 1; +pub const TIME_FOR_STAKING_FUNDING_STATUS_FAILED: u64 = (3 * 30 * 24 * 60 * 60) / 6; // 3 months time +pub const TIME_FOR_STAKING_FUNDING_STATUS_PASSED: u64 = (6 * 30 * 24 * 60 * 60) / 6; // 6 months time #[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)] @@ -35,4 +37,19 @@ pub struct DepartmentRequiredFund { } +#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)] +pub enum FundingStatus { + Processing, + Success, + Failed, +} + +#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)] +pub struct DepartmentFundingStatus { + pub block_number: BlockNumber, + pub status: FundingStatus, +} + + +