|
1 | 1 | use crate::c_oracle_header::*;
|
| 2 | +use crate::deserialize::{ |
| 3 | + deserialize_single_field_from_account, |
| 4 | + deserialize_single_field_from_buffer, |
| 5 | +}; |
2 | 6 | use crate::error::OracleError;
|
3 | 7 | use borsh::BorshDeserialize;
|
4 | 8 | use solana_program::account_info::AccountInfo;
|
| 9 | +use solana_program::clock::Clock; |
5 | 10 | use solana_program::entrypoint::ProgramResult;
|
6 | 11 | use solana_program::msg;
|
7 |
| -use std::mem::size_of; |
| 12 | +use solana_program::program_error::ProgramError; |
| 13 | +use solana_program::sysvar::Sysvar; |
8 | 14 |
|
9 | 15 | pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {
|
10 | 16 | msg!("Pyth oracle contract");
|
11 | 17 |
|
12 |
| - let instruction_header: cmd_hdr = cmd_hdr::try_from_slice(&instruction_data[..8])?; |
| 18 | + let instruction_header: cmd_hdr = |
| 19 | + deserialize_single_field_from_buffer::<cmd_hdr>(&instruction_data, None)?; |
13 | 20 | let instruction_id: u32 = instruction_header
|
14 | 21 | .cmd_
|
15 | 22 | .try_into()
|
16 |
| - .map_err(|_| OracleError::Generic)?; |
| 23 | + .map_err(|_| OracleError::IntegerCastingError)?; |
| 24 | + |
| 25 | + |
17 | 26 | match instruction_id {
|
18 | 27 | command_t_e_cmd_upd_price | command_t_e_cmd_agg_price => {
|
19 | 28 | let instruction: cmd_upd_price = cmd_upd_price::try_from_slice(instruction_data)?;
|
| 29 | + // Account 1 is price_info in this instruction |
| 30 | + let expo: i32 = deserialize_single_field_from_account::<i32>( |
| 31 | + accounts, |
| 32 | + 1, |
| 33 | + Some(PRICE_T_EXPO_OFFSET), |
| 34 | + )?; |
20 | 35 | msg!(
|
21 |
| - "UpdatePrice: publisher={:}, price_account={:}, price={:}, conf={:}, status={:}, slot={:}", |
| 36 | + "UpdatePrice: publisher={:}, price_account={:}, price={:}, conf={:}, expo={:}, status={:}, slot={:}, solana_time={:}", |
22 | 37 | accounts.get(0)
|
23 |
| - .ok_or(OracleError::Generic)?.key, |
| 38 | + .ok_or(ProgramError::NotEnoughAccountKeys)?.key, |
24 | 39 | accounts.get(1)
|
25 |
| - .ok_or(OracleError::Generic)?.key, |
| 40 | + .ok_or(ProgramError::NotEnoughAccountKeys)?.key, |
26 | 41 | instruction.price_,
|
27 | 42 | instruction.conf_,
|
| 43 | + expo, |
28 | 44 | instruction.status_,
|
29 |
| - instruction.pub_slot_ |
| 45 | + instruction.pub_slot_, |
| 46 | + Clock::get()?.unix_timestamp |
30 | 47 | );
|
31 | 48 | }
|
32 | 49 | command_t_e_cmd_upd_price_no_fail_on_error => {
|
33 | 50 | let instruction: cmd_upd_price = cmd_upd_price::try_from_slice(instruction_data)?;
|
| 51 | + // Account 1 is price_info in this instruction |
| 52 | + let expo: i32 = deserialize_single_field_from_account::<i32>( |
| 53 | + accounts, |
| 54 | + 1, |
| 55 | + Some(PRICE_T_EXPO_OFFSET), |
| 56 | + )?; |
34 | 57 | msg!(
|
35 |
| - "UpdatePriceNoFailOnError: publisher={:}, price_account={:}, price={:}, conf={:}, status={:}, slot={:}", |
| 58 | + "UpdatePriceNoFailOnError: publisher={:}, price_account={:}, price={:}, conf={:}, expo={:}, status={:}, slot={:}, solana_time={:}", |
36 | 59 | accounts.get(0)
|
37 |
| - .ok_or(OracleError::Generic)?.key, |
| 60 | + .ok_or(ProgramError::NotEnoughAccountKeys)?.key, |
38 | 61 | accounts.get(1)
|
39 |
| - .ok_or(OracleError::Generic)?.key, |
| 62 | + .ok_or(ProgramError::NotEnoughAccountKeys)?.key, |
40 | 63 | instruction.price_,
|
41 | 64 | instruction.conf_,
|
| 65 | + expo, |
42 | 66 | instruction.status_,
|
43 |
| - instruction.pub_slot_ |
| 67 | + instruction.pub_slot_, |
| 68 | + Clock::get()?.unix_timestamp |
44 | 69 | );
|
45 | 70 | }
|
46 | 71 | command_t_e_cmd_add_mapping => {
|
@@ -73,31 +98,37 @@ pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResu
|
73 | 98 | }
|
74 | 99 | _ => {
|
75 | 100 | msg!("UnrecognizedInstruction");
|
76 |
| - return Err(OracleError::Generic.into()); |
| 101 | + return Err(OracleError::UnrecognizedInstruction.into()); |
77 | 102 | }
|
78 | 103 | }
|
79 | 104 | Ok(())
|
80 | 105 | }
|
81 | 106 |
|
82 | 107 | pub fn post_log(c_ret_val: u64, accounts: &[AccountInfo]) -> ProgramResult {
|
83 | 108 | 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>())], |
| 109 | + // We trust that the C oracle has properly checked account 1, we can only get here through |
| 110 | + // the update price instructions |
| 111 | + let aggregate_price_info: pc_price_info = deserialize_single_field_from_account::< |
| 112 | + pc_price_info, |
| 113 | + >( |
| 114 | + accounts, 1, Some(PRICE_T_AGGREGATE_OFFSET) |
93 | 115 | )?;
|
| 116 | + let ema_info: pc_ema = |
| 117 | + deserialize_single_field_from_account::<pc_ema>(accounts, 1, Some(PRICE_T_EMA_OFFSET))?; |
| 118 | + let expo: i32 = |
| 119 | + deserialize_single_field_from_account::<i32>(accounts, 1, Some(PRICE_T_EXPO_OFFSET))?; |
| 120 | + |
94 | 121 | msg!(
|
95 |
| - "UpdateAggregate : price_account={:}, price={:}, conf={:}, status={:}, slot={:}", |
96 |
| - accounts.get(1).ok_or(OracleError::Generic)?.key, |
| 122 | + "UpdateAggregate : price_account={:}, price={:}, conf={:}, expo={:}, status={:}, slot={:}, solana_time={:}, ema={:}", |
| 123 | + accounts.get(1) |
| 124 | + .ok_or(ProgramError::NotEnoughAccountKeys)?.key, |
97 | 125 | aggregate_price_info.price_,
|
98 | 126 | aggregate_price_info.conf_,
|
| 127 | + expo, |
99 | 128 | aggregate_price_info.status_,
|
100 |
| - aggregate_price_info.pub_slot_ |
| 129 | + aggregate_price_info.pub_slot_, |
| 130 | + Clock::get()?.unix_timestamp, |
| 131 | + ema_info.val_ |
101 | 132 | );
|
102 | 133 | }
|
103 | 134 | Ok(())
|
|
0 commit comments