Skip to content

Commit

Permalink
ensure_can_stake_using_status department-funding
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Nov 24, 2023
1 parent 983a19a commit 03e957d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
41 changes: 29 additions & 12 deletions pallets/department-funding/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,43 @@ impl<T: Config> Pallet<T> {
) -> Result<DepartmentFundingStatus<BlockNumberOf<T>, FundingStatus>, DispatchError> {
let department_status_option =
DepartmentFundingStatusForDepartmentId::<T>::get(department_id);
let now = <frame_system::Pallet<T>>::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::<T>::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::<T>::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::<T>::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::<T>::ConditionDontMatch.into())
}
},
None => {
let now = <frame_system::Pallet<T>>::block_number();
let department_funding_status = DepartmentFundingStatus {
block_number: now,
status: FundingStatus::Processing,
};
Ok(department_funding_status)
},
None => Ok(department_funding_status),
}
}

Expand Down
18 changes: 14 additions & 4 deletions pallets/department-funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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<T> = <T as frame_system::Config>::AccountId;
type BalanceOf<T> = <<T as Config>::Currency as Currency<AccountIdOf<T>>>::Balance;
Expand Down Expand Up @@ -172,6 +174,8 @@ pub mod pallet {
BlockDepartmentRequiredFundIdNotExists,
ValidationForDepartmentRequiredFundIdIsOff,
FundingStatusProcessing,
ReapplicationTimeNotReached,
ConditionDontMatch,
}

// Check deparment exists, it will done using loose coupling
Expand Down Expand Up @@ -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::<T>::insert(
department_id,
department_funding_status,
);
Ok(())
}

Expand Down

0 comments on commit 03e957d

Please sign in to comment.