Skip to content

Commit 41fbaa3

Browse files
committed
merge master into dlp
2 parents 8b73b66 + 6b4f02e commit 41fbaa3

34 files changed

+1625
-111
lines changed

.devcontainer/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ RUN mkdir -p /workdir /tmp \
1717
&& apt-get update -qq \
1818
&& apt-get upgrade -qq \
1919
&& apt-get install -y --no-install-recommends \
20-
build-essential git curl wget jq pkg-config python3-pip xz-utils ca-certificates \
21-
libssl-dev libudev-dev bash software-properties-common \
20+
build-essential git curl wget jq pkg-config python3-pip xz-utils ca-certificates \
21+
libssl-dev libudev-dev bash software-properties-common \
2222
&& add-apt-repository 'deb http://deb.debian.org/debian bookworm main' \
2323
&& apt-get update -qq \
2424
&& apt-get install -y libc6 libc6-dev \

CHANGELOG.md

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

1414
### Breaking
1515

16+
## [2.141.0] - 2025-10-03
17+
18+
### Features
19+
20+
- program: disallow builder to be escrow authority ([#1930](https://github.com/drift-labs/protocol-v2/pull/1930))
21+
- dont panic on settle-pnl when no position ([#1928](https://github.com/drift-labs/protocol-v2/pull/1928))
22+
23+
### Fixes
24+
25+
### Breaking
26+
1627
## [2.140.0] - 2025-09-29
1728

1829
### Features

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

programs/drift/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "drift"
3-
version = "2.140.0"
3+
version = "2.141.0"
44
description = "Created with Anchor"
55
edition = "2018"
66

programs/drift/src/controller/orders.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,6 +2243,10 @@ pub fn fulfill_perp_order_with_amm(
22432243
direction,
22442244
order_id
22452245
);
2246+
let user_order_has_builder = user.orders[order_index].is_has_builder();
2247+
if user_order_has_builder && rev_share_escrow.is_none() {
2248+
msg!("Order has builder but no escrow account included, in the future this will fail.");
2249+
}
22462250

22472251
validation::perp_market::validate_amm_account_for_fill(&market.amm, order_direction)?;
22482252

@@ -2331,7 +2335,7 @@ pub fn fulfill_perp_order_with_amm(
23312335

23322336
if builder_fee != 0 {
23332337
if let (Some(idx), Some(escrow)) = (builder_order_idx, rev_share_escrow.as_mut()) {
2334-
let mut order = escrow.get_order_mut(idx)?;
2338+
let order = escrow.get_order_mut(idx)?;
23352339
order.fees_accrued = order.fees_accrued.safe_add(builder_fee)?;
23362340
} else {
23372341
msg!("Order has builder fee but no escrow account found, in the future this tx will fail.");
@@ -2593,6 +2597,10 @@ pub fn fulfill_perp_order_with_match(
25932597

25942598
let oracle_price = oracle_map.get_price_data(&market.oracle_id())?.price;
25952599
let taker_direction: PositionDirection = taker.orders[taker_order_index].direction;
2600+
let taker_order_has_builder = taker.orders[taker_order_index].is_has_builder();
2601+
if taker_order_has_builder && rev_share_escrow.is_none() {
2602+
msg!("Order has builder but no escrow account included, in the future this will fail.");
2603+
}
25962604

25972605
let taker_price = if let Some(taker_limit_price) = taker_limit_price {
25982606
taker_limit_price
@@ -2840,7 +2848,7 @@ pub fn fulfill_perp_order_with_match(
28402848

28412849
if builder_fee != 0 {
28422850
if let (Some(idx), Some(escrow)) = (builder_order_idx, rev_share_escrow.as_deref_mut()) {
2843-
let mut order = escrow.get_order_mut(idx)?;
2851+
let order = escrow.get_order_mut(idx)?;
28442852
order.fees_accrued = order.fees_accrued.safe_add(builder_fee)?;
28452853
} else {
28462854
msg!("Order has builder fee but no escrow account found, in the future this tx will fail.");
@@ -2906,7 +2914,7 @@ pub fn fulfill_perp_order_with_match(
29062914

29072915
if let (Some(idx), Some(escrow)) = (referrer_builder_order_idx, rev_share_escrow.as_deref_mut())
29082916
{
2909-
let mut order = escrow.get_order_mut(idx)?;
2917+
let order = escrow.get_order_mut(idx)?;
29102918
order.fees_accrued = order.fees_accrued.safe_add(referrer_reward)?;
29112919
} else if let (Some(referrer), Some(referrer_stats)) =
29122920
(referrer.as_mut(), referrer_stats.as_mut())

programs/drift/src/controller/pnl.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,16 @@ pub fn settle_pnl(
7979

8080
drop(market);
8181

82-
let position_index = get_position_index(&user.perp_positions, market_index)?;
82+
let position_index = match get_position_index(&user.perp_positions, market_index) {
83+
Ok(index) => index,
84+
Err(e) => {
85+
return mode.result(
86+
e,
87+
market_index,
88+
&format!("User has no position in market {}", market_index),
89+
)
90+
}
91+
};
8392
let unrealized_pnl = user.perp_positions[position_index].get_unrealized_pnl(oracle_price)?;
8493

8594
// cannot settle negative pnl this way on a user who is in liquidation territory

programs/drift/src/ids.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,8 @@ pub mod lp_pool_swap_wallet {
115115
use solana_program::declare_id;
116116
declare_id!("25qbsE2oWri76c9a86ubn17NKKdo6Am4HXD2Jm8vT8K4");
117117
}
118+
119+
pub mod dflow_mainnet_aggregator_4 {
120+
use solana_program::declare_id;
121+
declare_id!("DF1ow4tspfHX9JwWJsAb9epbkA8hmpSEAtxXy1V27QBH");
122+
}

programs/drift/src/instructions/keeper.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ 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;
2728
use crate::ids::{jupiter_mainnet_3, jupiter_mainnet_4, jupiter_mainnet_6, serum_program};
2829
use crate::instructions::constraints::*;
2930
use crate::instructions::optional_accounts::get_revenue_share_escrow_account;
@@ -1119,8 +1120,7 @@ pub fn handle_settle_pnl<'c: 'info, 'info>(
11191120
&mut oracle_map,
11201121
state,
11211122
&clock,
1122-
)
1123-
.map(|_| ErrorCode::InvalidOracleForSettlePnl)?;
1123+
)?;
11241124

11251125
controller::pnl::settle_pnl(
11261126
market_index,
@@ -1134,8 +1134,7 @@ pub fn handle_settle_pnl<'c: 'info, 'info>(
11341134
state,
11351135
None,
11361136
SettlePnlMode::MustSettle,
1137-
)
1138-
.map(|_| ErrorCode::InvalidOracleForSettlePnl)?;
1137+
)?;
11391138
}
11401139

11411140
if state.builder_codes_enabled() || state.builder_referral_enabled() {
@@ -1235,8 +1234,7 @@ pub fn handle_settle_multiple_pnls<'c: 'info, 'info>(
12351234
&mut oracle_map,
12361235
state,
12371236
&clock,
1238-
)
1239-
.map(|_| ErrorCode::InvalidOracleForSettlePnl)?;
1237+
)?;
12401238

12411239
controller::pnl::settle_pnl(
12421240
*market_index,
@@ -1250,8 +1248,7 @@ pub fn handle_settle_multiple_pnls<'c: 'info, 'info>(
12501248
state,
12511249
Some(meets_margin_requirement),
12521250
mode,
1253-
)
1254-
.map(|_| ErrorCode::InvalidOracleForSettlePnl)?;
1251+
)?;
12551252
}
12561253

12571254
if state.builder_codes_enabled() || state.builder_referral_enabled() {
@@ -1737,6 +1734,7 @@ pub fn handle_liquidate_spot_with_swap_begin<'c: 'info, 'info>(
17371734
jupiter_mainnet_3::ID,
17381735
jupiter_mainnet_4::ID,
17391736
jupiter_mainnet_6::ID,
1737+
dflow_mainnet_aggregator_4::ID,
17401738
];
17411739
validate!(
17421740
whitelisted_programs.contains(&ix.program_id),

programs/drift/src/instructions/user.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::controller::spot_position::{
2222
};
2323
use crate::error::ErrorCode;
2424
use crate::ids::admin_hot_wallet;
25+
use crate::ids::dflow_mainnet_aggregator_4;
2526
use crate::ids::{
2627
jupiter_mainnet_3, jupiter_mainnet_4, jupiter_mainnet_6, lighthouse, marinade_mainnet,
2728
serum_program,
@@ -581,6 +582,12 @@ pub fn handle_change_approved_builder<'c: 'info, 'info>(
581582
max_fee_tenth_bps: u16,
582583
add: bool,
583584
) -> Result<()> {
585+
validate!(
586+
ctx.accounts.escrow.authority != builder,
587+
ErrorCode::DefaultError,
588+
"Builder cannot be the same as the escrow authority"
589+
)?;
590+
584591
let existing_builder_index = ctx
585592
.accounts
586593
.escrow
@@ -3678,6 +3685,7 @@ pub fn handle_begin_swap<'c: 'info, 'info>(
36783685
jupiter_mainnet_3::ID,
36793686
jupiter_mainnet_4::ID,
36803687
jupiter_mainnet_6::ID,
3688+
dflow_mainnet_aggregator_4::ID,
36813689
];
36823690
if !delegate_is_signer {
36833691
whitelisted_programs.push(Token::id());

programs/drift/src/math/orders.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ pub fn calculate_max_perp_order_size(
809809
)?;
810810

811811
let user_custom_margin_ratio = user.max_margin_ratio;
812+
let perp_position_margin_ratio = user.perp_positions[position_index].max_margin_ratio as u32;
812813
let user_high_leverage_mode = user.is_high_leverage_mode(MarginRequirementType::Initial);
813814

814815
let free_collateral_before = total_collateral.safe_sub(margin_requirement.cast()?)?;
@@ -838,7 +839,8 @@ pub fn calculate_max_perp_order_size(
838839
MarginRequirementType::Initial,
839840
user_high_leverage_mode,
840841
)?
841-
.max(user_custom_margin_ratio);
842+
.max(user_custom_margin_ratio)
843+
.max(perp_position_margin_ratio);
842844

843845
let mut order_size_to_reduce_position = 0_u64;
844846
let mut free_collateral_released = 0_i128;
@@ -915,7 +917,8 @@ pub fn calculate_max_perp_order_size(
915917
MarginRequirementType::Initial,
916918
user_high_leverage_mode,
917919
)?
918-
.max(user_custom_margin_ratio);
920+
.max(user_custom_margin_ratio)
921+
.max(perp_position_margin_ratio);
919922

920923
Ok((new_order_size, new_margin_ratio))
921924
};

0 commit comments

Comments
 (0)