1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use anchor_lang::prelude::*;
use std::convert::TryInto;
use crate::{ErrorCode, MarginAccount, PriceChangeInfo, TokenConfig, TokenOracle};
#[derive(Accounts)]
pub struct RefreshDepositPosition<'info> {
#[account(mut)]
pub margin_account: AccountLoader<'info, MarginAccount>,
pub config: Account<'info, TokenConfig>,
pub price_oracle: AccountInfo<'info>,
}
pub fn refresh_deposit_position_handler(ctx: Context<RefreshDepositPosition>) -> Result<()> {
let mut margin_account = ctx.accounts.margin_account.load_mut()?;
let config = &ctx.accounts.config;
match config.oracle() {
Some(TokenOracle::Pyth { price, .. }) => {
let price_oracle_key = ctx.accounts.price_oracle.key();
if price_oracle_key != price {
msg!("expected oracle {} but got {}", price, price_oracle_key);
return err!(ErrorCode::InvalidOracle);
}
let price_feed = match pyth_sdk_solana::load_price_feed_from_account_info(
&ctx.accounts.price_oracle,
) {
Ok(pf) => pf,
Err(e) => {
msg!("the oracle account is not valid: {:?}", e);
return err!(ErrorCode::InvalidOracle);
}
};
let price_obj = price_feed.get_current_price().ok_or_else(|| {
msg!("current pyth price is invalid");
ErrorCode::InvalidOracle
})?;
let ema_obj = price_feed.get_ema_price().ok_or_else(|| {
msg!("current pyth ema price is invalid");
ErrorCode::InvalidOracle
})?;
let price_info = PriceChangeInfo {
value: price_obj.price,
confidence: price_obj.conf,
twap: ema_obj.price,
exponent: price_obj.expo,
publish_time: price_feed.publish_time,
};
let position = margin_account.get_position_mut(&config.mint).unwrap();
position.set_price(&price_info.try_into()?)?;
}
None => {
return err!(ErrorCode::InvalidOracle);
}
}
Ok(())
}