Skip to content

Commit 0bb1763

Browse files
fix: use StorageMap.setter() to properly store price feed updates
- Replace .get() with .setter() to get mutable reference to StorageMap entry - Change update_price_feeds_internal signature from &self to &mut self - Follows Stylus StorageMap documentation and existing pattern in codebase - Removes unused stored_price_info variable Co-Authored-By: [email protected] <[email protected]>
1 parent b08600d commit 0bb1763

File tree

1 file changed

+22
-31
lines changed
  • target_chains/stylus/contracts/pyth-receiver/src

1 file changed

+22
-31
lines changed

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

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod error;
1010

1111
use alloc::vec::Vec;
1212
use stylus_sdk::{alloy_primitives::{U16, U32, U256, U64, I32, I64, FixedBytes, Address},
13-
prelude::*,
13+
prelude::*,
1414
storage::{StorageGuardMut, StorageAddress, StorageVec, StorageMap, StorageUint, StorageBool, StorageU256, StorageU16, StorageFixedBytes},
1515
call::Call};
1616

@@ -59,22 +59,22 @@ pub struct PythReceiver {
5959
impl PythReceiver {
6060
pub fn initialize(&mut self, _wormhole: Address, _single_update_fee_in_wei: U256, _valid_time_period_seconds: U256,
6161
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],
6363
governance_initial_sequence: u64, _data: Vec<u8>) {
6464
self.wormhole.set(_wormhole);
6565
self.single_update_fee_in_wei.set(_single_update_fee_in_wei);
6666
self.valid_time_period_seconds.set(_valid_time_period_seconds);
6767

6868
self.governance_data_source_chain_id.set(U16::from(governance_emitter_chain_id));
6969
self.governance_data_source_emitter_address.set(FixedBytes::<32>::from(governance_emitter_address));
70-
70+
7171
// Initialize other fields
7272
self.last_executed_governance_sequence.set(U64::from(governance_initial_sequence));
7373
self.governance_data_source_index.set(U32::ZERO);
7474

7575
for (i, chain_id) in data_source_emitter_chain_ids.iter().enumerate() {
7676
let emitter_address = FixedBytes::<32>::from(data_source_emitter_addresses[i]);
77-
77+
7878
// Create a new data source storage slot
7979
let mut data_source = self.valid_data_sources.grow();
8080
data_source.chain_id.set(U16::from(*chain_id));
@@ -84,19 +84,19 @@ impl PythReceiver {
8484
chain_id: U16::from(*chain_id),
8585
emitter_address: emitter_address,
8686
};
87-
87+
8888

8989
self.is_valid_data_source
9090
.setter(data_source_key)
9191
.set(true);
9292
}
9393
}
94-
94+
9595
pub fn get_price_unsafe(&self, _id: [u8; 32]) -> Result<PriceInfoReturn, PythReceiverError> {
9696
let id_fb = FixedBytes::<32>::from(_id);
97-
97+
9898
let price_info = self.latest_price_info.get(id_fb);
99-
99+
100100
if price_info.publish_time.get() == U64::ZERO {
101101
return Err(PythReceiverError::PriceUnavailable);
102102
}
@@ -128,7 +128,7 @@ impl PythReceiver {
128128
}
129129

130130
pub fn update_price_feeds(&mut self, update_data: Vec<u8>) {
131-
131+
132132
}
133133

134134
pub fn update_price_feeds_if_necessary(
@@ -140,7 +140,7 @@ impl PythReceiver {
140140
// dummy implementation
141141
}
142142

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> {
144144
let update_data_array: &[u8] = &update_data;
145145
// Check the first 4 bytes of the update_data_array for the magic header
146146
if update_data_array.len() < 4 {
@@ -149,13 +149,13 @@ impl PythReceiver {
149149

150150
let mut header = [0u8; 4];
151151
header.copy_from_slice(&update_data_array[0..4]);
152-
152+
153153
if &header != PYTHNET_ACCUMULATOR_UPDATE_MAGIC {
154154
panic!("Invalid update_data magic header");
155155
}
156-
156+
157157
let update_data = AccumulatorUpdateData::try_from_slice(&update_data_array).unwrap();
158-
158+
159159
match update_data.proof {
160160
Proof::WormholeMerkle { vaa, updates } => {
161161
let wormhole: IWormholeContract = IWormholeContract::new(self.wormhole.get());
@@ -186,22 +186,13 @@ impl PythReceiver {
186186
// TODO: UPDATE THE PRICE INFO
187187
let msg = from_slice::<byteorder::BE, Message>(&message_vec)
188188
.map_err(|_| PythReceiverError::PriceUnavailable)?;
189-
189+
190190
match msg {
191191
Message::PriceFeedMessage(price_feed_message) => {
192192
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)
205196
|| recent_price_info.price.get() == I64::ZERO {
206197
recent_price_info.publish_time.set(U64::from(price_feed_message.publish_time));
207198
recent_price_info.price.set(I64::from_le_bytes(price_feed_message.price.to_le_bytes()));
@@ -210,7 +201,7 @@ impl PythReceiver {
210201
recent_price_info.ema_price.set(I64::from_le_bytes(price_feed_message.ema_price.to_le_bytes()));
211202
recent_price_info.ema_conf.set(U64::from(price_feed_message.ema_conf));
212203
}
213-
204+
214205

215206
},
216207
Message::TwapMessage(_) => {
@@ -224,7 +215,7 @@ impl PythReceiver {
224215
}
225216

226217

227-
// TODO: STORE PRICE INFO IN OUTPUT
218+
// TODO: STORE PRICE INFO IN OUTPUT
228219

229220
}
230221

@@ -287,7 +278,7 @@ impl PythReceiver {
287278
fn is_no_older_than(&self, publish_time: U64, max_age: u64) -> bool {
288279
let current_u64: u64 = self.vm().block_timestamp();
289280
let publish_time_u64: u64 = publish_time.to::<u64>();
290-
281+
291282
current_u64.saturating_sub(publish_time_u64) <= max_age
292283
}
293284

@@ -298,7 +289,7 @@ impl PythReceiver {
298289

299290
// }
300291

301-
292+
302293
}
303294

304295
fn parse_wormhole_proof(vaa: Vaa) -> Result<MerkleRoot<Keccak160>, PythReceiverError> {
@@ -308,4 +299,4 @@ fn parse_wormhole_proof(vaa: Vaa) -> Result<MerkleRoot<Keccak160>, PythReceiverE
308299
WormholePayload::Merkle(merkle_root) => merkle_root.root,
309300
});
310301
Ok(root)
311-
}
302+
}

0 commit comments

Comments
 (0)