Skip to content

Commit a1258ed

Browse files
chore: code format
1 parent 37ede70 commit a1258ed

File tree

5 files changed

+108
-155
lines changed

5 files changed

+108
-155
lines changed

target_chains/ethereum/sdk/stylus/contracts/src/pyth/functions.rs

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::pyth::mock::DecodeDataType;
22
use crate::pyth::types::{
3-
getEmaPriceNoOlderThanCall, getEmaPriceUnsafeCall, getPriceNoOlderThanCall,
4-
getPriceUnsafeCall, getUpdateFeeCall, getValidTimePeriodCall,
5-
parsePriceFeedUpdatesCall, parsePriceFeedUpdatesUniqueCall,
6-
updatePriceFeedsCall, updatePriceFeedsIfNecessaryCall, Price, PriceFeed,
3+
getEmaPriceNoOlderThanCall, getEmaPriceUnsafeCall, getPriceNoOlderThanCall, getPriceUnsafeCall,
4+
getUpdateFeeCall, getValidTimePeriodCall, parsePriceFeedUpdatesCall,
5+
parsePriceFeedUpdatesUniqueCall, updatePriceFeedsCall, updatePriceFeedsIfNecessaryCall, Price,
6+
PriceFeed,
77
};
88
use crate::utils::{call_helper, delegate_call_helper};
99
use alloc::vec::Vec;
@@ -27,11 +27,7 @@ pub fn get_price_no_older_than(
2727
id: B256,
2828
age: U256,
2929
) -> Result<Price, Vec<u8>> {
30-
let price_call = call_helper::<getPriceNoOlderThanCall>(
31-
storage,
32-
pyth_address,
33-
(id, age),
34-
)?;
30+
let price_call = call_helper::<getPriceNoOlderThanCall>(storage, pyth_address, (id, age))?;
3531
Ok(price_call.price)
3632
}
3733

@@ -49,8 +45,7 @@ pub fn get_update_fee(
4945
pyth_address: Address,
5046
update_data: Vec<Bytes>,
5147
) -> Result<U256, Vec<u8>> {
52-
let update_fee_call =
53-
call_helper::<getUpdateFeeCall>(storage, pyth_address, (update_data,))?;
48+
let update_fee_call = call_helper::<getUpdateFeeCall>(storage, pyth_address, (update_data,))?;
5449
Ok(update_fee_call.feeAmount)
5550
}
5651

@@ -68,8 +63,7 @@ pub fn get_ema_price_unsafe(
6863
pyth_address: Address,
6964
id: B256,
7065
) -> Result<Price, Vec<u8>> {
71-
let ema_price =
72-
call_helper::<getEmaPriceUnsafeCall>(storage, pyth_address, (id,))?;
66+
let ema_price = call_helper::<getEmaPriceUnsafeCall>(storage, pyth_address, (id,))?;
7367
Ok(ema_price.price)
7468
}
7569

@@ -89,11 +83,7 @@ pub fn get_ema_price_no_older_than(
8983
id: B256,
9084
age: U256,
9185
) -> Result<Price, Vec<u8>> {
92-
let ema_price = call_helper::<getEmaPriceNoOlderThanCall>(
93-
storage,
94-
pyth_address,
95-
(id, age),
96-
)?;
86+
let ema_price = call_helper::<getEmaPriceNoOlderThanCall>(storage, pyth_address, (id, age))?;
9787
Ok(ema_price.price)
9888
}
9989

