gmsol_store/instructions/exchange/
update_adl.rs

1use anchor_lang::prelude::*;
2
3use crate::{
4    states::{market::utils::Adl, Chainlink, Market, Oracle, Store, TokenMapHeader},
5    utils::internal,
6};
7
8/// The accounts definition for [`update_adl_state`](crate::gmsol_store::update_adl_state).
9///
10/// *[See also the documentation for the instruction.](crate::gmsol_store::update_adl_state)*
11///
12/// Remaining accounts expected by this instruction:
13///
14///   - 0..N. `[]` N feed accounts, where N represents the total number of unique tokens
15///     in the market.
16#[derive(Accounts)]
17pub struct UpdateAdlState<'info> {
18    /// The address authorized to execute this instruction.
19    pub authority: Signer<'info>,
20    /// The store that owns the market.
21    #[account(has_one = token_map)]
22    pub store: AccountLoader<'info, Store>,
23    /// Token map.
24    #[account(has_one = store)]
25    pub token_map: AccountLoader<'info, TokenMapHeader>,
26    /// The oracle buffer to use.
27    #[account(mut, has_one = store)]
28    pub oracle: AccountLoader<'info, Oracle>,
29    /// The market to update the ADL state.
30    #[account(mut, has_one = store)]
31    pub market: AccountLoader<'info, Market>,
32    /// Chainlink Program.
33    pub chainlink_program: Option<Program<'info, Chainlink>>,
34}
35
36/// CHECK: only ORDER_KEEPER is authorized to perform this action.
37pub(crate) fn unchecked_update_adl_state<'info>(
38    ctx: Context<'_, '_, 'info, 'info, UpdateAdlState<'info>>,
39    is_long: bool,
40) -> Result<()> {
41    let mut market = ctx.accounts.market.load_mut()?;
42    let tokens = market
43        .meta()
44        .ordered_tokens()
45        .into_iter()
46        .collect::<Vec<_>>();
47
48    ctx.accounts.oracle.load_mut()?.with_prices(
49        &ctx.accounts.store,
50        &ctx.accounts.token_map,
51        &tokens,
52        ctx.remaining_accounts,
53        ctx.accounts.chainlink_program.as_ref(),
54        |oracle, _remaining_accounts| market.update_adl_state(oracle, is_long),
55    )?;
56
57    Ok(())
58}
59
60impl<'info> internal::Authentication<'info> for UpdateAdlState<'info> {
61    fn authority(&self) -> &Signer<'info> {
62        &self.authority
63    }
64
65    fn store(&self) -> &AccountLoader<'info, Store> {
66        &self.store
67    }
68}