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

program: dynamic liq fee #601

Merged
merged 68 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
1419196
initial logic to scale initial weight
crispheaney Aug 12, 2023
3410cbe
add cargo test
crispheaney Aug 12, 2023
a07aaa6
add initial sdk logic
crispheaney Aug 12, 2023
4216eed
tweak field comments
crispheaney Aug 24, 2023
b50079a
program: add twap to initial asset weight calc
crispheaney Aug 24, 2023
7710759
inital work for worst case to account for free collateral contribution
crispheaney Aug 24, 2023
44ff8fa
add strict oracle price
crispheaney Aug 24, 2023
b9bc2d3
tests work
crispheaney Aug 25, 2023
4c0a440
used cache values from calc worst case token amounts
crispheaney Aug 25, 2023
37fc067
rm comment
crispheaney Aug 25, 2023
b1d271c
use free collateral contribution in place_spot_order
crispheaney Aug 25, 2023
693b1d8
use strict quote in calculate_perp_position_value_and_pnl
crispheaney Aug 25, 2023
093dd65
remove w bounds from pnl calc
crispheaney Aug 25, 2023
571471e
expand worst case tests
crispheaney Aug 25, 2023
eb1e0df
test the twap logic in worst case calc
crispheaney Aug 26, 2023
d04b263
some simplifications
crispheaney Aug 26, 2023
471be94
add some tests for scaled asset weight using twap
crispheaney Aug 26, 2023
59a206e
make calc max spot order size work
crispheaney Aug 28, 2023
4a9eb51
tweak place_spot_order on how it checks if spot order is risk decreasing
crispheaney Aug 28, 2023
bcd3224
Merge branch 'master' into crispheaney/scale-asset-weight
crispheaney Aug 29, 2023
3ccdbb0
always do margin check in place order
crispheaney Aug 29, 2023
f24c7bf
log margin type in meets_place_order_margin_requirement
crispheaney Aug 29, 2023
ebc003a
fix a bug
crispheaney Aug 29, 2023
27c6e20
better logic for deciding if trigger order is risk increasing
crispheaney Aug 29, 2023
b861927
sdk: rework calculateWorstCaseTokenAmounts
crispheaney Aug 30, 2023
a655af2
place order params keep track of risk increasing
crispheaney Aug 30, 2023
1ba66a6
tweaks
crispheaney Aug 30, 2023
22d3396
tweak some tests
crispheaney Aug 30, 2023
40e735d
add some comments
crispheaney Aug 30, 2023
2289506
tweak some tests
crispheaney Aug 30, 2023
57c3616
calculate_max_withdrawable_amount use spot_market.get_asset_weight
crispheaney Aug 30, 2023
a95bd83
fix ts tests
crispheaney Aug 30, 2023
80b5205
get_asset_weight just takes oracle price
crispheaney Aug 30, 2023
1404cb4
calculateAssetWeight doesn't look at twap
crispheaney Aug 30, 2023
65339a3
add strictOraclePrice
crispheaney Aug 30, 2023
3cb63d6
add strictOraclePrice
crispheaney Aug 30, 2023
c37b85e
add some sdk tests for worst case token amount calc
crispheaney Aug 30, 2023
32b69b4
Merge branch 'master' into crispheaney/scale-asset-weight
crispheaney Aug 30, 2023
e855c5c
add MarginCalculation struct
crispheaney Sep 1, 2023
23a97f3
add get_margin_ratio to MarginCalculation
crispheaney Sep 2, 2023
9bed08c
make strict() modifier for MarginContext
crispheaney Sep 2, 2023
76a74e3
dont deconstruct margin calculation in liq ix
crispheaney Sep 2, 2023
c6de11e
apply max if fee
crispheaney Sep 2, 2023
e2b03a5
make margin_requirement_with_buffer private
crispheaney Sep 3, 2023
4019210
if fee for spot liquidation still lets user outta liq territory
crispheaney Sep 3, 2023
03b9b42
rm comment
crispheaney Sep 3, 2023
0870a89
add new max if fee calcs
crispheaney Sep 5, 2023
0fd8f61
remove calculate_if_fee fn
crispheaney Sep 5, 2023
09921f2
remove logic to track margin ratio
crispheaney Sep 5, 2023
16ce7f0
tweak language for tracking mkt margin requirement
crispheaney Sep 5, 2023
c894ad7
add test for calculate_perp_if_fee
crispheaney Sep 5, 2023
3479d2a
calculate_spot_if_fee tests
crispheaney Sep 6, 2023
f0e73fa
Merge branch 'master' into crispheaney/dynamic-liq-fee
crispheaney Sep 6, 2023
fc3c1c7
fix bug w tracked_market_margin_shortage
crispheaney Sep 6, 2023
e317389
change how margin is check in fills
crispheaney Sep 6, 2023
5c47c42
extra safety check in MarginCalculation
crispheaney Sep 6, 2023
e3a1052
make MarketIdentifier a struct
crispheaney Sep 6, 2023
9bf1525
add back isolated liability
crispheaney Sep 7, 2023
bd9f072
fix tests/liquidateSpotSocialLoss.ts (no if liq fee)
0xbigz Sep 7, 2023
dd70f52
fix sdk/src/user.ts
0xbigz Sep 7, 2023
57fb4b9
fix tests/insuranceFundStake.ts
0xbigz Sep 7, 2023
fdec708
prettify tests/insuranceFundStake.ts
0xbigz Sep 7, 2023
bc8b5cb
try fix liq perp tests
crispheaney Sep 7, 2023
d0d2a45
yarn prettify:fix
crispheaney Sep 7, 2023
2135b32
Merge branch 'master' into crispheaney/dynamic-liq-fee
crispheaney Sep 14, 2023
81e02cd
fix merge
crispheaney Sep 14, 2023
354cbd4
try fix insurance fund stake test
crispheaney Sep 14, 2023
5d27b8d
CHANGELOG
crispheaney Sep 15, 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
653 changes: 309 additions & 344 deletions programs/drift/src/controller/liquidation.rs

