@@ -10,7 +10,7 @@ mod error;
10
10
11
11
use alloc:: vec:: Vec ;
12
12
use stylus_sdk:: { alloy_primitives:: { U16 , U32 , U256 , U64 , I32 , I64 , FixedBytes , Address } ,
13
- prelude:: * ,
13
+ prelude:: * ,
14
14
storage:: { StorageGuardMut , StorageAddress , StorageVec , StorageMap , StorageUint , StorageBool , StorageU256 , StorageU16 , StorageFixedBytes } ,
15
15
call:: Call } ;
16
16
@@ -59,22 +59,22 @@ pub struct PythReceiver {
59
59
impl PythReceiver {
60
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
- governance_emitter_chain_id : u16 , governance_emitter_address : [ u8 ; 32 ] ,
62
+ governance_emitter_chain_id : u16 , governance_emitter_address : [ u8 ; 32 ] ,
63
63
governance_initial_sequence : u64 , _data : Vec < u8 > ) {
64
64
self . wormhole . set ( _wormhole) ;
65
65
self . single_update_fee_in_wei . set ( _single_update_fee_in_wei) ;
66
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) ) ;
70
-
70
+
71
71
// Initialize other fields
72
72
self . last_executed_governance_sequence . set ( U64 :: from ( governance_initial_sequence) ) ;
73
73
self . governance_data_source_index . set ( U32 :: ZERO ) ;
74
74
75
75
for ( i, chain_id) in data_source_emitter_chain_ids. iter ( ) . enumerate ( ) {
76
76
let emitter_address = FixedBytes :: < 32 > :: from ( data_source_emitter_addresses[ i] ) ;
77
-
77
+
78
78
// Create a new data source storage slot
79
79
let mut data_source = self . valid_data_sources . grow ( ) ;
80
80
data_source. chain_id . set ( U16 :: from ( * chain_id) ) ;
@@ -84,19 +84,19 @@ impl PythReceiver {
84
84
chain_id : U16 :: from ( * chain_id) ,
85
85
emitter_address : emitter_address,
86
86
} ;
87
-
87
+
88
88
89
89
self . is_valid_data_source
90
90
. setter ( data_source_key)
91
91
. set ( true ) ;
92
92
}
93
93
}
94
-
94
+
95
95
pub fn get_price_unsafe ( & self , _id : [ u8 ; 32 ] ) -> Result < PriceInfoReturn , PythReceiverError > {
96
96
let id_fb = FixedBytes :: < 32 > :: from ( _id) ;
97
-
97
+
98
98
let price_info = self . latest_price_info . get ( id_fb) ;
99
-
99
+
100
100
if price_info. publish_time . get ( ) == U64 :: ZERO {
101
101
return Err ( PythReceiverError :: PriceUnavailable ) ;
102
102
}
@@ -128,7 +128,7 @@ impl PythReceiver {
128
128
}
129
129
130
130
pub fn update_price_feeds ( & mut self , update_data : Vec < u8 > ) {
131
-
131
+
132
132
}
133
133
134
134
pub fn update_price_feeds_if_necessary (
@@ -140,7 +140,7 @@ impl PythReceiver {
140
140
// dummy implementation
141
141
}
142
142
143
- fn update_price_feeds_internal ( & self , update_data : Vec < u8 > , price_ids : Vec < Address > , min_publish_time : u64 , max_publish_time : u64 , unique : bool ) -> Result < ( ) , PythReceiverError > {
143
+ fn update_price_feeds_internal ( & mut self , update_data : Vec < u8 > , price_ids : Vec < Address > , min_publish_time : u64 , max_publish_time : u64 , unique : bool ) -> Result < ( ) , PythReceiverError > {
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 {
@@ -149,13 +149,13 @@ impl PythReceiver {
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
154
panic ! ( "Invalid update_data magic header" ) ;
155
155
}
156
-
156
+
157
157
let update_data = AccumulatorUpdateData :: try_from_slice ( & update_data_array) . unwrap ( ) ;
158
-
158
+
159
159
match update_data. proof {
160
160
Proof :: WormholeMerkle { vaa, updates } => {
161
161
let wormhole: IWormholeContract = IWormholeContract :: new ( self . wormhole . get ( ) ) ;
@@ -186,22 +186,13 @@ impl PythReceiver {
186
186
// TODO: UPDATE THE PRICE INFO
187
187
let msg = from_slice :: < byteorder:: BE , Message > ( & message_vec)
188
188
. map_err ( |_| PythReceiverError :: PriceUnavailable ) ?;
189
-
189
+
190
190
match msg {
191
191
Message :: PriceFeedMessage ( price_feed_message) => {
192
192
let price_id_fb : FixedBytes < 32 > = FixedBytes :: from ( price_feed_message. feed_id ) ;
193
- let mut recent_price_info = self . latest_price_info . get ( price_id_fb) ;
194
-
195
- let stored_price_info = PriceInfo {
196
- price : recent_price_info. price . get ( ) ,
197
- conf : recent_price_info. conf . get ( ) ,
198
- expo : recent_price_info. expo . get ( ) ,
199
- publish_time : recent_price_info. publish_time . get ( ) ,
200
- ema_price : recent_price_info. ema_price . get ( ) ,
201
- ema_conf : recent_price_info. ema_conf . get ( ) ,
202
- } ;
203
-
204
- if recent_price_info. publish_time . get ( ) < U64 :: from ( price_feed_message. publish_time )
193
+ let mut recent_price_info = self . latest_price_info . setter ( price_id_fb) ;
194
+
195
+ if recent_price_info. publish_time . get ( ) < U64 :: from ( price_feed_message. publish_time )
205
196
|| recent_price_info. price . get ( ) == I64 :: ZERO {
206
197
recent_price_info. publish_time . set ( U64 :: from ( price_feed_message. publish_time ) ) ;
207
198
recent_price_info. price . set ( I64 :: from_le_bytes ( price_feed_message. price . to_le_bytes ( ) ) ) ;
@@ -210,7 +201,7 @@ impl PythReceiver {
210
201
recent_price_info. ema_price . set ( I64 :: from_le_bytes ( price_feed_message. ema_price . to_le_bytes ( ) ) ) ;
211
202
recent_price_info. ema_conf . set ( U64 :: from ( price_feed_message. ema_conf ) ) ;
212
203
}
213
-
204
+
214
205
215
206
} ,
216
207
Message :: TwapMessage ( _) => {
@@ -224,7 +215,7 @@ impl PythReceiver {
224
215
}
225
216
226
217
227
- // TODO: STORE PRICE INFO IN OUTPUT
218
+ // TODO: STORE PRICE INFO IN OUTPUT
228
219
229
220
}
230
221
@@ -287,7 +278,7 @@ impl PythReceiver {
287
278
fn is_no_older_than ( & self , publish_time : U64 , max_age : u64 ) -> bool {
288
279
let current_u64: u64 = self . vm ( ) . block_timestamp ( ) ;
289
280
let publish_time_u64: u64 = publish_time. to :: < u64 > ( ) ;
290
-
281
+
291
282
current_u64. saturating_sub ( publish_time_u64) <= max_age
292
283
}
293
284
@@ -298,7 +289,7 @@ impl PythReceiver {
298
289
299
290
// }
300
291
301
-
292
+
302
293
}
303
294
304
295
fn parse_wormhole_proof ( vaa : Vaa ) -> Result < MerkleRoot < Keccak160 > , PythReceiverError > {
@@ -308,4 +299,4 @@ fn parse_wormhole_proof(vaa: Vaa) -> Result<MerkleRoot<Keccak160>, PythReceiverE
308
299
WormholePayload :: Merkle ( merkle_root) => merkle_root. root ,
309
300
} ) ;
310
301
Ok ( root)
311
- }
302
+ }
0 commit comments