Add serum open orders balance logging. Remove price from token and perp balances. (#236)

This commit is contained in:
Nicholas Clarke 2022-09-23 10:42:43 -07:00 committed by GitHub
parent f6c9a93ac5
commit 1320451e1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 135 additions and 58 deletions

View File

@ -377,7 +377,6 @@ pub fn flash_loan_end<'key, 'accounts, 'remaining, 'info>(
indexed_position: position.indexed_position.to_bits(),
deposit_index: bank.deposit_index.to_bits(),
borrow_index: bank.borrow_index.to_bits(),
price: price.to_bits(),
});
}

View File

@ -64,7 +64,6 @@ pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Res
ctx.accounts.group.key(),
fill.maker,
perp_market.perp_market_index as u64,
fill.price,
ma.perp_position(perp_market.perp_market_index).unwrap(),
&perp_market,
);
@ -105,7 +104,6 @@ pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Res
ctx.accounts.group.key(),
fill.maker,
perp_market.perp_market_index as u64,
fill.price,
maker.perp_position(perp_market.perp_market_index).unwrap(),
&perp_market,
);
@ -113,7 +111,6 @@ pub fn perp_consume_events(ctx: Context<PerpConsumeEvents>, limit: usize) -> Res
ctx.accounts.group.key(),
fill.taker,
perp_market.perp_market_index as u64,
fill.price,
taker.perp_position(perp_market.perp_market_index).unwrap(),
&perp_market,
);

View File

@ -1,6 +1,9 @@
use anchor_lang::prelude::*;
use super::{OpenOrdersAmounts, OpenOrdersSlim};
use crate::error::*;
use crate::logs::Serum3OpenOrdersBalanceLog;
use crate::serum3_cpi::load_open_orders_ref;
use crate::state::*;
#[derive(Accounts)]
@ -72,6 +75,22 @@ pub fn serum3_cancel_all_orders(ctx: Context<Serum3CancelAllOrders>, limit: u8)
//
cpi_cancel_all_orders(ctx.accounts, limit)?;
let serum_market = ctx.accounts.serum_market.load()?;
let oo_ai = &ctx.accounts.open_orders.as_ref();
let open_orders = load_open_orders_ref(oo_ai)?;
let after_oo = OpenOrdersSlim::from_oo(&open_orders);
emit!(Serum3OpenOrdersBalanceLog {
mango_group: ctx.accounts.group.key(),
mango_account: ctx.accounts.account.key(),
base_token_index: serum_market.base_token_index,
quote_token_index: serum_market.quote_token_index,
base_total: after_oo.native_base_total(),
base_free: after_oo.native_base_free(),
quote_total: after_oo.native_quote_total(),
quote_free: after_oo.native_quote_free(),
referrer_rebates_accrued: after_oo.native_rebates(),
});
Ok(())
}

View File

@ -7,6 +7,10 @@ use crate::state::*;
use super::Serum3Side;
use super::{OpenOrdersAmounts, OpenOrdersSlim};
use crate::logs::Serum3OpenOrdersBalanceLog;
use crate::serum3_cpi::load_open_orders_ref;
#[derive(Accounts)]
pub struct Serum3CancelOrder<'info> {
pub group: AccountLoader<'info, Group>,
@ -85,6 +89,21 @@ pub fn serum3_cancel_order(
};
cpi_cancel_order(ctx.accounts, order)?;
let oo_ai = &ctx.accounts.open_orders.as_ref();
let open_orders = load_open_orders_ref(oo_ai)?;
let after_oo = OpenOrdersSlim::from_oo(&open_orders);
emit!(Serum3OpenOrdersBalanceLog {
mango_group: ctx.accounts.group.key(),
mango_account: ctx.accounts.account.key(),
base_token_index: serum_market.base_token_index,
quote_token_index: serum_market.quote_token_index,
base_total: after_oo.native_base_total(),
base_free: after_oo.native_base_free(),
quote_total: after_oo.native_quote_total(),
quote_free: after_oo.native_quote_free(),
referrer_rebates_accrued: after_oo.native_rebates(),
});
Ok(())
}

View File

