Skip to content

Commit

Permalink
chore: code format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifechukwudaniel committed Dec 31, 2024
1 parent 37ede70 commit a1258ed
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 155 deletions.
81 changes: 38 additions & 43 deletions target_chains/ethereum/sdk/stylus/contracts/src/pyth/functions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::pyth::mock::DecodeDataType;
use crate::pyth::types::{
getEmaPriceNoOlderThanCall, getEmaPriceUnsafeCall, getPriceNoOlderThanCall,
getPriceUnsafeCall, getUpdateFeeCall, getValidTimePeriodCall,
parsePriceFeedUpdatesCall, parsePriceFeedUpdatesUniqueCall,
updatePriceFeedsCall, updatePriceFeedsIfNecessaryCall, Price, PriceFeed,
getEmaPriceNoOlderThanCall, getEmaPriceUnsafeCall, getPriceNoOlderThanCall, getPriceUnsafeCall,
getUpdateFeeCall, getValidTimePeriodCall, parsePriceFeedUpdatesCall,
parsePriceFeedUpdatesUniqueCall, updatePriceFeedsCall, updatePriceFeedsIfNecessaryCall, Price,
PriceFeed,
};
use crate::utils::{call_helper, delegate_call_helper};
use alloc::vec::Vec;
Expand All @@ -27,11 +27,7 @@ pub fn get_price_no_older_than(
id: B256,
age: U256,
) -> Result<Price, Vec<u8>> {
let price_call = call_helper::<getPriceNoOlderThanCall>(
storage,
pyth_address,
(id, age),
)?;
let price_call = call_helper::<getPriceNoOlderThanCall>(storage, pyth_address, (id, age))?;
Ok(price_call.price)
}

Expand All @@ -49,8 +45,7 @@ pub fn get_update_fee(
pyth_address: Address,
update_data: Vec<Bytes>,
) -> Result<U256, Vec<u8>> {
let update_fee_call =
call_helper::<getUpdateFeeCall>(storage, pyth_address, (update_data,))?;
let update_fee_call = call_helper::<getUpdateFeeCall>(storage, pyth_address, (update_data,))?;
Ok(update_fee_call.feeAmount)
}

Expand All @@ -68,8 +63,7 @@ pub fn get_ema_price_unsafe(
pyth_address: Address,
id: B256,
) -> Result<Price, Vec<u8>> {
let ema_price =
call_helper::<getEmaPriceUnsafeCall>(storage, pyth_address, (id,))?;
let ema_price = call_helper::<getEmaPriceUnsafeCall>(storage, pyth_address, (id,))?;
Ok(ema_price.price)
}

Expand All @@ -89,11 +83,7 @@ pub fn get_ema_price_no_older_than(
id: B256,
age: U256,
) -> Result<Price, Vec<u8>> {
let ema_price = call_helper::<getEmaPriceNoOlderThanCall>(
storage,
pyth_address,
(id, age),
)?;
let ema_price = call_helper::<getEmaPriceNoOlderThanCall>(storage, pyth_address, (id, age))?;
Ok(ema_price.price)
}

