From 95c92f53d70514368102c175930635deb745c566 Mon Sep 17 00:00:00 2001 From: Binye Barwe Date: Mon, 26 Feb 2024 15:32:42 -0800 Subject: [PATCH] use testframe --- .../tests/cases/test_take_order.rs | 150 +++++------------- 1 file changed, 38 insertions(+), 112 deletions(-) diff --git a/programs/openbook-v2/tests/cases/test_take_order.rs b/programs/openbook-v2/tests/cases/test_take_order.rs index 356701767..fddbeaf5e 100644 --- a/programs/openbook-v2/tests/cases/test_take_order.rs +++ b/programs/openbook-v2/tests/cases/test_take_order.rs @@ -291,66 +291,34 @@ async fn test_take_bid_order() -> Result<(), TransportError> { #[tokio::test] async fn test_negative_spread_ask() -> Result<(), TransportError> { - let context = TestContext::new().await; - let solana = &context.solana.clone(); - - let collect_fee_admin = TestKeypair::new(); - let close_market_admin = TestKeypair::new(); - let owner = context.users[0].key; - let payer = context.users[1].key; - let mints = &context.mints[0..=2]; - - let owner_token_0 = context.users[0].token_accounts[0]; - - let meta_owner_token_2 = context.users[1].token_accounts[2]; - - let tokens = Token::create(mints.to_vec(), solana, collect_fee_admin, payer).await; - - let market = TestKeypair::new(); - - let openbook_v2::accounts::CreateMarket { + let TestInitialize { + context, + owner, + owner_token_1, market, market_base_vault, market_quote_vault, - event_heap, + account_1, + account_2, .. - } = send_tx( - solana, - CreateMarketInstruction { - collect_fee_admin: collect_fee_admin.pubkey(), - open_orders_admin: None, - close_market_admin: Some(close_market_admin.pubkey()), - payer, - market, - quote_lot_size: 100, - base_lot_size: 1_000_000_000, - maker_fee: 0, - taker_fee: 400, - base_mint: mints[2].pubkey, - quote_mint: mints[0].pubkey, - ..CreateMarketInstruction::with_new_book_and_heap(solana, Some(tokens[1].oracle), None) - .await - }, - ) - .await - .unwrap(); - - let _indexer = create_open_orders_indexer(solana, &context.users[0], owner, market).await; - let _indexer_2 = create_open_orders_indexer(solana, &context.users[1], payer, market).await; - let maker_1 = - create_open_orders_account(solana, owner, market, 1, &context.users[0], None).await; + } = TestContext::new_with_market(TestNewMarketInitialize { + quote_lot_size: 100, + base_lot_size: 1_000_000_000, + ..TestNewMarketInitialize::default() + }) + .await?; + let solana = &context.solana.clone(); - let maker_2 = - create_open_orders_account(solana, payer, market, 1, &context.users[1], None).await; + let meta_owner_token_2 = context.users[1].token_accounts[0]; send_tx( solana, PlaceOrderInstruction { - open_orders_account: maker_1, + open_orders_account: account_1, open_orders_admin: None, market, signer: owner, - user_token_account: owner_token_0, + user_token_account: owner_token_1, market_vault: market_quote_vault, side: Side::Bid, price_lots: 10_000, // $1 @@ -370,10 +338,10 @@ async fn test_negative_spread_ask() -> Result<(), TransportError> { send_tx( solana, PlaceOrderInstruction { - open_orders_account: maker_2, + open_orders_account: account_2, open_orders_admin: None, market, - signer: payer, + signer: owner, user_token_account: meta_owner_token_2, market_vault: market_base_vault, side: Side::Ask, @@ -391,83 +359,46 @@ async fn test_negative_spread_ask() -> Result<(), TransportError> { .unwrap(); let position = solana - .get_account::(maker_2) + .get_account::(account_2) .await .position; assert_eq!(position.asks_base_lots, 0); assert_eq!(position.bids_base_lots, 0); - { - let event_heap = solana.get_account_boxed::(event_heap).await; - assert_eq!(event_heap.header.count(), 0); - } - Ok(()) } #[tokio::test] async fn test_negative_spread_bid() -> Result<(), TransportError> { - let context = TestContext::new().await; - let solana = &context.solana.clone(); - - let collect_fee_admin = TestKeypair::new(); - let close_market_admin = TestKeypair::new(); - let owner = context.users[0].key; - let payer = context.users[1].key; - let mints = &context.mints[0..=2]; - - let owner_token_1 = context.users[0].token_accounts[2]; - - let meta_owner_token_1 = context.users[1].token_accounts[0]; - - let tokens = Token::create(mints.to_vec(), solana, collect_fee_admin, payer).await; - - let market = TestKeypair::new(); - - let openbook_v2::accounts::CreateMarket { + let TestInitialize { + context, + owner, + owner_token_0, market, market_base_vault, market_quote_vault, - event_heap, + account_1, + account_2, .. - } = send_tx( - solana, - CreateMarketInstruction { - collect_fee_admin: collect_fee_admin.pubkey(), - open_orders_admin: None, - close_market_admin: Some(close_market_admin.pubkey()), - payer, - market, - quote_lot_size: 1_000_000_000, - base_lot_size: 100, - maker_fee: 0, - taker_fee: 400, - base_mint: mints[2].pubkey, - quote_mint: mints[0].pubkey, - ..CreateMarketInstruction::with_new_book_and_heap(solana, Some(tokens[1].oracle), None) - .await - }, - ) - .await - .unwrap(); - - let _indexer = create_open_orders_indexer(solana, &context.users[0], owner, market).await; - let _indexer_2 = create_open_orders_indexer(solana, &context.users[1], payer, market).await; - let maker_1 = - create_open_orders_account(solana, owner, market, 1, &context.users[0], None).await; + } = TestContext::new_with_market(TestNewMarketInitialize { + quote_lot_size: 1_000_000_000, + base_lot_size: 100, + ..TestNewMarketInitialize::default() + }) + .await?; + let solana = &context.solana.clone(); - let maker_2 = - create_open_orders_account(solana, payer, market, 1, &context.users[1], None).await; + let meta_owner_token_1 = context.users[1].token_accounts[1]; send_tx( solana, PlaceOrderInstruction { - open_orders_account: maker_1, + open_orders_account: account_1, open_orders_admin: None, market, signer: owner, - user_token_account: owner_token_1, + user_token_account: owner_token_0, market_vault: market_base_vault, side: Side::Ask, price_lots: 1000000, // whatever @@ -487,10 +418,10 @@ async fn test_negative_spread_bid() -> Result<(), TransportError> { send_tx( solana, PlaceOrderInstruction { - open_orders_account: maker_2, + open_orders_account: account_2, open_orders_admin: None, market, - signer: payer, + signer: owner, user_token_account: meta_owner_token_1, market_vault: market_quote_vault, side: Side::Bid, @@ -508,17 +439,12 @@ async fn test_negative_spread_bid() -> Result<(), TransportError> { .unwrap(); let position = solana - .get_account::(maker_2) + .get_account::(account_2) .await .position; assert_eq!(position.asks_base_lots, 0); assert_eq!(position.bids_base_lots, 0); - { - let event_heap = solana.get_account_boxed::(event_heap).await; - assert_eq!(event_heap.header.count(), 0); - } - Ok(()) }