@ -4,8 +4,10 @@ use fixed::types::I80F48;
use crate::error::*;
use crate::instructions::{
apply_vault_difference, charge_loan_origination_fees, OODifference, OpenOrdersSlim,
apply_vault_difference, charge_loan_origination_fees, OODifference, OpenOrdersAmounts,
OpenOrdersSlim,
};
use crate::logs::Serum3OpenOrdersBalanceLog;
use crate::serum3_cpi::load_open_orders_ref;
use crate::state::*;
@ -178,6 +180,19 @@ pub fn serum3_liq_force_cancel_orders(
let oo_ai = &ctx.accounts.open_orders.as_ref();
let open_orders = load_open_orders_ref(oo_ai)?;
let after_oo = OpenOrdersSlim::from_oo(&open_orders);
emit!(Serum3OpenOrdersBalanceLog {
mango_group: ctx.accounts.group.key(),
mango_account: ctx.accounts.account.key(),
base_token_index: serum_market.base_token_index,
quote_token_index: serum_market.quote_token_index,
base_total: after_oo.native_base_total(),
base_free: after_oo.native_base_free(),
quote_total: after_oo.native_quote_total(),
quote_free: after_oo.native_quote_free(),
referrer_rebates_accrued: after_oo.native_rebates(),
});
OODifference::new(&before_oo, &after_oo)
.adjust_health_cache(&mut health_cache, &serum_market)?;
};
@ -196,6 +211,7 @@ pub fn serum3_liq_force_cancel_orders(
let mut base_bank = ctx.accounts.base_bank.load_mut()?;
let mut quote_bank = ctx.accounts.quote_bank.load_mut()?;
apply_vault_difference(
ctx.accounts.account.key(),
&mut account.borrow_mut(),
serum_market.market_index,
&mut base_bank,
@ -204,6 +220,7 @@ pub fn serum3_liq_force_cancel_orders(
)?
.adjust_health_cache(&mut health_cache)?;
apply_vault_difference(
ctx.accounts.account.key(),
&mut account.borrow_mut(),
serum_market.market_index,
&mut quote_bank,

View File

@ -1,5 +1,6 @@
use crate::error::*;
use crate::logs::{Serum3OpenOrdersBalanceLog, TokenBalanceLog};
use crate::serum3_cpi::{load_market_state, load_open_orders_ref};
use crate::state::*;
use anchor_lang::prelude::*;
@ -39,7 +40,9 @@ pub trait OpenOrdersAmounts {
fn native_quote_free(&self) -> u64;
fn native_quote_free_plus_rebates(&self) -> u64;
fn native_base_total(&self) -> u64;
fn native_quote_total(&self) -> u64;
fn native_quote_total_plus_rebates(&self) -> u64;
fn native_rebates(&self) -> u64;
}
impl OpenOrdersAmounts for OpenOrdersSlim {
@ -61,9 +64,15 @@ impl OpenOrdersAmounts for OpenOrdersSlim {
fn native_base_total(&self) -> u64 {
self.native_coin_total
}
fn native_quote_total(&self) -> u64 {
self.native_pc_total
}
fn native_quote_total_plus_rebates(&self) -> u64 {
cm!(self.native_pc_total + self.referrer_rebates_accrued)
}
fn native_rebates(&self) -> u64 {
self.referrer_rebates_accrued
}
}
impl OpenOrdersAmounts for OpenOrders {
@ -85,9 +94,15 @@ impl OpenOrdersAmounts for OpenOrders {
fn native_base_total(&self) -> u64 {
self.native_coin_total
}
fn native_quote_total(&self) -> u64 {
self.native_pc_total
}
fn native_quote_total_plus_rebates(&self) -> u64 {
cm!(self.native_pc_total + self.referrer_rebates_accrued)
}
fn native_rebates(&self) -> u64 {
self.referrer_rebates_accrued
}
}
/// Copy paste a bunch of enums so that we could AnchorSerialize & AnchorDeserialize them
@ -298,6 +313,19 @@ pub fn serum3_place_order(
let oo_ai = &ctx.accounts.open_orders.as_ref();
let open_orders = load_open_orders_ref(oo_ai)?;
let after_oo = OpenOrdersSlim::from_oo(&open_orders);
emit!(Serum3OpenOrdersBalanceLog {
mango_group: ctx.accounts.group.key(),
mango_account: ctx.accounts.account.key(),
base_token_index: serum_market.base_token_index,
quote_token_index: serum_market.quote_token_index,
base_total: after_oo.native_coin_total,
base_free: after_oo.native_coin_free,
quote_total: after_oo.native_pc_total,
quote_free: after_oo.native_pc_free,
referrer_rebates_accrued: after_oo.referrer_rebates_accrued,
});
OODifference::new(&before_oo, &after_oo)
};
@ -314,6 +342,7 @@ pub fn serum3_place_order(
let vault_difference = {
let mut payer_bank = ctx.accounts.payer_bank.load_mut()?;
apply_vault_difference(
ctx.accounts.account.key(),
&mut account.borrow_mut(),
serum_market.market_index,
&mut payer_bank,
@ -386,7 +415,9 @@ impl VaultDifference {
/// Called in settle_funds, place_order, liq_force_cancel to adjust token positions after
/// changing the vault balances
/// Also logs changes to token balances
pub fn apply_vault_difference(
account_pk: Pubkey,
account: &mut MangoAccountRefMut,
serum_market_index: Serum3MarketIndex,
bank: &mut Bank,
@ -406,6 +437,7 @@ pub fn apply_vault_difference(
.abs()
.to_num::<u64>();
let indexed_position = position.indexed_position;
let market = account.serum3_orders_mut(serum_market_index).unwrap();
let borrows_without_fee = if bank.token_index == market.base_token_index {
&mut market.base_borrows_without_fee
@ -426,6 +458,15 @@ pub fn apply_vault_difference(
*borrows_without_fee = (*borrows_without_fee).saturating_sub(needed_change.to_num::<u64>());
}
emit!(TokenBalanceLog {
mango_group: bank.group,
mango_account: account_pk,
token_index: bank.token_index,
indexed_position: indexed_position.to_bits(),
deposit_index: bank.deposit_index.to_bits(),
borrow_index: bank.borrow_index.to_bits(),
});
Ok(VaultDifference {
token_index: bank.token_index,
native_change,

View File

@ -8,6 +8,7 @@ use crate::serum3_cpi::load_open_orders_ref;
use crate::state::*;
use super::{apply_vault_difference, OpenOrdersAmounts, OpenOrdersSlim};
use crate::logs::Serum3OpenOrdersBalanceLog;
use crate::logs::{LoanOriginationFeeInstruction, WithdrawLoanOriginationFeeLog};
#[derive(Accounts)]
@ -158,6 +159,7 @@ pub fn serum3_settle_funds(ctx: Context<Serum3SettleFunds>) -> Result<()> {
let mut base_bank = ctx.accounts.base_bank.load_mut()?;
let mut quote_bank = ctx.accounts.quote_bank.load_mut()?;
apply_vault_difference(
ctx.accounts.account.key(),
&mut account.borrow_mut(),
serum_market.market_index,
&mut base_bank,
@ -165,6 +167,7 @@ pub fn serum3_settle_funds(ctx: Context<Serum3SettleFunds>) -> Result<()> {
before_base_vault,
)?;
apply_vault_difference(
ctx.accounts.account.key(),
&mut account.borrow_mut(),
serum_market.market_index,
&mut quote_bank,
@ -173,6 +176,21 @@ pub fn serum3_settle_funds(ctx: Context<Serum3SettleFunds>) -> Result<()> {
)?;
}
let oo_ai = &ctx.accounts.open_orders.as_ref();
let open_orders = load_open_orders_ref(oo_ai)?;
let after_oo = OpenOrdersSlim::from_oo(&open_orders);
emit!(Serum3OpenOrdersBalanceLog {
mango_group: ctx.accounts.group.key(),
mango_account: ctx.accounts.account.key(),
base_token_index: serum_market.base_token_index,
quote_token_index: serum_market.quote_token_index,
base_total: after_oo.native_base_total(),
base_free: after_oo.native_base_free(),
quote_total: after_oo.native_quote_total(),
quote_free: after_oo.native_quote_free(),
referrer_rebates_accrued: after_oo.native_rebates(),
});
Ok(())
}

View File

@ -88,7 +88,6 @@ pub fn token_deposit(ctx: Context<TokenDeposit>, amount: u64) -> Result<()> {
indexed_position: indexed_position.to_bits(),
deposit_index: bank.deposit_index.to_bits(),
borrow_index: bank.borrow_index.to_bits(),
price: oracle_price.to_bits(),
});
//

View File

@ -158,7 +158,7 @@ pub fn token_liq_bankruptcy(
)?;
// move quote assets into liqor and withdraw liab assets
if let Some((quote_bank, quote_price)) = opt_quote_bank_and_price {
if let Some((quote_bank, _)) = opt_quote_bank_and_price {
// account constraint #2 a)
require_keys_eq!(quote_bank.vault, ctx.accounts.quote_vault.key());
require_keys_eq!(quote_bank.mint, ctx.accounts.insurance_vault.mint);
@ -193,7 +193,6 @@ pub fn token_liq_bankruptcy(
indexed_position: liqor_quote_indexed_position.to_bits(),
deposit_index: quote_deposit_index.to_bits(),
borrow_index: quote_borrow_index.to_bits(),
price: quote_price.to_bits(),
});
if loan_origination_fee.is_positive() {
@ -274,7 +273,6 @@ pub fn token_liq_bankruptcy(
indexed_position: liqee_liab.indexed_position.to_bits(),
deposit_index: liab_deposit_index.to_bits(),
borrow_index: liab_borrow_index.to_bits(),
price: liab_price.to_bits(),
});
// liqee liab
@ -285,7 +283,6 @@ pub fn token_liq_bankruptcy(
indexed_position: liqee_liab.indexed_position.to_bits(),
deposit_index: liab_deposit_index.to_bits(),
borrow_index: liab_borrow_index.to_bits(),
price: liab_price.to_bits(),
});
let liab_bank = bank_ais[0].load::<Bank>()?;

View File

@ -187,7 +187,6 @@ pub fn token_liq_with_token(
indexed_position: liqee_asset_position_indexed.to_bits(),
deposit_index: asset_bank.deposit_index.to_bits(),
borrow_index: asset_bank.borrow_index.to_bits(),
price: asset_price.to_bits(),
});
// liqee liab
emit!(TokenBalanceLog {
@ -197,7 +196,6 @@ pub fn token_liq_with_token(
indexed_position: liqee_liab_position_indexed.to_bits(),
deposit_index: liab_bank.deposit_index.to_bits(),
borrow_index: liab_bank.borrow_index.to_bits(),
price: liab_price.to_bits(),
});
// liqor asset
emit!(TokenBalanceLog {
@ -207,7 +205,6 @@ pub fn token_liq_with_token(
indexed_position: liqor_asset_position_indexed.to_bits(),
deposit_index: asset_bank.deposit_index.to_bits(),
borrow_index: asset_bank.borrow_index.to_bits(),
price: asset_price.to_bits(),
});
// liqor liab
emit!(TokenBalanceLog {
@ -217,7 +214,6 @@ pub fn token_liq_with_token(
indexed_position: liqor_liab_position_indexed.to_bits(),
deposit_index: liab_bank.deposit_index.to_bits(),
borrow_index: liab_bank.borrow_index.to_bits(),
price: liab_price.to_bits(),
});
if loan_origination_fee.is_positive() {

View File

@ -131,7 +131,6 @@ pub fn token_withdraw(ctx: Context<TokenWithdraw>, amount: u64, allow_borrow: bo
indexed_position: position.indexed_position.to_bits(),
deposit_index: bank.deposit_index.to_bits(),
borrow_index: bank.borrow_index.to_bits(),
price: oracle_price.to_bits(),
});
// Update the net deposits - adjust by price so different tokens are on the same basis (in USD terms)

View File

@ -10,7 +10,6 @@ pub fn emit_perp_balances(
mango_group: Pubkey,
mango_account: Pubkey,
market_index: u64,
price: i64,
pp: &PerpPosition,
pm: &PerpMarket,
) {
@ -22,7 +21,6 @@ pub fn emit_perp_balances(
quote_position: pp.quote_position_native().to_bits(),
long_settled_funding: pp.long_settled_funding.to_bits(),
short_settled_funding: pp.short_settled_funding.to_bits(),
price,
long_funding: pm.long_funding.to_bits(),
short_funding: pm.short_funding.to_bits(),
});
@ -37,9 +35,8 @@ pub struct PerpBalanceLog {
pub quote_position: i128, // I80F48
pub long_settled_funding: i128, // I80F48
pub short_settled_funding: i128, // I80F48
pub price: i64,
pub long_funding: i128, // I80F48
pub short_funding: i128, // I80F48
pub long_funding: i128, // I80F48
pub short_funding: i128, // I80F48
}
#[event]
@ -50,7 +47,6 @@ pub struct TokenBalanceLog {
pub indexed_position: i128, // on client convert i128 to I80F48 easily by passing in the BN to I80F48 ctor
pub deposit_index: i128, // I80F48
pub borrow_index: i128, // I80F48
pub price: i128, // I80F48
}
#[derive(AnchorSerialize, AnchorDeserialize)]
@ -165,17 +161,17 @@ pub struct LiquidateTokenAndTokenLog {
}
#[event]
pub struct OpenOrdersBalanceLog {
pub struct Serum3OpenOrdersBalanceLog {
pub mango_group: Pubkey,
pub mango_account: Pubkey,
pub market_index: u16,
pub base_token_index: u16,
pub quote_token_index: u16,
pub base_total: u64,
pub base_free: u64,
/// this field does not include the referrer_rebates; need to add that in to get true total
pub quote_total: u64,
pub quote_free: u64,
pub referrer_rebates_accrued: u64,
pub price: i128, // I80F48
}
#[derive(PartialEq, Copy, Clone, Debug, AnchorSerialize, AnchorDeserialize)]

View File

@ -5425,11 +5425,6 @@ export type MangoV4 = {
"type": "i128",
"index": false
},
{
"name": "price",
"type": "i64",
"index": false
},
{
"name": "longFunding",
"type": "i128",
@ -5474,11 +5469,6 @@ export type MangoV4 = {
"name": "borrowIndex",
"type": "i128",
"index": false
},
{
"name": "price",
"type": "i128",
"index": false
}
]
},
@ -5844,7 +5834,7 @@ export type MangoV4 = {
]
},
{
"name": "OpenOrdersBalanceLog",
"name": "Serum3OpenOrdersBalanceLog",
"fields": [
{
"name": "mangoGroup",
@ -5857,7 +5847,12 @@ export type MangoV4 = {
"index": false
},
{
"name": "marketIndex",
"name": "baseTokenIndex",
"type": "u16",
"index": false
},
{
"name": "quoteTokenIndex",
"type": "u16",
"index": false
},
@ -5885,11 +5880,6 @@ export type MangoV4 = {
"name": "referrerRebatesAccrued",
"type": "u64",
"index": false
},
{
"name": "price",
"type": "i128",
"index": false
}
]
},
@ -11522,11 +11512,6 @@ export const IDL: MangoV4 = {
"type": "i128",
"index": false
},
{
"name": "price",
"type": "i64",
"index": false
},
{
"name": "longFunding",
"type": "i128",
@ -11571,11 +11556,6 @@ export const IDL: MangoV4 = {
"name": "borrowIndex",
"type": "i128",
"index": false
},
{
"name": "price",
"type": "i128",
"index": false
}
]
},
@ -11941,7 +11921,7 @@ export const IDL: MangoV4 = {
]
},
{
"name": "OpenOrdersBalanceLog",
"name": "Serum3OpenOrdersBalanceLog",
"fields": [
{
"name": "mangoGroup",
@ -11954,7 +11934,12 @@ export const IDL: MangoV4 = {
"index": false
},
{
"name": "marketIndex",
"name": "baseTokenIndex",
"type": "u16",
"index": false
},
{
"name": "quoteTokenIndex",
"type": "u16",
"index": false
},
@ -11982,11 +11967,6 @@ export const IDL: MangoV4 = {
"name": "referrerRebatesAccrued",
"type": "u64",
"index": false
},
{
"name": "price",
"type": "i128",
"index": false
}
]
},