@@ -111,8 +101,7 @@ pub fn get_price_unsafe(
111101
pyth_address: Address,
112102
id: B256,
113103
) -> Result<Price, Vec<u8>> {
114-
let price =
115-
call_helper::<getPriceUnsafeCall>(storage, pyth_address, (id,))?;
104+
let price = call_helper::<getPriceUnsafeCall>(storage, pyth_address, (id,))?;
116105
let price = Price {
117106
price: price._0,
118107
conf: price._1,
@@ -134,8 +123,7 @@ pub fn get_valid_time_period(
134123
storage: &mut impl TopLevelStorage,
135124
pyth_address: Address,
136125
) -> Result<U256, Vec<u8>> {
137-
let valid_time_period =
138-
call_helper::<getValidTimePeriodCall>(storage, pyth_address, ())?;
126+
let valid_time_period = call_helper::<getValidTimePeriodCall>(storage, pyth_address, ())?;
139127
Ok(valid_time_period.validTimePeriod)
140128
}
141129

@@ -153,11 +141,7 @@ pub fn update_price_feeds(
153141
pyth_address: Address,
154142
update_data: Vec<Bytes>,
155143
) -> Result<(), Vec<u8>> {
156-
delegate_call_helper::<updatePriceFeedsCall>(
157-
storage,
158-
pyth_address,
159-
(update_data,),
160-
)?;
144+
delegate_call_helper::<updatePriceFeedsCall>(storage, pyth_address, (update_data,))?;
161145
Ok(())
162146
}
163147

@@ -207,12 +191,11 @@ pub fn parse_price_feed_updates(
207191
min_publish_time: u64,
208192
max_publish_time: u64,
209193
) -> Result<Vec<PriceFeed>, Vec<u8>> {
210-
let parse_price_feed_updates_call =
211-
delegate_call_helper::<parsePriceFeedUpdatesCall>(
212-
storage,
213-
pyth_address,
214-
(update_data, price_ids, min_publish_time, max_publish_time),
215-
)?;
194+
let parse_price_feed_updates_call = delegate_call_helper::<parsePriceFeedUpdatesCall>(
195+
storage,
196+
pyth_address,
197+
(update_data, price_ids, min_publish_time, max_publish_time),
198+
)?;
216199
Ok(parse_price_feed_updates_call.priceFeeds)
217200
}
218201

@@ -236,12 +219,11 @@ pub fn parse_price_feed_updates_unique(
236219
min_publish_time: u64,
237220
max_publish_time: u64,
238221
) -> Result<Vec<PriceFeed>, Vec<u8>> {
239-
let parse_price_feed_updates_call =
240-
delegate_call_helper::<parsePriceFeedUpdatesUniqueCall>(
241-
storage,
242-
pyth_address,
243-
(update_data, price_ids, min_publish_time, max_publish_time),
244-
)?;
222+
let parse_price_feed_updates_call = delegate_call_helper::<parsePriceFeedUpdatesUniqueCall>(
223+
storage,
224+
pyth_address,
225+
(update_data, price_ids, min_publish_time, max_publish_time),
226+
)?;
245227
Ok(parse_price_feed_updates_call.priceFeeds)
246228
}
247229

@@ -270,11 +252,24 @@ pub fn create_price_feed_update_data(
270252
publish_time: U256,
271253
prev_publish_time: u64,
272254
) -> Vec<u8> {
273-
let price = Price { price, conf, expo, publish_time };
274-
let ema_price =
275-
Price { price: ema_price, conf: ema_conf, expo, publish_time };
255+
let price = Price {
256+
price,
257+
conf,
258+
expo,
259+
publish_time,
260+
};
261+
let ema_price = Price {
262+
price: ema_price,
263+
conf: ema_conf,
264+
expo,
265+
publish_time,
266+
};
276267

277-
let price_feed_data = PriceFeed { id, price, ema_price };
268+
let price_feed_data = PriceFeed {
269+
id,
270+
price,
271+
ema_price,
272+
};
278273

279274
let price_feed_data_encoding = (price_feed_data, prev_publish_time);
280275
return DecodeDataType::abi_encode(&price_feed_data_encoding);

target_chains/ethereum/sdk/stylus/contracts/src/pyth/mock.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
use alloc::vec::Vec;
1010
use alloy_primitives::{Bytes, B256, U256};
1111
use alloy_sol_types::{sol_data::Uint as SolUInt, SolType, SolValue};
12-
use stylus_sdk::{abi::Bytes as AbiBytes, evm, msg, prelude::*};
12+
use stylus_sdk::{abi::Bytes as AbiBytes, evm, msg, prelude::*};
1313

1414
////Decode data type PriceFeed and uint64
1515
pub type DecodeDataType = (PriceFeed, SolUInt<64>);
@@ -29,9 +29,7 @@ impl MockPythContract {
2929
single_update_fee_in_wei: U256,
3030
valid_time_period: U256,
3131
) -> Result<(), Vec<u8>> {
32-
if single_update_fee_in_wei <= U256::ZERO
33-
|| valid_time_period <= U256::ZERO
34-
{
32+
if single_update_fee_in_wei <= U256::ZERO || valid_time_period <= U256::ZERO {
3533
return Err(Error::InvalidArgument(InvalidArgument {}).into());
3634
}
3735
self.single_update_fee_in_wei.set(single_update_fee_in_wei);
@@ -72,23 +70,21 @@ impl MockPythContract {
7270
/// ]
7371
7472
#[payable]
75-
fn update_price_feeds(
76-
&mut self,
77-
update_data: Vec<AbiBytes>,
78-
) -> Result<(), Vec<u8>> {
73+
fn update_price_feeds(&mut self, update_data: Vec<AbiBytes>) -> Result<(), Vec<u8>> {
7974
let required_fee = self.get_update_fee(update_data.clone());
8075
if required_fee < msg::value() {
8176
return Err(Error::InsufficientFee(InsufficientFee {}).into());
8277
}
8378

8479
for item in update_data.iter() {
85-
let price_feed_data =
86-
<PriceFeed as SolType>::abi_decode(item, false)
87-
.map_err(|_| {
88-
CALL_RETDATA_DECODING_ERROR_MESSAGE.to_vec()
89-
})?;
90-
let last_publish_time =
91-
&self.price_feeds.get(price_feed_data.id).price.publish_time.get();
80+
let price_feed_data = <PriceFeed as SolType>::abi_decode(item, false)
81+
.map_err(|_| CALL_RETDATA_DECODING_ERROR_MESSAGE.to_vec())?;
82+
let last_publish_time = &self
83+
.price_feeds
84+
.get(price_feed_data.id)
85+
.price
86+
.publish_time
87+
.get();
9288
if last_publish_time < &price_feed_data.price.publish_time {
9389
self.price_feeds
9490
.setter(price_feed_data.id)
@@ -107,7 +103,7 @@ impl MockPythContract {
107103
fn get_update_fee(&self, update_data: Vec<AbiBytes>) -> U256 {
108104
self.single_update_fee_in_wei.get() * U256::from(update_data.len())
109105
}
110-
106+
111107
#[payable]
112108
fn parse_price_feed_updates(
113109
&mut self,
@@ -153,19 +149,31 @@ impl MockPythContract {
153149
publish_time: U256,
154150
prev_publish_time: u64,
155151
) -> Vec<u8> {
156-
let price = Price { price, conf, expo, publish_time };
157-
let ema_price =
158-
Price { price: ema_price, conf: ema_conf, expo, publish_time };
152+
let price = Price {
153+
price,
154+
conf,
155+
expo,
156+
publish_time,
157+
};
158+
let ema_price = Price {
159+
price: ema_price,
160+
conf: ema_conf,
161+
expo,
162+
publish_time,
163+
};
159164

160-
let price_feed_data = PriceFeed { id, price, ema_price };
165+
let price_feed_data = PriceFeed {
166+
id,
167+
price,
168+
ema_price,
169+
};
161170

162171
let price_feed_data_encoding = (price_feed_data, prev_publish_time);
163172
return DecodeDataType::abi_encode(&price_feed_data_encoding);
164173
}
165174
}
166175

167176
impl MockPythContract {
168-
169177
fn parse_price_feed_updates_internal(
170178
&mut self,
171179
update_data: Vec<AbiBytes>,
@@ -186,7 +194,8 @@ impl MockPythContract {
186194

187195
for data in &update_data {
188196
// Decode the update_data
189-
let (price_feed, _prev_publish_time) = match DecodeDataType::abi_decode(data, false) {
197+
let (price_feed, _prev_publish_time) = match DecodeDataType::abi_decode(data, false)
198+
{
190199
Ok(res) => res,
191200
Err(_) => {
192201
return Err(Error::FalledDecodeData(FalledDecodeData {}).into());
@@ -195,12 +204,8 @@ impl MockPythContract {
195204

196205
if price_feed.id == price_id {
197206
let publish_time = price_feed.price.publish_time;
198-
let previous_publish_time = self
199-
.price_feeds
200-
.get(price_id)
201-
.price
202-
.publish_time
203-
.get();
207+
let previous_publish_time =
208+
self.price_feeds.get(price_id).price.publish_time.get();
204209

205210
// Validate publish time and uniqueness
206211
if publish_time > U256::from(min_publish_time)
@@ -311,8 +316,7 @@ mod tests {
311316
publish_time,
312317
PREV_PUBLISH_TIME,
313318
);
314-
let price_feed_decoded =
315-
DecodeDataType::abi_decode(&price_feed_created, true).unwrap();
319+
let price_feed_decoded = DecodeDataType::abi_decode(&price_feed_created, true).unwrap();
316320
assert_eq!(price_feed_decoded.0.id, id);
317321
assert_eq!(price_feed_decoded.0.price.price, PRICE);
318322
assert_eq!(price_feed_decoded.0.price.conf, CONF);

0 commit comments

Comments
 (0)