11use crate :: c_oracle_header:: * ;
22use crate :: deserialize:: {
3- deserialize_single_field_from_account ,
4- deserialize_single_field_from_buffer ,
3+ load ,
4+ load_account_as ,
55} ;
66use crate :: error:: OracleError ;
7- use borsh:: BorshDeserialize ;
87use solana_program:: account_info:: AccountInfo ;
98use solana_program:: clock:: Clock ;
109use solana_program:: entrypoint:: ProgramResult ;
@@ -15,8 +14,7 @@ use solana_program::sysvar::Sysvar;
1514pub fn pre_log ( accounts : & [ AccountInfo ] , instruction_data : & [ u8 ] ) -> ProgramResult {
1615 msg ! ( "Pyth oracle contract" ) ;
1716
18- let instruction_header: cmd_hdr =
19- deserialize_single_field_from_buffer :: < cmd_hdr > ( instruction_data, None ) ?;
17+ let instruction_header: & cmd_hdr = load :: < cmd_hdr > ( instruction_data) ?;
2018 let instruction_id: u32 = instruction_header
2119 . cmd_
2220 . try_into ( )
@@ -25,13 +23,9 @@ pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResu
2523
2624 match instruction_id {
2725 command_t_e_cmd_upd_price | command_t_e_cmd_agg_price => {
28- let instruction: cmd_upd_price = cmd_upd_price :: try_from_slice ( instruction_data) ?;
26+ let instruction: & cmd_upd_price = load :: < cmd_upd_price > ( instruction_data) ?;
2927 // 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- ) ?;
28+ let price_account = load_account_as :: < pc_price_t > ( & accounts[ 1 ] ) ?;
3529 msg ! (
3630 "UpdatePrice: publisher={:}, price_account={:}, price={:}, conf={:}, expo={:}, status={:}, slot={:}, solana_time={:}" ,
3731 accounts. get( 0 )
@@ -40,20 +34,16 @@ pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResu
4034 . ok_or( ProgramError :: NotEnoughAccountKeys ) ?. key,
4135 instruction. price_,
4236 instruction. conf_,
43- expo ,
37+ price_account . expo_ ,
4438 instruction. status_,
4539 instruction. pub_slot_,
4640 Clock :: get( ) ?. unix_timestamp
4741 ) ;
4842 }
4943 command_t_e_cmd_upd_price_no_fail_on_error => {
50- let instruction: cmd_upd_price = cmd_upd_price :: try_from_slice ( instruction_data) ?;
44+ let instruction: & cmd_upd_price = load :: < cmd_upd_price > ( instruction_data) ?;
5145 // 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- ) ?;
46+ let price_account = load_account_as :: < pc_price_t > ( & accounts[ 1 ] ) ?;
5747 msg ! (
5848 "UpdatePriceNoFailOnError: publisher={:}, price_account={:}, price={:}, conf={:}, expo={:}, status={:}, slot={:}, solana_time={:}" ,
5949 accounts. get( 0 )
@@ -62,7 +52,7 @@ pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResu
6252 . ok_or( ProgramError :: NotEnoughAccountKeys ) ?. key,
6353 instruction. price_,
6454 instruction. conf_,
65- expo ,
55+ price_account . expo_ ,
6656 instruction. status_,
6757 instruction. pub_slot_,
6858 Clock :: get( ) ?. unix_timestamp
@@ -104,31 +94,23 @@ pub fn pre_log(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResu
10494 Ok ( ( ) )
10595}
10696
97+
10798pub fn post_log ( c_ret_val : u64 , accounts : & [ AccountInfo ] ) -> ProgramResult {
10899 if c_ret_val == SUCCESSFULLY_UPDATED_AGGREGATE {
109100 // We trust that the C oracle has properly checked account 1, we can only get here through
110101 // 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 )
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-
102+ let price_account = load_account_as :: < pc_price_t > ( & accounts[ 1 ] ) ?;
121103 msg ! (
122104 "UpdateAggregate : price_account={:}, price={:}, conf={:}, expo={:}, status={:}, slot={:}, solana_time={:}, ema={:}" ,
123105 accounts. get( 1 )
124106 . ok_or( ProgramError :: NotEnoughAccountKeys ) ?. key,
125- aggregate_price_info . price_,
126- aggregate_price_info . conf_,
127- expo ,
128- aggregate_price_info . status_,
129- aggregate_price_info . pub_slot_,
107+ price_account . agg_ . price_,
108+ price_account . agg_ . conf_,
109+ price_account . expo_ ,
110+ price_account . agg_ . status_,
111+ price_account . agg_ . pub_slot_,
130112 Clock :: get( ) ?. unix_timestamp,
131- ema_info . val_
113+ price_account . twap_ . val_
132114 ) ;
133115 }
134116 Ok ( ( ) )
0 commit comments