Skip to content

Commit

Permalink
program: scale initial asset weight for spot markets based on total d…
Browse files Browse the repository at this point in the history
…eposits (#575)

* initial logic to scale initial weight

* add cargo test

* add initial sdk logic

* tweak field comments

* program: add twap to initial asset weight calc

* inital work for worst case to account for free collateral contribution

* add strict oracle price

* tests work

* used cache values from calc worst case token amounts

* rm comment

* use free collateral contribution in place_spot_order

* use strict quote in calculate_perp_position_value_and_pnl

* remove w bounds from pnl calc

* expand worst case tests

* test the twap logic in worst case calc

* some simplifications

* add some tests for scaled asset weight using twap

* make calc max spot order size work

* tweak place_spot_order on how it checks if spot order is risk decreasing

* always do margin check in place order

* log margin type in meets_place_order_margin_requirement

* fix a bug

* better logic for deciding if trigger order is risk increasing

* sdk: rework calculateWorstCaseTokenAmounts

* place order params keep track of risk increasing

* tweaks

* tweak some tests

* add some comments

* tweak some tests

* calculate_max_withdrawable_amount use spot_market.get_asset_weight

* fix ts tests

* get_asset_weight just takes oracle price

* calculateAssetWeight doesn't look at twap

* add strictOraclePrice

* add strictOraclePrice

* add some sdk tests for worst case token amount calc

* add is riskier logic to OrderFillSimulation

* tweak to sdk

* add admin function to update scaleInitialAssetWeightStart

* CHANGELOG
  • Loading branch information
crispheaney committed Sep 6, 2023
1 parent 8f835a5 commit 4ebd00b
Show file tree
Hide file tree
Showing 32 changed files with 1,893 additions and 848 deletions.
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

0 comments on commit 4ebd00b

Please sign in to comment.