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: scale down init asset weights past threshold #575

Merged
merged 43 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 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
b7fd7ba
add is riskier logic to OrderFillSimulation
crispheaney Sep 5, 2023
ccceb9c
tweak to sdk
crispheaney Sep 5, 2023
83e13d1
Merge branch 'master' into crispheaney/scale-asset-weight
crispheaney Sep 6, 2023
2a4b71f
add admin function to update scaleInitialAssetWeightStart
crispheaney Sep 6, 2023
a4669a4
CHANGELOG
crispheaney Sep 6, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- sdk: updated anchor to 0.28.1-beta.2
- sdk: add priorityFeeSubscriber
- program: allow up to 12500 users
- program: scale initial asset weight for spot markets based on total deposits ([#575](https://github.com/drift-labs/protocol-v2/pull/575))

### Fixes

Expand Down
43 changes: 21 additions & 22 deletions programs/drift/src/controller/liquidation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1815,8 +1815,8 @@ pub mod liquidate_spot {
calculate_margin_requirement_and_total_collateral, MarginRequirementType,
};
use crate::math::spot_balance::{get_strict_token_value, get_token_amount, get_token_value};
use crate::state::oracle::HistoricalOracleData;
use crate::state::oracle::OracleSource;
use crate::state::oracle::{HistoricalOracleData, StrictOraclePrice};
use crate::state::oracle_map::OracleMap;
use crate::state::perp_market_map::PerpMarketMap;
use crate::state::spot_market::{SpotBalanceType, SpotMarket};
Expand Down Expand Up @@ -2267,27 +2267,26 @@ pub mod liquidate_spot {
let token_value =
get_token_value(token_amount as i128, 6, oracle_price_data.price).unwrap();

let strict_token_value_1 = get_strict_token_value(
token_amount as i128,
6,
oracle_price_data,
oracle_price_data.price / 10,
)
.unwrap();
let strict_token_value_2 = get_strict_token_value(
token_amount as i128,
6,
oracle_price_data,
oracle_price_data.price * 2,
)
.unwrap();
let strict_token_value_3 = get_strict_token_value(
-(token_amount as i128),
6,
oracle_price_data,
oracle_price_data.price * 2,
)
.unwrap();
let strict_price_1 = StrictOraclePrice {
current: oracle_price_data.price,
twap_5min: Some(oracle_price_data.price / 10),
};
let strict_token_value_1 =
get_strict_token_value(token_amount as i128, 6, &strict_price_1).unwrap();

let strict_price_2 = StrictOraclePrice {
current: oracle_price_data.price,
twap_5min: Some(oracle_price_data.price * 2),
};
let strict_token_value_2 =
get_strict_token_value(token_amount as i128, 6, &strict_price_2).unwrap();

let strict_price_3 = StrictOraclePrice {
current: oracle_price_data.price,
twap_5min: Some(oracle_price_data.price * 2),
};
let strict_token_value_3 =
get_strict_token_value(-(token_amount as i128), 6, &strict_price_3).unwrap();

assert_eq!(token_amount, 406769);
assert_eq!(token_value, 40676900);
Expand Down
8 changes: 3 additions & 5 deletions programs/drift/src/controller/lp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::math::margin::{
calculate_margin_requirement_and_total_collateral, calculate_perp_position_value_and_pnl,
meets_maintenance_margin_requirement, MarginRequirementType,
};
use crate::state::oracle::OraclePriceData;
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 @@ -722,18 +722,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
Loading