From 851eca88af0d52c1818254b0973b0d54acc5cb85 Mon Sep 17 00:00:00 2001 From: skrrb Date: Mon, 25 Sep 2023 16:20:42 +0200 Subject: [PATCH] init market vaults inside create_market ix --- programs/openbook-v2/fuzz/src/lib.rs | 3 +++ programs/openbook-v2/fuzz/src/processor.rs | 1 + .../src/accounts_ix/create_market.rs | 21 +++++++++++++++--- programs/openbook-v2/tests/cases/test.rs | 10 --------- .../openbook-v2/tests/cases/test_crank.rs | 22 ------------------- .../openbook-v2/tests/cases/test_indexer.rs | 18 --------------- .../openbook-v2/tests/program_test/client.rs | 6 ++--- .../openbook-v2/tests/program_test/mod.rs | 12 +--------- 8 files changed, 26 insertions(+), 67 deletions(-) diff --git a/programs/openbook-v2/fuzz/src/lib.rs b/programs/openbook-v2/fuzz/src/lib.rs index 1a652ee94..ed3da7f54 100644 --- a/programs/openbook-v2/fuzz/src/lib.rs +++ b/programs/openbook-v2/fuzz/src/lib.rs @@ -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( @@ -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, diff --git a/programs/openbook-v2/fuzz/src/processor.rs b/programs/openbook-v2/fuzz/src/processor.rs index 93055f636..d8d2e487b 100644 --- a/programs/openbook-v2/fuzz/src/processor.rs +++ b/programs/openbook-v2/fuzz/src/processor.rs @@ -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, diff --git a/programs/openbook-v2/src/accounts_ix/create_market.rs b/programs/openbook-v2/src/accounts_ix/create_market.rs index f6fe3078e..d1694e1ab 100644 --- a/programs/openbook-v2/src/accounts_ix/create_market.rs +++ b/programs/openbook-v2/src/accounts_ix/create_market.rs @@ -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)] @@ -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>, pub quote_mint: Box>, 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>, /// CHECK: The oracle can be one of several different account types diff --git a/programs/openbook-v2/tests/cases/test.rs b/programs/openbook-v2/tests/cases/test.rs index c53c8fba7..5ac8929ae 100644 --- a/programs/openbook-v2/tests/cases/test.rs +++ b/programs/openbook-v2/tests/cases/test.rs @@ -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, @@ -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 }, ) diff --git a/programs/openbook-v2/tests/cases/test_crank.rs b/programs/openbook-v2/tests/cases/test_crank.rs index f3d176272..d51540500 100644 --- a/programs/openbook-v2/tests/cases/test_crank.rs +++ b/programs/openbook-v2/tests/cases/test_crank.rs @@ -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, @@ -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 }, @@ -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, @@ -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 }, diff --git a/programs/openbook-v2/tests/cases/test_indexer.rs b/programs/openbook-v2/tests/cases/test_indexer.rs index e0fae5717..e2312e94f 100644 --- a/programs/openbook-v2/tests/cases/test_indexer.rs +++ b/programs/openbook-v2/tests/cases/test_indexer.rs @@ -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, @@ -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 }, @@ -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, @@ -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 }, diff --git a/programs/openbook-v2/tests/program_test/client.rs b/programs/openbook-v2/tests/program_test/client.rs index 3c3e33954..3cf5ba23c 100644 --- a/programs/openbook-v2/tests/program_test/client.rs +++ b/programs/openbook-v2/tests/program_test/client.rs @@ -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; @@ -286,8 +286,6 @@ pub struct CreateMarketInstruction { pub oracle_b: Option, 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, @@ -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, diff --git a/programs/openbook-v2/tests/program_test/mod.rs b/programs/openbook-v2/tests/program_test/mod.rs index 825a2d8da..5b5447b36 100644 --- a/programs/openbook-v2/tests/program_test/mod.rs +++ b/programs/openbook-v2/tests/program_test/mod.rs @@ -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, @@ -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) @@ -358,7 +351,6 @@ impl TestContext { let openbook_v2::accounts::CreateMarket { market, - market_base_vault, market_quote_vault, bids, @@ -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