Skip to content

Commit

Permalink
reverse back to PodOption for delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
binyebarwe committed Jun 19, 2023
1 parent 6c8be6c commit 85198a6
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion programs/openbook-v2/src/accounts_ix/init_open_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct InitOpenOrders<'info> {
pub owner: Signer<'info>,
#[account(mut)]
pub payer: Signer<'info>,
#[account()]
/// CHECK:
pub delegate_account: Option<UncheckedAccount<'info>>,
#[account(mut)]
pub market: AccountLoader<'info, Market>,
Expand Down
2 changes: 2 additions & 0 deletions programs/openbook-v2/src/accounts_ix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use init_open_orders::*;
pub use place_order::*;
pub use place_take_order::*;
pub use prune_orders::*;
pub use set_delegate::*;
pub use set_market_expired::*;
pub use settle_funds::*;
pub use stub_oracle_close::*;
Expand All @@ -29,6 +30,7 @@ mod init_open_orders;
mod place_order;
mod place_take_order;
mod prune_orders;
mod set_delegate;
mod set_market_expired;
mod settle_funds;
mod stub_oracle_close;
Expand Down
8 changes: 3 additions & 5 deletions programs/openbook-v2/src/accounts_ix/set_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use anchor_lang::prelude::*;

use crate::state::{Market, OpenOrdersAccount, OpenOrdersAccountFixed};
use crate::state::OpenOrdersAccountFixed;

#[derive(Accounts)]
pub struct SetDelegate<'info> {
#[account(
mut,
has_one = market,
has_one = owner,
)]
pub open_orders_account: AccountLoader<'info, OpenOrdersAccountFixed>,
#[account(mut)]
pub owner: Signer<'info>,
#[account()]
/// CHECK:
pub delegate_account: Option<UncheckedAccount<'info>>,
#[account(mut)]
pub market: AccountLoader<'info, Market>,
}
14 changes: 9 additions & 5 deletions programs/openbook-v2/src/instructions/init_open_orders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::accounts_ix::InitOpenOrders;
use crate::pod_option::PodOption;
use crate::state::*;
use anchor_lang::prelude::*;

Expand All @@ -9,15 +10,18 @@ pub fn init_open_orders(
) -> Result<()> {
let mut account = ctx.accounts.open_orders_account.load_full_init()?;

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

account.fixed.account_num = account_num;
account.fixed.market = ctx.accounts.market.key();
account.fixed.bump = *ctx.bumps.get("open_orders_account").unwrap();
account.fixed.owner = ctx.accounts.owner.key();
if let Some(delegate) = &ctx.accounts.delegate_account {
account.fixed.delegate = delegate.key();
} else {
account.fixed.delegate = ctx.accounts.owner.key()
};
account.fixed.delegate = delegate_account;

account.expand_dynamic_content(open_orders_count)?;

Expand Down
2 changes: 2 additions & 0 deletions programs/openbook-v2/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use init_open_orders::*;
pub use place_order::*;
pub use place_take_order::*;
pub use prune_orders::*;
pub use set_delegate::*;
pub use set_market_expired::*;
pub use settle_funds::*;
pub use stub_oracle_close::*;
Expand All @@ -29,6 +30,7 @@ mod init_open_orders;
mod place_order;
mod place_take_order;
mod prune_orders;
mod set_delegate;
mod set_market_expired;
mod settle_funds;
mod stub_oracle_close;
Expand Down
17 changes: 10 additions & 7 deletions programs/openbook-v2/src/instructions/set_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use anchor_lang::prelude::*;

use crate::accounts_ix::*;
use crate::error::*;
use crate::pod_option::PodOption;
use crate::state::*;

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

account.delegate = if let Some(delegate) = ctx.accounts.delegate_account {
delegate;
let delegate_account: PodOption<Pubkey> = ctx
.accounts
.delegate_account
.as_ref()
.map(|account| account.key())
.into();

account.fixed.delegate = delegate_account;

} else {
ctx.accounts.owner.key()
};
Ok(())
}
6 changes: 6 additions & 0 deletions programs/openbook-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ pub mod openbook_v2 {
Ok(())
}

pub fn set_delegate(ctx: Context<SetDelegate>) -> Result<()> {
#[cfg(feature = "enable-gpl")]
instructions::set_delegate(ctx)?;
Ok(())
}

/// Set market to expired before pruning orders and closing the market
pub fn set_market_expired(ctx: Context<SetMarketExpired>) -> Result<()> {
#[cfg(feature = "enable-gpl")]
Expand Down
18 changes: 12 additions & 6 deletions programs/openbook-v2/src/state/open_orders_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::mem::size_of;

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

use super::FillEvent;
use super::LeafNode;
Expand Down Expand Up @@ -35,7 +36,7 @@ pub struct OpenOrdersAccount {
pub name: [u8; 32],

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

pub account_num: u32,

Expand All @@ -59,7 +60,7 @@ impl OpenOrdersAccount {
name: Default::default(),
owner: Pubkey::default(),
market: Pubkey::default(),
delegate: Pubkey::default(),
delegate: PodOption::default(),
account_num: 0,
bump: 0,

Expand Down Expand Up @@ -99,7 +100,7 @@ pub struct OpenOrdersAccountFixed {
pub owner: Pubkey,
pub market: Pubkey,
pub name: [u8; 32],
pub delegate: Pubkey,
pub delegate: PodOption<Pubkey>,
pub account_num: u32,
pub bump: u8,
pub padding: [u8; 3],
Expand All @@ -110,13 +111,14 @@ pub struct OpenOrdersAccountFixed {
const_assert_eq!(
size_of::<Position>(),
size_of::<OpenOrdersAccountFixed>()
- size_of::<Pubkey>() * 4
- size_of::<Pubkey>() * 3
- 40
- size_of::<u32>()
- size_of::<u8>()
- size_of::<[u8; 3]>()
- size_of::<[u8; 208]>()
);
const_assert_eq!(size_of::<OpenOrdersAccountFixed>(), 496);
const_assert_eq!(size_of::<OpenOrdersAccountFixed>(), 504);
const_assert_eq!(size_of::<OpenOrdersAccountFixed>() % 8, 0);

impl OpenOrdersAccountFixed {
Expand All @@ -127,7 +129,11 @@ impl OpenOrdersAccountFixed {
}

pub fn is_owner_or_delegate(&self, ix_signer: Pubkey) -> bool {
self.delegate == ix_signer
let delegate_option: Option<Pubkey> = Option::from(self.delegate);
if let Some(delegate) = delegate_option {
return self.owner == ix_signer || delegate == ix_signer;
}
self.owner == ix_signer
}
}

Expand Down

0 comments on commit 85198a6

Please sign in to comment.