Skip to content

Commit

Permalink
Merge pull request #93 from openbook-dex/feature/pubkey-option
Browse files Browse the repository at this point in the history
NonZeroPubkeyOption
  • Loading branch information
binyebarwe authored Jul 7, 2023
2 parents bb757a3 + 513418a commit 15d6ba0
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 93 deletions.
8 changes: 4 additions & 4 deletions programs/openbook-v2/src/instructions/create_market.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anchor_lang::prelude::*;

use crate::error::*;
use crate::pod_option::PodOption;
use crate::pubkey_option::NonZeroPubkeyOption;
use crate::state::*;
use crate::util::fill_from_str;

Expand Down Expand Up @@ -32,21 +32,21 @@ pub fn create_market(
OpenBookError::InvalidInputMarketExpired
);

let open_orders_admin: PodOption<Pubkey> = ctx
let open_orders_admin: NonZeroPubkeyOption = ctx
.accounts
.open_orders_admin
.as_ref()
.map(|account| account.key())
.into();

let consume_events_admin: PodOption<Pubkey> = ctx
let consume_events_admin: NonZeroPubkeyOption = ctx
.accounts
.consume_events_admin
.as_ref()
.map(|account| account.key())
.into();

let close_market_admin: PodOption<Pubkey> = ctx
let close_market_admin: NonZeroPubkeyOption = ctx
.accounts
.close_market_admin
.as_ref()
Expand Down
4 changes: 2 additions & 2 deletions programs/openbook-v2/src/instructions/init_open_orders.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::accounts_ix::InitOpenOrders;
use crate::pod_option::PodOption;
use crate::pubkey_option::NonZeroPubkeyOption;
use crate::state::*;
use anchor_lang::prelude::*;

