Fees buyback: Credit users for ui part of serum fees (#481)

At least for markets with USDC quote currency.
This commit is contained in:
Christian Kamm 2023-02-28 09:48:15 +01:00 committed by GitHub
parent 9a13c1366a
commit df4a7b0474
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 4 deletions

View File

@ -143,7 +143,9 @@ pub fn serum3_liq_force_cancel_orders(
let mut account = ctx.accounts.account.load_full_mut()?;
let mut base_bank = ctx.accounts.base_bank.load_mut()?;
let mut quote_bank = ctx.accounts.quote_bank.load_mut()?;
let group = ctx.accounts.group.load()?;
apply_settle_changes(
&group,
ctx.accounts.account.key(),
&mut account.borrow_mut(),
&mut base_bank,

View File

@ -411,6 +411,7 @@ fn apply_vault_difference(
/// Uses the changes in OpenOrders and vaults to adjust the user token position,
/// collect fees and optionally adjusts the HealthCache.
pub fn apply_settle_changes(
group: &Group,
account_pk: Pubkey,
account: &mut MangoAccountRefMut,
base_bank: &mut Bank,
@ -430,6 +431,17 @@ pub fn apply_settle_changes(
.saturating_sub(after_oo.native_rebates());
quote_bank.collected_fees_native += I80F48::from(received_fees);
// Ideally we could credit buyback_fees at the current value of the received fees,
// but the settle_funds instruction currently doesn't receive the oracle account
// that would be needed for it.
if quote_bank.token_index == QUOTE_TOKEN_INDEX {
let now_ts = Clock::get()?.unix_timestamp.try_into().unwrap();
account
.fixed
.expire_buyback_fees(now_ts, group.buyback_fees_expiry_interval);
account.fixed.accrue_buyback_fees(received_fees);
}
// Don't count the referrer rebate fees as part of the vault change that should be
// credited to the user.
let after_quote_vault_adjusted = after_quote_vault - received_fees;

View File

@ -106,7 +106,9 @@ pub fn serum3_settle_funds(ctx: Context<Serum3SettleFunds>) -> Result<()> {
let mut account = ctx.accounts.account.load_full_mut()?;
let mut base_bank = ctx.accounts.base_bank.load_mut()?;
let mut quote_bank = ctx.accounts.quote_bank.load_mut()?;
let group = ctx.accounts.group.load()?;
apply_settle_changes(
&group,
ctx.accounts.account.key(),
&mut account.borrow_mut(),
&mut base_bank,

View File

@ -334,9 +334,9 @@ async fn test_serum_loan_origination_fees() -> Result<(), TransportError> {
}
.create(solana)
.await;
let base_token = &tokens[0];
let base_token = &tokens[1];
let base_bank = base_token.bank;
let quote_token = &tokens[1];
let quote_token = &tokens[0];
let quote_bank = quote_token.bank;
//
@ -597,6 +597,12 @@ async fn test_serum_loan_origination_fees() -> Result<(), TransportError> {
0.1
));
let account_data = solana.get_account::<MangoAccount>(account).await;
assert_eq!(
account_data.buyback_fees_accrued_current,
serum_fee(fill_amount) as u64
);
assert_eq!(
account_position(solana, account, quote_bank).await,
deposit_amount + without_serum_taker_fee(fill_amount)

View File

@ -142,8 +142,8 @@ impl TestContextBuilder {
index: i,
decimals: 6,
unit: 10u64.pow(6) as f64,
base_lot: 0 as f64,
quote_lot: 0 as f64,
base_lot: 100 as f64,
quote_lot: 10 as f64,
pubkey: Pubkey::default(),
authority: TestKeypair::new(),
});