Expand All @@ -111,8 +101,7 @@ pub fn get_price_unsafe(
pyth_address: Address,
id: B256,
) -> Result<Price, Vec<u8>> {
let price =
call_helper::<getPriceUnsafeCall>(storage, pyth_address, (id,))?;
let price = call_helper::<getPriceUnsafeCall>(storage, pyth_address, (id,))?;
let price = Price {
price: price._0,
conf: price._1,
Expand All @@ -134,8 +123,7 @@ pub fn get_valid_time_period(
storage: &mut impl TopLevelStorage,
pyth_address: Address,
) -> Result<U256, Vec<u8>> {
let valid_time_period =
call_helper::<getValidTimePeriodCall>(storage, pyth_address, ())?;
let valid_time_period = call_helper::<getValidTimePeriodCall>(storage, pyth_address, ())?;
Ok(valid_time_period.validTimePeriod)
}

Expand All @@ -153,11 +141,7 @@ pub fn update_price_feeds(
pyth_address: Address,
update_data: Vec<Bytes>,
) -> Result<(), Vec<u8>> {
delegate_call_helper::<updatePriceFeedsCall>(
storage,
pyth_address,
(update_data,),
)?;
delegate_call_helper::<updatePriceFeedsCall>(storage, pyth_address, (update_data,))?;
Ok(())
}

Expand Down Expand Up @@ -207,12 +191,11 @@ pub fn parse_price_feed_updates(
min_publish_time: u64,
max_publish_time: u64,
) -> Result<Vec<PriceFeed>, Vec<u8>> {
let parse_price_feed_updates_call =
delegate_call_helper::<parsePriceFeedUpdatesCall>(
storage,
pyth_address,
(update_data, price_ids, min_publish_time, max_publish_time),
)?;
let parse_price_feed_updates_call = delegate_call_helper::<parsePriceFeedUpdatesCall>(
storage,
pyth_address,
(update_data, price_ids, min_publish_time, max_publish_time),
)?;
Ok(parse_price_feed_updates_call.priceFeeds)
}

Expand All @@ -236,12 +219,11 @@ pub fn parse_price_feed_updates_unique(
min_publish_time: u64,
max_publish_time: u64,
) -> Result<Vec<PriceFeed>, Vec<u8>> {
let parse_price_feed_updates_call =
delegate_call_helper::<parsePriceFeedUpdatesUniqueCall>(
storage,
pyth_address,
(update_data, price_ids, min_publish_time, max_publish_time),
)?;
let parse_price_feed_updates_call = delegate_call_helper::<parsePriceFeedUpdatesUniqueCall>(
storage,
pyth_address,
(update_data, price_ids, min_publish_time, max_publish_time),
)?;
Ok(parse_price_feed_updates_call.priceFeeds)
}

Expand Down Expand Up @@ -270,11 +252,24 @@ pub fn create_price_feed_update_data(
publish_time: U256,
prev_publish_time: u64,
) -> Vec<u8> {
let price = Price { price, conf, expo, publish_time };
let ema_price =
Price { price: ema_price, conf: ema_conf, expo, publish_time };
let price = Price {
price,
conf,
expo,
publish_time,
};
let ema_price = Price {
price: ema_price,
conf: ema_conf,
expo,
publish_time,
};

let price_feed_data = PriceFeed { id, price, ema_price };
let price_feed_data = PriceFeed {
id,
price,
ema_price,
};

let price_feed_data_encoding = (price_feed_data, prev_publish_time);
return DecodeDataType::abi_encode(&price_feed_data_encoding);
Expand Down
64 changes: 34 additions & 30 deletions target_chains/ethereum/sdk/stylus/contracts/src/pyth/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
use alloc::vec::Vec;
use alloy_primitives::{Bytes, B256, U256};
use alloy_sol_types::{sol_data::Uint as SolUInt, SolType, SolValue};
use stylus_sdk::{abi::Bytes as AbiBytes, evm, msg, prelude::*};
use stylus_sdk::{abi::Bytes as AbiBytes, evm, msg, prelude::*};

////Decode data type PriceFeed and uint64
pub type DecodeDataType = (PriceFeed, SolUInt<64>);
Expand All @@ -29,9 +29,7 @@ impl MockPythContract {
single_update_fee_in_wei: U256,
valid_time_period: U256,
) -> Result<(), Vec<u8>> {
if single_update_fee_in_wei <= U256::ZERO
|| valid_time_period <= U256::ZERO
{
if single_update_fee_in_wei <= U256::ZERO || valid_time_period <= U256::ZERO {
return Err(Error::InvalidArgument(InvalidArgument {}).into());
}
self.single_update_fee_in_wei.set(single_update_fee_in_wei);
Expand Down Expand Up @@ -72,23 +70,21 @@ impl MockPythContract {
/// ]
#[payable]
fn update_price_feeds(
&mut self,
update_data: Vec<AbiBytes>,
) -> Result<(), Vec<u8>> {
fn update_price_feeds(&mut self, update_data: Vec<AbiBytes>) -> Result<(), Vec<u8>> {
let required_fee = self.get_update_fee(update_data.clone());
if required_fee < msg::value() {
return Err(Error::InsufficientFee(InsufficientFee {}).into());
}

for item in update_data.iter() {
let price_feed_data =
<PriceFeed as SolType>::abi_decode(item, false)
.map_err(|_| {
CALL_RETDATA_DECODING_ERROR_MESSAGE.to_vec()
})?;
let last_publish_time =
&self.price_feeds.get(price_feed_data.id).price.publish_time.get();
let price_feed_data = <PriceFeed as SolType>::abi_decode(item, false)
.map_err(|_| CALL_RETDATA_DECODING_ERROR_MESSAGE.to_vec())?;
let last_publish_time = &self
.price_feeds
.get(price_feed_data.id)
.price
.publish_time
.get();
if last_publish_time < &price_feed_data.price.publish_time {
self.price_feeds
.setter(price_feed_data.id)
Expand All @@ -107,7 +103,7 @@ impl MockPythContract {
fn get_update_fee(&self, update_data: Vec<AbiBytes>) -> U256 {
self.single_update_fee_in_wei.get() * U256::from(update_data.len())
}

#[payable]
fn parse_price_feed_updates(
&mut self,
Expand Down Expand Up @@ -153,19 +149,31 @@ impl MockPythContract {
publish_time: U256,
prev_publish_time: u64,
) -> Vec<u8> {
let price = Price { price, conf, expo, publish_time };
let ema_price =
Price { price: ema_price, conf: ema_conf, expo, publish_time };
let price = Price {
price,
conf,
expo,
publish_time,
};
let ema_price = Price {
price: ema_price,
conf: ema_conf,
expo,
publish_time,
};

let price_feed_data = PriceFeed { id, price, ema_price };
let price_feed_data = PriceFeed {
id,
price,
ema_price,
};

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

impl MockPythContract {

fn parse_price_feed_updates_internal(
&mut self,
update_data: Vec<AbiBytes>,
Expand All @@ -186,7 +194,8 @@ impl MockPythContract {

for data in &update_data {
// Decode the update_data
let (price_feed, _prev_publish_time) = match DecodeDataType::abi_decode(data, false) {
let (price_feed, _prev_publish_time) = match DecodeDataType::abi_decode(data, false)
{
Ok(res) => res,
Err(_) => {
return Err(Error::FalledDecodeData(FalledDecodeData {}).into());
Expand All @@ -195,12 +204,8 @@ impl MockPythContract {

if price_feed.id == price_id {
let publish_time = price_feed.price.publish_time;
let previous_publish_time = self
.price_feeds
.get(price_id)
.price
.publish_time
.get();
let previous_publish_time =
self.price_feeds.get(price_id).price.publish_time.get();

// Validate publish time and uniqueness
if publish_time > U256::from(min_publish_time)
Expand Down Expand Up @@ -311,8 +316,7 @@ mod tests {
publish_time,
PREV_PUBLISH_TIME,
);
let price_feed_decoded =
DecodeDataType::abi_decode(&price_feed_created, true).unwrap();
let price_feed_decoded = DecodeDataType::abi_decode(&price_feed_created, true).unwrap();
assert_eq!(price_feed_decoded.0.id, id);
assert_eq!(price_feed_decoded.0.price.price, PRICE);
assert_eq!(price_feed_decoded.0.price.conf, CONF);
Expand Down
Loading

0 comments on commit a1258ed

Please sign in to comment.