Skip to content

Commit

Permalink
Logic to be used in pallet-opf hook added
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Aug 5, 2024
1 parent 2699fd2 commit 843f558
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
2 changes: 2 additions & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2169,12 +2169,14 @@ impl pallet_distribution::Config for Runtime {
parameter_types!{

pub const MaxWhitelistedProjects: u32 = 1000;
pub const TemporaryRewards: Balance = 100000 * DOLLARS;
}
impl pallet_opf::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type VoteLockingPeriod = VoteLockingPeriod;
type VotingPeriod = VotingPeriod;
type MaxWhitelistedProjects = MaxCandidates;
type TemporaryRewards = TemporaryRewards;
}


Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/distribution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ pub mod pallet {
NotClaimingPeriod,
/// Funds locking failed
FundsReserveFailed,
/// An invalid result was returned
InvalidResult,
}

#[pallet::hooks]
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/distribution/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub use super::*;
pub use frame_support::{
pallet_prelude::*,
traits::{
DefensiveOption,
fungible,
fungible::{Inspect, Mutate, MutateHold},
fungibles,
Expand Down
39 changes: 31 additions & 8 deletions substrate/frame/opf/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,38 @@ impl<T: Config> Pallet<T> {
// To be executed in a hook, on_initialize
pub fn begin_block(now: BlockNumberFor<T>) -> Weight {
let max_block_weight = Weight::from_parts(1000_u64, 0);
let epoch = T::EpochDurationBlocks::get();
let voting_period = T::VotingPeriod::get();
// Check current round: If block is a multiple of round_locked_period,
let round_index = VotingRoundsNumber::<T>::get();

if round_index == 0 {
// Start the first voting round
let round0 = VotingRoundInfo::<T>::new();
}


let current_round_index = round_index.saturating_sub(1);
let round_infos = VotingRounds::<T>::get(current_round_index).expect("InvalidResult");
let voting_locked_block = round_infos.voting_locked_block;
let round_ending_block = round_infos.round_ending_block;

// Conditions for distribution prepartions are:
// We are within voting_round period
// We are past the voting_round_lock block
// We are at the beginning of an epoch
if (now > voting_locked_block) && (now < round_ending_block) && (now % epoch).is_zero() {
// prepare reward distribution
// for now we are using the temporary-constant reward.
let _= Self::calculate_rewards(T::TemporaryRewards::get()).map_err(|_| Error::<T>::FailedRewardCalculation);
}

// ToDo
// If the block is a multiple of "votingperiod + votingLockedperiod"
// Start new voting round
// We could make sure that the votingLockedBlock coincides with the beginning of an Epoch

// Check current round: If block is a multiple of round_locked_period,
// prepare reward distribution

// Create a new round when we reach the end of the round.
if (now % round_ending_block).is_zero(){
let _= VotingRoundInfo::<T>::new();
}


max_block_weight
}
}
8 changes: 7 additions & 1 deletion substrate/frame/opf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub mod pallet {
///
#[pallet::constant]
type VotingPeriod: Get<BlockNumberFor<Self>>;

#[pallet::constant]
type TemporaryRewards: Get<BalanceOf<Self>>;
}

/// Number of Voting Rounds executed so far
Expand Down Expand Up @@ -89,7 +92,10 @@ pub mod pallet {
VotePeriodClosed,

/// Not enough funds to vote, you need to decrease your stake
NotEnoughFunds
NotEnoughFunds,

/// The reward calculation failed due to an internal error
FailedRewardCalculation,
}

#[pallet::call]
Expand Down
12 changes: 6 additions & 6 deletions substrate/frame/opf/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ pub use super::*;
pub use frame_support::traits::tokens::{Precision, Preservation};
pub use frame_support::{
pallet_prelude::*,
traits::{fungible, fungibles, EnsureOrigin},
traits::{fungible, fungibles, EnsureOrigin, DefensiveOption},
PalletId, Serialize,
};
pub use frame_system::{pallet_prelude::*, RawOrigin};
pub use pallet_distribution::MutateHold;
pub use pallet_distribution::{AccountIdOf, BalanceOf, HoldReason, ProjectInfo, ProjectId};
pub use scale_info::prelude::vec::Vec;
pub use sp_runtime::traits::Saturating;
pub use sp_runtime::traits::{Saturating, CheckedSub};
pub use sp_runtime::traits::{AccountIdConversion, Convert, StaticLookup, Zero,CheckedAdd};
pub use sp_runtime::Percent;

Expand Down Expand Up @@ -42,11 +42,11 @@ pub struct VotingRoundInfo<T: Config>{

impl<T: Config> VotingRoundInfo<T>{
pub fn new() -> Self{
let round_starting_block = <frame_system::Pallet<T>>::block_number();
let voting_locked_block = round_starting_block.clone().checked_add(&T::VotingPeriod::get()).expect("Failed Operation");
let round_ending_block = voting_locked_block.clone().checked_add(&T::VoteLockingPeriod::get()).expect("Failed Operation");
let round_starting_block = <frame_system::Pallet<T>>::block_number();
let round_ending_block = round_starting_block.clone().checked_add(&T::VotingPeriod::get()).expect("Invalid Result");
let voting_locked_block = round_ending_block.checked_sub(&T::VoteLockingPeriod::get()).expect("Invalid Result");
let round_number = VotingRoundsNumber::<T>::get();
let new_number = round_number.checked_add(1).expect("Failed Operation");
let new_number = round_number.checked_add(1).expect("Invalid Result");
VotingRoundsNumber::<T>::put(new_number);

VotingRoundInfo{round_number, round_starting_block, voting_locked_block, round_ending_block}
Expand Down

0 comments on commit 843f558

Please sign in to comment.