Skip to content

Commit

Permalink
add stake in incentives
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Jun 17, 2024
1 parent 3e88b20 commit 7a65f50
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
3 changes: 2 additions & 1 deletion custom-pallets/project-tips/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ impl<T: Config> Project<T> {
}

impl<T: Config> Incentives<T> {
pub fn new(number_of_games: u32, winner: u32, loser: u32) -> Self {
pub fn new(number_of_games: u32, winner: u32, loser: u32, stake: u64) -> Self {
Incentives {
number_of_games: number_of_games,
winner: winner,
loser: loser,
total_stake: stake,
start: new_when_details::<T>(),
}
}
Expand Down
11 changes: 8 additions & 3 deletions custom-pallets/project-tips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pub mod pallet {
project_id,
block_number: block_number.clone(),
};
let juror_game_result =
let (juror_game_result, stake) =
T::SchellingGameSharedSource::get_result_of_juror(key.clone(), who.clone())?;

T::SchellingGameSharedSource::add_to_incentives_count(key, who.clone())?;
Expand All @@ -415,13 +415,18 @@ pub mod pallet {
JurorGameResult::Won => {
incentive.number_of_games += 1;
incentive.winner += 1;
incentive.total_stake += stake;
}
JurorGameResult::Lost => {
incentive.number_of_games += 1;
incentive.loser += 1;
incentive.total_stake += stake;
}

JurorGameResult::Draw => {}
JurorGameResult::Draw => {
incentive.number_of_games += 1;
incentive.total_stake += stake;
}
};
<IncentiveCount<T>>::mutate(&who, |incentive_option| {
*incentive_option = Some(incentive);
Expand All @@ -441,7 +446,7 @@ pub mod pallet {
};
let number_of_games = 1;
let new_incentives: Incentives<T> =
Incentives::new(number_of_games, winner, loser);
Incentives::new(number_of_games, winner, loser, stake);
<IncentiveCount<T>>::insert(&who, new_incentives);
}
}
Expand Down
2 changes: 2 additions & 0 deletions custom-pallets/project-tips/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ fn schelling_game_incentives_test() {
number_of_games: 1,
winner: 1,
loser: 0,
total_stake: 14 * 100,
start: WhenDetails {
block: 201,
time: 0,
Expand All @@ -541,6 +542,7 @@ fn schelling_game_incentives_test() {
number_of_games: 1,
winner: 0,
loser: 1,
total_stake: 15 * 100,
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 @@ -38,6 +38,7 @@ pub struct Incentives<T: Config> {
pub number_of_games: u32,
pub winner: u32,
pub loser: u32,
pub total_stake: u64,
pub start: WhenDetailsOf<T>,
}

Expand Down
16 changes: 9 additions & 7 deletions custom-pallets/schelling-game-shared/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ impl<T: Config> Pallet<T> {
pub(super) fn get_result_of_juror(
key: SumTreeNameType<T>,
who: AccountIdOf<T>,
) -> Result<JurorGameResult, DispatchError> {
) -> Result<(JurorGameResult, u64), DispatchError> {
match <PeriodName<T>>::get(&key) {
Some(period) => {
ensure!(period == Period::Execution, Error::<T>::PeriodDontMatch);
Expand All @@ -647,17 +647,19 @@ impl<T: Config> Pallet<T> {
Some(vote) => {
let decision_count: (u64, u64) = <DecisionCount<T>>::get(&key);
let winning_decision = Self::get_winning_decision(decision_count);
if let Ok(_) = drawn_juror.binary_search_by(|(c, _)| c.cmp(&who.clone())) {
if let Ok(i) = drawn_juror.binary_search_by(|(c, _)| c.cmp(&who.clone())) {
let stake = drawn_juror[i].1;

match winning_decision {
WinningDecision::WinnerYes => match vote {
RevealedVote::Yes => Ok(JurorGameResult::Won),
RevealedVote::No => Ok(JurorGameResult::Lost),
RevealedVote::Yes => Ok((JurorGameResult::Won, stake)),
RevealedVote::No => Ok((JurorGameResult::Lost, stake)),
},
WinningDecision::WinnerNo => match vote {
RevealedVote::Yes => Ok(JurorGameResult::Lost),
RevealedVote::No => Ok(JurorGameResult::Won),
RevealedVote::Yes => Ok((JurorGameResult::Lost, stake)),
RevealedVote::No => Ok((JurorGameResult::Won, stake)),
},
WinningDecision::Draw => Ok(JurorGameResult::Lost),
WinningDecision::Draw => Ok((JurorGameResult::Draw, stake)),
}
} else {
Err(Error::<T>::StakeDoesNotExists)?
Expand Down
2 changes: 1 addition & 1 deletion custom-pallets/schelling-game-shared/src/share_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl<T: Config> SchellingGameSharedLink for Pallet<T> {
fn get_result_of_juror(
key: Self::SumTreeName,
who: Self::AccountId,
) -> Result<JurorGameResult, DispatchError> {
) -> Result<(JurorGameResult, u64), DispatchError> {
Self::get_result_of_juror(key, who)
}

Expand Down
25 changes: 15 additions & 10 deletions custom-pallets/schelling-game-shared/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,21 @@ fn challenger_win_value_test() {
));
let period = TemplateModule::get_period(key.clone());
assert_eq!(Some(Period::Execution), period);
let result = TemplateModule::get_result_of_juror(key.clone(), 4);
assert_eq!(result.unwrap(), JurorGameResult::Won);
let result = TemplateModule::get_result_of_juror(key.clone(), 7);
assert_eq!(result.unwrap(), JurorGameResult::Won);
let result = TemplateModule::get_result_of_juror(key.clone(), 13);
assert_eq!(result.unwrap(), JurorGameResult::Won);
let result = TemplateModule::get_result_of_juror(key.clone(), 14);
assert_eq!(result.unwrap(), JurorGameResult::Won);
let result = TemplateModule::get_result_of_juror(key.clone(), 15);
assert_eq!(result.unwrap(), JurorGameResult::Lost);
let result_stake = TemplateModule::get_result_of_juror(key.clone(), 4);
let (result, _) = result_stake.unwrap();
assert_eq!(result, JurorGameResult::Won);
let result_stake = TemplateModule::get_result_of_juror(key.clone(), 7);
let (result, _) = result_stake.unwrap();
assert_eq!(result, JurorGameResult::Won);
let result_stake = TemplateModule::get_result_of_juror(key.clone(), 13);
let (result, _) = result_stake.unwrap();
assert_eq!(result, JurorGameResult::Won);
let result_stake = TemplateModule::get_result_of_juror(key.clone(), 14);
let (result, _) = result_stake.unwrap();
assert_eq!(result, JurorGameResult::Won);
let result_stake = TemplateModule::get_result_of_juror(key.clone(), 15);
let (result, _) = result_stake.unwrap();
assert_eq!(result, JurorGameResult::Lost);
});
}

Expand Down
2 changes: 1 addition & 1 deletion traits/trait-schelling-game-shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub trait SchellingGameSharedLink {
fn get_result_of_juror(
key: Self::SumTreeName,
who: Self::AccountId,
) -> Result<Self::JurorGameResult, DispatchError>;
) -> Result<(Self::JurorGameResult, u64), DispatchError>;
fn get_result_of_juror_score(
key: Self::SumTreeName,
who: Self::AccountId,
Expand Down

0 comments on commit 7a65f50

Please sign in to comment.