Skip to content

Commit 18c9ca6

Browse files
authored
Merge pull request #20 from pyth-network/docs-refactor
Docs refactor
2 parents b7c6f9f + 1e817c2 commit 18c9ca6

File tree

7 files changed

+134
-131
lines changed

7 files changed

+134
-131
lines changed

pyth-sdk-solana/src/state.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ impl Default for CorpAction {
8989
}
9090
}
9191

92-
/// The type of prices associated with a product -- each product may have multiple price feeds of different types.
92+
/// The type of prices associated with a product -- each product may have multiple price feeds of
93+
/// different types.
9394
#[derive(
9495
Copy,
9596
Clone,
@@ -210,10 +211,12 @@ unsafe impl Pod for ProductAccount {
210211
#[repr(C)]
211212
pub struct PriceInfo {
212213
/// the current price.
213-
/// For the aggregate price use price.get_current_price() whenever possible. It has more checks to make sure price is valid.
214+
/// For the aggregate price use price.get_current_price() whenever possible. It has more checks
215+
/// to make sure price is valid.
214216
pub price: i64,
215217
/// confidence interval around the price.
216-
/// For the aggregate confidence use price.get_current_price() whenever possible. It has more checks to make sure price is valid.
218+
/// For the aggregate confidence use price.get_current_price() whenever possible. It has more
219+
/// checks to make sure price is valid.
217220
pub conf: u64,
218221
/// status of price (Trading is valid).
219222
/// For the aggregate status use price.get_current_status() whenever possible.
@@ -388,7 +391,7 @@ fn load<T: Pod>(data: &[u8]) -> Result<&T, PodCastError> {
388391
}
389392
}
390393

391-
/** Get a `Mapping` account from the raw byte value of a Solana account. */
394+
/// Get a `Mapping` account from the raw byte value of a Solana account.
392395
pub fn load_mapping_account(data: &[u8]) -> Result<&MappingAccount, PythError> {
393396
let pyth_mapping = load::<MappingAccount>(&data).map_err(|_| PythError::InvalidAccountData)?;
394397

@@ -405,7 +408,7 @@ pub fn load_mapping_account(data: &[u8]) -> Result<&MappingAccount, PythError> {
405408
return Ok(pyth_mapping);
406409
}
407410

408-
/** Get a `Product` account from the raw byte value of a Solana account. */
411+
/// Get a `Product` account from the raw byte value of a Solana account.
409412
pub fn load_product_account(data: &[u8]) -> Result<&ProductAccount, PythError> {
410413
let pyth_product = load::<ProductAccount>(&data).map_err(|_| PythError::InvalidAccountData)?;
411414

@@ -422,7 +425,7 @@ pub fn load_product_account(data: &[u8]) -> Result<&ProductAccount, PythError> {
422425
return Ok(pyth_product);
423426
}
424427

425-
/** Get a `Price` account from the raw byte value of a Solana account. */
428+
/// Get a `Price` account from the raw byte value of a Solana account.
426429
pub fn load_price_account(data: &[u8]) -> Result<&PriceAccount, PythError> {
427430
let pyth_price = load::<PriceAccount>(&data).map_err(|_| PythError::InvalidAccountData)?;
428431

pyth-sdk-solana/test-contract/src/instruction.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ pub enum PythClientInstruction {
4444
Noop,
4545

4646
PriceStatusCheck {
47-
// A Price serialized as a vector of bytes. This field is stored as a vector of bytes (instead of a Price)
48-
// so that we do not have to add Borsh serialization to all structs, which is expensive.
47+
// A Price serialized as a vector of bytes. This field is stored as a vector of bytes
48+
// (instead of a Price) so that we do not have to add Borsh serialization to all
49+
// structs, which is expensive.
4950
price_account_data: Vec<u8>,
5051
expected_price_status: PriceStatus,
5152
},

pyth-sdk-solana/test-contract/tests/stale_price.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ async fn test_price_not_stale() {
8686
async fn test_price_stale() {
8787
let mut price = price_account_all_zero();
8888
price.agg.status = PriceStatus::Trading;
89-
// Value 100 will cause an overflow because this is bigger than Solana slot in the test suite (its ~1-5).
90-
// As the check will be 5u - 100u ~= 1e18 > MAX_SLOT_DIFFERENCE. It can only break when Solana slot in the test suite becomes
91-
// between 100 and 100+MAX_SLOT_DIFFERENCE.
89+
// Value 100 will cause an overflow because this is bigger than Solana slot in the test suite
90+
// (its ~1-5). As the check will be 5u - 100u ~= 1e18 > MAX_SLOT_DIFFERENCE. It can only
91+
// break when Solana slot in the test suite becomes between 100 and 100+MAX_SLOT_DIFFERENCE.
9292
price.agg.pub_slot = 100;
9393
test_instr_exec_ok(instruction::price_status_check(
9494
&price,

pyth-sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2018"
66
license = "Apache-2.0"
77
homepage = "https://pyth.network"
88
repository = "https://github.com/pyth-network/pyth-sdk-rs"
9-
description = "pyth price oracle data structures and example usage"
9+
description = "Data structures and utilites for the Pyth price oracle"
1010
keywords = [ "pyth", "oracle" ]
1111
readme = "README.md"
1212

pyth-sdk/src/lib.rs

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use schemars::JsonSchema;
88
mod price_conf;
99
pub use price_conf::PriceConf;
1010

11+
/// Consists of 32 bytes and it is currently based on largest Public Key size on various blockchains.
1112
pub type ProductIdentifier = [u8; 32];
1213

1314
/// Represents availability status of a price feed.
@@ -63,26 +64,27 @@ pub struct Price {
6364
pub conf: u64,
6465
/// Status of price (Trading is valid).
6566
pub status: PriceStatus,
66-
/// price exponent
67+
/// Price exponent.
6768
pub expo: i32,
68-
/// maximum number of allowed publishers that can contribute to a price
69+
/// Maximum number of allowed publishers that can contribute to a price.
6970
pub max_num_publishers: u32,
70-
/// number of publishers that made up current aggregate
71+
/// Number of publishers that made up current aggregate.
7172
pub num_publishers: u32,
72-
/// exponentially moving average price
73+
/// Exponentially moving average price.
7374
pub ema_price: i64,
74-
/// exponentially moving average confidence interval
75+
/// Exponentially moving average confidence interval.
7576
pub ema_conf: u64,
76-
/// product account key
77+
/// Product account key.
7778
pub product_id: ProductIdentifier,
7879
}
7980

8081
impl Price {
81-
/**
82-
* Get the current price and confidence interval as fixed-point numbers of the form a * 10^e.
83-
* Returns a struct containing the current price, confidence interval, and the exponent for both
84-
* numbers. Returns `None` if price information is currently unavailable for any reason.
85-
*/
82+
/// Get the current price and confidence interval as fixed-point numbers of the form a *
83+
/// 10^e.
84+
///
85+
/// Returns a struct containing the current price, confidence interval, and the exponent for
86+
/// both numbers. Returns `None` if price information is currently unavailable for any
87+
/// reason.
8688
pub fn get_current_price(&self) -> Option<PriceConf> {
8789
if !matches!(self.status, PriceStatus::Trading) {
8890
None
@@ -95,13 +97,12 @@ impl Price {
9597
}
9698
}
9799

98-
/**
99-
* Get the exponential moving average price (ema_price) and a confidence interval on the result.
100-
* Returns `None` if the ema price is currently unavailable.
101-
*
102-
* At the moment, the confidence interval returned by this method is computed in
103-
* a somewhat questionable way, so we do not recommend using it for high-value applications.
104-
*/
100+
/// Get the exponential moving average price (ema_price) and a confidence interval on the
101+
/// result.
102+
///
103+
/// Returns `None` if the ema price is currently unavailable.
104+
/// At the moment, the confidence interval returned by this method is computed in
105+
/// a somewhat questionable way, so we do not recommend using it for high-value applications.
105106
pub fn get_ema_price(&self) -> Option<PriceConf> {
106107
// This method currently cannot return None, but may do so in the future.
107108
Some(PriceConf {
@@ -111,25 +112,24 @@ impl Price {
111112
})
112113
}
113114

114-
/**
115-
* Get the current price of this account in a different quote currency. If this account
116-
* represents the price of the product X/Z, and `quote` represents the price of the product Y/Z,
117-
* this method returns the price of X/Y. Use this method to get the price of e.g., mSOL/SOL from
118-
* the mSOL/USD and SOL/USD accounts.
119-
*
120-
* `result_expo` determines the exponent of the result, i.e., the number of digits below the decimal
121-
* point. This method returns `None` if either the price or confidence are too large to be
122-
* represented with the requested exponent.
123-
*
124-
* Example:
125-
* ```ignore
126-
* let btc_usd: Price = ...;
127-
* let eth_usd: Price = ...;
128-
* // -8 is the desired exponent for the result
129-
* let btc_eth: PriceConf = btc_usd.get_price_in_quote(&eth_usd, -8);
130-
* println!("BTC/ETH price: ({} +- {}) x 10^{}", price.price, price.conf, price.expo);
131-
* ```
132-
*/
115+
/// Get the current price of this account in a different quote currency.
116+
///
117+
/// If this account represents the price of the product X/Z, and `quote` represents the price
118+
/// of the product Y/Z, this method returns the price of X/Y. Use this method to get the
119+
/// price of e.g., mSOL/SOL from the mSOL/USD and SOL/USD accounts.
120+
///
121+
/// `result_expo` determines the exponent of the result, i.e., the number of digits below the
122+
/// decimal point. This method returns `None` if either the price or confidence are too
123+
/// large to be represented with the requested exponent.
124+
///
125+
/// Example:
126+
/// ```ignore
127+
/// let btc_usd: Price = ...;
128+
/// let eth_usd: Price = ...;
129+
/// // -8 is the desired exponent for the result
130+
/// let btc_eth: PriceConf = btc_usd.get_price_in_quote(&eth_usd, -8);
131+
/// println!("BTC/ETH price: ({} +- {}) x 10^{}", price.price, price.conf, price.expo);
132+
/// ```
133133
pub fn get_price_in_quote(&self, quote: &Price, result_expo: i32) -> Option<PriceConf> {
134134
match (self.get_current_price(), quote.get_current_price()) {
135135
(Some(base_price_conf), Some(quote_price_conf)) => base_price_conf
@@ -139,28 +139,27 @@ impl Price {
139139
}
140140
}
141141

142-
/**
143-
* Get the price of a basket of currencies. Each entry in `amounts` is of the form
144-
* `(price, qty, qty_expo)`, and the result is the sum of `price * qty * 10^qty_expo`.
145-
* The result is returned with exponent `result_expo`.
146-
*
147-
* An example use case for this function is to get the value of an LP token.
148-
*
149-
* Example:
150-
* ```ignore
151-
* let btc_usd: Price = ...;
152-
* let eth_usd: Price = ...;
153-
* // Quantity of each asset in fixed-point a * 10^e.
154-
* // This represents 0.1 BTC and .05 ETH.
155-
* // -8 is desired exponent for result
156-
* let basket_price: PriceConf = Price::price_basket(&[
157-
* (btc_usd, 10, -2),
158-
* (eth_usd, 5, -2)
159-
* ], -8);
160-
* println!("0.1 BTC and 0.05 ETH are worth: ({} +- {}) x 10^{} USD",
161-
* basket_price.price, basket_price.conf, basket_price.expo);
162-
* ```
163-
*/
142+
/// Get the price of a basket of currencies.
143+
///
144+
/// Each entry in `amounts` is of the form `(price, qty, qty_expo)`, and the result is the sum
145+
/// of `price * qty * 10^qty_expo`. The result is returned with exponent `result_expo`.
146+
///
147+
/// An example use case for this function is to get the value of an LP token.
148+
///
149+
/// Example:
150+
/// ```ignore
151+
/// let btc_usd: Price = ...;
152+
/// let eth_usd: Price = ...;
153+
/// // Quantity of each asset in fixed-point a * 10^e.
154+
/// // This represents 0.1 BTC and .05 ETH.
155+
/// // -8 is desired exponent for result
156+
/// let basket_price: PriceConf = Price::price_basket(&[
157+
/// (btc_usd, 10, -2),
158+
/// (eth_usd, 5, -2)
159+
/// ], -8);
160+
/// println!("0.1 BTC and 0.05 ETH are worth: ({} +- {}) x 10^{} USD",
161+
/// basket_price.price, basket_price.conf, basket_price.expo);
162+
/// ```
164163
pub fn price_basket(amounts: &[(Price, i64, i32)], result_expo: i32) -> Option<PriceConf> {
165164
assert!(amounts.len() > 0);
166165
let mut res = PriceConf {

0 commit comments

Comments
 (0)