-
Notifications
You must be signed in to change notification settings - Fork 116
Implement init_price instruction #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,7 +93,6 @@ unsafe impl Pod for cmd_hdr { | |
} | ||
|
||
#[cfg(target_endian = "little")] | ||
|
||
unsafe impl Zeroable for pc_price_info { | ||
} | ||
|
||
|
@@ -117,13 +116,22 @@ unsafe impl Zeroable for pc_ema { | |
unsafe impl Pod for pc_ema { | ||
} | ||
|
||
#[cfg(target_endian = "little")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was missing before for some reason |
||
unsafe impl Zeroable for cmd_add_price_t { | ||
} | ||
|
||
#[cfg(target_endian = "little")] | ||
unsafe impl Pod for cmd_add_price_t { | ||
} | ||
|
||
#[cfg(target_endian = "little")] | ||
unsafe impl Zeroable for cmd_init_price_t { | ||
} | ||
|
||
#[cfg(target_endian = "little")] | ||
unsafe impl Pod for cmd_init_price_t { | ||
} | ||
|
||
#[cfg(target_endian = "little")] | ||
unsafe impl Zeroable for cmd_add_publisher_t { | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,11 +24,14 @@ use crate::c_oracle_header::{ | |
cmd_add_publisher_t, | ||
cmd_del_publisher_t, | ||
cmd_hdr_t, | ||
cmd_init_price_t, | ||
cmd_set_min_pub_t, | ||
cmd_upd_product_t, | ||
pc_acc, | ||
pc_ema_t, | ||
pc_map_table_t, | ||
pc_price_comp, | ||
pc_price_info_t, | ||
pc_price_t, | ||
pc_prod_t, | ||
pc_pub_key_t, | ||
|
@@ -141,12 +144,11 @@ pub fn add_price( | |
) -> OracleResult { | ||
let cmd_args = load::<cmd_add_price_t>(instruction_data)?; | ||
|
||
if cmd_args.expo_ > PC_MAX_NUM_DECIMALS as i32 | ||
|| cmd_args.expo_ < -(PC_MAX_NUM_DECIMALS as i32) | ||
|| cmd_args.ptype_ == PC_PTYPE_UNKNOWN | ||
{ | ||
return Err(ProgramError::InvalidArgument); | ||
} | ||
check_exponent_range(cmd_args.expo_)?; | ||
pyth_assert( | ||
cmd_args.ptype_ != PC_PTYPE_UNKNOWN, | ||
ProgramError::InvalidArgument, | ||
)?; | ||
|
||
let [funding_account, product_account, price_account] = match accounts { | ||
[x, y, z] => Ok([x, y, z]), | ||
|
@@ -170,6 +172,69 @@ pub fn add_price( | |
Ok(SUCCESS) | ||
} | ||
|
||
pub fn init_price( | ||
program_id: &Pubkey, | ||
accounts: &[AccountInfo], | ||
instruction_data: &[u8], | ||
) -> OracleResult { | ||
let cmd_args = load::<cmd_init_price_t>(instruction_data)?; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just flagging the differences with C, I think this is acceptable. |
||
check_exponent_range(cmd_args.expo_)?; | ||
|
||
let [funding_account, price_account] = match accounts { | ||
[x, y] => Ok([x, y]), | ||
_ => Err(ProgramError::InvalidArgument), | ||
}?; | ||
|
||
check_valid_funding_account(funding_account)?; | ||
check_valid_signable_account(program_id, price_account, size_of::<pc_price_t>())?; | ||
|
||
let mut price_data = load_checked::<pc_price_t>(price_account, cmd_args.ver_)?; | ||
pyth_assert( | ||
price_data.ptype_ == cmd_args.ptype_, | ||
ProgramError::InvalidArgument, | ||
)?; | ||
|
||
price_data.expo_ = cmd_args.expo_; | ||
|
||
price_data.last_slot_ = 0; | ||
price_data.valid_slot_ = 0; | ||
price_data.agg_.pub_slot_ = 0; | ||
price_data.prev_slot_ = 0; | ||
price_data.prev_price_ = 0; | ||
price_data.prev_conf_ = 0; | ||
price_data.prev_timestamp_ = 0; | ||
sol_memset( | ||
bytes_of_mut(&mut price_data.twap_), | ||
0, | ||
size_of::<pc_ema_t>(), | ||
); | ||
sol_memset( | ||
bytes_of_mut(&mut price_data.twac_), | ||
0, | ||
size_of::<pc_ema_t>(), | ||
); | ||
sol_memset( | ||
bytes_of_mut(&mut price_data.agg_), | ||
0, | ||
size_of::<pc_price_info_t>(), | ||
); | ||
for i in 0..(price_data.comp_.len() as usize) { | ||
guibescos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sol_memset( | ||
bytes_of_mut(&mut price_data.comp_[i].agg_), | ||
0, | ||
size_of::<pc_price_info_t>(), | ||
); | ||
sol_memset( | ||
bytes_of_mut(&mut price_data.comp_[i].latest_), | ||
0, | ||
size_of::<pc_price_info_t>(), | ||
); | ||
} | ||
|
||
Ok(SUCCESS) | ||
} | ||
|
||
/// add a publisher to a price account | ||
/// accounts[0] funding account [signer writable] | ||
/// accounts[1] price account to add the publisher to [signer writable] | ||
|
@@ -445,6 +510,14 @@ fn check_valid_fresh_account(account: &AccountInfo) -> Result<(), ProgramError> | |
) | ||
} | ||
|
||
// Check that an exponent is within the range of permitted exponents for price accounts. | ||
fn check_exponent_range(expo: i32) -> Result<(), ProgramError> { | ||
pyth_assert( | ||
expo >= -(PC_MAX_NUM_DECIMALS as i32) && expo <= PC_MAX_NUM_DECIMALS as i32, | ||
ProgramError::InvalidArgument, | ||
) | ||
} | ||
|
||
/// Sets the data of account to all-zero | ||
pub fn clear_account(account: &AccountInfo) -> Result<(), ProgramError> { | ||
let mut data = account | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no longer used, and the c build fails if you have unused functions