-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathadd_price.rs
More file actions
97 lines (87 loc) · 2.86 KB
/
add_price.rs
File metadata and controls
97 lines (87 loc) · 2.86 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
use {
super::reserve_new_price_feed_index,
crate::{
accounts::{
PriceAccount,
PriceAccountFlags,
ProductAccount,
PythAccount,
},
c_oracle_header::{
PC_PTYPE_UNKNOWN,
PRICE_ACCOUNT_DEFAULT_MIN_PUB,
},
deserialize::{
load,
load_checked,
},
instruction::AddPriceArgs,
utils::{
check_exponent_range,
check_permissioned_funding_account,
check_valid_funding_account,
check_valid_writable_account,
pyth_assert,
},
OracleError,
},
solana_program::{
account_info::AccountInfo,
entrypoint::ProgramResult,
program_error::ProgramError,
pubkey::Pubkey,
},
};
/// Add new price account to a product account
// account[0] funding account [signer writable]
// account[1] product account [writable]
// account[2] new price account [writable]
// account[3] permissions account [writable]
pub fn add_price(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
let cmd_args = load::<AddPriceArgs>(instruction_data)?;
check_exponent_range(cmd_args.exponent)?;
pyth_assert(
cmd_args.price_type != PC_PTYPE_UNKNOWN,
ProgramError::InvalidArgument,
)?;
let (funding_account, product_account, price_account, permissions_account) = match accounts {
[x, y, z, p] => Ok((x, y, z, p)),
_ => Err(OracleError::InvalidNumberOfAccounts),
}?;
check_valid_funding_account(funding_account)?;
check_permissioned_funding_account(
program_id,
product_account,
funding_account,
permissions_account,
&cmd_args.header,
)?;
check_permissioned_funding_account(
program_id,
price_account,
funding_account,
permissions_account,
&cmd_args.header,
)?;
check_valid_writable_account(program_id, permissions_account)?;
let mut product_data =
load_checked::<ProductAccount>(product_account, cmd_args.header.version)?;
let mut price_data = PriceAccount::initialize(price_account, cmd_args.header.version)?;
price_data.exponent = cmd_args.exponent;
price_data.price_type = cmd_args.price_type;
price_data.product_account = *product_account.key;
price_data.next_price_account = product_data.first_price_account;
price_data.min_pub_ = PRICE_ACCOUNT_DEFAULT_MIN_PUB;
price_data.feed_index = reserve_new_price_feed_index(permissions_account)?;
if !cfg!(feature = "no-default-accumulator-v2") {
price_data
.flags
.insert(PriceAccountFlags::ACCUMULATOR_V2 | PriceAccountFlags::MESSAGE_BUFFER_CLEARED);
}
product_data.first_price_account = *price_account.key;
Ok(())
}