From 23761ef28c193ad949dab8e2bb2e9bea71a419c8 Mon Sep 17 00:00:00 2001 From: Nicholas Clarke Date: Wed, 14 Jun 2023 00:41:54 -0700 Subject: [PATCH] Add price to WithdrawLoanLog (#603) --- .../serum3_liq_force_cancel_orders.rs | 2 ++ .../src/instructions/serum3_settle_funds.rs | 21 ++++++++++++++++++- .../src/instructions/token_liq_bankruptcy.rs | 3 ++- .../src/instructions/token_liq_with_token.rs | 3 ++- .../src/instructions/token_withdraw.rs | 1 + programs/mango-v4/src/logs.rs | 1 + 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/programs/mango-v4/src/instructions/serum3_liq_force_cancel_orders.rs b/programs/mango-v4/src/instructions/serum3_liq_force_cancel_orders.rs index 4faa92025..62e9c81c6 100644 --- a/programs/mango-v4/src/instructions/serum3_liq_force_cancel_orders.rs +++ b/programs/mango-v4/src/instructions/serum3_liq_force_cancel_orders.rs @@ -90,6 +90,8 @@ pub fn serum3_liq_force_cancel_orders( &mut quote_bank, &mut account.borrow_mut(), &before_oo, + None, + None, )?; before_oo diff --git a/programs/mango-v4/src/instructions/serum3_settle_funds.rs b/programs/mango-v4/src/instructions/serum3_settle_funds.rs index 81e9af44e..06fc08456 100644 --- a/programs/mango-v4/src/instructions/serum3_settle_funds.rs +++ b/programs/mango-v4/src/instructions/serum3_settle_funds.rs @@ -1,5 +1,4 @@ use anchor_lang::prelude::*; - use fixed::types::I80F48; use crate::error::*; @@ -11,6 +10,8 @@ use crate::accounts_ix::*; use crate::logs::Serum3OpenOrdersBalanceLogV2; use crate::logs::{LoanOriginationFeeInstruction, WithdrawLoanLog}; +use crate::accounts_zerocopy::AccountInfoRef; + /// Settling means moving free funds from the serum3 open orders account /// back into the mango account wallet. /// @@ -82,6 +83,8 @@ pub fn serum3_settle_funds<'info>( &mut quote_bank, &mut account.borrow_mut(), &before_oo, + v2.as_ref().map(|d| d.base_oracle.as_ref()), + v2.as_ref().map(|d| d.quote_oracle.as_ref()), )?; } @@ -154,6 +157,8 @@ pub fn charge_loan_origination_fees( quote_bank: &mut Bank, account: &mut MangoAccountRefMut, before_oo: &OpenOrdersSlim, + base_oracle: Option<&AccountInfo>, + quote_oracle: Option<&AccountInfo>, ) -> Result<()> { let serum3_account = account.serum3_orders_mut(market_index).unwrap(); @@ -177,6 +182,12 @@ pub fn charge_loan_origination_fees( now_ts, )?; + let base_oracle_price = base_oracle + .map(|ai| { + base_bank.oracle_price(&AccountInfoRef::borrow(ai)?, Some(Clock::get()?.slot)) + }) + .transpose()?; + emit!(WithdrawLoanLog { mango_group: *group_pubkey, mango_account: *account_pubkey, @@ -184,6 +195,7 @@ pub fn charge_loan_origination_fees( loan_amount: withdraw_result.loan_amount.to_bits(), loan_origination_fee: withdraw_result.loan_origination_fee.to_bits(), instruction: LoanOriginationFeeInstruction::Serum3SettleFunds, + price: base_oracle_price.map(|p| p.to_bits()) }); } @@ -206,6 +218,12 @@ pub fn charge_loan_origination_fees( now_ts, )?; + let quote_oracle_price = quote_oracle + .map(|ai| { + quote_bank.oracle_price(&AccountInfoRef::borrow(ai)?, Some(Clock::get()?.slot)) + }) + .transpose()?; + emit!(WithdrawLoanLog { mango_group: *group_pubkey, mango_account: *account_pubkey, @@ -213,6 +231,7 @@ pub fn charge_loan_origination_fees( loan_amount: withdraw_result.loan_amount.to_bits(), loan_origination_fee: withdraw_result.loan_origination_fee.to_bits(), instruction: LoanOriginationFeeInstruction::Serum3SettleFunds, + price: quote_oracle_price.map(|p| p.to_bits()) }); } diff --git a/programs/mango-v4/src/instructions/token_liq_bankruptcy.rs b/programs/mango-v4/src/instructions/token_liq_bankruptcy.rs index a8ca866de..ac8812ee4 100644 --- a/programs/mango-v4/src/instructions/token_liq_bankruptcy.rs +++ b/programs/mango-v4/src/instructions/token_liq_bankruptcy.rs @@ -178,7 +178,8 @@ pub fn token_liq_bankruptcy( token_index: liab_token_index, loan_amount: liqor_liab_withdraw_result.loan_amount.to_bits(), loan_origination_fee: liqor_liab_withdraw_result.loan_origination_fee.to_bits(), - instruction: LoanOriginationFeeInstruction::LiqTokenBankruptcy + instruction: LoanOriginationFeeInstruction::LiqTokenBankruptcy, + price: Some(liab_oracle_price.to_bits()) }); } diff --git a/programs/mango-v4/src/instructions/token_liq_with_token.rs b/programs/mango-v4/src/instructions/token_liq_with_token.rs index 1807d49a4..5f4059e8c 100644 --- a/programs/mango-v4/src/instructions/token_liq_with_token.rs +++ b/programs/mango-v4/src/instructions/token_liq_with_token.rs @@ -298,7 +298,8 @@ pub(crate) fn liquidation_action( token_index: liab_token_index, loan_amount: liqor_liab_withdraw_result.loan_amount.to_bits(), loan_origination_fee: liqor_liab_withdraw_result.loan_origination_fee.to_bits(), - instruction: LoanOriginationFeeInstruction::LiqTokenWithToken + instruction: LoanOriginationFeeInstruction::LiqTokenWithToken, + price: Some(liab_oracle_price.to_bits()) }); } diff --git a/programs/mango-v4/src/instructions/token_withdraw.rs b/programs/mango-v4/src/instructions/token_withdraw.rs index 754d8cb0d..618de0f6f 100644 --- a/programs/mango-v4/src/instructions/token_withdraw.rs +++ b/programs/mango-v4/src/instructions/token_withdraw.rs @@ -135,6 +135,7 @@ pub fn token_withdraw(ctx: Context, amount: u64, allow_borrow: bo loan_amount: withdraw_result.loan_amount.to_bits(), loan_origination_fee: withdraw_result.loan_origination_fee.to_bits(), instruction: LoanOriginationFeeInstruction::TokenWithdraw, + price: Some(oracle_price.to_bits()), }); } diff --git a/programs/mango-v4/src/logs.rs b/programs/mango-v4/src/logs.rs index c83eab50e..88b4271a2 100644 --- a/programs/mango-v4/src/logs.rs +++ b/programs/mango-v4/src/logs.rs @@ -265,6 +265,7 @@ pub struct WithdrawLoanLog { pub loan_amount: i128, pub loan_origination_fee: i128, pub instruction: LoanOriginationFeeInstruction, + pub price: Option, // Ideally would log price everywhere but in serum3_settle_funds oracle is not a passed in account } #[event]