Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Aug 20, 2024
1 parent 4e10567 commit 0bc3fc1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions custom-pallets/department-funding/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl<T: Config> DepartmentRequiredFund<T> {
tipping_name,
funding_needed,
creator,
released: false,
}
}
}
Expand Down
83 changes: 83 additions & 0 deletions custom-pallets/department-funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// Learn more about FRAME and the core library of Substrate FRAME pallets:
/// <https://docs.substrate.io/reference/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)]
Expand Down Expand Up @@ -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]
Expand All @@ -90,6 +93,7 @@ pub mod pallet {
RangePoint = RangePoint,
Period = Period,
PhaseData = PhaseData<Self>,
WinningDecision = WinningDecision,
JurorGameResult = JurorGameResult,
>;
type DepartmentsSource: DepartmentsLink<
Expand Down Expand Up @@ -623,5 +627,84 @@ pub mod pallet {
}
Ok(())
}

#[pallet::call_index(11)]
#[pallet::weight(0)]
pub fn add_to_department_fund(
origin: OriginFor<T>,
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 <DepartmentRequiredFunds<T>>::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;
<DepartmentRequiredFunds<T>>::mutate(
&department_required_fund_id,
|departmentrequiredfund_option| {
*departmentrequiredfund_option = Some(departmentrequiredfund);
},
);
} else if winning_decision == WinningDecision::WinnerNo && released == false {
departmentrequiredfund.released = true;

<DepartmentRequiredFunds<T>>::mutate(
&department_required_fund_id,
|departmentrequiredfund_option| {
*departmentrequiredfund_option = Some(departmentrequiredfund);
},
);

let r = <T as pallet::Config>::Currency::deposit_into_existing(
&creator,
stake_required,
)
.ok()
.unwrap();
<T as pallet::Config>::Reward::on_unbalanced(r);
} else if winning_decision == WinningDecision::Draw && released == false {
departmentrequiredfund.released = true;

<DepartmentRequiredFunds<T>>::mutate(
&department_required_fund_id,
|departmentrequiredfund_option| {
*departmentrequiredfund_option = Some(departmentrequiredfund);
},
);

let r = <T as pallet::Config>::Currency::deposit_into_existing(
&creator,
stake_required,
)
.ok()
.unwrap();
<T as pallet::Config>::Reward::on_unbalanced(r);
} else {
Err(Error::<T>::AlreadyFunded)?
}
},
None => {},
}

Ok(())
}
}
}
1 change: 1 addition & 0 deletions custom-pallets/department-funding/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct DepartmentRequiredFund<T: Config> {
pub tipping_name: TippingName,
pub funding_needed: BalanceOf<T>,
pub creator: T::AccountId,
pub released: bool,
}

#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)]
Expand Down

0 comments on commit 0bc3fc1

Please sign in to comment.