Skip to content

Commit

Permalink
tip released
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Jun 27, 2024
1 parent 20c38b1 commit 7ff0c47
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 9 deletions.
2 changes: 1 addition & 1 deletion custom-pallets/profile-validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ pub mod pallet {
let period = T::SchellingGameSharedSource::get_period_link(key.clone()).unwrap();
if period == Period::Execution {
let decision: WinningDecision =
T::SchellingGameSharedSource::get_winning_decision_value_link(key.clone());
T::SchellingGameSharedSource::get_winning_decision_value(key.clone())?;
if decision == WinningDecision::WinnerNo {
match <ProfileFundDetails<T>>::get(profile_user_account.clone(), who.clone()) {
Some(mut profile_fund_info) => {
Expand Down
1 change: 1 addition & 0 deletions custom-pallets/project-tips/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl<T: Config> Project<T> {
tipping_name,
funding_needed,
project_leader,
released: false,
}
}
}
Expand Down
82 changes: 80 additions & 2 deletions custom-pallets/project-tips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod types;
use frame_support::pallet_prelude::DispatchError;
use frame_support::pallet_prelude::*;
use frame_support::sp_runtime::traits::Saturating;
use frame_support::sp_runtime::traits::{CheckedAdd, CheckedSub};
use frame_support::sp_runtime::SaturatedConversion;
use frame_support::{dispatch::DispatchResult, ensure};
use frame_system::pallet_prelude::*;
Expand All @@ -35,7 +36,7 @@ use frame_support::{
PalletId,
};
use pallet_schelling_game_shared::types::{
JurorGameResult, Period, PhaseData, RangePoint, SchellingGameType,
JurorGameResult, Period, PhaseData, RangePoint, SchellingGameType, WinningDecision,
};
use pallet_sortition_sum_game::types::SumTreeName;
use pallet_support::{
Expand Down Expand Up @@ -85,6 +86,7 @@ pub mod pallet {
RangePoint = RangePoint,
Period = Period,
PhaseData = PhaseData<Self>,
WinningDecision = WinningDecision,
JurorGameResult = JurorGameResult,
>;
type Currency: ReservableCurrency<Self::AccountId>;
Expand Down Expand Up @@ -419,6 +421,82 @@ pub mod pallet {

#[pallet::call_index(8)]
#[pallet::weight(0)]
pub fn release_tip(origin: OriginFor<T>, project_id: ProjectId) -> DispatchResult {
let who = ensure_signed(origin)?;

let block_number = Self::get_block_number_of_schelling_game(project_id)?;
let key = SumTreeName::ProjectTips {
project_id,
block_number: block_number.clone(),
};

let winning_decision =
T::SchellingGameSharedSource::get_winning_decision_value(key.clone())?;

match <Projects<T>>::get(project_id) {
Some(mut project) => {
let tipping_name = project.tipping_name;
let tipping_value = Self::value_of_tipping_name(tipping_name);
let stake_required = tipping_value.stake_required;
let fund_needed = project.funding_needed;
let released = project.released;

let total_funding = stake_required.checked_add(&fund_needed).expect("overflow");
if winning_decision == WinningDecision::WinnerYes && released == false {
project.released = true;

<Projects<T>>::mutate(&project_id, |project_option| {
*project_option = Some(project);
});

let r = <T as pallet::Config>::Currency::deposit_into_existing(
&who,
total_funding,
)
.ok()
.unwrap();
<T as pallet::Config>::Reward::on_unbalanced(r);
} else if winning_decision == WinningDecision::WinnerNo && released == false {
project.released = true;

<Projects<T>>::mutate(&project_id, |project_option| {
*project_option = Some(project);
});

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

<Projects<T>>::mutate(&project_id, |project_option| {
*project_option = Some(project);
});

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

None => Err(Error::<T>::ProjectDontExists)?,
}

Ok(())
}

#[pallet::call_index(9)]
#[pallet::weight(0)]
pub fn add_incentive_count(origin: OriginFor<T>, project_id: ProjectId) -> DispatchResult {
let who = ensure_signed(origin)?;
let block_number = Self::get_block_number_of_schelling_game(project_id)?;
Expand Down Expand Up @@ -478,7 +556,7 @@ pub mod pallet {

// Provide incentives

#[pallet::call_index(9)]
#[pallet::call_index(10)]
#[pallet::weight(0)]
pub fn get_incentives(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
Expand Down
4 changes: 2 additions & 2 deletions custom-pallets/project-tips/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ fn schelling_game_incentives_get_test() {
number_of_games: 20,
winner: 18,
loser: 2,
total_stake: 14 * 100 *20,
total_stake: 14 * 100 * 20,
start: WhenDetails {
block: 201,
time: 0,
Expand All @@ -1130,7 +1130,7 @@ fn schelling_game_incentives_get_test() {
number_of_games: 20,
winner: 2,
loser: 18,
total_stake: 14 * 100 *20,
total_stake: 14 * 100 * 20,
start: WhenDetails {
block: 201,
time: 0,
Expand Down
1 change: 1 addition & 0 deletions custom-pallets/project-tips/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Project<T: Config> {
pub tipping_name: TippingName,
pub funding_needed: BalanceOf<T>,
pub project_leader: T::AccountId,
pub released: bool,
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
Expand Down
13 changes: 11 additions & 2 deletions custom-pallets/schelling-game-shared/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,18 @@ impl<T: Config> Pallet<T> {
}
}

pub(super) fn get_winning_decision_value(key: SumTreeNameType<T>) -> WinningDecision {
pub(super) fn get_winning_decision_value(
key: SumTreeNameType<T>,
) -> Result<WinningDecision, DispatchError> {
match <PeriodName<T>>::get(&key) {
Some(period) => {
ensure!(period == Period::Execution, Error::<T>::PeriodDontMatch);
}
None => Err(Error::<T>::PeriodDoesNotExists)?,
}
let decision_tuple: (u64, u64) = <DecisionCount<T>>::get(&key);
Self::get_winning_decision(decision_tuple)
let winning_decision = Self::get_winning_decision(decision_tuple);
Ok(winning_decision)
}

pub(super) fn get_winning_incentives(
Expand Down
4 changes: 3 additions & 1 deletion custom-pallets/schelling-game-shared/src/share_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ impl<T: Config> SchellingGameSharedLink for Pallet<T> {
Self::drawn_jurors(key)
}

fn get_winning_decision_value_link(key: Self::SumTreeName) -> WinningDecision {
fn get_winning_decision_value(
key: Self::SumTreeName,
) -> Result<WinningDecision, DispatchError> {
Self::get_winning_decision_value(key)
}

Expand Down
4 changes: 3 additions & 1 deletion traits/trait-schelling-game-shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ pub trait SchellingGameSharedLink {

fn get_drawn_jurors(key: Self::SumTreeName) -> Vec<(Self::AccountId, u64)>;

fn get_winning_decision_value_link(key: Self::SumTreeName) -> Self::WinningDecision;
fn get_winning_decision_value(
key: Self::SumTreeName,
) -> Result<Self::WinningDecision, DispatchError>;

fn get_result_of_juror(
key: Self::SumTreeName,
Expand Down

0 comments on commit 7ff0c47

Please sign in to comment.