Skip to content

Commit 1f70026

Browse files
authored
transfer oracle data ix to constituent (#1643)
* transfer oracle data ix to constituent * add lib entrypoint * simplify more * add spot market constraint
1 parent ee50a60 commit 1f70026

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

programs/drift/src/instructions/lp_pool.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ use crate::{
2020
ConstituentTargetBaseFixed, LPPool, TargetsDatum, WeightValidationFlags,
2121
},
2222
oracle::OraclePriceData,
23+
oracle_map::OracleMap,
2324
perp_market::{AmmCacheFixed, CacheInfo, AMM_POSITIONS_CACHE},
2425
perp_market_map::MarketSet,
26+
spot_market::SpotMarket,
2527
spot_market_map::get_writable_spot_market_set_from_many,
2628
state::State,
2729
user::MarketType,
@@ -912,6 +914,46 @@ pub fn handle_lp_pool_remove_liquidity<'c: 'info, 'info>(
912914
Ok(())
913915
}
914916

917+
pub fn handle_update_constituent_oracle_info<'c: 'info, 'info>(
918+
ctx: Context<'_, '_, 'c, 'info, UpdateConstituentOracleInfo<'info>>,
919+
) -> Result<()> {
920+
let clock = Clock::get()?;
921+
let mut constituent = ctx.accounts.constituent.load_mut()?;
922+
let spot_market = ctx.accounts.spot_market.load()?;
923+
924+
let oracle_id = spot_market.oracle_id();
925+
let mut oracle_map = OracleMap::load_one(
926+
&ctx.accounts.oracle,
927+
clock.slot,
928+
Some(ctx.accounts.state.oracle_guard_rails),
929+
)?;
930+
931+
let oracle_data = oracle_map.get_price_data(&oracle_id)?;
932+
let oracle_data_slot = clock.slot - oracle_data.delay.max(0i64).cast::<u64>()?;
933+
if constituent.last_oracle_slot < oracle_data_slot {
934+
constituent.last_oracle_price = oracle_data.price;
935+
constituent.last_oracle_slot = oracle_data_slot;
936+
}
937+
938+
Ok(())
939+
}
940+
941+
#[derive(Accounts)]
942+
pub struct UpdateConstituentOracleInfo<'info> {
943+
pub state: Box<Account<'info, State>>,
944+
#[account(mut)]
945+
pub keeper: Signer<'info>,
946+
#[account(mut)]
947+
pub constituent: AccountLoader<'info, Constituent>,
948+
#[account(
949+
owner = crate::ID,
950+
constraint = spot_market.load()?.market_index == constituent.load()?.spot_market_index
951+
)]
952+
pub spot_market: AccountLoader<'info, SpotMarket>,
953+
/// CHECK: checked when loading oracle in oracle map
954+
pub oracle: AccountInfo<'info>,
955+
}
956+
915957
#[derive(Accounts)]
916958
pub struct UpdateConstituentTargetBase<'info> {
917959
pub state: Box<Account<'info, State>>,

programs/drift/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,12 @@ pub mod drift {
18591859
) -> Result<()> {
18601860
handle_end_lp_swap(ctx)
18611861
}
1862+
1863+
pub fn update_constituent_oracle_info<'c: 'info, 'info>(
1864+
ctx: Context<'_, '_, 'c, 'info, UpdateConstituentOracleInfo<'info>>,
1865+
) -> Result<()> {
1866+
handle_update_constituent_oracle_info(ctx)
1867+
}
18621868
}
18631869

18641870
#[cfg(not(feature = "no-entrypoint"))]

0 commit comments

Comments
 (0)