Skip to content

Commit 26af54b

Browse files
authored
Refactor the code (#28)
1 parent cba4c51 commit 26af54b

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

pyth-sdk-solana/examples/eth_price.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ fn main() {
2020
loop {
2121
// get price data from key
2222
let mut eth_price_account = clnt.get_account(&eth_price_key).unwrap();
23-
let eth_price_feed = load_price_feed_from_account(&eth_price_key, &mut eth_price_account).unwrap();
23+
let eth_price_feed =
24+
load_price_feed_from_account(&eth_price_key, &mut eth_price_account).unwrap();
2425

2526
println!(".....ETH/USD.....");
2627
println!("status .......... {:?}", eth_price_feed.status);

pyth-sdk-solana/src/lib.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ pub use self::error::PythError;
77
mod error;
88
pub mod state;
99

10-
use solana_program::{
11-
account_info::{
12-
Account,
13-
AccountInfo,
14-
IntoAccountInfo,
15-
}, pubkey::Pubkey,
10+
use solana_program::account_info::{
11+
Account,
12+
AccountInfo,
13+
IntoAccountInfo,
1614
};
15+
use solana_program::pubkey::Pubkey;
1716

1817
use state::load_price_account;
1918

@@ -28,8 +27,12 @@ pub use pyth_sdk::{
2827
pub const VALID_SLOT_PERIOD: u64 = 25;
2928

3029
/// Loads Pyth Feed Price from Price Account Info.
31-
pub fn load_price_feed_from_account_info(price_account_info: &AccountInfo) -> Result<PriceFeed, PythError> {
32-
let data = price_account_info.try_borrow_data().map_err(|_| PythError::InvalidAccountData)?;
30+
pub fn load_price_feed_from_account_info(
31+
price_account_info: &AccountInfo,
32+
) -> Result<PriceFeed, PythError> {
33+
let data = price_account_info
34+
.try_borrow_data()
35+
.map_err(|_| PythError::InvalidAccountData)?;
3336
let price_account = load_price_account(*data)?;
3437

3538
Ok(price_account.to_price_feed(&price_account_info.key))
@@ -38,7 +41,10 @@ pub fn load_price_feed_from_account_info(price_account_info: &AccountInfo) -> Re
3841
/// Loads Pyth Price Feed from Account when using Solana Client.
3942
///
4043
/// It is a helper function which constructs Account Info when reading Account in clients.
41-
pub fn load_price_feed_from_account(price_key: &Pubkey, price_account: &mut impl Account) -> Result<PriceFeed, PythError> {
44+
pub fn load_price_feed_from_account(
45+
price_key: &Pubkey,
46+
price_account: &mut impl Account,
47+
) -> Result<PriceFeed, PythError> {
4248
let price_account_info = (price_key, price_account).into_account_info();
4349
load_price_feed_from_account_info(&price_account_info)
4450
}

pyth-sdk/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub struct PriceFeed {
8181
/// Exponentially moving average price.
8282
ema_price: i64,
8383
/// Exponentially moving average confidence interval.
84-
ema_conf: u64,
84+
ema_conf: u64,
8585
}
8686

8787
impl PriceFeed {

pyth-sdk/src/price.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ const PD_SCALE: u64 = 1_000_000_000;
1111
const MAX_PD_V_U64: u64 = (1 << 28) - 1;
1212

1313
/// A price with a degree of uncertainty, represented as a price +- a confidence interval.
14-
///
14+
///
1515
/// The confidence interval roughly corresponds to the standard error of a normal distribution.
1616
/// Both the price and confidence are stored in a fixed-point numeric representation, `x *
1717
/// 10^expo`, where `expo` is the exponent. For example:
18-
///
18+
///
1919
/// ```
2020
/// use pyth_sdk::Price;
2121
/// Price { price: 12345, conf: 267, expo: -2 }; // represents 123.45 +- 2.67
2222
/// Price { price: 123, conf: 1, expo: 2 }; // represents 12300 +- 100
2323
/// ```
24-
///
24+
///
2525
/// `Price` supports a limited set of mathematical operations. All of these operations will
2626
/// propagate any uncertainty in the arguments into the result. However, the uncertainty in the
2727
/// result may overestimate the true uncertainty (by at most a factor of `sqrt(2)`) due to
@@ -53,15 +53,15 @@ pub struct Price {
5353

5454
impl Price {
5555
/// Get the current price of this account in a different quote currency.
56-
///
56+
///
5757
/// If this account represents the price of the product X/Z, and `quote` represents the price
5858
/// of the product Y/Z, this method returns the price of X/Y. Use this method to get the
5959
/// price of e.g., mSOL/SOL from the mSOL/USD and SOL/USD accounts.
60-
///
60+
///
6161
/// `result_expo` determines the exponent of the result, i.e., the number of digits below the
6262
/// decimal point. This method returns `None` if either the price or confidence are too
6363
/// large to be represented with the requested exponent.
64-
///
64+
///
6565
/// Example:
6666
/// ```ignore
6767
/// let btc_usd: Price = ...;
@@ -76,7 +76,7 @@ impl Price {
7676

7777
/// Get the price of a basket of currencies.
7878
///
79-
/// Each entry in `amounts` is of the form `(price, qty, qty_expo)`, and the result is the sum
79+
/// Each entry in `amounts` is of the form `(price, qty, qty_expo)`, and the result is the sum
8080
/// of `price * qty * 10^qty_expo`. The result is returned with exponent `result_expo`.
8181
///
8282
/// An example use case for this function is to get the value of an LP token.
@@ -118,7 +118,7 @@ impl Price {
118118

119119
/// Divide this price by `other` while propagating the uncertainty in both prices into the
120120
/// result.
121-
///
121+
///
122122
/// This method will automatically select a reasonable exponent for the result. If both
123123
/// `self` and `other` are normalized, the exponent is `self.expo + PD_EXPO - other.expo`
124124
/// (i.e., the fraction has `PD_EXPO` digits of additional precision). If they are not
@@ -184,12 +184,12 @@ impl Price {
184184
None
185185
}
186186
}
187-
187+
188188
/// Add `other` to this, propagating uncertainty in both prices.
189-
///
189+
///
190190
/// Requires both `Price`s to have the same exponent -- use `scale_to_exponent` on
191191
/// the arguments if necessary.
192-
///
192+
///
193193
/// TODO: could generalize this method to support different exponents.
194194
pub fn add(&self, other: &Price) -> Option<Price> {
195195
assert_eq!(self.expo, other.expo);
@@ -269,10 +269,10 @@ impl Price {
269269
}
270270

271271
/// Scale this price/confidence so that its exponent is `target_expo`.
272-
///
272+
///
273273
/// Return `None` if this number is outside the range of numbers representable in `target_expo`,
274274
/// which will happen if `target_expo` is too small.
275-
///
275+
///
276276
/// Warning: if `target_expo` is significantly larger than the current exponent, this
277277
/// function will return 0 +- 0.
278278
pub fn scale_to_exponent(&self, target_expo: i32) -> Option<Price> {

0 commit comments

Comments
 (0)