Skip to content

Commit 5a828da

Browse files
chore: code format
1 parent a1258ed commit 5a828da

File tree

4 files changed

+60
-88
lines changed

4 files changed

+60
-88
lines changed
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#![cfg_attr(not(test), no_std, no_main)]
22
extern crate alloc;
33

4+
use alloc::vec;
5+
use alloc::vec::Vec;
46
use pyth_stylus::pyth::pyth_contract::PythContract;
57
use stylus_sdk::prelude::{entrypoint, public, sol_storage};
6-
use alloc::vec::Vec;
7-
use alloc::vec;
8-
98

109
sol_storage! {
1110
#[entrypoint]
@@ -19,8 +18,8 @@ sol_storage! {
1918
#[inherit(PythContract)]
2019
impl ExtendPythExample {
2120
/// Returns a vector of bytes containing the data.
22-
fn get_data(&self) -> Vec<u8> {
21+
fn get_data(&self) -> Vec<u8> {
2322
// just reteun data
24-
vec![1,2,3]
23+
vec![1, 2, 3]
2524
}
26-
}
25+
}

target_chains/ethereum/sdk/stylus/examples/extend-pyth-example/tests/extend-pyth.rs

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use e2e::{env, Account, ReceiptExt};
1010

1111
use crate::ExtendPythExample::constructorCall;
1212
use eyre::Result;
13-
use pyth_stylus::pyth::{
14-
mock::create_price_feed_update_data_list, types::Price,
15-
};
13+
use pyth_stylus::pyth::{mock::create_price_feed_update_data_list, types::Price};
1614

1715
mod abi;
1816

@@ -43,7 +41,9 @@ fn ctr(invaid_pyth_address: bool) -> constructorCall {
4341
env("MOCK_PYTH_ADDRESS").unwrap()
4442
};
4543
let address_addr = Address::parse_checksummed(&pyth_addr, None).unwrap();
46-
constructorCall { _pythAddress: address_addr }
44+
constructorCall {
45+
_pythAddress: address_addr,
46+
}
4747
}
4848

4949
#[e2e::test]
@@ -71,10 +71,8 @@ async fn can_get_price_unsafe(alice: Account) -> Result<()> {
7171
.address()?;
7272
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
7373
let id = generate_pyth_id_from_str("ETH");
74-
let ExtendPyth::getPriceUnsafeReturn { price } =
75-
contract.getPriceUnsafe(id).call().await?;
76-
let decoded_price =
77-
Price::abi_decode(&price, false).expect("Failed to decode price");
74+
let ExtendPyth::getPriceUnsafeReturn { price } = contract.getPriceUnsafe(id).call().await?;
75+
let decoded_price = Price::abi_decode(&price, false).expect("Failed to decode price");
7876
assert!(decoded_price.price > 0_i64);
7977
assert!(decoded_price.conf > 0_u64);
8078
assert!(decoded_price.expo > 0_i32);
@@ -83,9 +81,7 @@ async fn can_get_price_unsafe(alice: Account) -> Result<()> {
8381
}
8482

8583
#[e2e::test]
86-
async fn error_provided_invaild_id_get_price_unsafe(
87-
alice: Account,
88-
) -> Result<()> {
84+
async fn error_provided_invaild_id_get_price_unsafe(alice: Account) -> Result<()> {
8985
let contract_addr = alice
9086
.as_deployer()
9187
.with_default_constructor::<constructorCall>()
@@ -111,8 +107,7 @@ async fn can_get_ema_price_unsafe(alice: Account) -> Result<()> {
111107
let id = generate_pyth_id_from_str("ETH");
112108
let ExtendPyth::getEmaPriceUnsafeReturn { price } =
113109
contract.getEmaPriceUnsafe(id).call().await?;
114-
let decoded_price =
115-
Price::abi_decode(&price, false).expect("Failed to decode price");
110+
let decoded_price = Price::abi_decode(&price, false).expect("Failed to decode price");
116111
assert!(decoded_price.price > 0_i64);
117112
assert!(decoded_price.conf > 0_u64);
118113
assert!(decoded_price.expo > 0_i32);
@@ -121,9 +116,7 @@ async fn can_get_ema_price_unsafe(alice: Account) -> Result<()> {
121116
}
122117

123118
#[e2e::test]
124-
async fn error_provided_invaild_id_get_ema_price_unsafe(
125-
alice: Account,
126-
) -> Result<()> {
119+
async fn error_provided_invaild_id_get_ema_price_unsafe(alice: Account) -> Result<()> {
127120
let contract_addr = alice
128121
.as_deployer()
129122
.with_default_constructor::<constructorCall>()
@@ -147,10 +140,11 @@ async fn can_get_price_no_older_than(alice: Account) -> Result<()> {
147140
.address()?;
148141
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
149142
let id = generate_pyth_id_from_str("ETH");
150-
let ExtendPyth::getPriceNoOlderThanReturn { price } =
151-
contract.getPriceNoOlderThan(id, U256::from(1000)).call().await?;
152-
let decoded_price =
153-
Price::abi_decode(&price, false).expect("Failed to decode price");
143+
let ExtendPyth::getPriceNoOlderThanReturn { price } = contract
144+
.getPriceNoOlderThan(id, U256::from(1000))
145+
.call()
146+
.await?;
147+
let decoded_price = Price::abi_decode(&price, false).expect("Failed to decode price");
154148
assert!(decoded_price.price > 0_i64);
155149
assert!(decoded_price.conf > 0_u64);
156150
assert!(decoded_price.expo > 0_i32);
@@ -159,9 +153,7 @@ async fn can_get_price_no_older_than(alice: Account) -> Result<()> {
159153
}
160154

161155
#[e2e::test]
162-
async fn error_provided_invaild_id_get_price_no_older_than(
163-
alice: Account,
164-
) -> Result<()> {
156+
async fn error_provided_invaild_id_get_price_no_older_than(alice: Account) -> Result<()> {
165157
let contract_addr = alice
166158
.as_deployer()
167159
.with_default_constructor::<constructorCall>()
@@ -170,16 +162,16 @@ async fn error_provided_invaild_id_get_price_no_older_than(
170162
.address()?;
171163
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
172164
let id = generate_pyth_id_from_str("BALLON");
173-
let price_result =
174-
contract.getPriceNoOlderThan(id, U256::from(1000)).call().await;
165+
let price_result = contract
166+
.getPriceNoOlderThan(id, U256::from(1000))
167+
.call()
168+
.await;
175169
assert!(price_result.is_err());
176170
Ok(())
177171
}
178172

179173
#[e2e::test]
180-
async fn error_provided_invaild_period_get_price_no_older_than(
181-
alice: Account,
182-
) -> Result<()> {
174+
async fn error_provided_invaild_period_get_price_no_older_than(alice: Account) -> Result<()> {
183175
let contract_addr = alice
184176
.as_deployer()
185177
.with_default_constructor::<constructorCall>()
@@ -188,8 +180,7 @@ async fn error_provided_invaild_period_get_price_no_older_than(
188180
.address()?;
189181
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
190182
let id = generate_pyth_id_from_str("SOL");
191-
let price_result =
192-
contract.getPriceNoOlderThan(id, U256::from(1)).call().await;
183+
let price_result = contract.getPriceNoOlderThan(id, U256::from(1)).call().await;
193184
assert!(price_result.is_err());
194185
Ok(())
195186
}
@@ -204,8 +195,7 @@ async fn can_get_fee(alice: Account) -> Result<()> {
204195
.address()?;
205196
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
206197
let (data, _id) = create_price_feed_update_data_list();
207-
let ExtendPyth::getUpdateFeeReturn { fee } =
208-
contract.getUpdateFee(data).call().await?;
198+
let ExtendPyth::getUpdateFeeReturn { fee } = contract.getUpdateFee(data).call().await?;
209199
assert_eq!(fee, U256::from(300));
210200
Ok(())
211201
}
@@ -225,7 +215,6 @@ async fn can_get_valid_time_peroid(alice: Account) -> Result<()> {
225215
Ok(())
226216
}
227217

228-
229218
#[e2e::test]
230219
async fn can_get_data(alice: Account) -> Result<()> {
231220
let contract_addr = alice
@@ -235,9 +224,7 @@ async fn can_get_data(alice: Account) -> Result<()> {
235224
.await?
236225
.address()?;
237226
let contract = ExtendPyth::new(contract_addr, &alice.wallet);
238-
let ExtendPyth::getDataReturn { data } =
239-
contract.getData().call().await?;
227+
let ExtendPyth::getDataReturn { data } = contract.getData().call().await?;
240228
assert!(data.len() > 0);
241229
Ok(())
242230
}
243-

target_chains/ethereum/sdk/stylus/examples/function-example/src/lib.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use alloy_primitives::U256;
77
use alloy_sol_types::sol;
88
use pyth_stylus::pyth::{
99
functions::{
10-
get_ema_price_no_older_than, get_ema_price_unsafe,
11-
get_price_no_older_than, get_price_unsafe, get_update_fee,
12-
get_valid_time_period, update_price_feeds
10+
get_ema_price_no_older_than, get_ema_price_unsafe, get_price_no_older_than,
11+
get_price_unsafe, get_update_fee, get_valid_time_period, update_price_feeds,
1312
},
1413
mock::create_price_feed_update_data_list,
1514
types::{StoragePrice, StoragePriceFeed},
@@ -49,21 +48,14 @@ pub enum MultiCallErrors {
4948
#[public]
5049
impl FunctionCallsExample {
5150
pub fn get_price_unsafe(&mut self) -> Result<i64, Vec<u8>> {
52-
let price_result = get_price_unsafe(
53-
self,
54-
self.pyth_address.get(),
55-
self.price_id.get(),
56-
)?;
51+
let price_result = get_price_unsafe(self, self.pyth_address.get(), self.price_id.get())?;
5752
self.price.set(price_result);
5853
Ok(price_result.price)
5954
}
6055

6156
pub fn get_ema_price_unsafe(&mut self) -> Result<i64, Vec<u8>> {
62-
let price_result = get_ema_price_unsafe(
63-
self,
64-
self.pyth_address.get(),
65-
self.price_id.get(),
66-
)?;
57+
let price_result =
58+
get_ema_price_unsafe(self, self.pyth_address.get(), self.price_id.get())?;
6759
Ok(price_result.price)
6860
}
6961
pub fn get_price_no_older_than(&mut self) -> Result<i64, Vec<u8>> {
@@ -103,5 +95,4 @@ impl FunctionCallsExample {
10395
let _ = update_price_feeds(self, self.pyth_address.get(), data_bytes)?;
10496
Ok(())
10597
}
106-
107-
}
98+
}
Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
#![cfg_attr(not(test), no_std, no_main)]
22
extern crate alloc;
33

4-
use pyth_stylus::pyth::{pyth_contract::{IPyth, PythContract}, types::Price};
5-
use stylus_sdk::{
6-
abi::Bytes,
7-
alloy_primitives::U256,
8-
msg,
9-
prelude::*,
10-
stylus_proc::SolidityError
11-
};
124
use alloc::vec::Vec;
135
use alloy_sol_types::SolValue;
14-
6+
use pyth_stylus::pyth::{
7+
pyth_contract::{IPyth, PythContract},
8+
types::Price,
9+
};
10+
use stylus_sdk::{abi::Bytes, alloy_primitives::U256, msg, prelude::*, stylus_proc::SolidityError};
1511

1612
pub use sol::*;
1713
#[cfg_attr(coverage_nightly, coverage(off))]
@@ -26,13 +22,11 @@ mod sol {
2622
}
2723
}
2824

29-
3025
#[derive(SolidityError, Debug)]
3126
pub enum Error {
32-
InsufficientFee(InsufficientFee)
27+
InsufficientFee(InsufficientFee),
3328
}
3429

35-
3630
sol_storage! {
3731
#[entrypoint]
3832
struct PythInheritExample {
@@ -43,32 +37,33 @@ sol_storage! {
4337

4438
#[public]
4539
impl PythInheritExample {
46-
fn mint(&mut self) -> Result<(), Vec<u8>> {
47-
// Get the price if it is not older than 60 seconds.
48-
let price = self.pyth.get_ema_price_no_older_than(self.eth_usd_price_id.get(), U256::from(60))?;
49-
let decode_price = Price::abi_decode(&price, false).expect("Failed to decode price");
50-
51-
let eth_price_18_decimals = U256::from(decode_price.price) / U256::from(decode_price.expo);
40+
fn mint(&mut self) -> Result<(), Vec<u8>> {
41+
// Get the price if it is not older than 60 seconds.
42+
let price = self
43+
.pyth
44+
.get_ema_price_no_older_than(self.eth_usd_price_id.get(), U256::from(60))?;
45+
let decode_price = Price::abi_decode(&price, false).expect("Failed to decode price");
46+
47+
let eth_price_18_decimals = U256::from(decode_price.price) / U256::from(decode_price.expo);
5248

53-
let one_dollar_in_wei = U256::MAX / eth_price_18_decimals;
54-
55-
56-
if msg::value() >= one_dollar_in_wei {
57-
// User paid enough money.
58-
// TODO: mint the NFT here
59-
} else {
60-
return Err(Error::InsufficientFee(InsufficientFee {}).into());
49+
let one_dollar_in_wei = U256::MAX / eth_price_18_decimals;
50+
51+
if msg::value() >= one_dollar_in_wei {
52+
// User paid enough money.
53+
// TODO: mint the NFT here
54+
} else {
55+
return Err(Error::InsufficientFee(InsufficientFee {}).into());
56+
}
57+
Ok(())
6158
}
62-
Ok(())
63-
}
6459

65-
fn update_and_mint(&mut self,pyth_price_update: Vec<Bytes>) -> Result<(), Vec<u8>> {
66-
let update_fee = self.pyth.get_update_fee(pyth_price_update.clone())?;
60+
fn update_and_mint(&mut self, pyth_price_update: Vec<Bytes>) -> Result<(), Vec<u8>> {
61+
let update_fee = self.pyth.get_update_fee(pyth_price_update.clone())?;
6762
if update_fee < msg::value() {
6863
return Err(Error::InsufficientFee(InsufficientFee {}).into());
6964
}
7065
self.pyth.update_price_feeds(pyth_price_update)?;
7166
self.mint()?;
7267
Ok(())
7368
}
74-
}
69+
}

0 commit comments

Comments
 (0)