diff --git a/pallets/department-funding/src/extras.rs b/pallets/department-funding/src/extras.rs index e1e37f8..c57cbf7 100644 --- a/pallets/department-funding/src/extras.rs +++ b/pallets/department-funding/src/extras.rs @@ -51,26 +51,43 @@ impl Pallet { ) -> Result, FundingStatus>, DispatchError> { let department_status_option = DepartmentFundingStatusForDepartmentId::::get(department_id); + let now = >::block_number(); + let department_funding_status = + DepartmentFundingStatus { block_number: now, status: FundingStatus::Processing }; match department_status_option { Some(department_status) => { let funding_status = department_status.status; if funding_status == FundingStatus::Processing { Err(Error::::FundingStatusProcessing.into()) + } else if funding_status == FundingStatus::Failed { + // else check 3 month if status faild or 6 months if status success to reapply for funding + let status_failed_time = TIME_FOR_STAKING_FUNDING_STATUS_FAILED; + let status_failed_time_block = Self::u64_to_block_saturated(status_failed_time); + let funding_status_block = department_status.block_number; + let time = + now.checked_sub(&funding_status_block).expect("Overflow"); + if time >= status_failed_time_block { + Ok(department_funding_status) + } else { + Err(Error::::ReapplicationTimeNotReached.into()) + } + } else if funding_status == FundingStatus::Success { + let status_success_time = TIME_FOR_STAKING_FUNDING_STATUS_PASSED; + let status_success_time_block = + Self::u64_to_block_saturated(status_success_time); + let funding_status_block = department_status.block_number; + let time = + now.checked_sub(&funding_status_block).expect("Overflow"); + if time >= status_success_time_block { + Ok(department_funding_status) + } else { + Err(Error::::ReapplicationTimeNotReached.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) + Err(Error::::ConditionDontMatch.into()) } }, - None => { - let now = >::block_number(); - let department_funding_status = DepartmentFundingStatus { - block_number: now, - status: FundingStatus::Processing, - }; - Ok(department_funding_status) - }, + None => Ok(department_funding_status), } } diff --git a/pallets/department-funding/src/lib.rs b/pallets/department-funding/src/lib.rs index 0b715e2..8c0a8e8 100644 --- a/pallets/department-funding/src/lib.rs +++ b/pallets/department-funding/src/lib.rs @@ -20,7 +20,7 @@ pub use weights::*; mod extras; mod types; -use frame_support::sp_runtime::traits::Saturating; +use frame_support::sp_runtime::traits::{CheckedAdd, CheckedSub}; use frame_support::sp_runtime::SaturatedConversion; use frame_support::sp_std::prelude::*; use frame_support::{ @@ -40,7 +40,9 @@ 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::{DepartmentFundingStatus, DepartmentRequiredFund, TippingName, TippingValue, FundingStatus}; +use types::{ + DepartmentFundingStatus, DepartmentRequiredFund, FundingStatus, TippingName, TippingValue, +}; type AccountIdOf = ::AccountId; type BalanceOf = <::Currency as Currency>>::Balance; @@ -172,6 +174,8 @@ pub mod pallet { BlockDepartmentRequiredFundIdNotExists, ValidationForDepartmentRequiredFundIdIsOff, FundingStatusProcessing, + ReapplicationTimeNotReached, + ConditionDontMatch, } // Check deparment exists, it will done using loose coupling @@ -225,8 +229,14 @@ pub mod pallet { 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)?; - + let department_id = Self::get_department_id_from_department_required_fund_id( + department_required_fund_id, + )?; + let department_funding_status = Self::ensure_can_stake_using_status(department_id)?; + DepartmentFundingStatusForDepartmentId::::insert( + department_id, + department_funding_status, + ); Ok(()) }