@@ -57,13 +57,13 @@ pub struct PythReceiver {
57
57
58
58
#[ public]
59
59
impl PythReceiver {
60
- pub fn initialize ( & mut self , _wormhole : Address , _single_update_fee_in_wei : U256 , _valid_time_period_seconds : U256 ,
60
+ pub fn initialize ( & mut self , wormhole : Address , single_update_fee_in_wei : U256 , valid_time_period_seconds : U256 ,
61
61
data_source_emitter_chain_ids : Vec < u16 > , data_source_emitter_addresses : Vec < [ u8 ; 32 ] > ,
62
62
governance_emitter_chain_id : u16 , governance_emitter_address : [ u8 ; 32 ] ,
63
- governance_initial_sequence : u64 , _data : Vec < u8 > ) {
64
- self . wormhole . set ( _wormhole ) ;
65
- self . single_update_fee_in_wei . set ( _single_update_fee_in_wei ) ;
66
- self . valid_time_period_seconds . set ( _valid_time_period_seconds ) ;
63
+ governance_initial_sequence : u64 , data : Vec < u8 > ) {
64
+ self . wormhole . set ( wormhole ) ;
65
+ self . single_update_fee_in_wei . set ( single_update_fee_in_wei ) ;
66
+ self . valid_time_period_seconds . set ( valid_time_period_seconds ) ;
67
67
68
68
self . governance_data_source_chain_id . set ( U16 :: from ( governance_emitter_chain_id) ) ;
69
69
self . governance_data_source_emitter_address . set ( FixedBytes :: < 32 > :: from ( governance_emitter_address) ) ;
@@ -92,8 +92,8 @@ impl PythReceiver {
92
92
}
93
93
}
94
94
95
- pub fn get_price_unsafe ( & self , _id : [ u8 ; 32 ] ) -> Result < PriceInfoReturn , PythReceiverError > {
96
- let id_fb = FixedBytes :: < 32 > :: from ( _id ) ;
95
+ pub fn get_price_unsafe ( & self , id : [ u8 ; 32 ] ) -> Result < PriceInfoReturn , PythReceiverError > {
96
+ let id_fb = FixedBytes :: < 32 > :: from ( id ) ;
97
97
98
98
let price_info = self . latest_price_info . get ( id_fb) ;
99
99
@@ -111,10 +111,10 @@ impl PythReceiver {
111
111
) )
112
112
}
113
113
114
- pub fn get_price_no_older_than ( & self , _id : [ u8 ; 32 ] , _age : u64 ) -> Result < PriceInfoReturn , PythReceiverError > {
115
- let price_info = self . get_price_unsafe ( _id ) ?;
116
- if !self . is_no_older_than ( price_info. 0 , _age ) {
117
- return Err ( PythReceiverError :: PriceUnavailable ) ;
114
+ pub fn get_price_no_older_than ( & self , id : [ u8 ; 32 ] , age : u64 ) -> Result < PriceInfoReturn , PythReceiverError > {
115
+ let price_info = self . get_price_unsafe ( id ) ?;
116
+ if !self . is_no_older_than ( price_info. 0 , age ) {
117
+ return Err ( PythReceiverError :: NewPriceUnavailable ) ;
118
118
}
119
119
Ok ( price_info)
120
120
}
@@ -144,14 +144,14 @@ impl PythReceiver {
144
144
let update_data_array: & [ u8 ] = & update_data;
145
145
// Check the first 4 bytes of the update_data_array for the magic header
146
146
if update_data_array. len ( ) < 4 {
147
- panic ! ( "update_data too short for magic header check" ) ;
147
+ return Err ( PythReceiverError :: InvalidUpdateData ) ;
148
148
}
149
149
150
150
let mut header = [ 0u8 ; 4 ] ;
151
151
header. copy_from_slice ( & update_data_array[ 0 ..4 ] ) ;
152
152
153
153
if & header != PYTHNET_ACCUMULATOR_UPDATE_MAGIC {
154
- panic ! ( "Invalid update_data magic header" ) ;
154
+ return Err ( PythReceiverError :: InvalidAccumulatorMessage ) ;
155
155
}
156
156
157
157
let update_data = AccumulatorUpdateData :: try_from_slice ( & update_data_array) . unwrap ( ) ;
@@ -160,32 +160,25 @@ impl PythReceiver {
160
160
Proof :: WormholeMerkle { vaa, updates } => {
161
161
let wormhole: IWormholeContract = IWormholeContract :: new ( self . wormhole . get ( ) ) ;
162
162
let config = Call :: new ( ) ;
163
- let parsed_vaa = wormhole. parse_and_verify_vm ( config, Vec :: from ( vaa) ) . map_err ( |_| PythReceiverError :: PriceUnavailable ) . unwrap ( ) ;
163
+ let parsed_vaa = wormhole. parse_and_verify_vm ( config, Vec :: from ( vaa) ) . map_err ( |_| PythReceiverError :: InvalidWormholeMessage ) . unwrap ( ) ;
164
164
let vaa = Vaa :: read ( & mut parsed_vaa. as_slice ( ) ) . unwrap ( ) ;
165
165
166
166
// TODO: CHECK IF THE VAA IS FROM A VALID DATA SOURCE
167
167
168
168
let root_digest: MerkleRoot < Keccak160 > = parse_wormhole_proof ( vaa) . unwrap ( ) ;
169
169
170
170
for update in updates {
171
- // fill in update processing logic.
172
- // update is a merkle price update
173
-
174
- // pub struct MerklePriceUpdate {
175
- // pub message: PrefixedVec<u16, u8>,
176
- // pub proof: MerklePath<Keccak160>,
177
- // }
178
171
179
172
let message_vec = Vec :: from ( update. message ) ;
180
173
let proof: MerklePath < Keccak160 > = update. proof ;
181
174
182
175
if !root_digest. check ( proof, & message_vec) {
183
- return Err ( PythReceiverError :: PriceUnavailable ) ;
176
+ return Err ( PythReceiverError :: InvalidMerkleProof ) ;
184
177
}
185
178
186
- // TODO: UPDATE THE PRICE INFO
179
+ // UPDATE STORED PRICE INFO BASED ON THE UPDATES IN THE VAA
187
180
let msg = from_slice :: < byteorder:: BE , Message > ( & message_vec)
188
- . map_err ( |_| PythReceiverError :: PriceUnavailable ) ?;
181
+ . map_err ( |_| PythReceiverError :: InvalidAccumulatorMessage ) ?;
189
182
190
183
match msg {
191
184
Message :: PriceFeedMessage ( price_feed_message) => {
@@ -204,22 +197,11 @@ impl PythReceiver {
204
197
205
198
206
199
} ,
207
- Message :: TwapMessage ( _) => {
208
- // Handle TWAP message - currently not implemented
209
- // This could be extended to handle TWAP price updates
210
- } ,
211
- Message :: PublisherStakeCapsMessage ( _) => {
212
- // Handle publisher stake caps message - currently not implemented
213
- // This could be extended to handle publisher stake updates
200
+ _ => {
201
+ return Err ( PythReceiverError :: InvalidAccumulatorMessageType ) ;
214
202
} ,
215
203
}
216
-
217
-
218
- // TODO: STORE PRICE INFO IN OUTPUT
219
-
220
204
}
221
-
222
- // TODO: FORM OUTPUT ARRAY
223
205
}
224
206
} ;
225
207
@@ -300,3 +282,5 @@ fn parse_wormhole_proof(vaa: Vaa) -> Result<MerkleRoot<Keccak160>, PythReceiverE
300
282
} ) ;
301
283
Ok ( root)
302
284
}
285
+
286
+
0 commit comments