Skip to content

Commit

Permalink
need to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Dec 18, 2024
1 parent 379d441 commit 60bfc35
Show file tree
Hide file tree
Showing 18 changed files with 791 additions and 240 deletions.
530 changes: 498 additions & 32 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ solana-rpc-client-api = "~1.18"
solana-security-txt = "1.1.1"
spl-associated-token-account = { version = "2.2.0", features = ["no-entrypoint"] }
spl-token = { version = "4.0.0", features = ["no-entrypoint"] }
switchboard-on-demand = "0.1.0"
syn = "2.0.72"
thiserror = "1.0.57"
tokio = { version = "1.36.0", features = ["full"] }
Expand Down
9 changes: 1 addition & 8 deletions core/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,5 @@ pub const DEFAULT_CONSENSUS_REACHED_SLOT: u64 = u64::MAX;
pub const MAX_REALLOC_BYTES: u64 = MAX_PERMITTED_DATA_INCREASE as u64; // TODO just use this?

pub const WEIGHT_PRECISION: u128 = 1_000_000_000;
pub const DEFAULT_LST_WEIGHT: u128 = 1;

pub const DEFAULT_REWARD_MULTIPLIER_BPS: u64 = 10_000;
pub const JITO_SOL_REWARD_MULTIPLIER_BPS: u64 = 20_000;
pub const JTO_MINT: Pubkey = pubkey!("jtojtomepa8beP8AuQc6eXt5FriJwfFMwQx2v2f9mCL");
pub const JITO_SOL_MINT: Pubkey = pubkey!("J1toso1uCk3RLmjorhTtrVwY9HJ7X8V9yYac6Y7kGCPn");