Large diffs are not rendered by default.

629 changes: 472 additions & 157 deletions programs/drift/src/controller/liquidation/tests.rs

Large diffs are not rendered by default.

36 changes: 19 additions & 17 deletions programs/drift/src/controller/lp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ use crate::math::constants::{
SPOT_BALANCE_PRECISION_U64, SPOT_CUMULATIVE_INTEREST_PRECISION, SPOT_WEIGHT_PRECISION,
};
use crate::math::margin::{
calculate_margin_requirement_and_total_collateral, calculate_perp_position_value_and_pnl,
meets_maintenance_margin_requirement, MarginRequirementType,
calculate_margin_requirement_and_total_collateral_and_liability_info,
calculate_perp_position_value_and_pnl, meets_maintenance_margin_requirement,
MarginRequirementType,
};
use crate::state::oracle::OraclePriceData;
use crate::state::margin_calculation::{MarginCalculation, MarginContext};
use crate::state::oracle::{HistoricalOracleData, OracleSource};
use crate::state::oracle::{OraclePriceData, StrictOraclePrice};
use crate::state::oracle_map::OracleMap;
use crate::state::perp_market::{MarketStatus, PerpMarket, PoolBalance};
use crate::state::perp_market_map::PerpMarketMap;
Expand Down Expand Up @@ -529,16 +531,18 @@ pub fn test_lp_settle_pnl() {
..State::default()
};

let (margin_requirement1, total_collateral1, _, _) =
calculate_margin_requirement_and_total_collateral(
&user,
&market_map,
MarginRequirementType::Initial,
&spot_market_map,
&mut oracle_map,
None,
)
.unwrap();
let MarginCalculation {
total_collateral: total_collateral1,
margin_requirement: margin_requirement1,
..
} = calculate_margin_requirement_and_total_collateral_and_liability_info(
&user,
&market_map,
&spot_market_map,
&mut oracle_map,
MarginContext::standard(MarginRequirementType::Initial),
)
.unwrap();

assert_eq!(total_collateral1, 49999988);
assert_eq!(margin_requirement1, 2099020); // $2+ for margin req
Expand Down Expand Up @@ -722,18 +726,16 @@ fn test_lp_margin_calc() {
assert_eq!(sim_user_pos.quote_asset_amount, -20000000000);
assert_eq!(sim_user_pos.last_cumulative_funding_rate, 16900000000);

let strict_quote_price = StrictOraclePrice::test(1000000);
// ensure margin calc doesnt incorrectly count funding rate (funding pnl MUST come before settling lp)
let (margin_requirement, weighted_unrealized_pnl, worse_case_base_asset_value) =
calculate_perp_position_value_and_pnl(
&user.perp_positions[0],
&market,
&oracle_price_data,
1000000,
1000000,
&strict_quote_price,
crate::math::margin::MarginRequirementType::Initial,
0,
false,
false,
)
.unwrap();

Expand Down
Loading