Skip to content

Commit

Permalink
init market vaults inside create_market ix
Browse files Browse the repository at this point in the history
  • Loading branch information
skrrb committed Sep 25, 2023
1 parent 1b40b68 commit 851eca8
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 67 deletions.
3 changes: 3 additions & 0 deletions programs/openbook-v2/fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl FuzzContext {
.add_empty_system_account(self.market_authority)
.add_empty_system_account(self.event_authority)
.add_program(openbook_v2::ID) // optional accounts use this pubkey
.add_program(spl_associated_token_account::ID)
.add_program(spl_token::ID)
.add_program(system_program::ID)
.add_token_account_with_lamports(
Expand Down Expand Up @@ -306,6 +307,8 @@ impl FuzzContext {
oracle_a: self.oracle_a,
oracle_b: self.oracle_b,
system_program: system_program::ID,
token_program: spl_token::ID,
associated_token_program: spl_associated_token_account::ID,
collect_fee_admin: self.collect_fee_admin,
open_orders_admin: None,
consume_events_admin: None,
Expand Down
1 change: 1 addition & 0 deletions programs/openbook-v2/fuzz/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl program_stubs::SyscallStubs for TestSyscallStubs {
match instruction.program_id {
// accounts should already be created & reallocated
id if id == system_program::ID => Ok(()),
id if id == spl_associated_token_account::ID => Ok(()),
id if id == spl_token::ID => spl_token::processor::Processor::process(
&instruction.program_id,
&new_account_infos,
Expand Down
21 changes: 18 additions & 3 deletions programs/openbook-v2/src/accounts_ix/create_market.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::state::*;
use anchor_lang::prelude::*;
use anchor_spl::token::{Mint, TokenAccount};
use anchor_spl::{
associated_token::AssociatedToken,
token::{Mint, Token, TokenAccount},
};

#[event_cpi]
#[derive(Accounts)]
Expand Down Expand Up @@ -30,15 +33,27 @@ pub struct CreateMarket<'info> {
#[account(mut)]
pub payer: Signer<'info>,

#[account(token::mint = base_mint, token::authority = market_authority)]
#[account(
init,
payer = payer,
associated_token::mint = base_mint,
associated_token::authority = market_authority,
)]
pub market_base_vault: Account<'info, TokenAccount>,
#[account(token::mint = quote_mint, token::authority = market_authority)]
#[account(
init,
payer = payer,
associated_token::mint = quote_mint,
associated_token::authority = market_authority,
)]
pub market_quote_vault: Account<'info, TokenAccount>,

pub base_mint: Box<Account<'info, Mint>>,
pub quote_mint: Box<Account<'info, Mint>>,

pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
/// CHECK: The oracle can be one of several different account types
pub oracle_a: Option<UncheckedAccount<'info>>,
/// CHECK: The oracle can be one of several different account types
Expand Down
10 changes: 0 additions & 10 deletions programs/openbook-v2/tests/cases/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ async fn test_simple_settle() -> Result<(), TransportError> {
//

let market_2 = TestKeypair::new();
let market_2_authority = get_market_address(market_2);

let market_base_vault_2 = solana
.create_associated_token_account(&market_2_authority, mints[0].pubkey)
.await;
let market_quote_vault_2 = solana
.create_associated_token_account(&market_2_authority, mints[1].pubkey)
.await;

send_tx(
solana,
Expand All @@ -53,8 +45,6 @@ async fn test_simple_settle() -> Result<(), TransportError> {
taker_fee: 400,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
market_base_vault: market_base_vault_2,
market_quote_vault: market_quote_vault_2,
..CreateMarketInstruction::with_new_book_and_heap(solana, None, None).await
},
)
Expand Down
22 changes: 0 additions & 22 deletions programs/openbook-v2/tests/cases/test_crank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ async fn test_skip_missing_accounts() -> Result<(), TransportError> {
let tokens = Token::create(mints.to_vec(), solana, collect_fee_admin, payer).await;

let market = TestKeypair::new();
let market_authority = get_market_address(market);
let market_base_vault = solana
.create_associated_token_account(&market_authority, mints[0].pubkey)
.await;
let market_quote_vault = solana
.create_associated_token_account(&market_authority, mints[1].pubkey)
.await;

let openbook_v2::accounts::CreateMarket {
market,
Expand All @@ -46,8 +39,6 @@ async fn test_skip_missing_accounts() -> Result<(), TransportError> {
taker_fee: 400,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
market_base_vault,
market_quote_vault,
..CreateMarketInstruction::with_new_book_and_heap(solana, Some(tokens[1].oracle), None)
.await
},
Expand Down Expand Up @@ -207,17 +198,6 @@ async fn test_crank_given_events() -> Result<(), TransportError> {
let tokens = Token::create(mints.to_vec(), solana, collect_fee_admin, payer).await;

let market = TestKeypair::new();
let market_authority = Pubkey::find_program_address(
&[b"Market".as_ref(), market.pubkey().to_bytes().as_ref()],
&openbook_v2::id(),
)
.0;
let market_base_vault = solana
.create_associated_token_account(&market_authority, mints[0].pubkey)
.await;
let market_quote_vault = solana
.create_associated_token_account(&market_authority, mints[1].pubkey)
.await;

let openbook_v2::accounts::CreateMarket {
market,
Expand All @@ -239,8 +219,6 @@ async fn test_crank_given_events() -> Result<(), TransportError> {
taker_fee: 400,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
market_base_vault,
market_quote_vault,
..CreateMarketInstruction::with_new_book_and_heap(solana, Some(tokens[0].oracle), None)
.await
},
Expand Down
18 changes: 0 additions & 18 deletions programs/openbook-v2/tests/cases/test_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ async fn test_indexer() -> Result<(), TransportError> {
let tokens = Token::create(mints.to_vec(), solana, collect_fee_admin, payer).await;

let market = TestKeypair::new();
let market_authority = get_market_address(market);
let market_base_vault = solana
.create_associated_token_account(&market_authority, mints[0].pubkey)
.await;
let market_quote_vault = solana
.create_associated_token_account(&market_authority, mints[1].pubkey)
.await;

let openbook_v2::accounts::CreateMarket { market, .. } = send_tx(
solana,
Expand All @@ -36,8 +29,6 @@ async fn test_indexer() -> Result<(), TransportError> {
taker_fee: 400,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
market_base_vault,
market_quote_vault,
..CreateMarketInstruction::with_new_book_and_heap(solana, Some(tokens[1].oracle), None)
.await
},
Expand Down Expand Up @@ -123,13 +114,6 @@ async fn test_size_vector() -> Result<(), TransportError> {
let tokens = Token::create(mints.to_vec(), solana, collect_fee_admin, payer).await;

let market = TestKeypair::new();
let market_authority = get_market_address(market);
let market_base_vault = solana
.create_associated_token_account(&market_authority, mints[0].pubkey)
.await;
let market_quote_vault = solana
.create_associated_token_account(&market_authority, mints[1].pubkey)
.await;

let openbook_v2::accounts::CreateMarket { market, .. } = send_tx(
solana,
Expand All @@ -145,8 +129,6 @@ async fn test_size_vector() -> Result<(), TransportError> {
taker_fee: 400,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
market_base_vault,
market_quote_vault,
..CreateMarketInstruction::with_new_book_and_heap(solana, Some(tokens[1].oracle), None)
.await
},
Expand Down
6 changes: 3 additions & 3 deletions programs/openbook-v2/tests/program_test/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]

use anchor_lang::prelude::*;
use anchor_spl::token::Token;
use anchor_spl::{associated_token::AssociatedToken, token::Token};
use solana_program::instruction::Instruction;
use solana_program_test::BanksClientError;
use solana_sdk::instruction;
Expand Down Expand Up @@ -286,8 +286,6 @@ pub struct CreateMarketInstruction {
pub oracle_b: Option<Pubkey>,
pub base_mint: Pubkey,
pub quote_mint: Pubkey,
pub market_base_vault: Pubkey,
pub market_quote_vault: Pubkey,
pub name: String,
pub bids: Pubkey,
pub asks: Pubkey,
Expand Down Expand Up @@ -378,6 +376,8 @@ impl ClientInstruction for CreateMarketInstruction {
quote_mint: self.quote_mint,
base_mint: self.base_mint,
system_program: System::id(),
token_program: Token::id(),
associated_token_program: AssociatedToken::id(),
collect_fee_admin: self.collect_fee_admin,
open_orders_admin: self.open_orders_admin,
consume_events_admin: self.consume_events_admin,
Expand Down
12 changes: 1 addition & 11 deletions programs/openbook-v2/tests/program_test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl TestContextBuilder {
);

// intentionally set to as tight as possible, to catch potential problems early
test.set_compute_max_units(75000);
test.set_compute_max_units(120000);

Self {
test,
Expand Down Expand Up @@ -342,13 +342,6 @@ impl TestContext {
// Create a market

let market = TestKeypair::new();
let market_authority = get_market_address(market);
let market_base_vault = solana
.create_associated_token_account(&market_authority, mints[0].pubkey)
.await;
let market_quote_vault = solana
.create_associated_token_account(&market_authority, mints[1].pubkey)
.await;

let oracle = if args.with_oracle {
Some(tokens[0].oracle)
Expand All @@ -358,7 +351,6 @@ impl TestContext {

let openbook_v2::accounts::CreateMarket {
market,

market_base_vault,
market_quote_vault,
bids,
Expand All @@ -378,8 +370,6 @@ impl TestContext {
taker_fee: args.taker_fee,
base_mint: mints[0].pubkey,
quote_mint: mints[1].pubkey,
market_base_vault,
market_quote_vault,
fee_penalty: args.fee_penalty,
time_expiry: args.time_expiry,
..CreateMarketInstruction::with_new_book_and_heap(solana, oracle, None).await
Expand Down

0 comments on commit 851eca8

Please sign in to comment.