Skip to content

Commit 3c2bf31

Browse files
authored
Add new fields to Price Feed (#39)
Adds publish_time + previous price - Adds `get_prev_price_unchecked` to receive it. - Also modifies Solana account struct to be the same as the oracle - Bumps version of all packages as it is a breaking change.
1 parent d556da5 commit 3c2bf31

File tree

9 files changed

+114
-86
lines changed

9 files changed

+114
-86
lines changed

examples/terra-contract/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ cosmwasm-storage = { version = "0.16.0" }
3434
cw-storage-plus = "0.8.0"
3535
schemars = "0.8"
3636
serde = { version = "1.0", default-features = false, features = ["derive"] }
37-
pyth-sdk-terra = { version = "0.1.0", path = "../../pyth-sdk-terra" } # Remove path and use version only when you use this example on your own.
37+
pyth-sdk-terra = { version = "0.2.0", path = "../../pyth-sdk-terra" } # Remove path and use version only when you use this example on your own.
3838

3939
[dev-dependencies]
4040
cosmwasm-schema = { version = "0.16.0" }

pyth-sdk-solana/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-sdk-solana"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
authors = ["Pyth Data Foundation"]
55
edition = "2018"
66
license = "Apache-2.0"
@@ -19,7 +19,7 @@ num-derive = "0.3"
1919
num-traits = "0.2"
2020
thiserror = "1.0"
2121
serde = { version = "1.0.136", features = ["derive"] }
22-
pyth-sdk = { path = "../pyth-sdk", version = "0.2.0" }
22+
pyth-sdk = { path = "../pyth-sdk", version = "0.3.0" }
2323

2424
[dev-dependencies]
2525
solana-client = "1.8.1"

pyth-sdk-solana/src/state.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -280,49 +280,55 @@ pub struct Rational {
280280
#[repr(C)]
281281
pub struct PriceAccount {
282282
/// pyth magic number
283-
pub magic: u32,
283+
pub magic: u32,
284284
/// program version
285-
pub ver: u32,
285+
pub ver: u32,
286286
/// account type
287-
pub atype: u32,
287+
pub atype: u32,
288288
/// price account size
289-
pub size: u32,
289+
pub size: u32,
290290
/// price or calculation type
291-
pub ptype: PriceType,
291+
pub ptype: PriceType,
292292
/// price exponent
293-
pub expo: i32,
293+
pub expo: i32,
294294
/// number of component prices
295-
pub num: u32,
295+
pub num: u32,
296296
/// number of quoters that make up aggregate
297-
pub num_qt: u32,
297+
pub num_qt: u32,
298298
/// slot of last valid (not unknown) aggregate price
299-
pub last_slot: u64,
299+
pub last_slot: u64,
300300
/// valid slot-time of agg. price
301-
pub valid_slot: u64,
301+
pub valid_slot: u64,
302302
/// exponentially moving average price
303-
pub ema_price: Rational,
303+
pub ema_price: Rational,
304304
/// exponentially moving average confidence interval
305-
pub ema_conf: Rational,
305+
pub ema_conf: Rational,
306+
/// unix timestamp of aggregate price
307+
pub timestamp: i64,
308+
/// min publishers for valid price
309+
pub min_pub: u8,
306310
/// space for future derived values
307-
pub drv1: i64,
311+
pub drv2: u8,
308312
/// space for future derived values
309-
pub drv2: i64,
313+
pub drv3: u16,
314+
/// space for future derived values
315+
pub drv4: u32,
310316
/// product account key
311-
pub prod: AccKey,
317+
pub prod: AccKey,
312318
/// next Price account in linked list
313-
pub next: AccKey,
319+
pub next: AccKey,
314320
/// valid slot of previous update
315-
pub prev_slot: u64,
316-
/// aggregate price of previous update
317-
pub prev_price: i64,
318-
/// confidence interval of previous update
319-
pub prev_conf: u64,
320-
/// space for future derived values
321-
pub drv3: i64,
321+
pub prev_slot: u64,
322+
/// aggregate price of previous update with TRADING status
323+
pub prev_price: i64,
324+
/// confidence interval of previous update with TRADING status
325+
pub prev_conf: u64,
326+
/// unix timestamp of previous aggregate with TRADING status
327+
pub prev_timestamp: i64,
322328
/// aggregate price info
323-
pub agg: PriceInfo,
329+
pub agg: PriceInfo,
324330
/// price components one per quoter
325-
pub comp: [PriceComp; 32],
331+
pub comp: [PriceComp; 32],
326332
}
327333

328334
#[cfg(target_endian = "little")]
@@ -348,6 +354,7 @@ impl PriceAccount {
348354
PriceFeed::new(
349355
price_key.to_bytes(),
350356
status,
357+
self.timestamp,
351358
self.expo,
352359
self.num,
353360
self.num_qt,
@@ -356,6 +363,9 @@ impl PriceAccount {
356363
self.agg.conf,
357364
self.ema_price.val,
358365
self.ema_conf.val as u64,
366+
self.prev_price,
367+
self.prev_conf,
368+
self.prev_timestamp,
359369
)
360370
}
361371
}

pyth-sdk-solana/test-contract/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test-bpf = []
88
no-entrypoint = []
99

1010
[dependencies]
11-
pyth-sdk-solana = { path = "../", version = "0.2.0" }
11+
pyth-sdk-solana = { path = "../", version = "0.3.0" }
1212
solana-program = "1.8.1"
1313
bytemuck = "1.7.2"
1414
borsh = "0.9"

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

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
#![cfg(feature = "test-bpf")] // Only runs on bpf, where solana programs run
22

33
use pyth_sdk_solana::state::{
4-
AccKey,
54
AccountType,
6-
CorpAction,
75
PriceAccount,
8-
PriceComp,
9-
PriceInfo,
106
PriceStatus,
11-
PriceType,
12-
Rational,
137
MAGIC,
148
VERSION_2,
159
};
@@ -21,51 +15,11 @@ mod common;
2115
use common::test_instr_exec_ok;
2216

2317
fn price_account_all_zero() -> PriceAccount {
24-
let acc_key = AccKey { val: [0; 32] };
25-
26-
let rational = Rational {
27-
val: 0,
28-
numer: 0,
29-
denom: 0,
30-
};
31-
32-
let price_info = PriceInfo {
33-
conf: 0,
34-
corp_act: CorpAction::NoCorpAct,
35-
price: 0,
36-
pub_slot: 0,
37-
status: PriceStatus::Unknown,
38-
};
39-
40-
let price_comp = PriceComp {
41-
agg: price_info,
42-
latest: price_info,
43-
publisher: acc_key,
44-
};
45-
4618
PriceAccount {
4719
magic: MAGIC,
4820
ver: VERSION_2,
4921
atype: AccountType::Price as u32,
50-
size: 0,
51-
ptype: PriceType::Price,
52-
expo: 0,
53-
num: 0,
54-
num_qt: 0,
55-
last_slot: 0,
56-
valid_slot: 0,
57-
ema_price: rational,
58-
ema_conf: rational,
59-
drv1: 0,
60-
drv2: 0,
61-
prod: acc_key,
62-
next: acc_key,
63-
prev_slot: 0,
64-
prev_price: 0,
65-
prev_conf: 0,
66-
drv3: 0,
67-
agg: price_info,
68-
comp: [price_comp; 32],
22+
..Default::default()
6923
}
7024
}
7125

pyth-sdk-terra/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-sdk-terra"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
authors = ["Pyth Data Foundation"]
55
edition = "2018"
66
license = "Apache-2.0"
@@ -14,7 +14,7 @@ readme = "README.md"
1414
cosmwasm-std = { version = "0.16.0" }
1515
cosmwasm-storage = { version = "0.16.0" }
1616
serde = { version = "1.0.136", features = ["derive"] }
17-
pyth-sdk = { path = "../pyth-sdk", version = "0.2.0" }
17+
pyth-sdk = { path = "../pyth-sdk", version = "0.3.0" }
1818
schemars = "0.8.1"
1919

2020
[dev-dependencies]

pyth-sdk-terra/schema/price_feed_response.json

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@
2727
"id",
2828
"max_num_publishers",
2929
"num_publishers",
30+
"prev_conf",
31+
"prev_price",
32+
"prev_publish_time",
3033
"price",
3134
"product_id",
35+
"publish_time",
3236
"status"
3337
],
3438
"properties": {
3539
"conf": {
36-
"description": "Confidence interval around the price.",
40+
"description": "Confidence interval around the current aggregation price.",
3741
"type": "integer",
3842
"format": "uint64",
3943
"minimum": 0.0
@@ -77,8 +81,24 @@
7781
"format": "uint32",
7882
"minimum": 0.0
7983
},
84+
"prev_conf": {
85+
"description": "Confidence interval of previous aggregate with Trading status.",
86+
"type": "integer",
87+
"format": "uint64",
88+
"minimum": 0.0
89+
},
90+
"prev_price": {
91+
"description": "Price of previous aggregate with Trading status.",
92+
"type": "integer",
93+
"format": "int64"
94+
},
95+
"prev_publish_time": {
96+
"description": "Publish time of previous aggregate with Trading status.",
97+
"type": "integer",
98+
"format": "int64"
99+
},
80100
"price": {
81-
"description": "The current price.",
101+
"description": "The current aggregation price.",
82102
"type": "integer",
83103
"format": "int64"
84104
},
@@ -93,6 +113,11 @@
93113
"maxItems": 32,
94114
"minItems": 32
95115
},
116+
"publish_time": {
117+
"description": "Current price aggregation publish time",
118+
"type": "integer",
119+
"format": "int64"
120+
},
96121
"status": {
97122
"description": "Status of price (Trading is valid).",
98123
"allOf": [

pyth-sdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-sdk"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
authors = ["Pyth Data Foundation"]
55
edition = "2018"
66
license = "Apache-2.0"

pyth-sdk/src/lib.rs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ pub type PriceIdentifier = [u8; 32];
1515
/// blockchains.
1616
pub type ProductIdentifier = [u8; 32];
1717

18+
/// Unix Timestamp is represented as number of seconds passed since Unix epoch (00:00:00 UTC on 1
19+
/// Jan 1970). It is a signed integer because it's the standard in Unix systems and allows easier
20+
/// time difference.
21+
pub type UnixTimestamp = i64;
22+
1823
/// Represents availability status of a price feed.
1924
#[derive(
2025
Copy,
@@ -66,6 +71,8 @@ pub struct PriceFeed {
6671
pub id: PriceIdentifier,
6772
/// Status of price (Trading is valid).
6873
pub status: PriceStatus,
74+
/// Current price aggregation publish time
75+
pub publish_time: UnixTimestamp,
6976
/// Price exponent.
7077
pub expo: i32,
7178
/// Maximum number of allowed publishers that can contribute to a price.
@@ -74,21 +81,28 @@ pub struct PriceFeed {
7481
pub num_publishers: u32,
7582
/// Product account key.
7683
pub product_id: ProductIdentifier,
77-
/// The current price.
84+
/// The current aggregation price.
7885
price: i64,
79-
/// Confidence interval around the price.
86+
/// Confidence interval around the current aggregation price.
8087
conf: u64,
8188
/// Exponentially moving average price.
8289
ema_price: i64,
8390
/// Exponentially moving average confidence interval.
8491
ema_conf: u64,
92+
/// Price of previous aggregate with Trading status.
93+
prev_price: i64,
94+
/// Confidence interval of previous aggregate with Trading status.
95+
prev_conf: u64,
96+
/// Publish time of previous aggregate with Trading status.
97+
prev_publish_time: UnixTimestamp,
8598
}
8699

87100
impl PriceFeed {
88101
/// Constructs a new Price Feed
89102
pub fn new(
90103
id: PriceIdentifier,
91104
status: PriceStatus,
105+
publish_time: UnixTimestamp,
92106
expo: i32,
93107
max_num_publishers: u32,
94108
num_publishers: u32,
@@ -97,18 +111,25 @@ impl PriceFeed {
97111
conf: u64,
98112
ema_price: i64,
99113
ema_conf: u64,
114+
prev_price: i64,
115+
prev_conf: u64,
116+
prev_publish_time: UnixTimestamp,
100117
) -> PriceFeed {
101118
PriceFeed {
102119
id,
103-
price,
104-
conf,
105120
status,
121+
publish_time,
106122
expo,
107123
max_num_publishers,
108124
num_publishers,
125+
product_id,
126+
price,
127+
conf,
109128
ema_price,
110129
ema_conf,
111-
product_id,
130+
prev_price,
131+
prev_conf,
132+
prev_publish_time,
112133
}
113134
}
114135

@@ -177,4 +198,22 @@ impl PriceFeed {
177198
expo: self.expo,
178199
}
179200
}
201+
202+
/// Get the "unchecked" previous price with Trading status,
203+
/// along with the timestamp at which it was generated.
204+
///
205+
/// WARNING:
206+
/// We make no guarantees about the unchecked price and confidence returned by
207+
/// this function: it could differ significantly from the current price.
208+
/// We strongly encourage you to use `get_current_price` instead.
209+
pub fn get_prev_price_unchecked(&self) -> (Price, UnixTimestamp) {
210+
(
211+
Price {
212+
price: self.prev_price,
213+
conf: self.prev_conf,
214+
expo: self.expo,
215+
},
216+
self.prev_publish_time,
217+
)
218+
}
180219
}

0 commit comments

Comments
 (0)