pub fn init_open_orders(ctx: Context<InitOpenOrders>, account_num: u32) -> Result<()> {
let mut account = ctx.accounts.open_orders_account.load_init()?;

let delegate_account: PodOption<Pubkey> = ctx
let delegate_account: NonZeroPubkeyOption = ctx
.accounts
.delegate_account
.as_ref()
Expand Down
4 changes: 2 additions & 2 deletions programs/openbook-v2/src/instructions/set_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use anchor_lang::prelude::*;

use crate::accounts_ix::*;
use crate::pod_option::PodOption;
use crate::pubkey_option::NonZeroPubkeyOption;

pub fn set_delegate(ctx: Context<SetDelegate>) -> Result<()> {
let mut account = ctx.accounts.open_orders_account.load_mut()?;

let delegate_account: PodOption<Pubkey> = ctx
let delegate_account: NonZeroPubkeyOption = ctx
.accounts
.delegate_account
.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion programs/openbook-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod accounts_zerocopy;
pub mod error;
pub mod i80f48;
pub mod logs;
pub mod pod_option;
pub mod pubkey_option;
pub mod state;
pub mod types;

Expand Down
72 changes: 0 additions & 72 deletions programs/openbook-v2/src/pod_option.rs

This file was deleted.

51 changes: 51 additions & 0 deletions programs/openbook-v2/src/pubkey_option.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use anchor_lang::prelude::*;
use bytemuck::Zeroable;
use std::convert::From;

/// Like `Option`, but implemented for `Pubkey`.
#[zero_copy]
#[derive(AnchorSerialize, AnchorDeserialize, Debug, Default)]
pub struct NonZeroPubkeyOption {
key: Pubkey,
}

impl From<NonZeroPubkeyOption> for Option<Pubkey> {
fn from(pubkey_option: NonZeroPubkeyOption) -> Self {
if pubkey_option.key == Pubkey::zeroed() {
None
} else {
Some(pubkey_option.key)
}
}
}

impl From<Option<Pubkey>> for NonZeroPubkeyOption {
fn from(normal_option: Option<Pubkey>) -> Self {
match normal_option {
Some(key) => Self { key },
None => Self::zeroed(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
pub fn test_some() {
let pubkey_option: NonZeroPubkeyOption = Some(crate::ID).into();
assert_eq!(Option::<Pubkey>::from(pubkey_option), Some(crate::ID));
}

#[test]
pub fn test_none() {
let pubkey_option: NonZeroPubkeyOption = None.into();
assert_eq!(Option::<Pubkey>::from(pubkey_option), None);

// the default pubkey also matches none
assert_eq!(Pubkey::default(), Pubkey::zeroed());
let pubkey_option: NonZeroPubkeyOption = Some(Pubkey::default()).into();
assert_eq!(Option::<Pubkey>::from(pubkey_option), None);
}
}
16 changes: 8 additions & 8 deletions programs/openbook-v2/src/state/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::convert::{TryFrom, TryInto};
use std::mem::size_of;

use crate::error::OpenBookError;
use crate::pod_option::PodOption;
use crate::pubkey_option::NonZeroPubkeyOption;
use crate::state::oracle;
use crate::{accounts_zerocopy::KeyedAccountReader, state::orderbook::Side};

Expand Down Expand Up @@ -36,11 +36,11 @@ pub struct Market {
/// Admin who can collect fees from the market
pub collect_fee_admin: Pubkey,
/// Admin who must sign off on all order creations
pub open_orders_admin: PodOption<Pubkey>,
pub open_orders_admin: NonZeroPubkeyOption,
/// Admin who must sign off on all event consumptions
pub consume_events_admin: PodOption<Pubkey>,
pub consume_events_admin: NonZeroPubkeyOption,
/// Admin who can set market expired, prune orders and close the market
pub close_market_admin: PodOption<Pubkey>,
pub close_market_admin: NonZeroPubkeyOption,

/// Name. Trailing zero bytes are ignored.
pub name: [u8; 16],
Expand Down Expand Up @@ -114,9 +114,9 @@ pub struct Market {
const_assert_eq!(
size_of::<Market>(),
32 + // collect_fee_admin
40 + // open_order_admin
40 + // consume_event_admin
40 + // close_market_admin
32 + // open_order_admin
32 + // consume_event_admin
32 + // close_market_admin
size_of::<MarketIndex>() + // MarketIndex
1 + // bump
1 + // base_decimals
Expand Down Expand Up @@ -144,7 +144,7 @@ const_assert_eq!(
8 + // referrer_rebates_accrued
1768 // reserved
);
const_assert_eq!(size_of::<Market>(), 2416);
const_assert_eq!(size_of::<Market>(), 2392);
const_assert_eq!(size_of::<Market>() % 8, 0);

impl Market {
Expand Down
8 changes: 4 additions & 4 deletions programs/openbook-v2/src/state/open_orders_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::mem::size_of;

use crate::error::*;
use crate::logs::FillLog;
use crate::pod_option::PodOption;
use crate::pubkey_option::NonZeroPubkeyOption;

use super::FillEvent;
use super::LeafNode;
Expand All @@ -26,7 +26,7 @@ pub struct OpenOrdersAccount {
pub name: [u8; 32],

// Alternative authority/signer of transactions for a openbook account
pub delegate: PodOption<Pubkey>,
pub delegate: NonZeroPubkeyOption,

pub account_num: u32,

Expand All @@ -43,14 +43,14 @@ const_assert_eq!(
size_of::<OpenOrdersAccount>(),
size_of::<Pubkey>() * 2
+ 32
+ 40
+ 32
+ 4
+ 1
+ 3
+ size_of::<Position>()
+ MAX_OPEN_ORDERS * size_of::<OpenOrder>()
);
const_assert_eq!(size_of::<OpenOrdersAccount>(), 9512);
const_assert_eq!(size_of::<OpenOrdersAccount>(), 9504);
const_assert_eq!(size_of::<OpenOrdersAccount>() % 8, 0);

impl OpenOrdersAccount {
Expand Down

0 comments on commit 15d6ba0

Please sign in to comment.