Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Nov 29, 2024
1 parent 66abcda commit df27c9a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 96 deletions.
10 changes: 10 additions & 0 deletions core/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,14 @@ pub enum TipRouterInstruction {
first_slot_of_ncn_epoch: Option<u64>,
},

#[account(0, name = "restaking_config")]
#[account(1, name = "ncn_config")]
#[account(2, name = "ncn")]
#[account(3, writable, name = "epoch_reward_router")]
#[account(4, writable, name = "destination")]
#[account(5, name = "restaking_program_id")]
DistributeDaoRewards{
first_slot_of_ncn_epoch: Option<u64>,
},

}
65 changes: 65 additions & 0 deletions program/src/distribute_dao_rewards.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use jito_bytemuck::AccountDeserialize;
use jito_restaking_core::{config::Config, ncn::Ncn};
use jito_tip_router_core::{
epoch_reward_router::EpochRewardRouter, error::TipRouterError, loaders::load_ncn_epoch,
ncn_config::NcnConfig,
};
use solana_program::{
account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult, msg,
program_error::ProgramError, pubkey::Pubkey, sysvar::Sysvar,
};

/// Initializes a Epoch Reward Router
/// Can be backfilled for previous epochs
pub fn process_distribute_dao_rewards(
program_id: &Pubkey,
accounts: &[AccountInfo],
first_slot_of_ncn_epoch: Option<u64>,
) -> ProgramResult {
let [restaking_config, ncn_config, ncn, epoch_reward_router, destination, restaking_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};

if restaking_program.key.ne(&jito_restaking_program::id()) {
msg!("Incorrect restaking program ID");
return Err(ProgramError::InvalidAccountData);
}

Config::load(restaking_program.key, restaking_config, false)?;
Ncn::load(restaking_program.key, ncn, false)?;

let current_slot = Clock::get()?.slot;
let (ncn_epoch, _) = load_ncn_epoch(restaking_config, current_slot, first_slot_of_ncn_epoch)?;

NcnConfig::load(program_id, ncn.key, ncn_config, false)?;
EpochRewardRouter::load(program_id, ncn.key, ncn_epoch, epoch_reward_router, true)?;

// Get rewards and update state
let rewards = {
let ncn_config_data = ncn_config.try_borrow_data()?;
let ncn_config_account = NcnConfig::try_from_slice_unchecked(&ncn_config_data)?;
let fee_config = ncn_config_account.fee_config;

let mut epoch_reward_router_data = epoch_reward_router.try_borrow_mut_data()?;
let epoch_reward_router_account =
EpochRewardRouter::try_from_slice_unchecked_mut(&mut epoch_reward_router_data)?;

epoch_reward_router_account.distribute_dao_rewards(&fee_config, destination.key)?
};

// Send rewards
{
**destination.lamports.borrow_mut() = destination
.lamports()
.checked_add(rewards)
.ok_or(TipRouterError::ArithmeticOverflow)?;
**epoch_reward_router.lamports.borrow_mut() = epoch_reward_router
.lamports()
.checked_sub(rewards)
.ok_or(TipRouterError::ArithmeticOverflow)?;
}

Ok(())
}
95 changes: 0 additions & 95 deletions program/src/distribute_rewards.rs

This file was deleted.

9 changes: 8 additions & 1 deletion program/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod admin_update_weight_table;
mod distribute_rewards;
mod distribute_dao_rewards;
mod initialize_epoch_reward_router;
mod initialize_epoch_snapshot;
mod initialize_ncn_config;
Expand Down Expand Up @@ -28,6 +28,7 @@ use solana_security_txt::security_txt;

use crate::{
admin_update_weight_table::process_admin_update_weight_table,
distribute_dao_rewards::process_distribute_dao_rewards,
initialize_epoch_reward_router::process_initialize_epoch_reward_router,
initialize_epoch_snapshot::process_initialize_epoch_snapshot,
initialize_ncn_config::process_initialize_ncn_config,
Expand Down Expand Up @@ -193,5 +194,11 @@ pub fn process_instruction(
first_slot_of_ncn_epoch,
)
}
TipRouterInstruction::DistributeDaoRewards {
first_slot_of_ncn_epoch,
} => {
msg!("Instruction: DistributeDAORewards");
process_distribute_dao_rewards(program_id, accounts, first_slot_of_ncn_epoch)
}
}
}

0 comments on commit df27c9a

Please sign in to comment.