Skip to content

Commit a21a4cd

Browse files
committed
merge master
2 parents 2aa80ab + b4fa440 commit a21a4cd

File tree

26 files changed

+1634
-316
lines changed

26 files changed

+1634
-316
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Features
1111

12+
- program: add titan to whitelisted swap programs ([#1952](https://github.com/drift-labs/protocol-v2/pull/1952))
13+
1214
### Fixes
1315

1416
### Breaking

programs/drift/src/controller/amm.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,33 @@ pub fn update_spreads(
185185
let max_ref_offset = market.amm.get_max_reference_price_offset()?;
186186

187187
let reference_price_offset = if max_ref_offset > 0 {
188-
let liquidity_ratio = amm_spread::calculate_inventory_liquidity_ratio(
189-
market.amm.base_asset_amount_with_amm,
190-
market.amm.base_asset_reserve,
191-
market.amm.min_base_asset_reserve,
192-
market.amm.max_base_asset_reserve,
193-
)?;
188+
let liquidity_ratio =
189+
amm_spread::calculate_inventory_liquidity_ratio_for_reference_price_offset(
190+
market.amm.base_asset_amount_with_amm,
191+
market.amm.base_asset_reserve,
192+
market.amm.min_base_asset_reserve,
193+
market.amm.max_base_asset_reserve,
194+
)?;
194195

195196
let signed_liquidity_ratio =
196197
liquidity_ratio.safe_mul(market.amm.get_protocol_owned_position()?.signum().cast()?)?;
197198

199+
let deadband_pct = market.amm.get_reference_price_offset_deadband_pct()?;
200+
let liquidity_fraction_after_deadband =
201+
if signed_liquidity_ratio.unsigned_abs() <= deadband_pct {
202+
0
203+
} else {
204+
signed_liquidity_ratio.safe_sub(
205+
deadband_pct
206+
.cast::<i128>()?
207+
.safe_mul(signed_liquidity_ratio.signum())?,
208+
)?
209+
};
210+
198211
amm_spread::calculate_reference_price_offset(
199212
reserve_price,
200213
market.amm.last_24h_avg_funding_rate,
201-
signed_liquidity_ratio,
214+
liquidity_fraction_after_deadband,
202215
market.amm.min_order_size,
203216
market
204217
.amm

programs/drift/src/controller/position/tests.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ fn amm_ref_price_offset_decay_logic() {
844844
.unwrap();
845845
assert_eq!(perp_market.amm.last_update_slot, clock_slot);
846846
assert_eq!(perp_market.amm.last_oracle_valid, true);
847-
assert_eq!(perp_market.amm.reference_price_offset, 7350);
847+
assert_eq!(perp_market.amm.reference_price_offset, 4458);
848848

849849
perp_market.amm.last_mark_price_twap_5min = (perp_market
850850
.amm
@@ -894,28 +894,28 @@ fn amm_ref_price_offset_decay_logic() {
894894
assert_eq!(
895895
offsets,
896896
[
897-
7140, 6930, 6720, 6510, 6300, 6090, 6070, 6050, 6030, 6010, 5800, 5590, 5380, 5170,
898-
4960, 4750, 4540, 4330, 4120, 3910, 3700, 3490, 3280, 3070, 2860, 2650, 2440, 2230,
899-
2020, 1810, 1620, 1449, 1296, 1158, 1034, 922, 821, 730, 648, 575, 509, 450, 396, 348,
900-
305, 266, 231, 199, 171, 145, 122, 101, 81, 61, 41, 21, 1, 0, 0, 0
897+
4248, 4038, 3828, 3618, 3408, 3198, 3178, 3158, 3138, 3118, 2908, 2698, 2488, 2278,
898+
2068, 1858, 1664, 1489, 1332, 1190, 1062, 947, 844, 751, 667, 592, 524, 463, 408, 359,
899+
315, 275, 239, 207, 178, 152, 128, 107, 87, 67, 47, 27, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0,
900+
0, 0, 0, 0, 0, 0, 0, 0
901901
]
902902
);
903903
assert_eq!(
904904
lspreads,
905905
[
906-
726, 726, 726, 726, 726, 726, 536, 536, 536, 536, 726, 726, 726, 726, 726, 726, 726,
907-
726, 726, 726, 726, 726, 726, 726, 726, 726, 726, 726, 726, 726, 706, 687, 669, 654,
908-
640, 628, 617, 607, 598, 589, 582, 575, 570, 564, 559, 555, 551, 548, 544, 542, 539,
909-
537, 536, 536, 536, 536, 536, 526, 526, 526
906+
726, 726, 726, 726, 726, 726, 536, 536, 536, 536, 726, 726, 726, 726, 726, 726, 710,
907+
691, 673, 658, 644, 631, 619, 609, 600, 591, 584, 577, 571, 565, 560, 556, 552, 548,
908+
545, 542, 540, 537, 536, 536, 536, 536, 536, 526, 526, 526, 526, 526, 526, 526, 526,
909+
526, 526, 526, 526, 526, 526, 526, 526, 526
910910
]
911911
);
912912
assert_eq!(
913913
sspreads,
914914
[
915-
7150, 6940, 6730, 6520, 6310, 6100, 6080, 6060, 6040, 6020, 5810, 5600, 5390, 5180,
916-
4970, 4760, 4550, 4340, 4130, 3920, 3710, 3500, 3290, 3080, 2870, 2660, 2450, 2240,
917-
2030, 1820, 1630, 1459, 1306, 1168, 1044, 932, 831, 740, 658, 585, 519, 460, 406, 358,
918-
315, 276, 241, 209, 181, 155, 132, 111, 91, 71, 51, 31, 11, 10, 10, 10
915+
4258, 4048, 3838, 3628, 3418, 3208, 3188, 3168, 3148, 3128, 2918, 2708, 2498, 2288,
916+
2078, 1868, 1674, 1499, 1342, 1200, 1072, 957, 854, 761, 677, 602, 534, 473, 418, 369,
917+
325, 285, 249, 217, 188, 162, 138, 117, 97, 77, 57, 37, 17, 10, 10, 10, 10, 10, 10, 10,
918+
10, 10, 10, 10, 10, 10, 10, 10, 10, 10
919919
]
920920
);
921921
}
@@ -1018,7 +1018,7 @@ fn amm_negative_ref_price_offset_decay_logic() {
10181018
.unwrap();
10191019
assert_eq!(perp_market.amm.last_update_slot, clock_slot);
10201020
assert_eq!(perp_market.amm.last_oracle_valid, true);
1021-
assert_eq!(perp_market.amm.reference_price_offset, 7350);
1021+
assert_eq!(perp_market.amm.reference_price_offset, 4458);
10221022

10231023
perp_market.amm.last_mark_price_twap_5min = (perp_market
10241024
.amm
@@ -1069,34 +1069,31 @@ fn amm_negative_ref_price_offset_decay_logic() {
10691069
assert_eq!(
10701070
offsets,
10711071
[
1072-
-7140, -6930, -6720, -6510, -6300, -6090, -6070, -6050, -6030, -6010, -5800, -5590,
1073-
-5380, -5170, -4960, -4750, -4540, -4330, -4120, -3910, -3700, -3490, -3280, -3070,
1074-
-2860, -2650, -2440, -2230, -2020, -1810, -1600, -1390, -1180, -970, -760, -550, -340,
1075-
-130, 0, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000,
1076-
10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000,
1077-
10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000,
1078-
10000, 10000, 10000, 10000, 10000, 10000
1072+
-4248, -4038, -3828, -3618, -3408, -3198, -3178, -3158, -3138, -3118, -2908, -2698,
1073+
-2488, -2278, -2068, -1858, -1648, -1438, -1228, -1018, -808, -598, -388, -178, 0,
1074+
7654, 7652, 7651, 7649, 7648, 7646, 7645, 7643, 7641, 7640, 7638, 7637, 7635, 7634,
1075+
7632, 7631, 7629, 7628, 7626, 7625, 7623, 7622, 7620, 7619, 7618, 7616, 7615, 7613,
1076+
7612, 7610, 7609, 7607, 7606, 7605, 7603, 7602, 7600, 7599, 7597, 7596, 7595, 7593,
1077+
7592, 7591, 7589, 7588, 7586, 7585, 7584, 7582, 7581, 7580, 7578, 7577, 7576
10791078
]
10801079
);
10811080
assert_eq!(
10821081
sspreads,
10831082
[
10841083
210, 210, 210, 210, 210, 210, 20, 20, 20, 20, 210, 210, 210, 210, 210, 210, 210, 210,
1085-
210, 210, 210, 210, 210, 210, 210, 210, 210, 210, 210, 210, 210, 210, 210, 210, 210,
1086-
210, 210, 210, 130, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
1084+
210, 210, 210, 210, 210, 210, 178, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10871085
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
1088-
10, 10
1086+
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10
10891087
]
10901088
);
10911089
assert_eq!(
10921090
lspreads,
10931091
[
1094-
7666, 7456, 7246, 7036, 6826, 6616, 6596, 6576, 6556, 6536, 6326, 6116, 5906, 5696,
1095-
5486, 5276, 5066, 4856, 4646, 4436, 4226, 4016, 3806, 3596, 3386, 3176, 2966, 2756,
1096-
2546, 2336, 2126, 1916, 1706, 1496, 1286, 1076, 866, 656, 526, 526, 526, 526, 526, 526,
1092+
4774, 4564, 4354, 4144, 3934, 3724, 3704, 3684, 3664, 3644, 3434, 3224, 3014, 2804,
1093+
2594, 2384, 2174, 1964, 1754, 1544, 1334, 1124, 914, 704, 526, 526, 526, 526, 526, 526,
10971094
526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526,
10981095
526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526,
1099-
526, 526
1096+
526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526, 526
11001097
]
11011098
);
11021099
}

programs/drift/src/ids.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,8 @@ pub mod dflow_mainnet_aggregator_4 {
120120
use solana_program::declare_id;
121121
declare_id!("DF1ow4tspfHX9JwWJsAb9epbkA8hmpSEAtxXy1V27QBH");
122122
}
123+
124+
pub mod titan_mainnet_argos_v1 {
125+
use solana_program::declare_id;
126+
declare_id!("T1TANpTeScyeqVzzgNViGDNrkQ6qHz9KrSBS4aNXvGT");
127+
}

programs/drift/src/instructions/admin.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,8 @@ pub fn handle_initialize_perp_market(
10961096
quote_asset_amount_with_unsettled_lp: 0,
10971097
reference_price_offset: 0,
10981098
amm_inventory_spread_adjustment: 0,
1099-
padding: [0; 3],
1099+
reference_price_offset_deadband_pct: 0,
1100+
padding: [0; 2],
11001101
last_funding_oracle_twap: 0,
11011102
},
11021103
};
@@ -3487,6 +3488,49 @@ pub fn handle_update_perp_market_curve_update_intensity(
34873488
Ok(())
34883489
}
34893490

3491+
#[access_control(
3492+
perp_market_valid(&ctx.accounts.perp_market)
3493+
)]
3494+
pub fn handle_update_perp_market_reference_price_offset_deadband_pct(
3495+
ctx: Context<HotAdminUpdatePerpMarket>,
3496+
reference_price_offset_deadband_pct: u8,
3497+
) -> Result<()> {
3498+
validate!(
3499+
reference_price_offset_deadband_pct <= 100,
3500+
ErrorCode::DefaultError,
3501+
"invalid reference_price_offset_deadband_pct",
3502+
)?;
3503+
let perp_market = &mut load_mut!(ctx.accounts.perp_market)?;
3504+
msg!("perp market {}", perp_market.market_index);
3505+
3506+
msg!(
3507+
"perp_market.amm.reference_price_offset_deadband_pct: {} -> {}",
3508+
perp_market.amm.reference_price_offset_deadband_pct,
3509+
reference_price_offset_deadband_pct
3510+
);
3511+
3512+
let liquidity_ratio =
3513+
crate::math::amm_spread::calculate_inventory_liquidity_ratio_for_reference_price_offset(
3514+
perp_market.amm.base_asset_amount_with_amm,
3515+
perp_market.amm.base_asset_reserve,
3516+
perp_market.amm.min_base_asset_reserve,
3517+
perp_market.amm.max_base_asset_reserve,
3518+
)?;
3519+
3520+
let signed_liquidity_ratio = liquidity_ratio.safe_mul(
3521+
perp_market
3522+
.amm
3523+
.get_protocol_owned_position()?
3524+
.signum()
3525+
.cast()?,
3526+
)?;
3527+
3528+
msg!("current signed liquidity ratio: {}", signed_liquidity_ratio);
3529+
3530+
perp_market.amm.reference_price_offset_deadband_pct = reference_price_offset_deadband_pct;
3531+
Ok(())
3532+
}
3533+
34903534
pub fn handle_update_lp_cooldown_time(
34913535
ctx: Context<AdminUpdateState>,
34923536
lp_cooldown_time: u64,

programs/drift/src/instructions/keeper.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ use crate::controller::token::{receive, send_from_program_vault};
2424
use crate::error::ErrorCode;
2525
use crate::get_then_update_id;
2626
use crate::ids::admin_hot_wallet;
27-
use crate::ids::dflow_mainnet_aggregator_4;
28-
use crate::ids::{jupiter_mainnet_3, jupiter_mainnet_4, jupiter_mainnet_6, serum_program};
27+
use crate::ids::{
28+
dflow_mainnet_aggregator_4, jupiter_mainnet_3, jupiter_mainnet_4, jupiter_mainnet_6,
29+
serum_program, titan_mainnet_argos_v1,
30+
};
2931
use crate::instructions::constraints::*;
3032
use crate::instructions::optional_accounts::get_revenue_share_escrow_account;
3133
use crate::instructions::optional_accounts::{load_maps, AccountMaps};
@@ -1734,11 +1736,12 @@ pub fn handle_liquidate_spot_with_swap_begin<'c: 'info, 'info>(
17341736
jupiter_mainnet_4::ID,
17351737
jupiter_mainnet_6::ID,
17361738
dflow_mainnet_aggregator_4::ID,
1739+
titan_mainnet_argos_v1::ID,
17371740
];
17381741
validate!(
17391742
whitelisted_programs.contains(&ix.program_id),
17401743
ErrorCode::InvalidLiquidateSpotWithSwap,
1741-
"only allowed to pass in ixs to token, openbook, and Jupiter v3/v4/v6 programs"
1744+
"only allowed to pass in ixs to ATA, openbook, Jupiter v3/v4/v6, dflow, or titan programs"
17421745
)?;
17431746

17441747
for meta in ix.accounts.iter() {

programs/drift/src/instructions/user.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ use crate::controller::spot_position::{
2323
};
2424
use crate::error::ErrorCode;
2525
use crate::ids::admin_hot_wallet;
26-
use crate::ids::dflow_mainnet_aggregator_4;
2726
use crate::ids::{
28-
jupiter_mainnet_3, jupiter_mainnet_4, jupiter_mainnet_6, lighthouse, marinade_mainnet,
29-
serum_program,
27+
dflow_mainnet_aggregator_4, jupiter_mainnet_3, jupiter_mainnet_4, jupiter_mainnet_6,
28+
lighthouse, marinade_mainnet, serum_program, titan_mainnet_argos_v1,
3029
};
3130
use crate::instructions::constraints::*;
3231
use crate::instructions::optional_accounts::get_revenue_share_escrow_account;
@@ -3918,6 +3917,7 @@ pub fn handle_begin_swap<'c: 'info, 'info>(
39183917
jupiter_mainnet_4::ID,
39193918
jupiter_mainnet_6::ID,
39203919
dflow_mainnet_aggregator_4::ID,
3920+
titan_mainnet_argos_v1::ID,
39213921
];
39223922
if !delegate_is_signer {
39233923
whitelisted_programs.push(Token::id());
@@ -3927,7 +3927,7 @@ pub fn handle_begin_swap<'c: 'info, 'info>(
39273927
validate!(
39283928
whitelisted_programs.contains(&ix.program_id),
39293929
ErrorCode::InvalidSwap,
3930-
"only allowed to pass in ixs to token, openbook, and Jupiter v3/v4/v6 programs"
3930+
"only allowed to pass in ixs to ATA, openbook, Jupiter v3/v4/v6, dflow, or titan programs"
39313931
)?;
39323932

39333933
for meta in ix.accounts.iter() {

programs/drift/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,16 @@ pub mod drift {
14671467
handle_update_perp_market_curve_update_intensity(ctx, curve_update_intensity)
14681468
}
14691469

1470+
pub fn update_perp_market_reference_price_offset_deadband_pct(
1471+
ctx: Context<HotAdminUpdatePerpMarket>,
1472+
reference_price_offset_deadband_pct: u8,
1473+
) -> Result<()> {
1474+
handle_update_perp_market_reference_price_offset_deadband_pct(
1475+
ctx,
1476+
reference_price_offset_deadband_pct,
1477+
)
1478+
}
1479+
14701480
pub fn update_lp_cooldown_time(
14711481
ctx: Context<AdminUpdateState>,
14721482
lp_cooldown_time: u64,

programs/drift/src/math/amm_spread.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::math::constants::{
1212
BID_ASK_SPREAD_PRECISION, BID_ASK_SPREAD_PRECISION_I128, DEFAULT_LARGE_BID_ASK_FACTOR,
1313
DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT, FUNDING_RATE_BUFFER,
1414
MAX_BID_ASK_INVENTORY_SKEW_FACTOR, PEG_PRECISION, PERCENTAGE_PRECISION,
15-
PERCENTAGE_PRECISION_I128, PERCENTAGE_PRECISION_U64, PRICE_PRECISION, PRICE_PRECISION_I128,
16-
PRICE_PRECISION_I64,
15+
PERCENTAGE_PRECISION_I128, PERCENTAGE_PRECISION_I64, PERCENTAGE_PRECISION_U64, PRICE_PRECISION,
16+
PRICE_PRECISION_I128, PRICE_PRECISION_I64,
1717
};
1818
use crate::math::safe_math::SafeMath;
1919
use crate::state::perp_market::{ContractType, PerpMarket, AMM};
@@ -193,6 +193,35 @@ pub fn calculate_inventory_liquidity_ratio(
193193
Ok(amm_inventory_pct)
194194
}
195195

196+
pub fn calculate_inventory_liquidity_ratio_for_reference_price_offset(
197+
base_asset_amount_with_amm: i128,
198+
base_asset_reserve: u128,
199+
min_base_asset_reserve: u128,
200+
max_base_asset_reserve: u128,
201+
) -> DriftResult<i128> {
202+
// inventory scale
203+
let (max_bids, max_asks) = _calculate_market_open_bids_asks(
204+
base_asset_reserve,
205+
min_base_asset_reserve,
206+
max_base_asset_reserve,
207+
)?;
208+
209+
let avg_liquidity = (max_bids.safe_add(max_asks.abs())?).safe_div(2)?;
210+
211+
let amm_inventory_pct = if base_asset_amount_with_amm.abs() < avg_liquidity {
212+
base_asset_amount_with_amm
213+
.abs()
214+
.safe_mul(PERCENTAGE_PRECISION_I128)
215+
.unwrap_or(i128::MAX)
216+
.safe_div(avg_liquidity.max(1))?
217+
.min(PERCENTAGE_PRECISION_I128)
218+
} else {
219+
PERCENTAGE_PRECISION_I128 // 100%
220+
};
221+
222+
Ok(amm_inventory_pct)
223+
}
224+
196225
pub fn calculate_spread_inventory_scale(
197226
base_asset_amount_with_amm: i128,
198227
base_asset_reserve: u128,
@@ -570,7 +599,7 @@ pub fn calculate_reference_price_offset(
570599
mark_twap_slow: u64,
571600
max_offset_pct: i64,
572601
) -> DriftResult<i32> {
573-
if last_24h_avg_funding_rate == 0 {
602+
if last_24h_avg_funding_rate == 0 || liquidity_fraction == 0 {
574603
return Ok(0);
575604
}
576605

0 commit comments

Comments
 (0)