Skip to content

Commit

Permalink
feat: Remove clock account updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrowana committed Sep 21, 2024
1 parent b1c7ce6 commit 2deb20a
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 95 deletions.
10 changes: 1 addition & 9 deletions jup_interface/src/pool_pair/one_way.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub struct OneWayPoolPair<
> {
pub withdraw: W,
pub deposit: D,
clock: Clock,
prefund_repay_params: Option<PrefundRepayParams>,
underlying_liquidities: Option<HashSet<Pubkey>>,
}
Expand All @@ -41,7 +40,6 @@ where
Self {
withdraw,
deposit,
clock: Clock::default(),
prefund_repay_params: None,
underlying_liquidities,
}
Expand Down Expand Up @@ -89,7 +87,6 @@ where
[
self.withdraw.get_accounts_to_update().as_slice(),
self.deposit.get_accounts_to_update().as_slice(),
&[sysvar::clock::ID],
PrefundRepayParams::ACCOUNTS_TO_UPDATE.as_slice(),
]
.concat()
Expand All @@ -99,11 +96,6 @@ where
// TODO: not sure if should short-circuit and early return if first update() fails
let rw = self.withdraw.update(account_map);
let rd = self.deposit.update(account_map);
let rc = account_map
.get(&sysvar::clock::ID)
.ok_or_else(|| account_missing_err(&sysvar::clock::ID))
.map_or_else(Err, |acc| Ok(bincode::deserialize(&acc.data)?))
.map(|new_clock| self.clock = new_clock);
let rp = match self.prefund_repay_params.as_mut() {
None => {
let init_res = PrefundRepayParams::try_init(account_map);
Expand All @@ -113,7 +105,7 @@ where
}
Some(p) => p.update(account_map),
};
rw.and(rd).and(rc).and(rp)
rw.and(rd).and(rp)
}

