Skip to content

Commit f8970d6

Browse files
authored
Move get_price_in_quote and price_basket to Price (#25)
1 parent d545a61 commit f8970d6

File tree

3 files changed

+65
-71
lines changed

3 files changed

+65
-71
lines changed

pyth-sdk-solana/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ Most assets in Pyth are priced in USD.
9292
Applications can combine two USD prices to price an asset in a different quote currency:
9393

9494
```rust
95-
let btc_usd: PriceFeed = ...;
96-
let eth_usd: PriceFeed = ...;
95+
let btc_usd: Price = ...;
96+
let eth_usd: Price = ...;
9797
// -8 is the desired exponent for the result
9898
let btc_eth: Price = btc_usd.get_price_in_quote(&eth_usd, -8);
9999
println!("BTC/ETH price: ({} +- {}) x 10^{}", price.price, price.conf, price.expo);
@@ -104,8 +104,8 @@ println!("BTC/ETH price: ({} +- {}) x 10^{}", price.price, price.conf, price.exp
104104
Applications can also compute the value of a basket of multiple assets:
105105

106106
```rust
107-
let btc_usd: PriceFeed = ...;
108-
let eth_usd: PriceFeed = ...;
107+
let btc_usd: Price = ...;
108+
let eth_usd: Price = ...;
109109
// Quantity of each asset in fixed-point a * 10^e.
110110
// This represents 0.1 BTC and .05 ETH.
111111
// -8 is desired exponent for result

pyth-sdk/src/lib.rs

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -117,71 +117,4 @@ impl PriceFeed {
117117
expo: self.expo,
118118
})
119119
}
120-
121-
/// Get the current price of this account in a different quote currency.
122-
///
123-
/// If this account represents the price of the product X/Z, and `quote` represents the price
124-
/// of the product Y/Z, this method returns the price of X/Y. Use this method to get the
125-
/// price of e.g., mSOL/SOL from the mSOL/USD and SOL/USD accounts.
126-
///
127-
/// `result_expo` determines the exponent of the result, i.e., the number of digits below the
128-
/// decimal point. This method returns `None` if either the price or confidence are too
129-
/// large to be represented with the requested exponent.
130-
///
131-
/// Example:
132-
/// ```ignore
133-
/// let btc_usd: PriceFeed = ...;
134-
/// let eth_usd: PriceFeed = ...;
135-
/// // -8 is the desired exponent for the result
136-
/// let btc_eth: Price = btc_usd.get_price_in_quote(&eth_usd, -8);
137-
/// println!("BTC/ETH price: ({} +- {}) x 10^{}", price.price, price.conf, price.expo);
138-
/// ```
139-
pub fn get_price_in_quote(&self, quote: &PriceFeed, result_expo: i32) -> Option<Price> {
140-
match (self.get_current_price(), quote.get_current_price()) {
141-
(Some(base_price_conf), Some(quote_price_conf)) => base_price_conf
142-
.div(&quote_price_conf)?
143-
.scale_to_exponent(result_expo),
144-
(_, _) => None,
145-
}
146-
}
147-
148-
/// Get the price of a basket of currencies.
149-
///
150-
/// Each entry in `amounts` is of the form `(price, qty, qty_expo)`, and the result is the sum
151-
/// of `price * qty * 10^qty_expo`. The result is returned with exponent `result_expo`.
152-
///
153-
/// An example use case for this function is to get the value of an LP token.
154-
///
155-
/// Example:
156-
/// ```ignore
157-
/// let btc_usd: PriceFeed = ...;
158-
/// let eth_usd: PriceFeed = ...;
159-
/// // Quantity of each asset in fixed-point a * 10^e.
160-
/// // This represents 0.1 BTC and .05 ETH.
161-
/// // -8 is desired exponent for result
162-
/// let basket_price: Price = Price::price_basket(&[
163-
/// (btc_usd, 10, -2),
164-
/// (eth_usd, 5, -2)
165-
/// ], -8);
166-
/// println!("0.1 BTC and 0.05 ETH are worth: ({} +- {}) x 10^{} USD",
167-
/// basket_price.price, basket_price.conf, basket_price.expo);
168-
/// ```
169-
pub fn price_basket(amounts: &[(PriceFeed, i64, i32)], result_expo: i32) -> Option<Price> {
170-
assert!(amounts.len() > 0);
171-
let mut res = Price {
172-
price: 0,
173-
conf: 0,
174-
expo: result_expo,
175-
};
176-
for i in 0..amounts.len() {
177-
res = res.add(
178-
&amounts[i]
179-
.0
180-
.get_current_price()?
181-
.cmul(amounts[i].1, amounts[i].2)?
182-
.scale_to_exponent(result_expo)?,
183-
)?
184-
}
185-
Some(res)
186-
}
187120
}

