|
| 1 | +use crate::c_oracle_header::*; |
| 2 | +use crate::error::OracleError; |
| 3 | +use borsh::BorshDeserialize; |
| 4 | +use solana_program::account_info::AccountInfo; |
| 5 | +use solana_program::entrypoint::ProgramResult; |
| 6 | +use solana_program::msg; |
| 7 | +use std::mem::size_of; |
| 8 | + |
| 9 | +pub fn pre_log(instruction_data: &[u8]) -> ProgramResult { |
| 10 | + msg!("Pyth oracle contract"); |
| 11 | + |
| 12 | + let instruction_header: cmd_hdr = cmd_hdr::try_from_slice(&instruction_data[..8])?; |
| 13 | + let instruction_id: u32 = instruction_header |
| 14 | + .cmd_ |
| 15 | + .try_into() |
| 16 | + .map_err(|_| OracleError::Generic)?; |
| 17 | + match instruction_id { |
| 18 | + command_t_e_cmd_upd_price => { |
| 19 | + let instruction: cmd_upd_price = cmd_upd_price::try_from_slice(instruction_data)?; |
| 20 | + msg!( |
| 21 | + "Update price: price={:}, conf={:}, status={:}", |
| 22 | + instruction.price_, |
| 23 | + instruction.conf_, |
| 24 | + instruction.status_ |
| 25 | + ); |
| 26 | + } |
| 27 | + command_t_e_cmd_agg_price => { |
| 28 | + let instruction: cmd_upd_price = cmd_upd_price::try_from_slice(instruction_data)?; |
| 29 | + msg!( |
| 30 | + "Update price: price={:}, conf={:}, status={:}", |
| 31 | + instruction.price_, |
| 32 | + instruction.conf_, |
| 33 | + instruction.status_ |
| 34 | + ); |
| 35 | + } |
| 36 | + command_t_e_cmd_upd_price_no_fail_on_error => { |
| 37 | + let instruction: cmd_upd_price = cmd_upd_price::try_from_slice(instruction_data)?; |
| 38 | + msg!( |
| 39 | + "Update price no fail on error: price={:}, conf={:}, status={:}", |
| 40 | + instruction.price_, |
| 41 | + instruction.conf_, |
| 42 | + instruction.status_ |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + command_t_e_cmd_add_mapping => { |
| 47 | + msg!("Add mapping"); |
| 48 | + } |
| 49 | + command_t_e_cmd_add_price => { |
| 50 | + msg!("Add price"); |
| 51 | + } |
| 52 | + command_t_e_cmd_add_product => { |
| 53 | + msg!("Add product") |
| 54 | + } |
| 55 | + command_t_e_cmd_add_publisher => { |
| 56 | + msg!("Add publisher") |
| 57 | + } |
| 58 | + command_t_e_cmd_del_publisher => { |
| 59 | + msg!("Delete publisher") |
| 60 | + } |
| 61 | + command_t_e_cmd_init_price => { |
| 62 | + msg!("Initialize price") |
| 63 | + } |
| 64 | + command_t_e_cmd_init_mapping => { |
| 65 | + msg!("Initialize mapping account"); |
| 66 | + } |
| 67 | + |
| 68 | + command_t_e_cmd_set_min_pub => { |
| 69 | + msg!("Set minimum number of publishers"); |
| 70 | + } |
| 71 | + command_t_e_cmd_upd_product => { |
| 72 | + msg!("Update product"); |
| 73 | + } |
| 74 | + _ => { |
| 75 | + msg!("Unrecognized instruction"); |
| 76 | + return Err(OracleError::Generic.into()); |
| 77 | + } |
| 78 | + } |
| 79 | + Ok(()) |
| 80 | +} |
| 81 | + |
| 82 | +pub fn post_log(c_ret_val: u64, accounts: &[AccountInfo]) -> ProgramResult { |
| 83 | + if c_ret_val == SUCCESSFULLY_UPDATED_AGGREGATE { |
| 84 | + let start: usize = PRICE_T_AGGREGATE_OFFSET |
| 85 | + .try_into() |
| 86 | + .map_err(|_| OracleError::Generic)?; |
| 87 | + // We trust that the C oracle has properly checked this account |
| 88 | + let aggregate_price_info: pc_price_info = pc_price_info::try_from_slice( |
| 89 | + &accounts |
| 90 | + .get(1) |
| 91 | + .ok_or(OracleError::Generic)? |
| 92 | + .try_borrow_data()?[start..(start + size_of::<pc_price_info>())], |
| 93 | + )?; |
| 94 | + msg!( |
| 95 | + "Aggregate updated : price={:}, conf={:}, status={:}", |
| 96 | + aggregate_price_info.price_, |
| 97 | + aggregate_price_info.conf_, |
| 98 | + aggregate_price_info.status_ |
| 99 | + ); |
| 100 | + } |
| 101 | + Ok(()) |
| 102 | +} |
0 commit comments