Tests: allow checking of logged "post_health"

This commit is contained in:
Christian Kamm 2022-08-24 14:08:08 +02:00
parent c8159746e7
commit cb0a9e5d2b
5 changed files with 48 additions and 0 deletions

View File

@ -319,6 +319,28 @@ pub async fn account_position_f64(solana: &SolanaCookie, account: Pubkey, bank:
native.to_num::<f64>()
}
// Verifies that the "post_health: ..." log emitted by the previous instruction
// matches the init health of the account.
pub async fn check_prev_instruction_post_health(solana: &SolanaCookie, account: Pubkey) {
let logs = solana.program_log();
let post_health_str = logs
.iter()
.find_map(|line| line.strip_prefix("post_health: "))
.unwrap();
let post_health = post_health_str.parse::<f64>().unwrap();
solana.advance_by_slots(1).await; // ugly, just to avoid sending the same tx next
send_tx(solana, ComputeAccountDataInstruction { account })
.await
.unwrap();
let health_data = solana
.program_log_events::<mango_v4::events::MangoAccountData>()
.pop()
.unwrap();
assert_eq!(health_data.init_health.to_num::<f64>(), post_health);
}
//
// a struct for each instruction along with its
// ClientInstruction impl

View File

@ -68,6 +68,8 @@ impl Log for LoggerWrapper {
let msg = record.args().to_string();
if let Some(data) = msg.strip_prefix("Program log: ") {
self.program_log.write().unwrap().push(data.into());
} else if let Some(data) = msg.strip_prefix("Program data: ") {
self.program_log.write().unwrap().push(data.into());
}
}
self.inner.log(record);

View File

@ -223,4 +223,19 @@ impl SolanaCookie {
pub fn program_log(&self) -> Vec<String> {
self.program_log.read().unwrap().clone()
}
pub fn program_log_events<T: anchor_lang::Event + anchor_lang::AnchorDeserialize>(
&self,
) -> Vec<T> {
self.program_log()
.iter()
.filter_map(|data| {
let bytes = base64::decode(data).ok()?;
if bytes[0..8] != T::discriminator() {
return None;
}
T::try_from_slice(&bytes[8..]).ok()
})
.collect()
}
}

View File

@ -114,6 +114,11 @@ async fn test_basic() -> Result<(), TransportError> {
send_tx(solana, ComputeAccountDataInstruction { account })
.await
.unwrap();
let health_data = solana
.program_log_events::<mango_v4::events::MangoAccountData>()
.pop()
.unwrap();
assert_eq!(health_data.init_health.to_num::<i64>(), 60);
//
// TEST: Withdraw funds
@ -137,6 +142,8 @@ async fn test_basic() -> Result<(), TransportError> {
.await
.unwrap();
check_prev_instruction_post_health(&solana, account).await;
assert_eq!(solana.token_account_balance(vault).await, withdraw_amount);
assert_eq!(
solana.token_account_balance(payer_mint0_account).await,

View File

@ -160,6 +160,8 @@ async fn test_serum() -> Result<(), TransportError> {
.await
.unwrap();
check_prev_instruction_post_health(&solana, account).await;
let native0 = account_position(solana, account, base_token.bank).await;
let native1 = account_position(solana, account, quote_token.bank).await;
assert_eq!(native0, 1000);