Skip to content

Commit 088d0d8

Browse files
committed
setting up internal parse fxn
1 parent 4cc13db commit 088d0d8

File tree

1 file changed

+83
-44
lines changed
  • target_chains/stylus/contracts/pyth-receiver/src

1 file changed

+83
-44
lines changed

target_chains/stylus/contracts/pyth-receiver/src/lib.rs

Lines changed: 83 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl PythReceiver {
151151

152152
#[payable]
153153
pub fn update_price_feeds(&mut self, update_data: Vec<u8>) -> Result<(), PythReceiverError> {
154-
self.update_price_feeds_internal(update_data)?;
154+
self.update_price_feeds_internal(update_data, Vec::new(), 0, 0, false)?;
155155
Ok(())
156156
}
157157

@@ -164,7 +164,70 @@ impl PythReceiver {
164164
// dummy implementation
165165
}
166166

167-
fn update_price_feeds_internal(&mut self, update_data: Vec<u8>) -> Result<(), PythReceiverError> {
167+
fn update_price_feeds_internal(&mut self, update_data: Vec<u8>, price_ids: Vec<[u8; 32]>, min_publish_time: u64, max_publish_time: u64, unique: bool) -> Result<(), PythReceiverError> {
168+
let price_returns= self.parse_price_feed_updates(update_data, price_ids.clone(), min_publish_time, max_publish_time)?;
169+
for i in 0..price_ids.clone().len() {
170+
let cur_price_id = &price_ids[i];
171+
let cur_price_return = &price_returns[i];
172+
let price_id_fb : FixedBytes<32> = FixedBytes::from(cur_price_id);
173+
let mut recent_price_info = self.latest_price_info.setter(price_id_fb);
174+
175+
if recent_price_info.publish_time.get() < cur_price_return.0
176+
|| recent_price_info.price.get() == I64::ZERO {
177+
recent_price_info.publish_time.set(cur_price_return.0);
178+
recent_price_info.expo.set(cur_price_return.1);
179+
recent_price_info.price.set(cur_price_return.2);
180+
recent_price_info.conf.set(cur_price_return.3);
181+
recent_price_info.ema_price.set(cur_price_return.4);
182+
recent_price_info.ema_conf.set(cur_price_return.5);
183+
}
184+
}
185+
186+
Ok(())
187+
}
188+
189+
fn get_total_fee(&self, num_updates: u8) -> U256 {
190+
U256::from(num_updates).saturating_mul(self.single_update_fee_in_wei.get())
191+
}
192+
193+
pub fn get_twap_update_fee(&self, _update_data: Vec<Vec<u8>>) -> U256 {
194+
U256::from(0u8)
195+
}
196+
197+
pub fn parse_price_feed_updates(
198+
&mut self,
199+
update_data: Vec<u8>,
200+
price_ids: Vec<[u8; 32]>,
201+
min_publish_time: u64,
202+
max_publish_time: u64,
203+
) -> Result<Vec<PriceInfoReturn>, PythReceiverError> {
204+
let price_feeds = self.parse_price_feed_updates_with_config(update_data, price_ids, min_publish_time, max_publish_time, false, false, false);
205+
price_feeds
206+
}
207+
208+
pub fn parse_price_feed_updates_with_config(
209+
&mut self,
210+
update_data: Vec<u8>,
211+
price_ids: Vec<[u8; 32]>,
212+
min_allowed_publish_time: u64,
213+
max_allowed_publish_time: u64,
214+
check_uniqueness: bool,
215+
check_update_data_is_minimal: bool,
216+
store_updates_if_fresh: bool,
217+
) -> Result<Vec<PriceInfoReturn>, PythReceiverError> {
218+
219+
}
220+
221+
fn parse_price_feed_updates_internal(
222+
&mut self,
223+
update_data: Vec<u8>,
224+
price_ids: Vec<[u8; 32]>,
225+
min_allowed_publish_time: u64,
226+
max_allowed_publish_time: u64,
227+
check_uniqueness: bool,
228+
check_update_data_is_minimal: bool,
229+
store_updates_if_fresh: bool,
230+
) -> Result<Vec<PriceInfoReturn>, PythReceiverError> {
168231
let update_data_array: &[u8] = &update_data;
169232
// Check the first 4 bytes of the update_data_array for the magic header
170233
if update_data_array.len() < 4 {
@@ -180,6 +243,8 @@ impl PythReceiver {
180243

181244
let update_data = AccumulatorUpdateData::try_from_slice(&update_data_array).map_err(|_| PythReceiverError::InvalidAccumulatorMessage)?;
182245

246+
let mut price_feeds: Vec<PriceInfoReturn> = Vec::new();
247+
183248
match update_data.proof {
184249
Proof::WormholeMerkle { vaa, updates } => {
185250
let wormhole: IWormholeContract = IWormholeContract::new(self.wormhole.get());
@@ -233,17 +298,22 @@ impl PythReceiver {
233298
let price_id_fb : FixedBytes<32> = FixedBytes::from(price_feed_message.feed_id);
234299
let mut recent_price_info = self.latest_price_info.setter(price_id_fb);
235300

236-
if recent_price_info.publish_time.get() < U64::from(price_feed_message.publish_time)
237-
|| recent_price_info.price.get() == I64::ZERO {
238-
recent_price_info.publish_time.set(U64::from(price_feed_message.publish_time));
239-
recent_price_info.price.set(I64::from_le_bytes(price_feed_message.price.to_le_bytes()));
240-
recent_price_info.conf.set(U64::from(price_feed_message.conf));
241-
recent_price_info.expo.set(I32::from_le_bytes(price_feed_message.exponent.to_le_bytes()));
242-
recent_price_info.ema_price.set(I64::from_le_bytes(price_feed_message.ema_price.to_le_bytes()));
243-
recent_price_info.ema_conf.set(U64::from(price_feed_message.ema_conf));
301+
let price_info_return = (
302+
recent_price_info.publish_time.get(),
303+
recent_price_info.expo.get(),
304+
recent_price_info.price.get(),
305+
recent_price_info.conf.get(),
306+
recent_price_info.ema_price.get(),
307+
recent_price_info.ema_conf.get(),
308+
);
309+
310+
// Find the index of the price_id in the input price_ids vector
311+
if let Some(idx) = price_ids.iter().position(|id| *id == price_feed_message.feed_id) {
312+
if price_feeds.len() <= idx {
313+
price_feeds.resize(idx + 1, Default::default());
314+
}
315+
price_feeds[idx] = price_info_return;
244316
}
245-
246-
247317
},
248318
_ => {
249319
return Err(PythReceiverError::InvalidAccumulatorMessageType);
@@ -253,38 +323,7 @@ impl PythReceiver {
253323
}
254324
};
255325

256-
Ok(())
257-
}
258-
259-
fn get_total_fee(&self, num_updates: u8) -> U256 {
260-
U256::from(num_updates).saturating_mul(self.single_update_fee_in_wei.get())
261-
}
262-
263-
pub fn get_twap_update_fee(&self, _update_data: Vec<Vec<u8>>) -> U256 {
264-
U256::from(0u8)
265-
}
266-
267-
pub fn parse_price_feed_updates(
268-
&mut self,
269-
update_data: Vec<Vec<u8>>,
270-
price_ids: Vec<[u8; 32]>,
271-
min_publish_time: u64,
272-
max_publish_time: u64,
273-
) -> Vec<PriceInfoReturn> {
274-
parse_price_feed_updates_with_config(update_data, price_ids, min_publish_time, max_publish_time, false, false, false)
275-
}
276-
277-
pub fn parse_price_feed_updates_with_config(
278-
&mut self,
279-
_update_data: Vec<Vec<u8>>,
280-
_price_ids: Vec<[u8; 32]>,
281-
_min_allowed_publish_time: u64,
282-
_max_allowed_publish_time: u64,
283-
_check_uniqueness: bool,
284-
_check_update_data_is_minimal: bool,
285-
_store_updates_if_fresh: bool,
286-
) -> (Vec<PriceInfoReturn>, Vec<u64>) {
287-
(Vec::new(), Vec::new())
326+
Ok(price_feeds)
288327
}
289328

290329
pub fn parse_twap_price_feed_updates(

0 commit comments

Comments
 (0)