Skip to content

Commit

Permalink
fixed thing
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Nov 28, 2024
1 parent 50df4fa commit 66abcda
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 65 deletions.
136 changes: 103 additions & 33 deletions core/src/epoch_reward_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,93 @@ use solana_program::{account_info::AccountInfo, msg, program_error::ProgramError
use spl_math::precise_number::PreciseNumber;

use crate::{
ballot_box::BallotBox, discriminators::Discriminators, error::TipRouterError, fees::Fees,
ncn_fee_group::NcnFeeGroup, operator_epoch_reward_router::OperatorEpochRewardRouter,
ballot_box::BallotBox,
discriminators::Discriminators,
error::TipRouterError,
fees::{FeeConfig, Fees},
ncn_fee_group::NcnFeeGroup,
operator_epoch_reward_router::OperatorEpochRewardRouter,
};

#[derive(Debug, Clone, PartialEq, Eq, Copy, Zeroable, ShankType, Pod, ShankType)]
#[derive(Default, Debug, Clone, PartialEq, Eq, Copy, Zeroable, ShankType, Pod)]
#[repr(C)]
pub struct RewardRoutes {
destination: Pubkey,
pub struct OperatorRewards {
rewards: PodU64,
reserved: [u8; 128],
}

impl OperatorRewards {
pub fn rewards(self) -> u64 {
self.rewards.into()
}
}

#[derive(Debug, Clone, PartialEq, Eq, Copy, Zeroable, ShankType, Pod)]
#[repr(C)]
pub struct RewardRoutes {
operator: Pubkey,
rewards: [OperatorRewards; 8],
}

impl Default for RewardRoutes {
fn default() -> Self {
Self {
destination: Pubkey::default(),
rewards: PodU64::from(0),
reserved: [0; 128],
operator: Pubkey::default(),
rewards: [OperatorRewards::default(); NcnFeeGroup::FEE_GROUP_COUNT],
}
}
}

impl RewardRoutes {
pub const fn destination(&self) -> Pubkey {
self.destination
self.operator
}

pub fn set_destination(&mut self, destination: Pubkey) {
self.destination = destination;
self.operator = destination;
}

pub fn rewards(&self) -> u64 {
self.rewards.into()
pub fn rewards(&self, ncn_fee_group: NcnFeeGroup) -> Result<u64, TipRouterError> {
let group_index = ncn_fee_group.group_index()?;
Ok(self.rewards[group_index].rewards())
}

pub fn increment_rewards(&mut self, rewards: u64) -> Result<(), TipRouterError> {
self.rewards = PodU64::from(
self.rewards()
.checked_add(rewards)
.ok_or(TipRouterError::ArithmeticOverflow)?,
);
pub fn set_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<(), TipRouterError> {
let group_index = ncn_fee_group.group_index()?;
self.rewards[group_index].rewards = PodU64::from(rewards);

Ok(())
}

pub fn decrement_rewards(&mut self, rewards: u64) -> Result<(), TipRouterError> {
self.rewards = PodU64::from(
self.rewards()
.checked_sub(rewards)
.ok_or(TipRouterError::ArithmeticUnderflowError)?,
);
Ok(())
pub fn increment_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<(), TipRouterError> {
let current_rewards = self.rewards(ncn_fee_group)?;

let new_rewards = current_rewards
.checked_add(rewards)
.ok_or(TipRouterError::ArithmeticOverflow)?;

self.set_rewards(ncn_fee_group, new_rewards)
}

pub fn decrement_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
rewards: u64,
) -> Result<(), TipRouterError> {
let current_rewards = self.rewards(ncn_fee_group)?;

let new_rewards = current_rewards
.checked_sub(rewards)
.ok_or(TipRouterError::ArithmeticOverflow)?;

self.set_rewards(ncn_fee_group, new_rewards)
}
}

Expand Down Expand Up @@ -183,6 +219,36 @@ impl EpochRewardRouter {
Ok(())
}

pub fn distribute_dao_rewards(
&mut self,
fee_config: &FeeConfig,
destination: &Pubkey,
) -> Result<u64, TipRouterError> {
if destination.ne(&fee_config.fee_wallet()) {
return Err(TipRouterError::DestinationMismatch);
}

let dao_rewards = self.doa_rewards();

self.decrement_dao_rewards(dao_rewards)?;

Ok(self.doa_rewards())
}

// pub fn distribute_ncn_rewards(
// &mut self,
// destination: &Pubkey,
// ) -> Result<u64, TipRouterError> {

// for route in

// let group_rewards = self.ncn_rewards(group)?;

// self.decrement_ncn_rewards(group, group_rewards)?;

// Ok(self.ncn_rewards(group)?)
// }

pub fn process_incoming_rewards(&mut self, account_balance: u64) -> Result<(), TipRouterError> {
let total_rewards = self.total_rewards()?;

Expand Down Expand Up @@ -265,13 +331,15 @@ impl EpochRewardRouter {
let operator_epoch_reward_router =
OperatorEpochRewardRouter::find_program_address(
program_id,
*group,
&operator,
&self.ncn,
self.ncn_epoch.into(),
)
.0;

self.insert_or_increment_operator_rewards(
*group,
operator_epoch_reward_router,
operator_rewards,
)?;
Expand Down Expand Up @@ -360,6 +428,7 @@ impl EpochRewardRouter {

pub fn insert_or_increment_operator_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
operator: Pubkey,
rewards: u64,
) -> Result<(), TipRouterError> {
Expand All @@ -368,16 +437,16 @@ impl EpochRewardRouter {
}

for operator_reward in self.reward_routes.iter_mut() {
if operator_reward.destination == operator {
operator_reward.increment_rewards(rewards)?;
if operator_reward.operator == operator {
operator_reward.increment_rewards(ncn_fee_group, rewards)?;
return Ok(());
}
}

for operator_reward in self.reward_routes.iter_mut() {
if operator_reward.destination == Pubkey::default() {
operator_reward.destination = operator;
operator_reward.rewards = PodU64::from(rewards);
if operator_reward.operator == Pubkey::default() {
operator_reward.operator = operator;
operator_reward.increment_rewards(ncn_fee_group, rewards)?;
return Ok(());
}
}
Expand All @@ -387,6 +456,7 @@ impl EpochRewardRouter {

pub fn decrement_operator_rewards(
&mut self,
ncn_fee_group: NcnFeeGroup,
operator: Pubkey,
rewards: u64,
) -> Result<(), TipRouterError> {
Expand All @@ -395,8 +465,8 @@ impl EpochRewardRouter {
}

for operator_reward in self.reward_routes.iter_mut() {
if operator_reward.destination == operator {
operator_reward.decrement_rewards(rewards)?;
if operator_reward.operator == operator {
operator_reward.decrement_rewards(ncn_fee_group, rewards)?;
return Ok(());
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub enum TipRouterError {
OperatorRewardListFull,
#[error("Operator Reward not found")]
OperatorRewardNotFound,
#[error("Destination mismatch")]
DestinationMismatch,
}

impl<T> DecodeError<T> for TipRouterError {
Expand Down
1 change: 1 addition & 0 deletions core/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub enum TipRouterInstruction {
#[account(6, name = "restaking_program_id")]
#[account(7, name = "system_program")]
InitializeOperatorEpochRewardRouter{
ncn_fee_group: u8,
first_slot_of_ncn_epoch: Option<u64>,
},

Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod loaders;
pub mod ncn_config;
pub mod ncn_fee_group;
pub mod operator_epoch_reward_router;
pub mod rewards;
pub mod stake_weight;
pub mod tracked_mints;
pub mod weight_entry;
Expand Down
48 changes: 26 additions & 22 deletions core/src/ncn_fee_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ impl Default for NcnFeeGroup {
}
}

impl TryFrom<u8> for NcnFeeGroup {
type Error = TipRouterError;

fn try_from(group: u8) -> Result<Self, Self::Error> {
match group {
0x0 => Ok(Self::new(NcnFeeGroupType::Default)),
0x1 => Ok(Self::new(NcnFeeGroupType::JTO)),
0x2 => Ok(Self::new(NcnFeeGroupType::Reserved2)),
0x3 => Ok(Self::new(NcnFeeGroupType::Reserved3)),
0x4 => Ok(Self::new(NcnFeeGroupType::Reserved4)),
0x5 => Ok(Self::new(NcnFeeGroupType::Reserved5)),
0x6 => Ok(Self::new(NcnFeeGroupType::Reserved6)),
0x7 => Ok(Self::new(NcnFeeGroupType::Reserved7)),
// 0x8 => Ok(Self::new(NcnFeeGroupType::Reserved8)),
// 0x9 => Ok(Self::new(NcnFeeGroupType::Reserved9)),
// 0xA => Ok(Self::new(NcnFeeGroupType::ReservedA)),
// 0xB => Ok(Self::new(NcnFeeGroupType::ReservedB)),
// 0xC => Ok(Self::new(NcnFeeGroupType::ReservedC)),
// 0xD => Ok(Self::new(NcnFeeGroupType::ReservedD)),
// 0xE => Ok(Self::new(NcnFeeGroupType::ReservedE)),
// 0xF => Ok(Self::new(NcnFeeGroupType::ReservedF)),
_ => Err(TipRouterError::InvalidNcnFeeGroup),
}
}
}

impl NcnFeeGroup {
pub const FEE_GROUP_COUNT: usize = 8;

Expand All @@ -63,28 +89,6 @@ impl NcnFeeGroup {
}
}

pub const fn from_u8(group: u8) -> Result<Self, TipRouterError> {
match group {
0x0 => Ok(Self::new(NcnFeeGroupType::Default)),
0x1 => Ok(Self::new(NcnFeeGroupType::JTO)),
0x2 => Ok(Self::new(NcnFeeGroupType::Reserved2)),
0x3 => Ok(Self::new(NcnFeeGroupType::Reserved3)),
0x4 => Ok(Self::new(NcnFeeGroupType::Reserved4)),
0x5 => Ok(Self::new(NcnFeeGroupType::Reserved5)),
0x6 => Ok(Self::new(NcnFeeGroupType::Reserved6)),
0x7 => Ok(Self::new(NcnFeeGroupType::Reserved7)),
// 0x8 => Ok(Self::new(NcnFeeGroupType::Reserved8)),
// 0x9 => Ok(Self::new(NcnFeeGroupType::Reserved9)),
// 0xA => Ok(Self::new(NcnFeeGroupType::ReservedA)),
// 0xB => Ok(Self::new(NcnFeeGroupType::ReservedB)),
// 0xC => Ok(Self::new(NcnFeeGroupType::ReservedC)),
// 0xD => Ok(Self::new(NcnFeeGroupType::ReservedD)),
// 0xE => Ok(Self::new(NcnFeeGroupType::ReservedE)),
// 0xF => Ok(Self::new(NcnFeeGroupType::ReservedF)),
_ => Err(TipRouterError::InvalidNcnFeeGroup),
}
}

pub const fn group_type(&self) -> Result<NcnFeeGroupType, TipRouterError> {
match self.group {
0x0 => Ok(NcnFeeGroupType::Default),
Expand Down
Loading

0 comments on commit 66abcda

Please sign in to comment.