Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bigz/amm-fee-pool-insurance #332

Merged
merged 24 commits into from
Jan 29, 2023
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f4d0fff
bigz/amm-fee-pool-insurance
0xbigz Jan 19, 2023
10e26b3
dead import / sign cleanup on losses_remaining etc
0xbigz Jan 23, 2023
aa48329
sdk: calculateEstimatedPerpEntryPrice use getLimitBids/getLimitAsks
crispheaney Jan 19, 2023
9610eb2
program: fix paying fee_pool_delta when filling with open book
crispheaney Jan 19, 2023
86ffc2b
program: better rounding for openbook limit price
crispheaney Jan 20, 2023
ec6f378
sdk: return worst and best price from calculateEstimatedPerpEntryPrice
crispheaney Jan 20, 2023
c6b18d8
sdk: array of users to skip in calculateEstimatedPerpEntryPrice
crispheaney Jan 20, 2023
0be3d4f
sdk: calculateEstimatedPerpEntryPrice accounts for limited vamm liqui…
crispheaney Jan 21, 2023
4f93fc2
sdk: tweak available liquidity calc in calculateEstimatedPerpEntryPrice
crispheaney Jan 21, 2023
21359aa
sdk: add calculateEstimatedSpotEntryPrice
crispheaney Jan 22, 2023
8bc27be
program: only let market orders match against resting limit orders (#…
crispheaney Jan 22, 2023
b679000
program: allow for 2000 users
crispheaney Jan 22, 2023
a46292d
program: add additional logging for casting error in calculate_fee_fo…
crispheaney Jan 22, 2023
34a0318
v2.12.0
crispheaney Jan 22, 2023
5be6773
sdk: add loadDlob example
crispheaney Jan 23, 2023
b55a938
tests: fix multipleMakerOrders.ts
crispheaney Jan 23, 2023
f190c16
merge master
0xbigz Jan 25, 2023
aa27c3c
more progress on cargo test (wip)
0xbigz Jan 27, 2023
c51066b
working cargo test
0xbigz Jan 27, 2023
d567bfb
tests/delistMarketLiq.ts: fix for fee pool as insurance
0xbigz Jan 27, 2023
b80d950
Merge branch 'master' into bigz/amm-fee-pool-insurance
0xbigz Jan 28, 2023
4db11ac
fix tests/liquidatePerp.ts
0xbigz Jan 28, 2023
118b756
fix tests/liquidatePerpAndLp.ts
0xbigz Jan 28, 2023
26a4918
fix tests/liquidatePerpAndLp.ts cumulativeFundingRateDelta
0xbigz Jan 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion programs/drift/src/controller/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ops::{Deref, DerefMut};
use anchor_lang::prelude::*;
use solana_program::msg;

use crate::controller::amm::get_fee_pool_tokens;
use crate::controller::funding::settle_funding_payment;
use crate::controller::lp::burn_lp_shares;
use crate::controller::orders;
Expand Down Expand Up @@ -45,6 +46,7 @@ use crate::math::orders::{
};
use crate::math::position::calculate_base_asset_value_with_oracle_price;
use crate::math::safe_math::SafeMath;
use crate::math::spot_balance::get_token_amount;
use crate::math::spot_balance::get_token_value;
use crate::state::events::{
LiquidateBorrowForPerpPnlRecord, LiquidatePerpPnlForDepositRecord, LiquidatePerpRecord,
Expand Down Expand Up @@ -2081,7 +2083,32 @@ pub fn resolve_perp_bankruptcy(
if_payment
};

let loss_to_socialize = loss.safe_add(if_payment.cast::<i128>()?)?;
let losses_remaining: i128 = loss.safe_add(if_payment.cast::<i128>()?)?;

let fee_pool_payment: i128 = if losses_remaining > 0 {
let perp_market = &mut perp_market_map.get_ref_mut(&market_index)?;
let spot_market = &mut spot_market_map.get_ref_mut(&QUOTE_SPOT_MARKET_INDEX)?;
let fee_pool_tokens = get_fee_pool_tokens(perp_market, spot_market)?;

let payment = losses_remaining.min(fee_pool_tokens.cast()?);
0xbigz marked this conversation as resolved.
Show resolved Hide resolved
payment
} else {
0
};

if fee_pool_payment != 0 {
let perp_market = &mut perp_market_map.get_ref_mut(&market_index)?;
let spot_market = &mut spot_market_map.get_ref_mut(&QUOTE_SPOT_MARKET_INDEX)?;
update_spot_balances(
fee_pool_payment.unsigned_abs(),
&SpotBalanceType::Borrow,
spot_market,
&mut perp_market.amm.fee_pool,
false,
)?;
}

let loss_to_socialize = losses_remaining.safe_add(fee_pool_payment.cast::<i128>()?)?;

let cumulative_funding_rate_delta = calculate_funding_rate_deltas_to_resolve_bankruptcy(
loss_to_socialize,
Expand Down