Skip to content

Commit

Permalink
give incentives
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Jun 18, 2024
1 parent 3da61a0 commit bb48ed5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions container-chains/runtime-templates/simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ impl pallet_project_tips::Config for Runtime {
type WeightInfo = pallet_project_tips::weights::SubstrateWeight<Runtime>;
type SharedStorageSource = SharedStorage;
type Currency = Balances;
type Reward = ();
type SchellingGameSharedSource = SchellingGameShared;
}

Expand Down
2 changes: 1 addition & 1 deletion custom-pallets/project-tips/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<T: Config> Project<T> {
}

impl<T: Config> Incentives<T> {
pub fn new(number_of_games: u32, winner: u32, loser: u32, stake: u64) -> Self {
pub fn new(number_of_games: u64, winner: u64, loser: u64, stake: u64) -> Self {
Incentives {
number_of_games: number_of_games,
winner: winner,
Expand Down
45 changes: 44 additions & 1 deletion custom-pallets/project-tips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use frame_system::pallet_prelude::*;
use sp_std::prelude::*;

use frame_support::{
traits::{Currency, ExistenceRequirement, Get, ReservableCurrency, WithdrawReasons},
traits::{
Currency, ExistenceRequirement, Get, OnUnbalanced, ReservableCurrency, WithdrawReasons,
},
PalletId,
};
use pallet_schelling_game_shared::types::{
Expand All @@ -47,6 +49,9 @@ use types::{Incentives, IncentivesMetaData, Project, TippingName, TippingValue};

type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
type BalanceOf<T> = <<T as Config>::Currency as Currency<AccountIdOf<T>>>::Balance;
type PositiveImbalanceOf<T> = <<T as Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::PositiveImbalance;
pub type BlockNumberOf<T> = BlockNumberFor<T>;
pub type SumTreeNameType<T> = SumTreeName<AccountIdOf<T>, BlockNumberOf<T>>;
type DepartmentId = u64;
Expand Down Expand Up @@ -83,6 +88,9 @@ pub mod pallet {
JurorGameResult = JurorGameResult,
>;
type Currency: ReservableCurrency<Self::AccountId>;

/// Handler for the unbalanced increment when rewarding (minting rewards)
type Reward: OnUnbalanced<PositiveImbalanceOf<Self>>;
}

// The pallet's runtime storage items.
Expand Down Expand Up @@ -472,6 +480,41 @@ pub mod pallet {
*incentive_option = Some(new_incentives);
});

let total_win = incentive.winner;
let total_lost = incentive.loser;

// Define multipliers
let win_multiplier = 10 * 100;
let lost_multiplier = incentive_meta.disincentive_times * 100;

// Calculate total_win_incentives and total_lost_incentives
let total_win_incentives = total_win.checked_mul(win_multiplier);
let total_lost_incentives = total_lost.checked_mul(lost_multiplier);

// Calculate total_incentives, handling overflow or negative errors
let total_incentives = match (total_win_incentives, total_lost_incentives) {
(Some(win), Some(lost)) => win.checked_sub(lost).unwrap_or(0),
_ => 0, // If multiplication overflowed, set total_incentives to 0
};

let mut stake = incentive.total_stake;
// Deduct 1% of the stake if total_lost > total_win
if total_lost > total_win {
let stake_deduction = stake / 100; // 1% of the stake
stake = stake.checked_sub(stake_deduction).unwrap_or(stake);
// Safe subtraction
// println!("Stake deducted by 1%: {}", stake);
}

let total_fund = stake.checked_add(total_incentives).unwrap_or(0);

let balance = Self::u64_to_balance_saturated(total_fund);

let r =
<T as pallet::Config>::Currency::deposit_into_existing(&who, balance)
.ok()
.unwrap();
<T as pallet::Config>::Reward::on_unbalanced(r);
// Provide the incentives
} else {
// Error
Expand Down
1 change: 1 addition & 0 deletions custom-pallets/project-tips/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl pallet_template::Config for Test {
type SharedStorageSource = SharedStorage;
type Currency = Balances; // New code
type SchellingGameSharedSource = SchellingGameShared;
type Reward = ();
}

impl pallet_balances::Config for Test {
Expand Down
10 changes: 5 additions & 5 deletions custom-pallets/project-tips/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ pub struct Project<T: Config> {
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct Incentives<T: Config> {
pub number_of_games: u32,
pub winner: u32,
pub loser: u32,
pub number_of_games: u64,
pub winner: u64,
pub loser: u64,
pub total_stake: u64,
pub start: WhenDetailsOf<T>,
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct IncentivesMetaData<T: Config> {
pub total_number: u32,
pub disincentive_times: u32,
pub total_number: u64,
pub disincentive_times: u64,
pub total_block: BlockNumberOf<T>,
}

Expand Down
3 changes: 2 additions & 1 deletion docs/new_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ impl pallet_project_tips::Config for Runtime {
type WeightInfo = pallet_project_tips::weights::SubstrateWeight<Runtime>;
type SharedStorageSource = SharedStorage;
type Currency = Balances;
type SchellingGameSharedSource = SchellingGameShared;
type Reward = ();
type SchellingGameSharedSource = SchellingGameShared;
}


Expand Down

0 comments on commit bb48ed5

Please sign in to comment.