pyth-sdk/src/price.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,67 @@ pub struct Price {
5252
}
5353

5454
impl Price {
55+
/// Get the current price of this account in a different quote currency.
56+
///
57+
/// If this account represents the price of the product X/Z, and `quote` represents the price
58+
/// of the product Y/Z, this method returns the price of X/Y. Use this method to get the
59+
/// price of e.g., mSOL/SOL from the mSOL/USD and SOL/USD accounts.
60+
///
61+
/// `result_expo` determines the exponent of the result, i.e., the number of digits below the
62+
/// decimal point. This method returns `None` if either the price or confidence are too
63+
/// large to be represented with the requested exponent.
64+
///
65+
/// Example:
66+
/// ```ignore
67+
/// let btc_usd: Price = ...;
68+
/// let eth_usd: Price = ...;
69+
/// // -8 is the desired exponent for the result
70+
/// let btc_eth: Price = btc_usd.get_price_in_quote(&eth_usd, -8);
71+
/// println!("BTC/ETH price: ({} +- {}) x 10^{}", price.price, price.conf, price.expo);
72+
/// ```
73+
pub fn get_price_in_quote(&self, quote: &Price, result_expo: i32) -> Option<Price> {
74+
self.div(quote)?.scale_to_exponent(result_expo)
75+
}
76+
77+
/// Get the price of a basket of currencies.
78+
///
79+
/// Each entry in `amounts` is of the form `(price, qty, qty_expo)`, and the result is the sum
80+
/// of `price * qty * 10^qty_expo`. The result is returned with exponent `result_expo`.
81+
///
82+
/// An example use case for this function is to get the value of an LP token.
83+
///
84+
/// Example:
85+
/// ```ignore
86+
/// let btc_usd: Price = ...;
87+
/// let eth_usd: Price = ...;
88+
/// // Quantity of each asset in fixed-point a * 10^e.
89+
/// // This represents 0.1 BTC and .05 ETH.
90+
/// // -8 is desired exponent for result
91+
/// let basket_price: Price = Price::price_basket(&[
92+
/// (btc_usd, 10, -2),
93+
/// (eth_usd, 5, -2)
94+
/// ], -8);
95+
/// println!("0.1 BTC and 0.05 ETH are worth: ({} +- {}) x 10^{} USD",
96+
/// basket_price.price, basket_price.conf, basket_price.expo);
97+
/// ```
98+
pub fn price_basket(amounts: &[(Price, i64, i32)], result_expo: i32) -> Option<Price> {
99+
assert!(amounts.len() > 0);
100+
let mut res = Price {
101+
price: 0,
102+
conf: 0,
103+
expo: result_expo,
104+
};
105+
for i in 0..amounts.len() {
106+
res = res.add(
107+
&amounts[i]
108+
.0
109+
.cmul(amounts[i].1, amounts[i].2)?
110+
.scale_to_exponent(result_expo)?,
111+
)?
112+
}
113+
Some(res)
114+
}
115+
55116
/// Divide this price by `other` while propagating the uncertainty in both prices into the
56117
/// result.
57118
///

0 commit comments

Comments
 (0)