gmsol_store/instructions/oracle/
mod.rs1pub mod custom;
3
4use std::ops::Deref;
5
6use anchor_lang::prelude::*;
7
8use crate::{
9 states::{Chainlink, Oracle, PriceValidator, Store, TokenMapHeader, TokenMapLoader},
10 utils::internal,
11};
12
13pub use self::custom::*;
14
15#[derive(Accounts)]
19pub struct InitializeOracle<'info> {
20 pub payer: Signer<'info>,
22 pub authority: UncheckedAccount<'info>,
25 pub store: AccountLoader<'info, Store>,
27 #[account(zero)]
29 pub oracle: AccountLoader<'info, Oracle>,
30 pub system_program: Program<'info, System>,
32}
33
34pub(crate) fn initialize_oracle(ctx: Context<InitializeOracle>) -> Result<()> {
35 ctx.accounts
36 .oracle
37 .load_init()?
38 .init(ctx.accounts.store.key(), ctx.accounts.authority.key());
39 Ok(())
40}
41
42#[derive(Accounts)]
44pub struct ClearAllPrices<'info> {
45 pub authority: Signer<'info>,
47 pub store: AccountLoader<'info, Store>,
49 #[account(
51 mut,
52 has_one = store,
53 has_one = authority,
54 )]
55 pub oracle: AccountLoader<'info, Oracle>,
56}
57
58pub(crate) fn unchecked_clear_all_prices(ctx: Context<ClearAllPrices>) -> Result<()> {
61 ctx.accounts.oracle.load_mut()?.clear_all_prices();
62 Ok(())
63}
64
65impl<'info> internal::Authentication<'info> for ClearAllPrices<'info> {
66 fn authority(&self) -> &Signer<'info> {
67 &self.authority
68 }
69
70 fn store(&self) -> &AccountLoader<'info, Store> {
71 &self.store
72 }
73}
74
75#[derive(Accounts)]
81pub struct SetPricesFromPriceFeed<'info> {
82 pub authority: Signer<'info>,
84 #[account(has_one = token_map)]
86 pub store: AccountLoader<'info, Store>,
87 #[account(
89 mut,
90 has_one = store,
91 has_one = authority,
92 )]
93 pub oracle: AccountLoader<'info, Oracle>,
94 #[account(has_one = store)]
96 pub token_map: AccountLoader<'info, TokenMapHeader>,
97 pub chainlink_program: Option<Program<'info, Chainlink>>,
99}
100
101pub(crate) fn unchecked_set_prices_from_price_feed<'info>(
104 ctx: Context<'_, '_, 'info, 'info, SetPricesFromPriceFeed<'info>>,
105 tokens: Vec<Pubkey>,
106) -> Result<()> {
107 let validator = PriceValidator::try_from(ctx.accounts.store.load()?.deref())?;
108 let token_map = ctx.accounts.token_map.load_token_map()?;
109 ctx.accounts
110 .oracle
111 .load_mut()?
112 .set_prices_from_remaining_accounts(
113 validator,
114 &token_map,
115 &tokens,
116 ctx.remaining_accounts,
117 ctx.accounts.chainlink_program.as_ref(),
118 )
119}
120
121impl<'info> internal::Authentication<'info> for SetPricesFromPriceFeed<'info> {
122 fn authority(&self) -> &Signer<'info> {
123 &self.authority
124 }
125
126 fn store(&self) -> &AccountLoader<'info, Store> {
127 &self.store
128 }
129}