Skip to content

Commit 8f0b980

Browse files
authored
Create rust structs and replace bindgen structs (#270)
* Create rust structs and replace bindgen structs * Fix comments
1 parent d2646c5 commit 8f0b980

22 files changed

+427
-309
lines changed

program/rust/src/c_oracle_header.rs

Lines changed: 135 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -34,94 +34,156 @@ pub trait PythAccount: Pod {
3434
}
3535
}
3636

37-
impl PythAccount for pc_map_table_t {
37+
impl PythAccount for MappingAccount {
3838
const ACCOUNT_TYPE: u32 = PC_ACCTYPE_MAPPING;
39+
/// Equal to the offset of `prod_` in `MappingAccount`, see the trait comment for more detail
3940
const INITIAL_SIZE: u32 = PC_MAP_TABLE_T_PROD_OFFSET as u32;
4041
}
4142

42-
impl PythAccount for pc_prod_t {
43+
impl PythAccount for ProductAccount {
4344
const ACCOUNT_TYPE: u32 = PC_ACCTYPE_PRODUCT;
44-
const INITIAL_SIZE: u32 = size_of::<pc_prod_t>() as u32;
45+
const INITIAL_SIZE: u32 = size_of::<ProductAccount>() as u32;
4546
fn minimum_size() -> usize {
4647
PC_PROD_ACC_SIZE as usize
4748
}
4849
}
4950

50-
impl PythAccount for pc_price_t {
51+
impl PythAccount for PriceAccount {
5152
const ACCOUNT_TYPE: u32 = PC_ACCTYPE_PRICE;
53+
/// Equal to the offset of `comp_` in `PriceAccount`, see the trait comment for more detail
5254
const INITIAL_SIZE: u32 = PC_PRICE_T_COMP_OFFSET as u32;
5355
}
5456

55-
#[cfg(target_endian = "little")]
56-
unsafe impl Zeroable for pc_acc {
57-
}
58-
59-
#[cfg(target_endian = "little")]
60-
unsafe impl Pod for pc_acc {
61-
}
62-
63-
#[cfg(target_endian = "little")]
64-
unsafe impl Zeroable for pc_map_table {
65-
}
66-
67-
#[cfg(target_endian = "little")]
68-
unsafe impl Pod for pc_map_table {
69-
}
70-
71-
#[cfg(target_endian = "little")]
72-
unsafe impl Zeroable for pc_prod {
73-
}
74-
75-
#[cfg(target_endian = "little")]
76-
unsafe impl Pod for pc_prod {
77-
}
78-
79-
#[cfg(target_endian = "little")]
80-
unsafe impl Zeroable for pc_price {
81-
}
82-
83-
#[cfg(target_endian = "little")]
84-
unsafe impl Pod for pc_price {
85-
}
86-
87-
#[cfg(target_endian = "little")]
88-
unsafe impl Zeroable for pc_price_info {
89-
}
90-
91-
#[cfg(target_endian = "little")]
92-
unsafe impl Pod for pc_price_info {
93-
}
94-
95-
#[cfg(target_endian = "little")]
96-
unsafe impl Zeroable for pc_ema {
97-
}
98-
99-
#[cfg(target_endian = "little")]
100-
unsafe impl Pod for pc_ema {
101-
}
102-
103-
104-
#[cfg(target_endian = "little")]
105-
unsafe impl Zeroable for pc_pub_key_t {
106-
}
107-
108-
#[cfg(target_endian = "little")]
109-
unsafe impl Pod for pc_pub_key_t {
110-
}
111-
112-
113-
#[cfg(target_endian = "little")]
114-
unsafe impl Zeroable for pc_price_comp_t {
115-
}
116-
117-
#[cfg(target_endian = "little")]
118-
unsafe impl Pod for pc_price_comp_t {
119-
}
120-
121-
impl pc_pub_key_t {
122-
pub fn new_unique() -> pc_pub_key_t {
57+
#[repr(C)]
58+
#[derive(Copy, Clone, Pod, Zeroable)]
59+
pub struct PriceAccount {
60+
pub magic_: u32,
61+
pub ver_: u32,
62+
pub type_: u32,
63+
pub size_: u32,
64+
/// Type of the price account
65+
pub ptype_: u32,
66+
/// Exponent for the published prices
67+
pub expo_: i32,
68+
/// Current number of authorized publishers
69+
pub num_: u32,
70+
/// Number of valid quotes for the last aggregation
71+
pub num_qt_: u32,
72+
/// Last slot with a succesful aggregation (status : TRADING)
73+
pub last_slot_: u64,
74+
/// Second to last slot where aggregation was attempted
75+
pub valid_slot_: u64,
76+
/// Ema for price
77+
pub twap_: PriceEma,
78+
/// Ema for confidence
79+
pub twac_: PriceEma,
80+
/// Last time aggregation was attempted
81+
pub timestamp_: i64,
82+
/// Minimum valid publisher quotes for a succesful aggregation
83+
pub min_pub_: u8,
84+
pub unused_1_: i8,
85+
pub unused_2_: i16,
86+
pub unused_3_: i32,
87+
/// Corresponding product account
88+
pub prod_: CPubkey,
89+
/// Next price account in the list
90+
pub next_: CPubkey,
91+
/// Second to last slot where aggregation was succesful (i.e. status : TRADING)
92+
pub prev_slot_: u64,
93+
/// Aggregate price at prev_slot_
94+
pub prev_price_: i64,
95+
/// Confidence interval at prev_slot_
96+
pub prev_conf_: u64,
97+
/// Timestamp of prev_slot_
98+
pub prev_timestamp_: i64,
99+
/// Last attempted aggregate results
100+
pub agg_: PriceInfo,
101+
/// Publishers' price components
102+
pub comp_: [PriceComponent; PC_COMP_SIZE as usize],
103+
}
104+
105+
#[repr(C)]
106+
#[derive(Copy, Clone, Pod, Zeroable)]
107+
pub struct PriceComponent {
108+
pub pub_: CPubkey,
109+
pub agg_: PriceInfo,
110+
pub latest_: PriceInfo,
111+
}
112+
113+
#[repr(C)]
114+
#[derive(Debug, Copy, Clone, Pod, Zeroable)]
115+
pub struct PriceInfo {
116+
pub price_: i64,
117+
pub conf_: u64,
118+
pub status_: u32,
119+
pub corp_act_status_: u32,
120+
pub pub_slot_: u64,
121+
}
122+
123+
#[repr(C)]
124+
#[derive(Debug, Copy, Clone, Pod, Zeroable)]
125+
pub struct PriceEma {
126+
pub val_: i64,
127+
pub numer_: i64,
128+
pub denom_: i64,
129+
}
130+
131+
#[repr(C)]
132+
#[derive(Copy, Clone, Zeroable, Pod)]
133+
pub struct AccountHeader {
134+
pub magic_: u32,
135+
pub ver_: u32,
136+
pub type_: u32,
137+
pub size_: u32,
138+
}
139+
140+
#[repr(C)]
141+
#[derive(Copy, Clone, Pod, Zeroable)]
142+
pub struct ProductAccount {
143+
pub magic_: u32,
144+
pub ver_: u32,
145+
pub type_: u32,
146+
pub size_: u32,
147+
pub px_acc_: CPubkey,
148+
}
149+
150+
#[repr(C)]
151+
#[derive(Copy, Clone)]
152+
pub struct MappingAccount {
153+
pub magic_: u32,
154+
pub ver_: u32,
155+
pub type_: u32,
156+
pub size_: u32,
157+
pub num_: u32,
158+
pub unused_: u32,
159+
pub next_: CPubkey,
160+
pub prod_: [CPubkey; PC_MAP_TABLE_SIZE as usize],
161+
}
162+
163+
// Unsafe impl because CPubkey is a union
164+
unsafe impl Pod for CPubkey {
165+
}
166+
unsafe impl Zeroable for CPubkey {
167+
}
168+
169+
170+
// Unsafe impl because product_list is of size 640 and there's no derived trait for this size
171+
unsafe impl Pod for MappingAccount {
172+
}
173+
unsafe impl Zeroable for MappingAccount {
174+
}
175+
176+
#[repr(C)]
177+
#[derive(Copy, Clone)]
178+
pub union CPubkey {
179+
pub k1_: [u8; 32usize],
180+
pub k8_: [u64; 4usize],
181+
}
182+
183+
impl CPubkey {
184+
pub fn new_unique() -> CPubkey {
123185
let solana_unique = Pubkey::new_unique();
124-
pc_pub_key_t {
186+
CPubkey {
125187
k1_: solana_unique.to_bytes(),
126188
}
127189
}

program/rust/src/deserialize.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bytemuck::{
77
};
88

99
use crate::c_oracle_header::{
10-
pc_acc,
10+
AccountHeader,
1111
PythAccount,
1212
PC_MAGIC,
1313
};
@@ -71,7 +71,7 @@ pub fn load_checked<'a, T: PythAccount>(
7171
version: u32,
7272
) -> Result<RefMut<'a, T>, ProgramError> {
7373
{
74-
let account_header = load_account_as::<pc_acc>(account)?;
74+
let account_header = load_account_as::<AccountHeader>(account)?;
7575
pyth_assert(
7676
account_header.magic_ == PC_MAGIC
7777
&& account_header.ver_ == version
@@ -90,7 +90,7 @@ pub fn initialize_pyth_account_checked<'a, T: PythAccount>(
9090
clear_account(account)?;
9191

9292
{
93-
let mut account_header = load_account_as_mut::<pc_acc>(account)?;
93+
let mut account_header = load_account_as_mut::<AccountHeader>(account)?;
9494
account_header.magic_ = PC_MAGIC;
9595
account_header.ver_ = version;
9696
account_header.type_ = T::ACCOUNT_TYPE;

program/rust/src/instruction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::c_oracle_header::{
2-
pc_pub_key_t,
2+
CPubkey,
33
PC_VERSION,
44
};
55
use crate::deserialize::load;
@@ -122,7 +122,7 @@ pub type InitPriceArgs = AddPriceArgs;
122122
#[derive(Zeroable, Pod, Copy, Clone)]
123123
pub struct AddPublisherArgs {
124124
pub header: CommandHeader,
125-
pub publisher: pc_pub_key_t,
125+
pub publisher: CPubkey,
126126
}
127127

128128
pub type DelPublisherArgs = AddPublisherArgs;

0 commit comments

Comments
 (0)