Skip to content

Commit

Permalink
use testframe
Browse files Browse the repository at this point in the history
  • Loading branch information
binyebarwe committed Feb 26, 2024
1 parent 1a83df5 commit 95c92f5
Showing 1 changed file with 38 additions and 112 deletions.
150 changes: 38 additions & 112 deletions programs/openbook-v2/tests/cases/test_take_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -391,83 +359,46 @@ async fn test_negative_spread_ask() -> Result<(), TransportError> {
.unwrap();

let position = solana
.get_account::<OpenOrdersAccount>(maker_2)
.get_account::<OpenOrdersAccount>(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::<EventHeap>(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
Expand All @@ -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,
Expand All @@ -508,17 +439,12 @@ async fn test_negative_spread_bid() -> Result<(), TransportError> {
.unwrap();

let position = solana
.get_account::<OpenOrdersAccount>(maker_2)
.get_account::<OpenOrdersAccount>(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::<EventHeap>(event_heap).await;
assert_eq!(event_heap.header.count(), 0);
}

Ok(())
}

0 comments on commit 95c92f5

Please sign in to comment.