fn quote(&self, quote_params: &QuoteParams) -> Result<Quote> {
Expand Down
12 changes: 2 additions & 10 deletions jup_interface/src/pool_pair/two_way.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use jupiter_amm_interface::{
AccountMap, Amm, AmmContext, KeyedAccount, Quote, QuoteParams, Swap, SwapAndAccountMetas,
SwapParams,
};
use solana_sdk::{clock::Clock, pubkey::Pubkey, sysvar};
use solana_sdk::{pubkey::Pubkey, sysvar};
use stakedex_interface::PREFUND_SWAP_VIA_STAKE_IX_ACCOUNTS_LEN;
use stakedex_sdk_common::{
account_missing_err, find_stake_pool_pair_amm_key, spl_deposit_cap_guard_program,
Expand All @@ -23,7 +23,6 @@ pub struct TwoWayPoolPair<
> {
pub p1: P1,
pub p2: P2,
clock: Clock,
prefund_repay_params: Option<PrefundRepayParams>,
underlying_liquidities: Option<HashSet<Pubkey>>,
}
Expand All @@ -43,7 +42,6 @@ where
Self {
p1,
p2,
clock: Clock::default(),
prefund_repay_params: None,
underlying_liquidities,
}
Expand Down Expand Up @@ -84,7 +82,6 @@ where
[
self.p1.get_accounts_to_update().as_slice(),
self.p2.get_accounts_to_update().as_slice(),
&[sysvar::clock::ID],
PrefundRepayParams::ACCOUNTS_TO_UPDATE.as_slice(),
]
.concat()
Expand All @@ -94,11 +91,6 @@ where
// TODO: not sure if should short-circuit and early return if first update() fails
let r1 = self.p1.update(account_map);
let r2 = self.p2.update(account_map);
let rc = account_map
.get(&sysvar::clock::ID)
.ok_or_else(|| account_missing_err(&sysvar::clock::ID))
.map_or_else(Err, |acc| Ok(bincode::deserialize(&acc.data)?))
.map(|new_clock| self.clock = new_clock);
let rp = match self.prefund_repay_params.as_mut() {
None => {
let init_res = PrefundRepayParams::try_init(account_map);
Expand All @@ -108,7 +100,7 @@ where
}
Some(p) => p.update(account_map),
};
r1.and(r2).and(rc).and(rp)
r1.and(r2).and(rp)
}

fn quote(&self, quote_params: &QuoteParams) -> Result<Quote> {
Expand Down
14 changes: 4 additions & 10 deletions libs/lido/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::sync::{atomic::AtomicU64, Arc};

use anyhow::Result;
use lido::state::{AccountType, Lido, Validator};

mod stakedex_traits;

use solana_program::{
borsh0_10::try_from_slice_unchecked, clock::Clock, program_pack::Pack, stake_history::Epoch,
};
use solana_program::{borsh0_10::try_from_slice_unchecked, program_pack::Pack};
pub use stakedex_traits::*;

pub const LIDO_LABEL: &str = "Lido";
Expand All @@ -18,7 +18,7 @@ pub const LIST_HEADER_LEN: usize =
pub struct LidoStakedex {
lido_state: Lido,
validator_list: Vec<Validator>,
curr_epoch: Epoch,
curr_epoch: Arc<AtomicU64>,
}

impl LidoStakedex {
Expand Down Expand Up @@ -47,10 +47,4 @@ impl LidoStakedex {
self.validator_list = validator_list;
Ok(())
}

pub fn update_curr_epoch(&mut self, clock_data: &[u8]) -> Result<()> {
let clock: Clock = bincode::deserialize(clock_data)?;
self.curr_epoch = clock.epoch;
Ok(())
}
}
17 changes: 4 additions & 13 deletions libs/lido/src/stakedex_traits/base.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use jupiter_amm_interface::{AccountMap, AmmContext, KeyedAccount};
use solana_program::{pubkey::Pubkey, sysvar};
use solana_program::pubkey::Pubkey;
use stakedex_sdk_common::{
account_missing_err, lido_program, lido_state, stsol, BaseStakePoolAmm, InitFromKeyedAccount,
};
Expand All @@ -9,9 +9,10 @@ use crate::{LidoStakedex, LIDO_LABEL};

impl InitFromKeyedAccount for LidoStakedex {
/// Initialize from lido
fn from_keyed_account(keyed_account: &KeyedAccount, _amm_context: &AmmContext) -> Result<Self> {
fn from_keyed_account(keyed_account: &KeyedAccount, amm_context: &AmmContext) -> Result<Self> {
let mut res = Self::default();
res.update_lido_state(&keyed_account.account.data)?;
res.curr_epoch = amm_context.clock_ref.epoch.clone();
// NOTE: validator_list is not initialized until self.update() is
// called for the first time with fetched on-chain data
Ok(res)
Expand All @@ -36,11 +37,7 @@ impl BaseStakePoolAmm for LidoStakedex {
}

fn get_accounts_to_update(&self) -> Vec<Pubkey> {
vec![
lido_state::ID,
self.lido_state.validator_list,
sysvar::clock::ID,
]
vec![lido_state::ID, self.lido_state.validator_list]
}

fn update(&mut self, accounts_map: &AccountMap) -> Result<()> {
Expand All @@ -56,12 +53,6 @@ impl BaseStakePoolAmm for LidoStakedex {
.data
.as_ref();
self.update_validator_list(validator_list_data)?;
let clock_data = accounts_map
.get(&sysvar::clock::ID)
.ok_or_else(|| account_missing_err(&sysvar::clock::ID))?
.data
.as_ref();
self.update_curr_epoch(clock_data)?;
Ok(())
}
}
4 changes: 2 additions & 2 deletions libs/lido/src/stakedex_traits/withdraw_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use stakedex_sdk_common::{
use stakedex_withdraw_stake_interface::{
lido_withdraw_stake_ix, LidoWithdrawStakeKeys, LIDO_WITHDRAW_STAKE_IX_ACCOUNTS_LEN,
};
use std::ops::Add;
use std::{ops::Add, sync::atomic::Ordering};

use crate::LidoStakedex;

Expand Down Expand Up @@ -132,7 +132,7 @@ impl WithdrawStakeIter for LidoStakedex {

impl WithdrawStakeBase for LidoStakedex {
fn can_accept_stake_withdrawals(&self) -> bool {
self.lido_state.exchange_rate.computed_in_epoch >= self.curr_epoch
self.lido_state.exchange_rate.computed_in_epoch >= self.curr_epoch.load(Ordering::Relaxed)
}

fn virtual_ix(&self, quote: &WithdrawStakeQuote) -> Result<Instruction> {
Expand Down
10 changes: 8 additions & 2 deletions libs/lido/tests/test_mainnet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::iter::zip;

use jupiter_amm_interface::KeyedAccount;
use jupiter_amm_interface::{AmmContext, ClockRef, KeyedAccount};
use solana_client::rpc_client::RpcClient;
use solana_program::sysvar;
use stakedex_lido::LidoStakedex;
Expand All @@ -19,6 +19,12 @@ fn test_mainnet() {
params: None,
};
let accounts_map = zip(keys, accounts.into_iter().map(|o| o.unwrap())).collect();
let mut lido = LidoStakedex::from_keyed_account(&keyed_state).unwrap();
let mut lido = LidoStakedex::from_keyed_account(
&keyed_state,
&AmmContext {
clock_ref: ClockRef::default(),
},
)
.unwrap();
lido.update(&accounts_map).unwrap();
}
1 change: 1 addition & 0 deletions libs/marinade/src/stakedex_traits/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl InitFromKeyedAccount for MarinadeStakedex {
fn from_keyed_account(keyed_account: &KeyedAccount, _amm_context: &AmmContext) -> Result<Self> {
let mut res = Self::default();
res.update_state(&keyed_account.account.data)?;

// NOTE: validator_records is not initialized until self.update() is
// called for the first time with fetched on-chain data
Ok(res)
Expand Down
11 changes: 8 additions & 3 deletions libs/spl_stake_pool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::{atomic::AtomicU64, Arc};

use anyhow::{anyhow, Result};
use deposit_cap_guard::{find_spl_deposit_cap_guard_state, DepositCap};
use solana_program::{borsh0_10::try_from_slice_unchecked, pubkey::Pubkey, stake_history::Epoch};
use solana_program::{borsh0_10::try_from_slice_unchecked, pubkey::Pubkey};
use spl_stake_pool::{
error::StakePoolError,
find_deposit_authority_program_address, find_withdraw_authority_program_address,
Expand Down Expand Up @@ -28,7 +30,7 @@ pub struct SplStakePoolStakedex {
pub stake_pool_label: String,
pub stake_pool: StakePool,
pub validator_list: ValidatorList,
pub curr_epoch: Epoch,
pub curr_epoch: Arc<AtomicU64>,
pub deposit_authority_program_address: Pubkey,
pub spl_deposit_cap_guard_program_address: Pubkey,
pub deposit_cap_state: Option<DepositCap>,
Expand All @@ -40,6 +42,7 @@ impl SplStakePoolStakedex {
stake_pool_program,
stake_pool_addr,
}: SplStakePoolStakedexInitKeys,
curr_epoch: Arc<AtomicU64>,
) -> Self {
let (deposit_authority_program_address, _bump) =
find_deposit_authority_program_address(&stake_pool_program, &stake_pool_addr);
Expand All @@ -50,6 +53,7 @@ impl SplStakePoolStakedex {
stake_pool_program,
deposit_authority_program_address,
spl_deposit_cap_guard_program_address,
curr_epoch,
..Default::default()
}
}
Expand Down Expand Up @@ -79,7 +83,8 @@ impl SplStakePoolStakedex {
}

pub fn is_updated_this_epoch(&self) -> bool {
self.stake_pool.last_update_epoch >= self.curr_epoch
self.stake_pool.last_update_epoch
>= self.curr_epoch.load(std::sync::atomic::Ordering::Relaxed)
}

/// Computes and returns the stake withdraw authority PDA
Expand Down
28 changes: 10 additions & 18 deletions libs/spl_stake_pool/src/stakedex_traits/base.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use jupiter_amm_interface::{AccountMap, AmmContext, KeyedAccount};
use solana_program::{clock::Clock, pubkey::Pubkey, sysvar};
use solana_program::pubkey::Pubkey;
use stakedex_sdk_common::{account_missing_err, BaseStakePoolAmm, InitFromKeyedAccount};

use crate::SplStakePoolStakedex;
Expand All @@ -13,12 +13,15 @@ impl InitFromKeyedAccount for SplStakePoolStakedex {
account,
params,
}: &KeyedAccount,
_amm_context: &AmmContext,
amm_context: &AmmContext,
) -> Result<Self> {
let mut res = Self::new_uninitialized(crate::SplStakePoolStakedexInitKeys {
stake_pool_program: account.owner,
stake_pool_addr: *key,
});
let mut res = Self::new_uninitialized(
crate::SplStakePoolStakedexInitKeys {
stake_pool_program: account.owner,
stake_pool_addr: *key,
},
amm_context.clock_ref.epoch.clone(),
);

res.update_stake_pool(&account.data)?;

Expand Down Expand Up @@ -53,11 +56,7 @@ impl BaseStakePoolAmm for SplStakePoolStakedex {
}

fn get_accounts_to_update(&self) -> Vec<Pubkey> {
let mut res = Vec::from([
self.stake_pool_addr,
self.stake_pool.validator_list,
sysvar::clock::ID,
]);
let mut res = Vec::from([self.stake_pool_addr, self.stake_pool.validator_list]);
if self.is_sol_deposit_capped() || self.is_stake_deposit_capped() {
res.push(self.spl_deposit_cap_guard_program_address);
}
Expand All @@ -77,13 +76,6 @@ impl BaseStakePoolAmm for SplStakePoolStakedex {
.data
.as_ref();
self.update_validator_list(validator_list_data)?;
let clock_data = accounts_map
.get(&sysvar::clock::ID)
.ok_or_else(|| account_missing_err(&sysvar::clock::ID))?
.data
.as_ref();
let clock: Clock = bincode::deserialize(clock_data)?;
self.curr_epoch = clock.epoch;
if self.is_sol_deposit_capped() || self.is_stake_deposit_capped() {
let deposit_cap_data = accounts_map
.get(&self.spl_deposit_cap_guard_program_address)
Expand Down
4 changes: 3 additions & 1 deletion libs/spl_stake_pool/src/stakedex_traits/withdraw_stake.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::atomic::Ordering;

use anyhow::Result;
use solana_program::{instruction::Instruction, pubkey::Pubkey, stake, system_program, sysvar};
use spl_stake_pool::{find_stake_program_address, MINIMUM_ACTIVE_STAKE};
Expand Down Expand Up @@ -101,7 +103,7 @@ impl WithdrawStakeIter for SplStakePoolStakedex {

impl WithdrawStakeBase for SplStakePoolStakedex {
fn can_accept_stake_withdrawals(&self) -> bool {
self.stake_pool.last_update_epoch >= self.curr_epoch
self.stake_pool.last_update_epoch >= self.curr_epoch.load(Ordering::Relaxed)
}

fn virtual_ix(&self, quote: &WithdrawStakeQuote) -> Result<Instruction> {
Expand Down
Loading

0 comments on commit 2deb20a

Please sign in to comment.