Skip to content

Commit

Permalink
all functions running
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Dec 9, 2024
1 parent c22377e commit 15becfe
Show file tree
Hide file tree
Showing 16 changed files with 527 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type DistributeNcnOperatorRewardsInstruction<
: TAccountNcnConfig,
TAccountNcn extends string ? ReadonlyAccount<TAccountNcn> : TAccountNcn,
TAccountOperator extends string
? ReadonlyAccount<TAccountOperator>
? WritableAccount<TAccountOperator>
: TAccountOperator,
TAccountNcnRewardRouter extends string
? WritableAccount<TAccountNcnRewardRouter>
Expand Down Expand Up @@ -172,7 +172,7 @@ export function getDistributeNcnOperatorRewardsInstruction<
},
ncnConfig: { value: input.ncnConfig ?? null, isWritable: false },
ncn: { value: input.ncn ?? null, isWritable: false },
operator: { value: input.operator ?? null, isWritable: false },
operator: { value: input.operator ?? null, isWritable: true },
ncnRewardRouter: { value: input.ncnRewardRouter ?? null, isWritable: true },
restakingProgram: {
value: input.restakingProgram ?? null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export type DistributeNcnVaultRewardsInstruction<
? ReadonlyAccount<TAccountOperator>
: TAccountOperator,
TAccountVault extends string
? ReadonlyAccount<TAccountVault>
? WritableAccount<TAccountVault>
: TAccountVault,
TAccountNcnRewardRouter extends string
? WritableAccount<TAccountNcnRewardRouter>
Expand Down Expand Up @@ -191,7 +191,7 @@ export function getDistributeNcnVaultRewardsInstruction<
ncnConfig: { value: input.ncnConfig ?? null, isWritable: false },
ncn: { value: input.ncn ?? null, isWritable: false },
operator: { value: input.operator ?? null, isWritable: false },
vault: { value: input.vault ?? null, isWritable: false },
vault: { value: input.vault ?? null, isWritable: true },
ncnRewardRouter: { value: input.ncnRewardRouter ?? null, isWritable: true },
restakingProgram: {
value: input.restakingProgram ?? null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl DistributeNcnOperatorRewards {
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.ncn, false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
accounts.push(solana_program::instruction::AccountMeta::new(
self.operator,
false,
));
Expand Down Expand Up @@ -104,7 +104,7 @@ pub struct DistributeNcnOperatorRewardsInstructionArgs {
/// 0. `[]` restaking_config
/// 1. `[]` ncn_config
/// 2. `[]` ncn
/// 3. `[]` operator
/// 3. `[writable]` operator
/// 4. `[writable]` ncn_reward_router
/// 5. `[]` restaking_program
#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -316,7 +316,7 @@ impl<'a, 'b> DistributeNcnOperatorRewardsCpi<'a, 'b> {
*self.ncn.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
accounts.push(solana_program::instruction::AccountMeta::new(
*self.operator.key,
false,
));
Expand Down Expand Up @@ -373,7 +373,7 @@ impl<'a, 'b> DistributeNcnOperatorRewardsCpi<'a, 'b> {
/// 0. `[]` restaking_config
/// 1. `[]` ncn_config
/// 2. `[]` ncn
/// 3. `[]` operator
/// 3. `[writable]` operator
/// 4. `[writable]` ncn_reward_router
/// 5. `[]` restaking_program
#[derive(Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl DistributeNcnVaultRewards {
self.operator,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
accounts.push(solana_program::instruction::AccountMeta::new(
self.vault, false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
Expand Down Expand Up @@ -116,7 +116,7 @@ pub struct DistributeNcnVaultRewardsInstructionArgs {
/// 1. `[]` ncn_config
/// 2. `[]` ncn
/// 3. `[]` operator
/// 4. `[]` vault
/// 4. `[writable]` vault
/// 5. `[writable]` ncn_reward_router
/// 6. `[]` restaking_program
/// 7. `[]` vault_program
Expand Down Expand Up @@ -357,7 +357,7 @@ impl<'a, 'b> DistributeNcnVaultRewardsCpi<'a, 'b> {
*self.operator.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
accounts.push(solana_program::instruction::AccountMeta::new(
*self.vault.key,
false,
));
Expand Down Expand Up @@ -421,7 +421,7 @@ impl<'a, 'b> DistributeNcnVaultRewardsCpi<'a, 'b> {
/// 1. `[]` ncn_config
/// 2. `[]` ncn
/// 3. `[]` operator
/// 4. `[]` vault
/// 4. `[writable]` vault
/// 5. `[writable]` ncn_reward_router
/// 6. `[]` restaking_program
/// 7. `[]` vault_program
Expand Down
56 changes: 43 additions & 13 deletions core/src/base_reward_router.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use core::mem::size_of;

use bytemuck::{Pod, Zeroable};
use jito_bytemuck::{types::PodU64, AccountDeserialize, Discriminator};
use shank::{ShankAccount, ShankType};
use solana_program::{account_info::AccountInfo, msg, program_error::ProgramError, pubkey::Pubkey};
use solana_program::{
account_info::AccountInfo, msg, program_error::ProgramError, pubkey::Pubkey, rent::Rent,
};
use spl_math::precise_number::PreciseNumber;

use crate::{
Expand Down Expand Up @@ -116,14 +120,22 @@ impl BaseRewardRouter {
}

// ----------------- ROUTE REWARDS ---------------------
pub fn route_incoming_rewards(&mut self, account_balance: u64) -> Result<(), TipRouterError> {
pub fn route_incoming_rewards(
&mut self,
rent_cost: u64,
account_balance: u64,
) -> Result<(), TipRouterError> {
let total_rewards = self.total_rewards_in_transit()?;

let incoming_rewards = account_balance
.checked_sub(total_rewards)
.ok_or(TipRouterError::ArithmeticUnderflowError)?;

self.route_to_reward_pool(incoming_rewards)?;
let rewards_to_route = incoming_rewards
.checked_sub(rent_cost)
.ok_or(TipRouterError::ArithmeticUnderflowError)?;

self.route_to_reward_pool(rewards_to_route)?;

Ok(())
}
Expand Down Expand Up @@ -290,6 +302,14 @@ impl BaseRewardRouter {
Ok(total_rewards)
}

pub fn rent_cost(&self, rent: &Rent) -> Result<u64, TipRouterError> {
let size = 8_u64
.checked_add(size_of::<Self>() as u64)
.ok_or(TipRouterError::ArithmeticOverflow)?;

Ok(rent.minimum_balance(size as usize))
}

pub fn total_rewards(&self) -> u64 {
self.total_rewards.into()
}
Expand Down Expand Up @@ -364,7 +384,7 @@ impl BaseRewardRouter {

// ------------------ BASE FEE GROUP REWARDS ---------------------

pub fn base_fee_group_rewards(&self, group: BaseFeeGroup) -> Result<u64, TipRouterError> {
pub fn base_fee_group_reward(&self, group: BaseFeeGroup) -> Result<u64, TipRouterError> {
let group_index = group.group_index()?;
Ok(self.base_fee_group_rewards[group_index].rewards())
}
Expand All @@ -380,7 +400,7 @@ impl BaseRewardRouter {

let group_index = group.group_index()?;
self.base_fee_group_rewards[group_index].rewards = PodU64::from(
self.base_fee_group_rewards(group)?
self.base_fee_group_reward(group)?
.checked_add(rewards)
.ok_or(TipRouterError::ArithmeticOverflow)?,
);
Expand All @@ -396,7 +416,7 @@ impl BaseRewardRouter {
) -> Result<u64, TipRouterError> {
let group_index = group.group_index()?;

let rewards = self.base_fee_group_rewards(group)?;
let rewards = self.base_fee_group_reward(group)?;
self.base_fee_group_rewards[group_index].rewards = PodU64::from(
rewards
.checked_sub(rewards)
Expand Down Expand Up @@ -457,6 +477,16 @@ impl BaseRewardRouter {

// ------------------ NCN REWARD ROUTES ---------------------

pub fn has_operator_reward_route(&self, operator: &Pubkey) -> bool {
for ncn_route_reward in self.ncn_fee_group_reward_routes.iter() {
if ncn_route_reward.operator.eq(operator) {
return true;
}
}

false
}

pub fn ncn_fee_group_reward_route(
&self,
operator: &Pubkey,
Expand Down Expand Up @@ -633,7 +663,7 @@ mod tests {

// Test routing 1000 lamports
let account_balance = 1000;
router.route_incoming_rewards(account_balance).unwrap();
router.route_incoming_rewards(0, account_balance).unwrap();

// Verify rewards were routed correctly
assert_eq!(router.total_rewards(), 1000);
Expand All @@ -642,15 +672,15 @@ mod tests {

// Test routing additional 500 lamports
let account_balance = 1500;
router.route_incoming_rewards(account_balance).unwrap();
router.route_incoming_rewards(0, account_balance).unwrap();

// Verify total rewards increased by difference
assert_eq!(router.total_rewards(), 1500);
assert_eq!(router.reward_pool(), 1500);
assert_eq!(router.rewards_processed(), 0);

// Test attempting to route with lower balance (should fail)
let result = router.route_incoming_rewards(1000);
let result = router.route_incoming_rewards(0, 1000);
assert!(result.is_err());

// Verify state didn't change after failed routing
Expand Down Expand Up @@ -678,7 +708,7 @@ mod tests {
let fees = Fees::new(900, 100, 1).unwrap();

// Route incoming rewards
router.route_incoming_rewards(INCOMING_REWARDS).unwrap();
router.route_incoming_rewards(0, INCOMING_REWARDS).unwrap();

assert_eq!(router.total_rewards(), INCOMING_REWARDS);
assert_eq!(router.reward_pool(), INCOMING_REWARDS);
Expand All @@ -687,7 +717,7 @@ mod tests {

assert_eq!(router.total_rewards(), INCOMING_REWARDS);
assert_eq!(router.reward_pool(), 0);
assert_eq!(router.base_fee_group_rewards(base_group).unwrap(), 900);
assert_eq!(router.base_fee_group_reward(base_group).unwrap(), 900);
assert_eq!(router.ncn_fee_group_rewards(ncn_group).unwrap(), 100);
}

Expand All @@ -714,7 +744,7 @@ mod tests {
}

// Route incoming rewards
router.route_incoming_rewards(INCOMING_REWARDS).unwrap();
router.route_incoming_rewards(0, INCOMING_REWARDS).unwrap();

assert_eq!(router.total_rewards(), INCOMING_REWARDS);
assert_eq!(router.reward_pool(), INCOMING_REWARDS);
Expand All @@ -725,7 +755,7 @@ mod tests {
assert_eq!(router.reward_pool(), 0);

for group in BaseFeeGroup::all_groups().iter() {
assert_eq!(router.base_fee_group_rewards(*group).unwrap(), 100);
assert_eq!(router.base_fee_group_reward(*group).unwrap(), 100);
}

for group in NcnFeeGroup::all_groups().iter() {
Expand Down
4 changes: 2 additions & 2 deletions core/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub enum TipRouterInstruction {
#[account(0, name = "restaking_config")]
#[account(1, name = "ncn_config")]
#[account(2, name = "ncn")]
#[account(3, name = "operator")]
#[account(3, writable, name = "operator")]
#[account(4, writable, name = "ncn_reward_router")]
#[account(5, name = "restaking_program")]
DistributeNcnOperatorRewards{
Expand All @@ -236,7 +236,7 @@ pub enum TipRouterInstruction {
#[account(1, name = "ncn_config")]
#[account(2, name = "ncn")]
#[account(3, name = "operator")]
#[account(4, name = "vault")]
#[account(4, writable, name = "vault")]
#[account(5, writable, name = "ncn_reward_router")]
#[account(6, name = "restaking_program")]
#[account(7, name = "vault_program")]
Expand Down
37 changes: 31 additions & 6 deletions core/src/ncn_reward_router.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use core::mem::size_of;

use bytemuck::{Pod, Zeroable};
use jito_bytemuck::{types::PodU64, AccountDeserialize, Discriminator};
use jito_vault_core::MAX_BPS;
use shank::{ShankAccount, ShankType};
use solana_program::{account_info::AccountInfo, msg, program_error::ProgramError, pubkey::Pubkey};
use solana_program::{
account_info::AccountInfo, msg, program_error::ProgramError, pubkey::Pubkey, rent::Rent,
};
use spl_math::precise_number::PreciseNumber;

use crate::{
Expand Down Expand Up @@ -142,14 +146,22 @@ impl NcnRewardRouter {
}

// ------------------------ ROUTING ------------------------
pub fn route_incoming_rewards(&mut self, account_balance: u64) -> Result<(), TipRouterError> {
pub fn route_incoming_rewards(
&mut self,
rent_cost: u64,
account_balance: u64,
) -> Result<(), TipRouterError> {
let total_rewards = self.total_rewards_in_transit()?;

let incoming_rewards = account_balance
.checked_sub(total_rewards)
.ok_or(TipRouterError::ArithmeticUnderflowError)?;

self.route_to_reward_pool(incoming_rewards)?;
let rewards_to_route = incoming_rewards
.checked_sub(rent_cost)
.ok_or(TipRouterError::ArithmeticUnderflowError)?;

self.route_to_reward_pool(rewards_to_route)?;

Ok(())
}
Expand Down Expand Up @@ -294,6 +306,14 @@ impl NcnRewardRouter {
Ok(total_rewards)
}

pub fn rent_cost(&self, rent: &Rent) -> Result<u64, TipRouterError> {
let size = 8_u64
.checked_add(size_of::<Self>() as u64)
.ok_or(TipRouterError::ArithmeticOverflow)?;

Ok(rent.minimum_balance(size as usize))
}

pub fn total_rewards(&self) -> u64 {
self.total_rewards.into()
}
Expand Down Expand Up @@ -377,6 +397,8 @@ impl NcnRewardRouter {
return Ok(());
}

self.increment_rewards_processed(rewards)?;

self.operator_rewards = PodU64::from(
self.operator_rewards()
.checked_add(rewards)
Expand Down Expand Up @@ -418,6 +440,8 @@ impl NcnRewardRouter {
return Ok(());
}

self.increment_rewards_processed(rewards)?;

for vault_reward in self.vault_reward_routes.iter_mut() {
if vault_reward.vault().eq(&vault) {
vault_reward.increment_rewards(rewards)?;
Expand All @@ -439,6 +463,7 @@ impl NcnRewardRouter {
for route in self.vault_reward_routes.iter_mut() {
if route.vault().eq(vault) {
let rewards = route.rewards();

route.decrement_rewards(rewards)?;
self.decrement_rewards_processed(rewards)?;
return Ok(rewards);
Expand Down Expand Up @@ -521,7 +546,7 @@ mod tests {

// Test routing 1000 lamports
let account_balance = 1000;
router.route_incoming_rewards(account_balance).unwrap();
router.route_incoming_rewards(0, account_balance).unwrap();

// Verify rewards were routed correctly
assert_eq!(router.total_rewards(), 1000);
Expand All @@ -530,15 +555,15 @@ mod tests {

// Test routing additional 500 lamports
let account_balance = 1500;
router.route_incoming_rewards(account_balance).unwrap();
router.route_incoming_rewards(0, account_balance).unwrap();

// Verify total rewards increased by difference
assert_eq!(router.total_rewards(), 1500);
assert_eq!(router.reward_pool(), 1500);
assert_eq!(router.rewards_processed(), 0);

// Test attempting to route with lower balance (should fail)
let result = router.route_incoming_rewards(1000);
let result = router.route_incoming_rewards(0, 1000);
assert!(result.is_err());

// Verify state didn't change after failed routing
Expand Down
Loading

0 comments on commit 15becfe

Please sign in to comment.