Skip to content

Commit b08600d

Browse files
committed
almost done with updating price, need to reinsert into StorageMap
1 parent d35a5ea commit b08600d

File tree

1 file changed

+24
-16
lines changed
  • target_chains/stylus/contracts/pyth-receiver/src

1 file changed

+24
-16
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,28 @@ impl PythReceiver {
189189

190190
match msg {
191191
Message::PriceFeedMessage(price_feed_message) => {
192-
// if self.update_price_feed_if_new(price_feed_message.feed_id,PriceInfoStorage::from(&price_feed_message)) {
193-
// count_updates += 1;
194-
// }
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)
205+
|| recent_price_info.price.get() == I64::ZERO {
206+
recent_price_info.publish_time.set(U64::from(price_feed_message.publish_time));
207+
recent_price_info.price.set(I64::from_le_bytes(price_feed_message.price.to_le_bytes()));
208+
recent_price_info.conf.set(U64::from(price_feed_message.conf));
209+
recent_price_info.expo.set(I32::from_le_bytes(price_feed_message.exponent.to_le_bytes()));
210+
recent_price_info.ema_price.set(I64::from_le_bytes(price_feed_message.ema_price.to_le_bytes()));
211+
recent_price_info.ema_conf.set(U64::from(price_feed_message.ema_conf));
212+
}
213+
195214

196215
},
197216
Message::TwapMessage(_) => {
@@ -275,19 +294,8 @@ impl PythReceiver {
275294
// Updates the Price Feed only if it is newer than the current one. This function never fails
276295
// and will either update in-place or not update at all. The return value indicates whether
277296
// the update was performed or not.
278-
// fn update_price_feed_if_new(&mut self, price_id: [u8; 32], price_feed: PriceInfo) -> bool {
279-
// match self.latest_price_info.get(&price_id) {
280-
// Some(stored_price_feed) => {
281-
// let update = price_feed.price.publish_time > stored_price_feed.price.publish_time;
282-
// update.then(|| self.prices.insert(&price_feed.id, &price_feed));
283-
// update
284-
// }
285-
286-
// None => {
287-
// self.prices.insert(&price_feed.id, &price_feed);
288-
// true
289-
// }
290-
// }
297+
// fn update_price_feed_if_new(&self, price_id: [u8; 32], price_feed: PriceInfo) -> bool {
298+
291299
// }
292300

293301

0 commit comments

Comments
 (0)