Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/comments #180

Merged
merged 14 commits into from
Oct 24, 2023
3 changes: 2 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test TEST_NAME:
cargo test-sbf --features enable-gpl -- {{ TEST_NAME }}

test-all:
(cd ./programs/openbook-v2 && RUST_LOG=ERROR cargo +1.70.0 test-sbf --features enable-gpl)
(cd ./programs/openbook-v2 && RUST_LOG=ERROR cargo test-sbf --features enable-gpl)

test-dev:
(find programs) | entr -s 'just test-all'
Expand All @@ -19,6 +19,7 @@ idl:
anchor build --arch sbf -- --features enable-gpl
bash {{ justfile_directory() }}/idl-fixup.sh
cp -v ./target/types/openbook_v2.ts ./ts/client/src/openbook_v2.ts
yarn lint --fix
yarn format

fuzz:
Expand Down
21 changes: 16 additions & 5 deletions lib/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,17 @@ impl OpenBookClient {
Some(tuple) => tuple.1.account_num + 1,
None => 0u32,
};
Self::create_open_orders_account(client, market, owner, payer, None, account_num)
.await
.context("Failed to create account...")?;
Self::create_open_orders_account(
client,
market,
owner,
payer,
None,
account_num,
openbook_account_name,
)
.await
.context("Failed to create account...")?;
}
let openbook_account_tuples =
fetch_openbook_accounts(&rpc, program, owner.pubkey()).await?;
Expand Down Expand Up @@ -191,6 +199,7 @@ impl OpenBookClient {
payer: &Keypair, // pays the SOL for the new account
delegate: Option<Pubkey>,
account_num: u32,
name: &str,
) -> anyhow::Result<(Pubkey, Signature)> {
let open_orders_indexer = Pubkey::find_program_address(
&[
Expand All @@ -206,7 +215,6 @@ impl OpenBookClient {
&[
b"OpenOrdersAccount".as_ref(),
owner.pubkey().as_ref(),
market.as_ref(),
&account_num.to_le_bytes(),
],
&openbook_v2::id(),
Expand All @@ -228,7 +236,9 @@ impl OpenBookClient {
None,
),
data: anchor_lang::InstructionData::data(
&openbook_v2::instruction::CreateOpenOrdersAccount {},
&openbook_v2::instruction::CreateOpenOrdersAccount {
name: name.to_string(),
},
),
};

Expand Down Expand Up @@ -535,6 +545,7 @@ impl OpenBookClient {
anchor_lang::ToAccountMetas::to_account_metas(
&openbook_v2::accounts::SettleFunds {
owner: self.owner(),
penalty_payer: self.owner(),
open_orders_account: self.open_orders_account,
market: market_address,
market_authority: market.market_authority,
Expand Down
1 change: 1 addition & 0 deletions lib/client/src/jup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ impl Amm for OpenBookMarket {

let accounts = PlaceTakeOrder {
signer: *user_transfer_authority,
penalty_payer: *user_transfer_authority,
market: self.key,
market_authority: self.market.market_authority,
bids: self.market.bids,
Expand Down
13 changes: 6 additions & 7 deletions programs/openbook-v2/fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,7 @@ impl FuzzContext {
.0;

let open_orders = Pubkey::find_program_address(
&[
b"OpenOrders".as_ref(),
owner.as_ref(),
self.market.as_ref(),
&1_u32.to_le_bytes(),
],
&[b"OpenOrders".as_ref(), owner.as_ref(), &1_u32.to_le_bytes()],
&openbook_v2::ID,
)
.0;
Expand Down Expand Up @@ -241,7 +236,9 @@ impl FuzzContext {
market: self.market,
system_program: system_program::ID,
};
let data = openbook_v2::instruction::CreateOpenOrdersAccount {};
let data = openbook_v2::instruction::CreateOpenOrdersAccount {
name: "fuzz test".to_string(),
};
process_instruction(&mut self.state, &data, &accounts, &[]).unwrap();

UserAccounts {
Expand Down Expand Up @@ -472,6 +469,7 @@ impl FuzzContext {

let accounts = openbook_v2::accounts::PlaceTakeOrder {
signer: user.owner,
penalty_payer: user.owner,
user_base_account: user.base_vault,
user_quote_account: user.quote_vault,
market: self.market,
Expand Down Expand Up @@ -767,6 +765,7 @@ impl FuzzContext {

let accounts = openbook_v2::accounts::SettleFunds {
owner: user.owner,
penalty_payer: user.owner,
open_orders_account: user.open_orders,
user_base_account: user.base_vault,
user_quote_account: user.quote_vault,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct CloseOpenOrdersAccount<'info> {
seeds = [b"OpenOrdersIndexer".as_ref(), owner.key().as_ref()],
bump = open_orders_indexer.bump,
realloc = OpenOrdersIndexer::space(open_orders_indexer.addresses.len()-1),
realloc::payer = payer,
realloc::payer = sol_destination,
realloc::zero = false,
)]
pub open_orders_indexer: Account<'info, OpenOrdersIndexer>,
Expand Down
1 change: 1 addition & 0 deletions programs/openbook-v2/src/accounts_ix/create_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct CreateMarket<'info> {
)]
pub market_quote_vault: Account<'info, TokenAccount>,

#[account(constraint = base_mint.key() != quote_mint.key())]
pub base_mint: Box<Account<'info, Mint>>,
pub quote_mint: Box<Account<'info, Mint>>,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct CreateOpenOrdersAccount<'info> {
pub open_orders_indexer: Account<'info, OpenOrdersIndexer>,
#[account(
init,
seeds = [b"OpenOrders".as_ref(), owner.key().as_ref(), market.key().as_ref(), &(open_orders_indexer.created_counter + 1).to_le_bytes()],
seeds = [b"OpenOrders".as_ref(), owner.key().as_ref(), &(open_orders_indexer.created_counter + 1).to_le_bytes()],
bump,
payer = payer,
space = OpenOrdersAccount::space(),
Expand Down
2 changes: 2 additions & 0 deletions programs/openbook-v2/src/accounts_ix/place_take_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use anchor_spl::token::{Token, TokenAccount};
pub struct PlaceTakeOrder<'info> {
#[account(mut)]
pub signer: Signer<'info>,
#[account(mut)]
pub penalty_payer: Signer<'info>,

#[account(
mut,
Expand Down
3 changes: 3 additions & 0 deletions programs/openbook-v2/src/accounts_ix/settle_funds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use anchor_spl::token::{Token, TokenAccount};
pub struct SettleFunds<'info> {
#[account(mut)]
pub owner: Signer<'info>,
#[account(mut)]
pub penalty_payer: Signer<'info>,

#[account(
mut,
has_one = owner,
Expand Down
7 changes: 5 additions & 2 deletions programs/openbook-v2/src/accounts_ix/settle_funds_expired.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use anchor_spl::token::{Token, TokenAccount};
pub struct SettleFundsExpired<'info> {
pub close_market_admin: Signer<'info>,
#[account(mut)]
pub payer: Signer<'info>,
pub owner: Signer<'info>,
#[account(mut)]
pub penalty_payer: Signer<'info>,
#[account(
mut,
has_one = market,
Expand Down Expand Up @@ -53,7 +55,8 @@ pub struct SettleFundsExpired<'info> {
impl<'info> SettleFundsExpired<'info> {
pub fn to_settle_funds_accounts(&self) -> SettleFunds<'info> {
SettleFunds {
owner: self.payer.clone(),
owner: self.owner.clone(),
penalty_payer: self.penalty_payer.clone(),
open_orders_account: self.open_orders_account.clone(),
market: self.market.clone(),
market_authority: self.market_authority.clone(),
Expand Down
22 changes: 5 additions & 17 deletions programs/openbook-v2/src/instructions/consume_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ pub const MAX_EVENTS_CONSUME: usize = 8;
///
/// Message and return Ok() if it's missing, to lock in successful processing
/// of previous events.
///
/// Special handling for testing, where events for accounts with bad
/// owners (most likely due to force closure of the account) are being skipped.
macro_rules! load_open_orders_account {
($name:ident, $key:expr, $ais:expr, $event_heap:expr) => {
($name:ident, $key:expr, $ais:expr) => {
let loader = match $ais.iter().find(|ai| ai.key == &$key) {
None => {
msg!(
Expand All @@ -30,17 +27,8 @@ macro_rules! load_open_orders_account {
}

Some(ai) => {
if ai.owner != &crate::id() {
msg!(
"OpenOrdersAccount ({}) not owned by openbook program",
stringify!($name)
);
$event_heap.pop_front()?;
continue;
}

let mal: AccountLoader<OpenOrdersAccount> = AccountLoader::try_from(ai)?;
mal
let ooa: AccountLoader<OpenOrdersAccount> = AccountLoader::try_from(ai)?;
ooa
}
};
let mut $name = loader.load_mut()?;
Expand Down Expand Up @@ -73,12 +61,12 @@ pub fn consume_events(
match EventType::try_from(event.event_type).map_err(|_| error!(OpenBookError::SomeError))? {
EventType::Fill => {
let fill: &FillEvent = cast_ref(event);
load_open_orders_account!(maker, fill.maker, remaining_accs, event_heap);
load_open_orders_account!(maker, fill.maker, remaining_accs);
maker.execute_maker(&mut market, fill);
}
EventType::Out => {
let out: &OutEvent = cast_ref(event);
load_open_orders_account!(owner, out.owner, remaining_accs, event_heap);
load_open_orders_account!(owner, out.owner, remaining_accs);
owner.cancel_order(out.owner_slot as usize, out.quantity, *market);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use crate::accounts_ix::CreateOpenOrdersAccount;
use crate::pubkey_option::NonZeroKey;
use crate::state::*;
use crate::util::fill_from_str;
use anchor_lang::prelude::*;

pub fn create_open_orders_account(ctx: Context<CreateOpenOrdersAccount>) -> Result<()> {
pub fn create_open_orders_account(
ctx: Context<CreateOpenOrdersAccount>,
name: String,
) -> Result<()> {
let mut account = ctx.accounts.open_orders_account.load_init()?;
let indexer = &mut ctx.accounts.open_orders_indexer;
indexer
.addresses
.push(ctx.accounts.open_orders_account.key());
indexer.created_counter += 1;

account.name = fill_from_str(&name)?;
account.account_num = indexer.created_counter;
account.market = ctx.accounts.market.key();
account.bump = *ctx.bumps.get("open_orders_account").unwrap();
Expand Down
1 change: 1 addition & 0 deletions programs/openbook-v2/src/instructions/edit_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn edit_order<'info>(
)?;

let filled_amount = expected_cancel_size - leaf_node_quantity;
// note that order.max_base_lots is checked to be > 0 inside `place_order`
if order.max_base_lots > filled_amount {
// Do not reduce max_quote_lots_including_fees as implicitly it's limited by max_base_lots.
order.max_base_lots -= filled_amount;
Expand Down
4 changes: 2 additions & 2 deletions programs/openbook-v2/src/instructions/place_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ pub fn place_order(ctx: Context<PlaceOrder>, order: Order, limit: u8) -> Result<
}

Side::Ask => {
let free_assets_native = position.base_free_native;
let free_base = position.base_free_native;
let max_base_native = total_base_taken_native + posted_base_native;

let free_qty_to_lock = cmp::min(max_base_native, free_assets_native);
let free_qty_to_lock = cmp::min(max_base_native, free_base);
let deposit_amount = max_base_native - free_qty_to_lock;

// Update market deposit total
Expand Down
2 changes: 1 addition & 1 deletion programs/openbook-v2/src/instructions/place_take_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn place_take_order<'info>(
system_program_transfer(
PENALTY_EVENT_HEAP,
&ctx.accounts.system_program,
&ctx.accounts.signer,
&ctx.accounts.penalty_payer,
&ctx.accounts.market,
)?;
}
Expand Down
6 changes: 1 addition & 5 deletions programs/openbook-v2/src/instructions/set_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ pub fn set_delegate(ctx: Context<SetDelegate>) -> Result<()> {

emit!(SetDelegateLog {
open_orders_account: ctx.accounts.open_orders_account.key(),
delegate: ctx
.accounts
.delegate_account
.as_ref()
.map(|account| account.key()),
delegate: delegate_account.into()
});

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion programs/openbook-v2/src/instructions/settle_funds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn settle_funds<'info>(ctx: Context<'_, '_, '_, 'info, SettleFunds<'info>>)
system_program_transfer(
pa.penalty_heap_count * PENALTY_EVENT_HEAP,
&ctx.accounts.system_program,
&ctx.accounts.owner,
&ctx.accounts.penalty_payer,
&ctx.accounts.market,
)?;
pa.penalty_heap_count = 0;
Expand Down
7 changes: 5 additions & 2 deletions programs/openbook-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ pub mod openbook_v2 {
}

/// Create an [`OpenOrdersAccount`](crate::state::OpenOrdersAccount).
pub fn create_open_orders_account(ctx: Context<CreateOpenOrdersAccount>) -> Result<()> {
pub fn create_open_orders_account(
ctx: Context<CreateOpenOrdersAccount>,
name: String,
) -> Result<()> {
#[cfg(feature = "enable-gpl")]
instructions::create_open_orders_account(ctx)?;
instructions::create_open_orders_account(ctx, name)?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion programs/openbook-v2/src/pubkey_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anchor_lang::prelude::*;
use bytemuck::Zeroable;
use std::convert::From;

/// Like `Option`, but implemented for `Pubkey`.
/// Like `Option`, but implemented for `Pubkey` to be used with `zero_copy`
#[zero_copy]
#[derive(AnchorSerialize, AnchorDeserialize, Debug, Default, PartialEq)]
pub struct NonZeroPubkeyOption {
Expand Down
1 change: 1 addition & 0 deletions programs/openbook-v2/src/state/open_orders_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ impl Position {
&& self.quote_free_native == 0
&& self.locked_maker_fees == 0
&& self.referrer_rebates_available == 0
&& self.penalty_heap_count == 0
}
}

Expand Down
2 changes: 0 additions & 2 deletions programs/openbook-v2/src/state/open_orders_indexer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use anchor_lang::prelude::*;
use static_assertions::const_assert_eq;
use std::mem::size_of;

#[account]
#[derive(Default)]
Expand Down
2 changes: 1 addition & 1 deletion programs/openbook-v2/tests/cases/test_permissioned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ async fn test_close_market_admin() -> Result<(), TransportError> {
let settle_funds_expired_ix = SettleFundsExpiredInstruction {
close_market_admin,
market,
payer,
owner: payer,
open_orders_account: account_2,
market_base_vault,
market_quote_vault,
Expand Down
Loading
Loading