@@ -8,6 +8,7 @@ use schemars::JsonSchema;
8
8
mod price_conf;
9
9
pub use price_conf:: PriceConf ;
10
10
11
+ /// Consists of 32 bytes and it is currently based on largest Public Key size on various blockchains.
11
12
pub type ProductIdentifier = [ u8 ; 32 ] ;
12
13
13
14
/// Represents availability status of a price feed.
@@ -63,26 +64,27 @@ pub struct Price {
63
64
pub conf : u64 ,
64
65
/// Status of price (Trading is valid).
65
66
pub status : PriceStatus ,
66
- /// price exponent
67
+ /// Price exponent.
67
68
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.
69
70
pub max_num_publishers : u32 ,
70
- /// number of publishers that made up current aggregate
71
+ /// Number of publishers that made up current aggregate.
71
72
pub num_publishers : u32 ,
72
- /// exponentially moving average price
73
+ /// Exponentially moving average price.
73
74
pub ema_price : i64 ,
74
- /// exponentially moving average confidence interval
75
+ /// Exponentially moving average confidence interval.
75
76
pub ema_conf : u64 ,
76
- /// product account key
77
+ /// Product account key.
77
78
pub product_id : ProductIdentifier ,
78
79
}
79
80
80
81
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.
86
88
pub fn get_current_price ( & self ) -> Option < PriceConf > {
87
89
if !matches ! ( self . status, PriceStatus :: Trading ) {
88
90
None
@@ -95,13 +97,12 @@ impl Price {
95
97
}
96
98
}
97
99
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.
105
106
pub fn get_ema_price ( & self ) -> Option < PriceConf > {
106
107
// This method currently cannot return None, but may do so in the future.
107
108
Some ( PriceConf {
@@ -111,25 +112,24 @@ impl Price {
111
112
} )
112
113
}
113
114
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(ð_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(ð_usd, -8);
131
+ /// println!("BTC/ETH price: ({} +- {}) x 10^{}", price.price, price.conf, price.expo);
132
+ /// ```
133
133
pub fn get_price_in_quote ( & self , quote : & Price , result_expo : i32 ) -> Option < PriceConf > {
134
134
match ( self . get_current_price ( ) , quote. get_current_price ( ) ) {
135
135
( Some ( base_price_conf) , Some ( quote_price_conf) ) => base_price_conf
@@ -139,28 +139,27 @@ impl Price {
139
139
}
140
140
}
141
141
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
+ /// ```
164
163
pub fn price_basket ( amounts : & [ ( Price , i64 , i32 ) ] , result_expo : i32 ) -> Option < PriceConf > {
165
164
assert ! ( amounts. len( ) > 0 ) ;
166
165
let mut res = PriceConf {
0 commit comments