Skip to content

Commit

Permalink
added set weight
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Dec 17, 2024
1 parent b3f0d11 commit 1eea24d
Showing 1 changed file with 48 additions and 21 deletions.
69 changes: 48 additions & 21 deletions program/src/set_weight.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,77 @@
use jito_bytemuck::AccountDeserialize;
use jito_jsm_core::loader::{load_signer, load_token_mint};
use jito_restaking_core::ncn::Ncn;
use jito_tip_router_core::{error::TipRouterError, weight_table::WeightTable};
use jito_tip_router_core::{
constants::{JTO_MINT, JTO_USD_FEED, MAX_STALE_SLOTS, WEIGHT_PRECISION},
error::TipRouterError,
weight_table::WeightTable,
};
use solana_program::{
account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult, msg,
program_error::ProgramError, pubkey::Pubkey, sysvar::Sysvar,
};
use switchboard_on_demand::{
prelude::rust_decimal::{prelude::ToPrimitive, Decimal},
PullFeedAccountData,
};

/// Updates weight table
pub fn process_set_weight(
pub fn process_set_jto_weight(
program_id: &Pubkey,
accounts: &[AccountInfo],
epoch: u64,
weight: u128,
) -> ProgramResult {
let [ncn, weight_table, weight_table_admin, mint, restaking_program] = accounts else {
let [ncn, weight_table, jto_usd_feed] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};

Ncn::load(restaking_program.key, ncn, false)?;
let ncn_weight_table_admin = {
let ncn_data = ncn.data.borrow();
let ncn = Ncn::try_from_slice_unchecked(&ncn_data)?;
ncn.weight_table_admin
};
Ncn::load(&jito_restaking_program::id(), ncn, false)?;

load_signer(weight_table_admin, true)?;
load_token_mint(mint)?;
WeightTable::load(program_id, weight_table, ncn, ncn_epoch, true)?;
WeightTable::load(program_id, weight_table, ncn, epoch, true)?;

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

if ncn_weight_table_admin.ne(weight_table_admin.key) {
msg!("Vault update delegations ticket is not at the correct PDA");
return Err(TipRouterError::IncorrectWeightTableAdmin.into());
}
let weight: u128 = {
let feed = PullFeedAccountData::parse(jto_usd_feed.data.borrow())
.map_err(|_| TipRouterError::BadSwitchboardFeed)?;
let price: Decimal = feed.value().ok_or(TipRouterError::BadSwitchboardValue)?;

let current_slot = Clock::get()?.slot;
let stale_slot = {
feed.result
.slot
.checked_add(MAX_STALE_SLOTS)
.ok_or(TipRouterError::ArithmeticOverflow)?
};

if current_slot > stale_slot {
msg!("Stale feed");
return Err(ProgramError::InvalidAccountData);
}

msg!("Oracle Price: {}", price);
let weight = price
.checked_mul(WEIGHT_PRECISION.into())
.ok_or(TipRouterError::ArithmeticOverflow)?
.round();

msg!("Weight: {}", weight);

weight.to_u128().ok_or(TipRouterError::CastToU128Error)?
};

let mut weight_table_data = weight_table.try_borrow_mut_data()?;
let weight_table_account = WeightTable::try_from_slice_unchecked_mut(&mut weight_table_data)?;

weight_table_account.check_initialized()?;
if weight_table_account.finalized() {
msg!("Weight table is finalized");
return Err(ProgramError::InvalidAccountData);
}

weight_table_account.set_weight(mint.key, weight, Clock::get()?.slot)?;
weight_table_account.set_weight(&JTO_MINT, weight, Clock::get()?.slot)?;

Ok(())
}

0 comments on commit 1eea24d

Please sign in to comment.