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