Skip to content

Commit

Permalink
mv account state constraint to ix
Browse files Browse the repository at this point in the history
  • Loading branch information
skrrb committed Jul 5, 2023
1 parent caef0c0 commit d7b6bd2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
5 changes: 2 additions & 3 deletions programs/openbook-v2/src/accounts_ix/cancel_order.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::error::OpenBookError;
use crate::state::{BookSide, Market, OpenOrdersAccount};
use anchor_lang::prelude::*;

#[derive(Accounts)]
pub struct CancelOrder<'info> {
#[account(
mut,
has_one = market,
constraint = open_orders_account.load()?.is_owner_or_delegate(owner.key()) @ OpenBookError::NoOwnerOrDelegate,
has_one = market
// also is_owner_or_delegate check inside ix
)]
pub open_orders_account: AccountLoader<'info, OpenOrdersAccount>,
pub owner: Signer<'info>,
Expand Down
3 changes: 1 addition & 2 deletions programs/openbook-v2/src/accounts_ix/place_order.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::error::OpenBookError;
use crate::state::*;
use anchor_lang::prelude::*;
use anchor_spl::token::{Token, TokenAccount};
Expand All @@ -8,7 +7,7 @@ pub struct PlaceOrder<'info> {
#[account(
mut,
has_one = market,
constraint = open_orders_account.load()?.is_owner_or_delegate(owner_or_delegate.key()) @ OpenBookError::NoOwnerOrDelegate,
// also is_owner_or_delegate check inside ix
)]
pub open_orders_account: AccountLoader<'info, OpenOrdersAccount>,
pub owner_or_delegate: Signer<'info>,
Expand Down
8 changes: 7 additions & 1 deletion programs/openbook-v2/src/instructions/cancel_all_orders.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use anchor_lang::prelude::*;

use crate::accounts_ix::*;
use crate::error::OpenBookError;
use crate::state::*;
use anchor_lang::prelude::*;

pub fn cancel_all_orders(
ctx: Context<CancelOrder>,
side_option: Option<Side>,
limit: u8,
) -> Result<()> {
let mut account = ctx.accounts.open_orders_account.load_mut()?;
require!(
account.is_owner_or_delegate(ctx.accounts.owner.key()),
OpenBookError::NoOwnerOrDelegate
);

let market = ctx.accounts.market.load()?;
let mut book = Orderbook {
Expand Down
4 changes: 4 additions & 0 deletions programs/openbook-v2/src/instructions/cancel_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub fn cancel_order(ctx: Context<CancelOrder>, order_id: u128) -> Result<()> {
require_gt!(order_id, 0, OpenBookError::InvalidInputOrderId);

let mut account = ctx.accounts.open_orders_account.load_mut()?;
require!(
account.is_owner_or_delegate(ctx.accounts.owner.key()),
OpenBookError::NoOwnerOrDelegate
);

let market = ctx.accounts.market.load()?;
let mut book = Orderbook {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub fn cancel_order_by_client_order_id(
client_order_id: u64,
) -> Result<()> {
let mut account = ctx.accounts.open_orders_account.load_mut()?;
require!(
account.is_owner_or_delegate(ctx.accounts.owner.key()),
OpenBookError::SomeError
);

let market = ctx.accounts.market.load()?;
let mut book = Orderbook {
Expand Down
4 changes: 4 additions & 0 deletions programs/openbook-v2/src/instructions/place_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub fn place_order(ctx: Context<PlaceOrder>, order: Order, limit: u8) -> Result<
);

let mut open_orders_account = ctx.accounts.open_orders_account.load_mut()?;
require!(
open_orders_account.is_owner_or_delegate(ctx.accounts.owner_or_delegate.key()),
OpenBookError::NoOwnerOrDelegate
);
let open_orders_account_pk = ctx.accounts.open_orders_account.key();

let mut market = ctx.accounts.market.load_mut()?;
Expand Down

0 comments on commit d7b6bd2

Please sign in to comment.