Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit 396b7e9

Browse files
jayantkJayant Krishnamurthy
andauthored
Add method for getting twap (#6)
Co-authored-by: Jayant Krishnamurthy <[email protected]>
1 parent 6703266 commit 396b7e9

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

examples/get_accounts.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,18 @@ fn main() {
108108
let pd = clnt.get_account_data( &px_pkey ).unwrap();
109109
let pa = cast::<Price>( &pd );
110110

111-
let maybe_price = pa.get_current_price();
112111
assert_eq!( pa.magic, MAGIC, "not a valid pyth account" );
113112
assert_eq!( pa.atype, AccountType::Price as u32,
114113
"not a valid pyth price account" );
115114
assert_eq!( pa.ver, VERSION_2,
116115
"unexpected pyth price account version" );
117116
println!( " price_account .. {:?}", px_pkey );
117+
118+
let maybe_price = pa.get_current_price();
118119
match maybe_price {
119-
Some((price, confidence, _)) => {
120-
println!(" price ........ {}", price);
121-
println!(" conf ......... {}", confidence);
120+
Some((price, confidence, expo)) => {
121+
println!(" price ........ {} x 10^{}", price, expo);
122+
println!(" conf ......... {} x 10^{}", confidence, expo);
122123
}
123124
None => {
124125
println!(" price ........ unavailable");
@@ -134,7 +135,17 @@ fn main() {
134135
println!( " num_qt ....... {}", pa.num_qt );
135136
println!( " valid_slot ... {}", pa.valid_slot );
136137
println!( " publish_slot . {}", pa.agg.pub_slot );
137-
println!( " twap ......... {}", pa.twap.val );
138+
139+
let maybe_twap = pa.get_twap();
140+
match maybe_twap {
141+
Some((twap, expo)) => {
142+
println!( " twap ......... {} x 10^{}", twap, expo );
143+
}
144+
None => {
145+
println!( " twap ......... unavailable");
146+
}
147+
}
148+
138149
println!( " twac ......... {}", pa.twac.val );
139150

140151
// go to next price account in list

src/lib.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,9 @@ pub struct Price
133133

134134
impl Price {
135135
/**
136-
* Get the current price and confidence interval as fixed-point numbers. Returns a triple of
137-
* the current price, confidence interval, and the exponent for both numbers (i.e., the number
138-
* of decimal places.)
139-
* For example:
136+
* Get the current price and confidence interval as fixed-point numbers of the form a * 10^e.
137+
* Returns a triple of the current price, confidence interval, and the exponent for both
138+
* numbers. For example:
140139
*
141140
* get_current_price() -> Some((12345, 267, -2)) // represents 123.45 +- 2.67
142141
* get_current_price() -> Some((123, 1, 2)) // represents 12300 +- 100
@@ -150,6 +149,20 @@ impl Price {
150149
Some((self.agg.price, self.agg.conf, self.expo))
151150
}
152151
}
152+
153+
/**
154+
* Get the time-weighted average price (TWAP) as a fixed point number of the form a * 10^e.
155+
* Returns a tuple of the current twap and its exponent. For example:
156+
*
157+
* get_twap() -> Some((123, -2)) // represents 1.23
158+
* get_twap() -> Some((45, 3)) // represents 45000
159+
*
160+
* Returns None if the twap is currently unavailable.
161+
*/
162+
pub fn get_twap(&self) -> Option<(i64, i32)> {
163+
// This method currently cannot return None, but may do so in the future.
164+
Some((self.twap.val, self.expo))
165+
}
153166
}
154167

155168
struct AccKeyU64

0 commit comments

Comments
 (0)