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 a1258ed commit 5a828da
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![cfg_attr(not(test), no_std, no_main)]
extern crate alloc;

use alloc::vec;
use alloc::vec::Vec;
use pyth_stylus::pyth::pyth_contract::PythContract;
use stylus_sdk::prelude::{entrypoint, public, sol_storage};
use alloc::vec::Vec;
use alloc::vec;


sol_storage! {
#[entrypoint]
Expand All @@ -19,8 +18,8 @@ sol_storage! {
#[inherit(PythContract)]
impl ExtendPythExample {
/// Returns a vector of bytes containing the data.
fn get_data(&self) -> Vec<u8> {
fn get_data(&self) -> Vec<u8> {
// just reteun data
vec![1,2,3]
vec![1, 2, 3]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ use e2e::{env, Account, ReceiptExt};

use crate::ExtendPythExample::constructorCall;
use eyre::Result;
use pyth_stylus::pyth::{
mock::create_price_feed_update_data_list, types::Price,
};
use pyth_stylus::pyth::{mock::create_price_feed_update_data_list, types::Price};

mod abi;

Expand Down Expand Up @@ -43,7 +41,9 @@ fn ctr(invaid_pyth_address: bool) -> constructorCall {
env("MOCK_PYTH_ADDRESS").unwrap()
};
let address_addr = Address::parse_checksummed(&pyth_addr, None).unwrap();
constructorCall { _pythAddress: address_addr }
constructorCall {
_pythAddress: address_addr,
}
}

#[e2e::test]
Expand Down Expand Up @@ -71,10 +71,8 @@ async fn can_get_price_unsafe(alice: Account) -> Result<()> {
.address()?;
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
let id = generate_pyth_id_from_str("ETH");
let ExtendPyth::getPriceUnsafeReturn { price } =
contract.getPriceUnsafe(id).call().await?;
let decoded_price =
Price::abi_decode(&price, false).expect("Failed to decode price");
let ExtendPyth::getPriceUnsafeReturn { price } = contract.getPriceUnsafe(id).call().await?;
let decoded_price = Price::abi_decode(&price, false).expect("Failed to decode price");
assert!(decoded_price.price > 0_i64);
assert!(decoded_price.conf > 0_u64);
assert!(decoded_price.expo > 0_i32);
Expand All @@ -83,9 +81,7 @@ async fn can_get_price_unsafe(alice: Account) -> Result<()> {
}

#[e2e::test]
async fn error_provided_invaild_id_get_price_unsafe(
alice: Account,
) -> Result<()> {
async fn error_provided_invaild_id_get_price_unsafe(alice: Account) -> Result<()> {
let contract_addr = alice
.as_deployer()
.with_default_constructor::<constructorCall>()
Expand All @@ -111,8 +107,7 @@ async fn can_get_ema_price_unsafe(alice: Account) -> Result<()> {
let id = generate_pyth_id_from_str("ETH");
let ExtendPyth::getEmaPriceUnsafeReturn { price } =
contract.getEmaPriceUnsafe(id).call().await?;
let decoded_price =
Price::abi_decode(&price, false).expect("Failed to decode price");
let decoded_price = Price::abi_decode(&price, false).expect("Failed to decode price");
assert!(decoded_price.price > 0_i64);
assert!(decoded_price.conf > 0_u64);
assert!(decoded_price.expo > 0_i32);
Expand All @@ -121,9 +116,7 @@ async fn can_get_ema_price_unsafe(alice: Account) -> Result<()> {
}

#[e2e::test]
async fn error_provided_invaild_id_get_ema_price_unsafe(
alice: Account,
) -> Result<()> {
async fn error_provided_invaild_id_get_ema_price_unsafe(alice: Account) -> Result<()> {
let contract_addr = alice
.as_deployer()
.with_default_constructor::<constructorCall>()
Expand All @@ -147,10 +140,11 @@ async fn can_get_price_no_older_than(alice: Account) -> Result<()> {
.address()?;
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
let id = generate_pyth_id_from_str("ETH");
let ExtendPyth::getPriceNoOlderThanReturn { price } =
contract.getPriceNoOlderThan(id, U256::from(1000)).call().await?;
let decoded_price =
Price::abi_decode(&price, false).expect("Failed to decode price");
let ExtendPyth::getPriceNoOlderThanReturn { price } = contract
.getPriceNoOlderThan(id, U256::from(1000))
.call()
.await?;
let decoded_price = Price::abi_decode(&price, false).expect("Failed to decode price");
assert!(decoded_price.price > 0_i64);
assert!(decoded_price.conf > 0_u64);
assert!(decoded_price.expo > 0_i32);
Expand All @@ -159,9 +153,7 @@ async fn can_get_price_no_older_than(alice: Account) -> Result<()> {
}

#[e2e::test]
async fn error_provided_invaild_id_get_price_no_older_than(
alice: Account,
) -> Result<()> {
async fn error_provided_invaild_id_get_price_no_older_than(alice: Account) -> Result<()> {
let contract_addr = alice
.as_deployer()
.with_default_constructor::<constructorCall>()
Expand All @@ -170,16 +162,16 @@ async fn error_provided_invaild_id_get_price_no_older_than(
.address()?;
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
let id = generate_pyth_id_from_str("BALLON");
let price_result =
contract.getPriceNoOlderThan(id, U256::from(1000)).call().await;
let price_result = contract
.getPriceNoOlderThan(id, U256::from(1000))
.call()
.await;
assert!(price_result.is_err());
Ok(())
}

#[e2e::test]
async fn error_provided_invaild_period_get_price_no_older_than(
alice: Account,
) -> Result<()> {
async fn error_provided_invaild_period_get_price_no_older_than(alice: Account) -> Result<()> {
let contract_addr = alice
.as_deployer()
.with_default_constructor::<constructorCall>()
Expand All @@ -188,8 +180,7 @@ async fn error_provided_invaild_period_get_price_no_older_than(
.address()?;
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
let id = generate_pyth_id_from_str("SOL");
let price_result =
contract.getPriceNoOlderThan(id, U256::from(1)).call().await;
let price_result = contract.getPriceNoOlderThan(id, U256::from(1)).call().await;
assert!(price_result.is_err());
Ok(())
}
Expand All @@ -204,8 +195,7 @@ async fn can_get_fee(alice: Account) -> Result<()> {
.address()?;
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
let (data, _id) = create_price_feed_update_data_list();
let ExtendPyth::getUpdateFeeReturn { fee } =
contract.getUpdateFee(data).call().await?;
let ExtendPyth::getUpdateFeeReturn { fee } = contract.getUpdateFee(data).call().await?;
assert_eq!(fee, U256::from(300));
Ok(())
}
Expand All @@ -225,7 +215,6 @@ async fn can_get_valid_time_peroid(alice: Account) -> Result<()> {
Ok(())
}


#[e2e::test]
async fn can_get_data(alice: Account) -> Result<()> {
let contract_addr = alice
Expand All @@ -235,9 +224,7 @@ async fn can_get_data(alice: Account) -> Result<()> {
.await?
.address()?;
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
let ExtendPyth::getDataReturn { data } =
contract.getData().call().await?;
let ExtendPyth::getDataReturn { data } = contract.getData().call().await?;
assert!(data.len() > 0);
Ok(())
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use alloy_primitives::U256;
use alloy_sol_types::sol;
use pyth_stylus::pyth::{
functions::{
get_ema_price_no_older_than, get_ema_price_unsafe,
get_price_no_older_than, get_price_unsafe, get_update_fee,
get_valid_time_period, update_price_feeds
get_ema_price_no_older_than, get_ema_price_unsafe, get_price_no_older_than,
get_price_unsafe, get_update_fee, get_valid_time_period, update_price_feeds,
},
mock::create_price_feed_update_data_list,
types::{StoragePrice, StoragePriceFeed},
Expand Down Expand Up @@ -49,21 +48,14 @@ pub enum MultiCallErrors {
#[public]
impl FunctionCallsExample {
pub fn get_price_unsafe(&mut self) -> Result<i64, Vec<u8>> {
let price_result = get_price_unsafe(
self,
self.pyth_address.get(),
self.price_id.get(),
)?;
let price_result = get_price_unsafe(self, self.pyth_address.get(), self.price_id.get())?;
self.price.set(price_result);
Ok(price_result.price)
}

pub fn get_ema_price_unsafe(&mut self) -> Result<i64, Vec<u8>> {
let price_result = get_ema_price_unsafe(
self,
self.pyth_address.get(),
self.price_id.get(),
)?;
let price_result =
get_ema_price_unsafe(self, self.pyth_address.get(), self.price_id.get())?;
Ok(price_result.price)
}
pub fn get_price_no_older_than(&mut self) -> Result<i64, Vec<u8>> {
Expand Down Expand Up @@ -103,5 +95,4 @@ impl FunctionCallsExample {
let _ = update_price_feeds(self, self.pyth_address.get(), data_bytes)?;
Ok(())
}

}
}
57 changes: 26 additions & 31 deletions target_chains/ethereum/sdk/stylus/examples/pyth-example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#![cfg_attr(not(test), no_std, no_main)]
extern crate alloc;

use pyth_stylus::pyth::{pyth_contract::{IPyth, PythContract}, types::Price};
use stylus_sdk::{
abi::Bytes,
alloy_primitives::U256,
msg,
prelude::*,
stylus_proc::SolidityError
};
use alloc::vec::Vec;
use alloy_sol_types::SolValue;

use pyth_stylus::pyth::{
pyth_contract::{IPyth, PythContract},
types::Price,
};
use stylus_sdk::{abi::Bytes, alloy_primitives::U256, msg, prelude::*, stylus_proc::SolidityError};

pub use sol::*;
#[cfg_attr(coverage_nightly, coverage(off))]
Expand All @@ -26,13 +22,11 @@ mod sol {
}
}


#[derive(SolidityError, Debug)]
pub enum Error {
InsufficientFee(InsufficientFee)
InsufficientFee(InsufficientFee),
}


sol_storage! {
#[entrypoint]
struct PythInheritExample {
Expand All @@ -43,32 +37,33 @@ sol_storage! {

#[public]
impl PythInheritExample {
fn mint(&mut self) -> Result<(), Vec<u8>> {
// Get the price if it is not older than 60 seconds.
let price = self.pyth.get_ema_price_no_older_than(self.eth_usd_price_id.get(), U256::from(60))?;
let decode_price = Price::abi_decode(&price, false).expect("Failed to decode price");

let eth_price_18_decimals = U256::from(decode_price.price) / U256::from(decode_price.expo);
fn mint(&mut self) -> Result<(), Vec<u8>> {
// Get the price if it is not older than 60 seconds.
let price = self
.pyth
.get_ema_price_no_older_than(self.eth_usd_price_id.get(), U256::from(60))?;
let decode_price = Price::abi_decode(&price, false).expect("Failed to decode price");

let eth_price_18_decimals = U256::from(decode_price.price) / U256::from(decode_price.expo);

let one_dollar_in_wei = U256::MAX / eth_price_18_decimals;


if msg::value() >= one_dollar_in_wei {
// User paid enough money.
// TODO: mint the NFT here
} else {
return Err(Error::InsufficientFee(InsufficientFee {}).into());
let one_dollar_in_wei = U256::MAX / eth_price_18_decimals;

if msg::value() >= one_dollar_in_wei {
// User paid enough money.
// TODO: mint the NFT here
} else {
return Err(Error::InsufficientFee(InsufficientFee {}).into());
}
Ok(())
}
Ok(())
}

fn update_and_mint(&mut self,pyth_price_update: Vec<Bytes>) -> Result<(), Vec<u8>> {
let update_fee = self.pyth.get_update_fee(pyth_price_update.clone())?;
fn update_and_mint(&mut self, pyth_price_update: Vec<Bytes>) -> Result<(), Vec<u8>> {
let update_fee = self.pyth.get_update_fee(pyth_price_update.clone())?;
if update_fee < msg::value() {
return Err(Error::InsufficientFee(InsufficientFee {}).into());
}
self.pyth.update_price_feeds(pyth_price_update)?;
self.mint()?;
Ok(())
}
}
}

0 comments on commit 5a828da

Please sign in to comment.