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

Fix all of the seat manager tests #9

Open
wants to merge 1 commit into
base: crate
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,927 changes: 5,096 additions & 831 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ toml_edit = "0.18.1"
proc-macro-crate = "1.3.0"

[dev-dependencies]
rand = "0.7.3"
rand = "0.7.3"
tokio = { version="*", features=["full"]}
solana-sdk = "1.16.3"
solana-program-test = "1.16.3"
ellipsis-client = "1.0.0"
phoenix-sdk = "0.7.0"
25 changes: 19 additions & 6 deletions tests/setup/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,20 @@ pub async fn mint_tokens(
#[allow(dead_code)]
pub async fn get_and_bootstrap_maker(
sdk: &mut SDKClient,
market: &Pubkey,
mint_authority: &Keypair,
) -> PhoenixTestAccount {
let maker = setup_account(&sdk.client, &mint_authority, sdk.base_mint, sdk.quote_mint).await;
let meta = sdk.get_market_metadata(market).await.unwrap();
let maker = setup_account(
&sdk.client,
&mint_authority,
meta.base_mint,
meta.quote_mint,
)
.await;
sdk.client.add_keypair(&maker.user);
let mut init_instructions = vec![];
init_instructions.push(create_claim_seat_instruction(
&maker.user.pubkey(),
&sdk.active_market_key,
));
init_instructions.push(create_claim_seat_instruction(&maker.user.pubkey(), market));
println!("maker: {:?}", maker.user.pubkey());
sdk.client
.sign_send_instructions_with_payer(init_instructions, vec![&sdk.client.payer, &maker.user])
Expand All @@ -143,9 +148,17 @@ pub async fn get_and_bootstrap_maker(
#[allow(dead_code)]
pub async fn get_and_bootstrap_taker(
sdk: &mut SDKClient,
market: &Pubkey,
mint_authority: &Keypair,
) -> PhoenixTestAccount {
let taker = setup_account(&sdk.client, &mint_authority, sdk.base_mint, sdk.quote_mint).await;
let meta = sdk.get_market_metadata(market).await.unwrap();
let taker = setup_account(
&sdk.client,
&mint_authority,
meta.base_mint,
meta.quote_mint,
)
.await;
sdk.client.add_keypair(&taker.user);
taker
}
8 changes: 6 additions & 2 deletions tests/setup/init.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ellipsis_client::program_test::*;
use ellipsis_client::EllipsisClient;
use phoenix::program::instruction_builders::*;
use phoenix_seat_manager::get_seat_manager_address;
use phoenix_seat_manager::instruction_builders::create_claim_market_authority_instruction;
use solana_program_test::*;

use phoenix::program::status::MarketStatus;
use phoenix::program::*;
Expand Down Expand Up @@ -34,6 +34,7 @@ pub struct PhoenixTestClient {
pub ctx: ProgramTestContext,
pub sdk: SDKClient,
pub mint_authority: Keypair,
pub market: Pubkey,
}

pub fn phoenix_test() -> ProgramTest {
Expand Down Expand Up @@ -225,8 +226,11 @@ async fn bootstrap_with_parameters(

PhoenixTestClient {
ctx: context,
sdk: SDKClient::new_from_ellipsis_client(&market.pubkey(), ellipsis_client).await,
sdk: SDKClient::new_from_ellipsis_client(ellipsis_client)
.await
.unwrap(),
mint_authority: authority,
market: market.pubkey(),
}
}

Expand Down
39 changes: 15 additions & 24 deletions tests/test_add_remove_mm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,29 @@ async fn test_add_remove_happy_path() {
ctx: _,
sdk,
mint_authority: _,
market,
} = bootstrap_default(5).await;

// Add seat for trader
let trader = Pubkey::new_unique();

let claim_seat = create_claim_seat_authorized_instruction(
&trader,
&sdk.active_market_key,
&sdk.client.payer.pubkey(),
);
let claim_seat =
create_claim_seat_authorized_instruction(&trader, &market, &sdk.client.payer.pubkey());

sdk.client
.sign_send_instructions(vec![claim_seat], vec![])
.await
.unwrap();

// Add designated MM
let add_as_mm =
create_add_dmm_instruction(&sdk.active_market_key, &sdk.client.payer.pubkey(), &trader);
let add_as_mm = create_add_dmm_instruction(&market, &sdk.client.payer.pubkey(), &trader);

sdk.client
.sign_send_instructions(vec![add_as_mm], vec![])
.await
.unwrap();

let (seat_manager_address, _) = get_seat_manager_address(&sdk.active_market_key);
let (seat_manager_address, _) = get_seat_manager_address(&market);
let seat_manager_data = sdk
.client
.get_account_data(&seat_manager_address)
Expand All @@ -54,14 +51,13 @@ async fn test_add_remove_happy_path() {
assert_eq!(seat_manager.num_makers, 1);

// Remove designated MM
let remove_as_mm =
create_remove_dmm_instruction(&sdk.active_market_key, &sdk.client.payer.pubkey(), &trader);
let remove_as_mm = create_remove_dmm_instruction(&market, &sdk.client.payer.pubkey(), &trader);
sdk.client
.sign_send_instructions(vec![remove_as_mm], vec![])
.await
.unwrap();

let (seat_manager_address, _) = get_seat_manager_address(&sdk.active_market_key);
let (seat_manager_address, _) = get_seat_manager_address(&market);
let seat_manager_data = sdk
.client
.get_account_data(&seat_manager_address)
Expand All @@ -77,21 +73,20 @@ async fn test_add_remove_fails_if_trader_has_no_seat() {
ctx: _,
sdk,
mint_authority: _,
market,
} = bootstrap_default(5).await;

let trader = Pubkey::new_unique();

// Add designated MM errors if no seat
let add_as_mm =
create_add_dmm_instruction(&sdk.active_market_key, &sdk.client.payer.pubkey(), &trader);
let add_as_mm = create_add_dmm_instruction(&market, &sdk.client.payer.pubkey(), &trader);

assert!(sdk
.client
.sign_send_instructions(vec![add_as_mm], vec![])
.await
.is_err());
let remove_as_mm =
create_remove_dmm_instruction(&sdk.active_market_key, &sdk.client.payer.pubkey(), &trader);
let remove_as_mm = create_remove_dmm_instruction(&market, &sdk.client.payer.pubkey(), &trader);

assert!(sdk
.client
Expand All @@ -106,15 +101,13 @@ async fn test_add_remove_fails_if_authority_not_signer_or_authority_mismatch() {
ctx: _,
mut sdk,
mint_authority: _,
market,
} = bootstrap_default(5).await;

let trader = Pubkey::new_unique();

let claim_seat = create_claim_seat_authorized_instruction(
&trader,
&sdk.active_market_key,
&sdk.client.payer.pubkey(),
);
let claim_seat =
create_claim_seat_authorized_instruction(&trader, &market, &sdk.client.payer.pubkey());

sdk.client
.sign_send_instructions(vec![claim_seat], vec![])
Expand All @@ -126,8 +119,7 @@ async fn test_add_remove_fails_if_authority_not_signer_or_authority_mismatch() {
.await
.unwrap();

let add_as_mm =
create_add_dmm_instruction(&sdk.active_market_key, &unauthorized.pubkey(), &trader);
let add_as_mm = create_add_dmm_instruction(&market, &unauthorized.pubkey(), &trader);

// Signer that does not match seat manager authority (client.payer) fails
assert!(sdk
Expand All @@ -137,8 +129,7 @@ async fn test_add_remove_fails_if_authority_not_signer_or_authority_mismatch() {
.is_err());

// Missing authority signer fails
let mut add_as_mm =
create_add_dmm_instruction(&sdk.active_market_key, &sdk.client.payer.pubkey(), &trader);
let mut add_as_mm = create_add_dmm_instruction(&market, &sdk.client.payer.pubkey(), &trader);

add_as_mm.accounts[3].is_signer = false;
sdk.client.add_keypair(&unauthorized);
Expand Down
77 changes: 38 additions & 39 deletions tests/test_change_market_fee_recipient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,27 @@ use crate::setup::init::*;

#[tokio::test]
async fn test_change_market_fee_recipient_no_unclaimed() {
let PhoenixTestClient { ctx: _, sdk, .. } = bootstrap_default(0).await;
let PhoenixTestClient {
ctx: _,
sdk,
market,
..
} = bootstrap_default(0).await;
println!("Seat program authority: {}", sdk.client.payer.pubkey());

let new_fee_recipient = Pubkey::new_unique();

let meta = sdk.get_market_metadata(&market).await.unwrap();

let change_market_fee_recipient_ix = create_change_market_fee_recipient_instruction(
&sdk.active_market_key,
&market,
&sdk.client.payer.pubkey(),
&new_fee_recipient,
&sdk.quote_mint,
&meta.quote_mint,
&sdk.client.payer.pubkey(),
);

let market_account_data = sdk
.client
.get_account_data(&sdk.active_market_key)
.await
.unwrap();
let market_account_data = sdk.client.get_account_data(&market).await.unwrap();
let (header_bytes, _market_bytes) = market_account_data.split_at(size_of::<MarketHeader>());
let header: &MarketHeader = bytemuck::try_from_bytes(header_bytes).unwrap();

Expand All @@ -51,11 +54,7 @@ async fn test_change_market_fee_recipient_no_unclaimed() {
.await
.unwrap();
// Verify that the market authority successor is set to the new keypair
let market_account_data = sdk
.client
.get_account_data(&sdk.active_market_key)
.await
.unwrap();
let market_account_data = sdk.client.get_account_data(&market).await.unwrap();
let (header_bytes, _bytes) = market_account_data.split_at(size_of::<MarketHeader>());

let header = bytemuck::try_from_bytes::<MarketHeader>(header_bytes).unwrap();
Expand All @@ -69,20 +68,22 @@ async fn test_change_market_fee_recipient_unclaimed_fees() {
let PhoenixTestClient {
ctx: _,
mut sdk,
market,
mint_authority,
} = bootstrap_default(5).await;
//Place limit order with maker (keypair 1)
let maker = get_and_bootstrap_maker(&mut sdk, &mint_authority).await;
let maker = get_and_bootstrap_maker(&mut sdk, &market, &mint_authority).await;
let meta = sdk.get_market_metadata(&market).await.unwrap();
let maker_order_packet = OrderPacket::new_limit_order_default(
Side::Ask,
sdk.float_price_to_ticks(10.0),
meta.float_price_to_ticks_rounded_up(10.0),
1_000_000_000,
);
let limit_order_ix = create_new_order_instruction(
&sdk.active_market_key,
&market,
&maker.user.pubkey(),
&sdk.base_mint,
&sdk.quote_mint,
&meta.base_mint,
&meta.quote_mint,
&maker_order_packet,
);
sdk.client
Expand All @@ -91,20 +92,20 @@ async fn test_change_market_fee_recipient_unclaimed_fees() {
.unwrap();

//Place cross order with taker (keypair 2)
let taker = get_and_bootstrap_taker(&mut sdk, &mint_authority).await;
let taker = get_and_bootstrap_taker(&mut sdk, &market, &mint_authority).await;
let taker_order_packet = OrderPacket::new_ioc_buy_with_limit_price(
sdk.float_price_to_ticks(10.0),
meta.float_price_to_ticks_rounded_down(10.0),
10_000_000,
SelfTradeBehavior::Abort,
None,
10,
false,
);
let taker_order_ix = create_new_order_instruction(
&sdk.active_market_key,
&market,
&taker.user.pubkey(),
&sdk.base_mint,
&sdk.quote_mint,
&meta.base_mint,
&meta.quote_mint,
&taker_order_packet,
);

Expand All @@ -116,17 +117,13 @@ async fn test_change_market_fee_recipient_unclaimed_fees() {
// Assert that market has unclaimed fees
let (unclaimed_fees, current_fee_recipient) = {
// Check if there are unclaimed fees in the market account. If so, generate change fee with unclaimed ix
let market_data = sdk
.client
.get_account_data(&sdk.active_market_key)
.await
.unwrap();
let market_data = sdk.client.get_account_data(&market).await.unwrap();
let (header_bytes, market_bytes) = market_data.split_at(size_of::<MarketHeader>());
let market_header = bytemuck::try_from_bytes::<MarketHeader>(header_bytes).unwrap();

println!("Current authority: {}", market_header.authority);

let seat = get_seat_manager_address(&sdk.active_market_key);
let seat = get_seat_manager_address(&market);
println!("Seat manager address: {:?}", seat);
println!("Seat manager id: {}", phoenix_seat_manager::id());
println!("Current fee recipient: {}", market_header.fee_recipient);
Expand All @@ -148,10 +145,10 @@ async fn test_change_market_fee_recipient_unclaimed_fees() {
let new_fee_recipient = Pubkey::new_unique();

let claim_fees_ix = create_change_market_fee_recipient_instruction(
&sdk.active_market_key,
&market,
&sdk.client.payer.pubkey(),
&new_fee_recipient,
&sdk.quote_mint,
&meta.quote_mint,
&current_fee_recipient,
);

Expand All @@ -167,11 +164,7 @@ async fn test_change_market_fee_recipient_unclaimed_fees() {
.unwrap();

// Assert that the market authority successor is set to the new keypair
let market_account_data = sdk
.client
.get_account_data(&sdk.active_market_key)
.await
.unwrap();
let market_account_data = sdk.client.get_account_data(&market).await.unwrap();
let (header_bytes, _bytes) = market_account_data.split_at(size_of::<MarketHeader>());

let header = bytemuck::try_from_bytes::<MarketHeader>(header_bytes).unwrap();
Expand All @@ -181,17 +174,23 @@ async fn test_change_market_fee_recipient_unclaimed_fees() {

#[tokio::test]
async fn test_change_market_fee_recipient_invalid_authority() {
let PhoenixTestClient { ctx: _, sdk, .. } = bootstrap_default(0).await;
let PhoenixTestClient {
ctx: _,
market,
sdk,
..
} = bootstrap_default(0).await;
println!("Seat program authority: {}", sdk.client.payer.pubkey());

let new_fee_recipient = Pubkey::new_unique();
let incorrect_authority = Keypair::new();
let meta = sdk.get_market_metadata(&market).await.unwrap();

let change_market_fee_recipient_ix = create_change_market_fee_recipient_instruction(
&sdk.active_market_key,
&market,
&incorrect_authority.pubkey(),
&new_fee_recipient,
&sdk.quote_mint,
&meta.quote_mint,
&sdk.client.payer.pubkey(),
);

Expand Down
Loading