From 0bc3fc105aced63d75cdcfc2c406d8b0a0452173 Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Tue, 20 Aug 2024 12:49:23 +0530 Subject: [PATCH] update --- .../department-funding/src/extras.rs | 1 + custom-pallets/department-funding/src/lib.rs | 83 +++++++++++++++++++ .../department-funding/src/types.rs | 1 + 3 files changed, 85 insertions(+) diff --git a/custom-pallets/department-funding/src/extras.rs b/custom-pallets/department-funding/src/extras.rs index df1547c..536eecc 100644 --- a/custom-pallets/department-funding/src/extras.rs +++ b/custom-pallets/department-funding/src/extras.rs @@ -18,6 +18,7 @@ impl DepartmentRequiredFund { tipping_name, funding_needed, creator, + released: false, } } } diff --git a/custom-pallets/department-funding/src/lib.rs b/custom-pallets/department-funding/src/lib.rs index 74d3f3f..65c28e4 100644 --- a/custom-pallets/department-funding/src/lib.rs +++ b/custom-pallets/department-funding/src/lib.rs @@ -4,6 +4,7 @@ /// Learn more about FRAME and the core library of Substrate FRAME pallets: /// // One can enhance validation measures by increasing staking power for local residents or individuals with positive externalities—those who contribute to the network for a good cause. +// To Do! Staking is not yet done while applying for staking period. pub use pallet::*; #[cfg(test)] @@ -64,6 +65,8 @@ type DepartmentRequiredFundId = u64; #[frame_support::pallet(dev_mode)] pub mod pallet { + use pallet_schelling_game_shared::types::WinningDecision; + use super::*; #[pallet::pallet] @@ -90,6 +93,7 @@ pub mod pallet { RangePoint = RangePoint, Period = Period, PhaseData = PhaseData, + WinningDecision = WinningDecision, JurorGameResult = JurorGameResult, >; type DepartmentsSource: DepartmentsLink< @@ -623,5 +627,84 @@ pub mod pallet { } Ok(()) } + + #[pallet::call_index(11)] + #[pallet::weight(0)] + pub fn add_to_department_fund( + origin: OriginFor, + department_required_fund_id: DepartmentRequiredFundId, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + let block_number = + Self::get_block_number_of_schelling_game(department_required_fund_id)?; + let key = SumTreeName::DepartmentRequiredFund { + department_required_fund_id, + block_number: block_number.clone(), + }; + let winning_decision = + T::SchellingGameSharedSource::get_winning_decision_value(key.clone())?; + match >::get(department_required_fund_id) { + Some(mut departmentrequiredfund) => { + let tipping_name = departmentrequiredfund.tipping_name; + let tipping_value = Self::value_of_tipping_name(tipping_name); + let stake_required = tipping_value.stake_required; + let fund_needed = departmentrequiredfund.funding_needed; + let released = departmentrequiredfund.released; + let creator = departmentrequiredfund.creator.clone(); + + let total_funding = stake_required.checked_add(&fund_needed).expect("overflow"); + + if winning_decision == WinningDecision::WinnerYes && released == false { + departmentrequiredfund.released = true; + >::mutate( + &department_required_fund_id, + |departmentrequiredfund_option| { + *departmentrequiredfund_option = Some(departmentrequiredfund); + }, + ); + } else if winning_decision == WinningDecision::WinnerNo && released == false { + departmentrequiredfund.released = true; + + >::mutate( + &department_required_fund_id, + |departmentrequiredfund_option| { + *departmentrequiredfund_option = Some(departmentrequiredfund); + }, + ); + + let r = ::Currency::deposit_into_existing( + &creator, + stake_required, + ) + .ok() + .unwrap(); + ::Reward::on_unbalanced(r); + } else if winning_decision == WinningDecision::Draw && released == false { + departmentrequiredfund.released = true; + + >::mutate( + &department_required_fund_id, + |departmentrequiredfund_option| { + *departmentrequiredfund_option = Some(departmentrequiredfund); + }, + ); + + let r = ::Currency::deposit_into_existing( + &creator, + stake_required, + ) + .ok() + .unwrap(); + ::Reward::on_unbalanced(r); + } else { + Err(Error::::AlreadyFunded)? + } + }, + None => {}, + } + + Ok(()) + } } } diff --git a/custom-pallets/department-funding/src/types.rs b/custom-pallets/department-funding/src/types.rs index e1b0a27..ec826fe 100644 --- a/custom-pallets/department-funding/src/types.rs +++ b/custom-pallets/department-funding/src/types.rs @@ -34,6 +34,7 @@ pub struct DepartmentRequiredFund { pub tipping_name: TippingName, pub funding_needed: BalanceOf, pub creator: T::AccountId, + pub released: bool, } #[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)]