Perp tests: Don't pass derivable accounts explicitly

This makes it impossible to test passing bad accounts for these through
this interface, but I think that's an ok tradeoff.
This commit is contained in:
Christian Kamm 2022-09-09 09:45:32 +02:00
parent 2dfada7377
commit bb962c6781
6 changed files with 40 additions and 231 deletions

View File

@ -2254,13 +2254,8 @@ impl ClientInstruction for PerpDeactivatePositionInstruction {
}
pub struct PerpPlaceOrderInstruction {
pub group: Pubkey,
pub account: Pubkey,
pub perp_market: Pubkey,
pub asks: Pubkey,
pub bids: Pubkey,
pub event_queue: Pubkey,
pub oracle: Pubkey,
pub owner: TestKeypair,
pub side: Side,
pub price_lots: i64,
@ -2287,16 +2282,6 @@ impl ClientInstruction for PerpPlaceOrderInstruction {
expiry_timestamp: 0,
limit: 1,
};
let accounts = Self::Accounts {
group: self.group,
account: self.account,
perp_market: self.perp_market,
asks: self.asks,
bids: self.bids,
event_queue: self.event_queue,
oracle: self.oracle,
owner: self.owner.pubkey(),
};
let perp_market: PerpMarket = account_loader.load(&self.perp_market).await.unwrap();
let account = account_loader
@ -2312,6 +2297,16 @@ impl ClientInstruction for PerpPlaceOrderInstruction {
)
.await;
let accounts = Self::Accounts {
group: account.fixed.group,
account: self.account,
perp_market: self.perp_market,
asks: perp_market.asks,
bids: perp_market.bids,
event_queue: perp_market.event_queue,
oracle: perp_market.oracle,
owner: self.owner.pubkey(),
};
let mut instruction = make_instruction(program_id, &accounts, instruction);
instruction.accounts.extend(health_check_metas);
@ -2402,11 +2397,8 @@ impl ClientInstruction for PerpCancelOrderByClientOrderIdInstruction {
}
pub struct PerpCancelAllOrdersInstruction {
pub group: Pubkey,
pub account: Pubkey,
pub perp_market: Pubkey,
pub asks: Pubkey,
pub bids: Pubkey,
pub owner: TestKeypair,
}
#[async_trait::async_trait(?Send)]
@ -2415,16 +2407,17 @@ impl ClientInstruction for PerpCancelAllOrdersInstruction {
type Instruction = mango_v4::instruction::PerpCancelAllOrders;
async fn to_instruction(
&self,
_loader: impl ClientAccountLoader + 'async_trait,
account_loader: impl ClientAccountLoader + 'async_trait,
) -> (Self::Accounts, instruction::Instruction) {
let program_id = mango_v4::id();
let instruction = Self::Instruction { limit: 5 };
let perp_market: PerpMarket = account_loader.load(&self.perp_market).await.unwrap();
let accounts = Self::Accounts {
group: self.group,
group: perp_market.group,
account: self.account,
perp_market: self.perp_market,
asks: self.asks,
bids: self.bids,
asks: perp_market.asks,
bids: perp_market.bids,
owner: self.owner.pubkey(),
};
@ -2438,9 +2431,7 @@ impl ClientInstruction for PerpCancelAllOrdersInstruction {
}
pub struct PerpConsumeEventsInstruction {
pub group: Pubkey,
pub perp_market: Pubkey,
pub event_queue: Pubkey,
pub mango_accounts: Vec<Pubkey>,
}
#[async_trait::async_trait(?Send)]
@ -2449,14 +2440,16 @@ impl ClientInstruction for PerpConsumeEventsInstruction {
type Instruction = mango_v4::instruction::PerpConsumeEvents;
async fn to_instruction(
&self,
_loader: impl ClientAccountLoader + 'async_trait,
account_loader: impl ClientAccountLoader + 'async_trait,
) -> (Self::Accounts, instruction::Instruction) {
let program_id = mango_v4::id();
let instruction = Self::Instruction { limit: 10 };
let perp_market: PerpMarket = account_loader.load(&self.perp_market).await.unwrap();
let accounts = Self::Accounts {
group: self.group,
group: perp_market.group,
perp_market: self.perp_market,
event_queue: self.event_queue,
event_queue: perp_market.event_queue,
};
let mut instruction = make_instruction(program_id, &accounts, instruction);
@ -2511,11 +2504,9 @@ impl ClientInstruction for PerpUpdateFundingInstruction {
}
pub struct PerpSettlePnlInstruction {
pub group: Pubkey,
pub account_a: Pubkey,
pub account_b: Pubkey,
pub perp_market: Pubkey,
pub oracle: Pubkey,
pub quote_bank: Pubkey,
pub max_settle_amount: u64,
}
@ -2531,14 +2522,6 @@ impl ClientInstruction for PerpSettlePnlInstruction {
let instruction = Self::Instruction {
max_settle_amount: self.max_settle_amount,
};
let accounts = Self::Accounts {
group: self.group,
perp_market: self.perp_market,
account_a: self.account_a,
account_b: self.account_b,
oracle: self.oracle,
quote_bank: self.quote_bank,
};
let perp_market: PerpMarket = account_loader.load(&self.perp_market).await.unwrap();
let account_b = account_loader
@ -2554,6 +2537,15 @@ impl ClientInstruction for PerpSettlePnlInstruction {
)
.await;
let accounts = Self::Accounts {
group: perp_market.group,
perp_market: self.perp_market,
account_a: self.account_a,
account_b: self.account_b,
oracle: perp_market.oracle,
quote_bank: self.quote_bank,
};
let mut instruction = make_instruction(program_id, &accounts, instruction);
instruction.accounts.extend(health_check_metas);
@ -2566,10 +2558,8 @@ impl ClientInstruction for PerpSettlePnlInstruction {
}
pub struct PerpSettleFeesInstruction {
pub group: Pubkey,
pub account: Pubkey,
pub perp_market: Pubkey,
pub oracle: Pubkey,
pub quote_bank: Pubkey,
pub max_settle_amount: u64,
}
@ -2585,13 +2575,6 @@ impl ClientInstruction for PerpSettleFeesInstruction {
let instruction = Self::Instruction {
max_settle_amount: self.max_settle_amount,
};
let accounts = Self::Accounts {
group: self.group,
perp_market: self.perp_market,
account: self.account,
oracle: self.oracle,
quote_bank: self.quote_bank,
};
let perp_market: PerpMarket = account_loader.load(&self.perp_market).await.unwrap();
let account = account_loader
@ -2607,6 +2590,13 @@ impl ClientInstruction for PerpSettleFeesInstruction {
)
.await;
let accounts = Self::Accounts {
group: perp_market.group,
perp_market: self.perp_market,
account: self.account,
oracle: perp_market.oracle,
quote_bank: self.quote_bank,
};
let mut instruction = make_instruction(program_id, &accounts, instruction);
instruction.accounts.extend(health_check_metas);

View File

@ -255,18 +255,13 @@ async fn test_health_compute_perp() -> Result<(), TransportError> {
//
// TEST: Create a perp order for each market
//
for (i, &(perp_market, asks, bids, event_queue)) in perp_markets.iter().enumerate() {
for (i, &(perp_market, _asks, _bids, _event_queue)) in perp_markets.iter().enumerate() {
println!("adding market {}", i);
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[i + 1].oracle,
owner,
side: Side::Bid,
price_lots,

View File

@ -43,13 +43,7 @@ async fn test_liq_perps_force_cancel() -> Result<(), TransportError> {
//
// TEST: Create a perp market
//
let mango_v4::accounts::PerpCreateMarket {
perp_market,
asks,
bids,
event_queue,
..
} = send_tx(
let mango_v4::accounts::PerpCreateMarket { perp_market, .. } = send_tx(
solana,
PerpCreateMarketInstruction {
group,
@ -127,13 +121,8 @@ async fn test_liq_perps_force_cancel() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account,
perp_market,
asks,
bids,
event_queue,
oracle: base_token.oracle,
owner,
side: Side::Ask,
price_lots,

View File

@ -116,13 +116,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_0,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Bid,
price_lots,
@ -163,13 +158,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_0,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Bid,
price_lots,
@ -205,13 +195,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_0,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Bid,
price_lots,
@ -226,13 +211,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_0,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Bid,
price_lots,
@ -247,13 +227,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_0,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Bid,
price_lots,
@ -269,11 +244,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpCancelAllOrdersInstruction {
group,
account: account_0,
perp_market,
asks,
bids,
owner,
},
)
@ -288,13 +260,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_0,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Bid,
price_lots,
@ -310,13 +277,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_1,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Ask,
price_lots,
@ -332,9 +294,7 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpConsumeEventsInstruction {
group,
perp_market,
event_queue,
mango_accounts: vec![account_0, account_1],
},
)
@ -378,13 +338,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_0,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Ask,
price_lots,
@ -400,13 +355,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_1,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Bid,
price_lots,
@ -422,9 +372,7 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpConsumeEventsInstruction {
group,
perp_market,
event_queue,
mango_accounts: vec![account_0, account_1],
},
)
@ -451,11 +399,9 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_0,
account_b: account_1,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},
@ -465,10 +411,8 @@ async fn test_perp() -> Result<(), TransportError> {
send_tx(
solana,
PerpSettleFeesInstruction {
group,
account: account_1,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},

View File

@ -1,6 +1,5 @@
#![cfg(all(feature = "test-bpf"))]
use anchor_lang::prelude::ErrorCode;
use fixed::types::I80F48;
use mango_v4::{error::MangoError, state::*};
use program_test::*;
@ -134,13 +133,7 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
//
// TEST: Create a perp market
//
let mango_v4::accounts::PerpCreateMarket {
perp_market,
asks,
bids,
event_queue,
..
} = send_tx(
let mango_v4::accounts::PerpCreateMarket { perp_market, .. } = send_tx(
solana,
PerpCreateMarketInstruction {
group,
@ -247,13 +240,8 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_0,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Bid,
price_lots,
@ -268,13 +256,8 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_1,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Ask,
price_lots,
@ -289,9 +272,7 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
send_tx(
solana,
PerpConsumeEventsInstruction {
group,
perp_market,
event_queue,
mango_accounts: vec![account_0, account_1],
},
)
@ -315,11 +296,9 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
let result = send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_1,
account_b: account_0,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[1].bank,
max_settle_amount: u64::MAX,
},
@ -332,36 +311,13 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
"Bank must be valid for quote currency".to_string(),
);
// Oracle must be valid for the perp market
let result = send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_1,
account_b: account_0,
perp_market,
oracle: tokens[1].oracle, // Using oracle for token 1 not 0
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},
)
.await;
assert_mango_error(
&result,
ErrorCode::ConstraintHasOne.into(),
"Oracle must be valid for perp market".to_string(),
);
// Cannot settle with yourself
let result = send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_0,
account_b: account_0,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},
@ -378,11 +334,9 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
let result = send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_0,
account_b: account_1,
perp_market: perp_market_2,
oracle: tokens[1].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},
@ -399,11 +353,9 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
let result = send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_0,
account_b: account_1,
perp_market: perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: 0,
},
@ -452,11 +404,9 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
let result = send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_1,
account_b: account_0,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},
@ -472,11 +422,9 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
let result = send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_0,
account_b: account_1,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},
@ -525,11 +473,9 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_0,
account_b: account_1,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: partial_settle_amount,
},
@ -590,11 +536,9 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_0,
account_b: account_1,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},
@ -685,11 +629,9 @@ async fn test_perp_settle_pnl() -> Result<(), TransportError> {
send_tx(
solana,
PerpSettlePnlInstruction {
group,
account_a: account_1,
account_b: account_0,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},

View File

@ -1,6 +1,5 @@
#![cfg(all(feature = "test-bpf"))]
use anchor_lang::prelude::ErrorCode;
use fixed::types::I80F48;
use mango_v4::{error::MangoError, state::*};
use program_test::*;
@ -134,13 +133,7 @@ async fn test_perp_settle_fees() -> Result<(), TransportError> {
//
// TEST: Create a perp market
//
let mango_v4::accounts::PerpCreateMarket {
perp_market,
asks,
bids,
event_queue,
..
} = send_tx(
let mango_v4::accounts::PerpCreateMarket { perp_market, .. } = send_tx(
solana,
PerpCreateMarketInstruction {
group,
@ -247,13 +240,8 @@ async fn test_perp_settle_fees() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_0,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Bid,
price_lots,
@ -268,13 +256,8 @@ async fn test_perp_settle_fees() -> Result<(), TransportError> {
send_tx(
solana,
PerpPlaceOrderInstruction {
group,
account: account_1,
perp_market,
asks,
bids,
event_queue,
oracle: tokens[0].oracle,
owner,
side: Side::Ask,
price_lots,
@ -289,9 +272,7 @@ async fn test_perp_settle_fees() -> Result<(), TransportError> {
send_tx(
solana,
PerpConsumeEventsInstruction {
group,
perp_market,
event_queue,
mango_accounts: vec![account_0, account_1],
},
)
@ -313,10 +294,8 @@ async fn test_perp_settle_fees() -> Result<(), TransportError> {
let result = send_tx(
solana,
PerpSettleFeesInstruction {
group,
account: account_0,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[1].bank,
max_settle_amount: u64::MAX,
},
@ -329,34 +308,12 @@ async fn test_perp_settle_fees() -> Result<(), TransportError> {
"Bank must be valid for quote currency".to_string(),
);
// Oracle must be valid for the perp market
let result = send_tx(
solana,
PerpSettleFeesInstruction {
group,
account: account_0,
perp_market,
oracle: tokens[1].oracle, // Using oracle for token 1 not 0
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},
)
.await;
assert_mango_error(
&result,
ErrorCode::ConstraintHasOne.into(),
"Oracle must be valid for perp market".to_string(),
);
// Cannot settle position that does not exist
let result = send_tx(
solana,
PerpSettleFeesInstruction {
group,
account: account_1,
perp_market: perp_market_2,
oracle: tokens[1].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},
@ -373,10 +330,8 @@ async fn test_perp_settle_fees() -> Result<(), TransportError> {
let result = send_tx(
solana,
PerpSettleFeesInstruction {
group,
account: account_1,
perp_market: perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: 0,
},
@ -423,10 +378,8 @@ async fn test_perp_settle_fees() -> Result<(), TransportError> {
let result = send_tx(
solana,
PerpSettleFeesInstruction {
group,
account: account_0,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},
@ -509,10 +462,8 @@ async fn test_perp_settle_fees() -> Result<(), TransportError> {
send_tx(
solana,
PerpSettleFeesInstruction {
group,
account: account_1,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: partial_settle_amount,
},
@ -565,10 +516,8 @@ async fn test_perp_settle_fees() -> Result<(), TransportError> {
send_tx(
solana,
PerpSettleFeesInstruction {
group,
account: account_1,
perp_market,
oracle: tokens[0].oracle,
quote_bank: tokens[0].bank,
max_settle_amount: u64::MAX,
},