pub const MAX_STALE_SLOTS: u64 = 100;
pub const SWITCHBOARD_MAX_STALE_SLOTS: u64 = 100;
pub const JTO_USD_FEED: Pubkey = pubkey!("5S7ErPSkFmyXuq2aE3rZ6ofwVyZpwzUt6w7m6kqekvMe");
12 changes: 12 additions & 0 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum TipRouterError {
CastToImpreciseNumberError,
#[error("Cast to u64 error")]
CastToU64Error,
#[error("Cast to u128 error")]
CastToU128Error,

#[error("Incorrect weight table admin")]
IncorrectWeightTableAdmin = 0x2200,
Expand Down Expand Up @@ -121,6 +123,16 @@ pub enum TipRouterError {
FeeNotActive,
#[error("No rewards to distribute")]
NoRewards,
#[error("Switchboard not registered")]
SwitchboardNotRegistered,
#[error("Bad switchboard feed")]
BadSwitchboardFeed,
#[error("Bad switchboard value")]
BadSwitchboardValue,
#[error("Stale switchboard feed")]
StaleSwitchboardFeed,
#[error("Weight entry needs either a feed or a no feed weight")]
NoFeedWeightOrSwitchboardFeed,
}

impl<T> DecodeError<T> for TipRouterError {
Expand Down
42 changes: 35 additions & 7 deletions core/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,19 @@ pub enum TipRouterInstruction {
#[account(3, name = "mint")]
#[account(4, name = "restaking_program")]
AdminSetWeight{
ncn_epoch: u64,
st_mint: Pubkey,
weight: u128,
epoch: u64,
},

// Sets the weight table for a given epoch
#[account(0, name = "ncn")]
#[account(1, writable, name = "weight_table")]
#[account(2, signer, name = "switchboard_feed")]
#[account(3, name = "st_mint")]
SwitchboardSetWeight{
st_mint: Pubkey,
epoch: u64,
},

/// Initializes the Epoch Snapshot
Expand Down Expand Up @@ -238,17 +249,34 @@ pub enum TipRouterInstruction {
epoch: u64,
},

/// Sets the NCN fee group for a mint
/// restaking_config, ncn_config, ncn, st_mint, vault_registry, admin, restaking_program
#[account(0, name = "restaking_config")]
#[account(1, name = "config")]
#[account(2, name = "ncn")]
#[account(3, name = "weight_table")]
#[account(4, writable, name = "tracked_mints")]
#[account(3, name = "st_mint")]
#[account(4, writable, name = "vault_registry")]
#[account(5, signer, writable, name = "admin")]
#[account(6, name = "restaking_program")]
AdminSetRegisteredStMint{
vault_index: u64,
AdminRegisterStMint{
ncn_fee_group: u8,
reward_multiplier_bps: u64,
switchboard_feed: Option<Pubkey>,
no_feed_weight: Option<u128>,
},

/// Sets the NCN fee group for a mint
#[account(0, name = "restaking_config")]
#[account(1, name = "config")]
#[account(2, name = "ncn")]
#[account(3, writable, name = "tracked_mints")]
#[account(4, signer, writable, name = "admin")]
#[account(5, name = "restaking_program")]
AdminSetStMint{
st_mint: Pubkey,
ncn_fee_group: Option<u8>,
reward_multiplier_bps: Option<u64>,
switchboard_feed: Option<Pubkey>,
no_feed_weight: Option<u128>,
},

/// Initializes the ballot box for an NCN
Expand Down Expand Up @@ -298,7 +326,7 @@ pub enum TipRouterInstruction {
#[account(2, name = "ncn")]
#[account(3, signer, name = "tie_breaker_admin")]
#[account(4, name = "restaking_program")]
SetTieBreaker {
AdminSetTieBreaker {
meta_merkle_root: [u8; 32],
epoch: u64,
},
Expand Down
57 changes: 47 additions & 10 deletions core/src/vault_registry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::mem::size_of;

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

Expand All @@ -19,6 +22,7 @@ pub struct StMintEntry {
ncn_fee_group: NcnFeeGroup,
reward_multiplier_bps: PodU64,
switchboard_feed: Pubkey,
no_feed_weight: PodU128,
reserved: [u8; 32],
}

Expand All @@ -28,16 +32,22 @@ impl StMintEntry {
ncn_fee_group: NcnFeeGroup,
reward_multiplier_bps: u64,
switchboard_feed: Pubkey,
no_feed_weight: u128,
) -> Self {
Self {
st_mint,
ncn_fee_group,
reward_multiplier_bps: PodU64::from(reward_multiplier_bps),
switchboard_feed,
no_feed_weight: PodU128::from(no_feed_weight),
reserved: [0; 32],
}
}

pub fn no_feed_weight(&self) -> u128 {
self.no_feed_weight.into()
}

pub fn st_mint(&self) -> Pubkey {
self.st_mint
}
Expand Down Expand Up @@ -66,6 +76,7 @@ impl Default for StMintEntry {
NcnFeeGroup::default(),
0,
Pubkey::default(),
0,
)
}
}
Expand Down Expand Up @@ -192,15 +203,26 @@ impl VaultRegistry {
self.st_mint_list.iter().any(|m| m.st_mint.eq(mint))
}

pub fn check_st_mint(&self, st_mint: &Pubkey) -> Result<(), ProgramError> {
let entry = self.get_mint_entry(st_mint)?;

if entry.no_feed_weight() == 0 && entry.switchboard_feed().eq(&Pubkey::default()) {
return Err(TipRouterError::NoFeedWeightOrSwitchboardFeed.into());
}

Ok(())
}

pub fn register_st_mint(
&mut self,
mint: Pubkey,
st_mint: &Pubkey,
ncn_fee_group: NcnFeeGroup,
reward_multiplier_bps: u64,
switchboard_feed: Pubkey,
no_feed_weight: u128,
) -> Result<(), ProgramError> {
// Check if mint is already in the list
if self.st_mint_list.iter().any(|m| m.st_mint == mint) {
if self.st_mint_list.iter().any(|m| m.st_mint.eq(st_mint)) {
return Err(TipRouterError::MintInTable.into());
}

Expand All @@ -211,26 +233,35 @@ impl VaultRegistry {
.find(|m| m.st_mint == StMintEntry::default().st_mint)
.ok_or(TipRouterError::TrackedMintListFull)?;

*mint_entry =
StMintEntry::new(mint, ncn_fee_group, reward_multiplier_bps, switchboard_feed);
*mint_entry = StMintEntry::new(
*st_mint,
ncn_fee_group,
reward_multiplier_bps,
switchboard_feed,
no_feed_weight,
);

self.check_st_mint(st_mint)?;

Ok(())
}

pub fn st_st_mint(
pub fn set_st_mint(
&mut self,
mint: Pubkey,
ncn_fee_group: Option<NcnFeeGroup>,
st_mint: &Pubkey,
ncn_fee_group: Option<u8>,
reward_multiplier_bps: Option<u64>,
switchboard_feed: Option<Pubkey>,
no_feed_weight: Option<u128>,
) -> Result<(), ProgramError> {
let mint_entry = self
.st_mint_list
.iter_mut()
.find(|m| m.st_mint == mint)
.find(|m| m.st_mint.eq(st_mint))
.ok_or(TipRouterError::MintEntryNotFound)?;

if let Some(ncn_fee_group) = ncn_fee_group {
mint_entry.ncn_fee_group = ncn_fee_group;
mint_entry.ncn_fee_group = NcnFeeGroup::try_from(ncn_fee_group)?;
}

if let Some(reward_multiplier_bps) = reward_multiplier_bps {
Expand All @@ -241,6 +272,12 @@ impl VaultRegistry {
mint_entry.switchboard_feed = switchboard_feed;
}

if let Some(no_feed_weight) = no_feed_weight {
mint_entry.no_feed_weight = PodU128::from(no_feed_weight);
}

self.check_st_mint(st_mint)?;

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/weight_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mod tests {
#[test]
fn test_weight_entry_new() {
let mint = Pubkey::new_unique();
let mint_entry = StMintEntry::new(mint, NcnFeeGroup::default(), 0, Pubkey::new_unique());
let mint_entry = StMintEntry::new(mint, NcnFeeGroup::default(), 0, Pubkey::new_unique(), 0);
let weight_entry = WeightEntry::new(mint_entry);

assert_eq!(weight_entry.st_mint(), mint);
Expand Down
7 changes: 4 additions & 3 deletions core/src/weight_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl WeightTable {
) -> Result<(), TipRouterError> {
self.table
.iter_mut()
.find(|entry| entry.st_mint() == *mint)
.find(|entry| entry.st_mint().eq(mint))
.map_or(Err(TipRouterError::InvalidMintForWeightTable), |entry| {
entry.set_weight(weight, current_slot);
Ok(())
Expand All @@ -146,7 +146,7 @@ impl WeightTable {
pub fn get_weight(&self, mint: &Pubkey) -> Result<u128, TipRouterError> {
self.table
.iter()
.find(|entry| entry.st_mint() == *mint)
.find(|entry| entry.st_mint().eq(mint))
.map_or(Err(TipRouterError::InvalidMintForWeightTable), |entry| {
Ok(entry.weight())
})
Expand All @@ -155,7 +155,7 @@ impl WeightTable {
pub fn get_weight_entry(&self, mint: &Pubkey) -> Result<&WeightEntry, TipRouterError> {
self.table
.iter()
.find(|entry| entry.st_mint() == *mint)
.find(|entry| entry.st_mint().eq(mint))
.ok_or(TipRouterError::InvalidMintForWeightTable)
}

Expand Down Expand Up @@ -260,6 +260,7 @@ mod tests {
NcnFeeGroup::default(),
0,
Pubkey::default(),
0,
)
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ solana-program = { workspace = true }
solana-security-txt = { workspace = true }
spl-associated-token-account = { workspace = true }
spl-token = { workspace = true }
switchboard-on-demand = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
Expand Down
Loading

0 comments on commit 60bfc35

Please sign in to comment.