From ddfe759f43026fa8b71218289502e9bbee49f829 Mon Sep 17 00:00:00 2001 From: skrrb Date: Sun, 17 Dec 2023 22:30:11 +0100 Subject: [PATCH 1/2] allows the delegate to settle funds --- programs/openbook-v2/src/accounts_ix/settle_funds.rs | 9 ++++++--- .../openbook-v2/src/accounts_ix/settle_funds_expired.rs | 2 +- programs/openbook-v2/src/state/open_orders_account.rs | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/programs/openbook-v2/src/accounts_ix/settle_funds.rs b/programs/openbook-v2/src/accounts_ix/settle_funds.rs index 467c9b615..0c9ae73c8 100644 --- a/programs/openbook-v2/src/accounts_ix/settle_funds.rs +++ b/programs/openbook-v2/src/accounts_ix/settle_funds.rs @@ -1,3 +1,4 @@ +use crate::error::OpenBookError; use crate::state::*; use anchor_lang::prelude::*; use anchor_spl::token::{Token, TokenAccount}; @@ -11,8 +12,8 @@ pub struct SettleFunds<'info> { #[account( mut, - has_one = owner, has_one = market, + constraint = open_orders_account.load()?.is_owner_or_delegate(owner.key()) @ OpenBookError::NoOwnerOrDelegate )] pub open_orders_account: AccountLoader<'info, OpenOrdersAccount>, #[account( @@ -30,12 +31,14 @@ pub struct SettleFunds<'info> { pub market_quote_vault: Account<'info, TokenAccount>, #[account( mut, - token::mint = market_base_vault.mint + token::mint = market_base_vault.mint, + constraint = open_orders_account.load()?.check_owner_only_if_delegate(user_base_account.owner) )] pub user_base_account: Account<'info, TokenAccount>, #[account( mut, - token::mint = market_quote_vault.mint + token::mint = market_quote_vault.mint, + constraint = open_orders_account.load()?.check_owner_only_if_delegate(user_quote_account.owner) )] pub user_quote_account: Account<'info, TokenAccount>, #[account( diff --git a/programs/openbook-v2/src/accounts_ix/settle_funds_expired.rs b/programs/openbook-v2/src/accounts_ix/settle_funds_expired.rs index 0181f0228..23f712547 100644 --- a/programs/openbook-v2/src/accounts_ix/settle_funds_expired.rs +++ b/programs/openbook-v2/src/accounts_ix/settle_funds_expired.rs @@ -40,7 +40,7 @@ pub struct SettleFundsExpired<'info> { #[account( mut, token::mint = market_quote_vault.mint, - constraint = user_base_account.owner == open_orders_account.load()?.owner + constraint = user_quote_account.owner == open_orders_account.load()?.owner )] pub user_quote_account: Account<'info, TokenAccount>, #[account( diff --git a/programs/openbook-v2/src/state/open_orders_account.rs b/programs/openbook-v2/src/state/open_orders_account.rs index d8c3211a5..981f5640c 100644 --- a/programs/openbook-v2/src/state/open_orders_account.rs +++ b/programs/openbook-v2/src/state/open_orders_account.rs @@ -81,6 +81,10 @@ impl OpenOrdersAccount { self.owner == ix_signer } + pub fn check_owner_only_if_delegate(&self, owner: Pubkey) -> bool { + self.delegate.is_none() || self.owner == owner + } + pub fn all_orders(&self) -> impl Iterator { self.open_orders.iter() } From 681280491611d87d4b46d7d41a9cd91955ed052a Mon Sep 17 00:00:00 2001 From: skrrb Date: Mon, 18 Dec 2023 14:43:47 +0100 Subject: [PATCH 2/2] settle_funds always check token owner --- programs/openbook-v2/src/accounts_ix/settle_funds.rs | 4 ++-- programs/openbook-v2/src/state/open_orders_account.rs | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/programs/openbook-v2/src/accounts_ix/settle_funds.rs b/programs/openbook-v2/src/accounts_ix/settle_funds.rs index 0c9ae73c8..50eb5ecc3 100644 --- a/programs/openbook-v2/src/accounts_ix/settle_funds.rs +++ b/programs/openbook-v2/src/accounts_ix/settle_funds.rs @@ -32,13 +32,13 @@ pub struct SettleFunds<'info> { #[account( mut, token::mint = market_base_vault.mint, - constraint = open_orders_account.load()?.check_owner_only_if_delegate(user_base_account.owner) + constraint = user_base_account.owner == open_orders_account.load()?.owner )] pub user_base_account: Account<'info, TokenAccount>, #[account( mut, token::mint = market_quote_vault.mint, - constraint = open_orders_account.load()?.check_owner_only_if_delegate(user_quote_account.owner) + constraint = user_quote_account.owner == open_orders_account.load()?.owner )] pub user_quote_account: Account<'info, TokenAccount>, #[account( diff --git a/programs/openbook-v2/src/state/open_orders_account.rs b/programs/openbook-v2/src/state/open_orders_account.rs index 981f5640c..d8c3211a5 100644 --- a/programs/openbook-v2/src/state/open_orders_account.rs +++ b/programs/openbook-v2/src/state/open_orders_account.rs @@ -81,10 +81,6 @@ impl OpenOrdersAccount { self.owner == ix_signer } - pub fn check_owner_only_if_delegate(&self, owner: Pubkey) -> bool { - self.delegate.is_none() || self.owner == owner - } - pub fn all_orders(&self) -> impl Iterator { self.open_orders.iter() }