Skip to content

Commit 340b6c9

Browse files
committed
stable target base liquidity fix + cleanup
1 parent c768376 commit 340b6c9

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

.devcontainer/Dockerfile

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ ENV HOME="/root"
1313
ENV PATH="/usr/local/cargo/bin:${PATH}"
1414
ENV PATH="/root/.local/share/solana/install/active_release/bin:${PATH}"
1515

16-
RUN mkdir -p /workdir /tmp && \
17-
apt-get update -qq && apt-get upgrade -qq && apt-get install -y --no-install-recommends \
18-
build-essential git curl wget jq pkg-config python3-pip xz-utils ca-certificates \
19-
libssl-dev libudev-dev bash && \
20-
rm -rf /var/lib/apt/lists/*
21-
22-
RUN apt-get install -y software-properties-common \
16+
RUN mkdir -p /workdir /tmp \
17+
&& apt-get update -qq \
18+
&& apt-get upgrade -qq \
19+
&& 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 \
2322
&& add-apt-repository 'deb http://deb.debian.org/debian bookworm main' \
24-
&& apt-get update && apt-get install -y libc6 libc6-dev
23+
&& apt-get update -qq \
24+
&& apt-get install -y libc6 libc6-dev \
25+
&& rm -rf /var/lib/apt/lists/*
2526

2627
RUN rustup component add rustfmt
2728

programs/drift/src/instructions/lp_pool.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,7 @@ pub fn handle_update_lp_pool_aum<'c: 'info, 'info>(
223223

224224
// Set USDC stable weight
225225
msg!("aum: {}", aum);
226-
let total_stable_target_base = aum
227-
.cast::<i128>()?
228-
.safe_sub(crypto_delta.abs())?
229-
.max(0_i128);
226+
let total_stable_target_base = aum.cast::<i128>()?.safe_sub(crypto_delta)?;
230227
constituent_target_base
231228
.get_mut(lp_pool.quote_consituent_index as u32)
232229
.target_base = total_stable_target_base.cast::<i64>()?;
@@ -861,7 +858,6 @@ pub fn handle_view_lp_pool_add_liquidity_fees<'c: 'info, 'info>(
861858
in_amount: u128,
862859
) -> Result<()> {
863860
let slot = Clock::get()?.slot;
864-
let now = Clock::get()?.unix_timestamp;
865861
let state = &ctx.accounts.state;
866862
let lp_pool = ctx.accounts.lp_pool.load()?;
867863

@@ -1043,7 +1039,7 @@ pub fn handle_lp_pool_remove_liquidity<'c: 'info, 'info>(
10431039
out_spot_market.get_max_confidence_interval_multiplier()?,
10441040
0,
10451041
)?;
1046-
let out_oracle = out_oracle.clone();
1042+
let out_oracle = *out_oracle;
10471043

10481044
// TODO: check self.aum validity
10491045

@@ -1247,7 +1243,6 @@ pub fn handle_view_lp_pool_remove_liquidity_fees<'c: 'info, 'info>(
12471243
lp_to_burn: u64,
12481244
) -> Result<()> {
12491245
let slot = Clock::get()?.slot;
1250-
let now = Clock::get()?.unix_timestamp;
12511246
let state = &ctx.accounts.state;
12521247
let lp_pool = ctx.accounts.lp_pool.load()?;
12531248

@@ -1520,7 +1515,7 @@ pub fn handle_withdraw_from_program_vault<'c: 'info, 'info>(
15201515

15211516
controller::spot_balance::update_spot_market_cumulative_interest(
15221517
&mut spot_market,
1523-
Some(&oracle_data),
1518+
Some(oracle_data),
15241519
clock.unix_timestamp,
15251520
)?;
15261521
let token_balance_after_cumulative_interest_update = constituent

programs/drift/src/math/lp_pool.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub mod perp_lp_pool_settlement {
1010
state::{amm_cache::CacheInfo, perp_market::PerpMarket, spot_market::SpotMarket},
1111
*,
1212
};
13-
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};
13+
use anchor_spl::token_interface::{TokenAccount, TokenInterface};
1414

1515
#[derive(Debug, Clone, Copy)]
1616
pub struct SettlementResult {
@@ -55,7 +55,7 @@ pub mod perp_lp_pool_settlement {
5555
ctx: &SettlementContext,
5656
result: &SettlementResult,
5757
) -> Result<()> {
58-
if result.amount_transferred > ctx.max_settle_quote_amount as u64 {
58+
if result.amount_transferred > ctx.max_settle_quote_amount {
5959
msg!(
6060
"Amount to settle exceeds maximum allowed, {} > {}",
6161
result.amount_transferred,
@@ -94,7 +94,8 @@ pub mod perp_lp_pool_settlement {
9494
}
9595

9696
fn calculate_perp_to_lp_settlement(ctx: &SettlementContext) -> Result<SettlementResult> {
97-
let amount_to_send = (ctx.quote_owed_from_lp.abs() as u64).min(ctx.max_settle_quote_amount);
97+
let amount_to_send =
98+
(ctx.quote_owed_from_lp.abs().cast::<u64>()?).min(ctx.max_settle_quote_amount);
9899

99100
if ctx.fee_pool_balance >= amount_to_send as u128 {
100101
// Fee pool can cover entire amount

programs/drift/src/state/lp_pool.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl LPPool {
220220

221221
let out_fee_amount = out_amount
222222
.cast::<i128>()?
223-
.safe_mul(out_fee as i128)?
223+
.safe_mul(out_fee)?
224224
.safe_div(PERCENTAGE_PRECISION_I128)?;
225225

226226
Ok((in_amount, out_amount, in_fee_amount, out_fee_amount))
@@ -472,7 +472,7 @@ impl LPPool {
472472
let out_spot_market = out_spot_market.unwrap();
473473
let out_oracle_price = out_oracle_price.unwrap();
474474
let out_amount = notional_trade_size
475-
.safe_mul(10_i128.pow(out_spot_market.decimals as u32))?
475+
.safe_mul(10_i128.pow(out_spot_market.decimals))?
476476
.safe_div(out_oracle_price.cast::<i128>()?)?;
477477
(
478478
false,
@@ -693,10 +693,10 @@ impl LPPool {
693693

694694
if total_quote_owed > 0 {
695695
aum = aum
696-
.saturating_sub(total_quote_owed as i128)
696+
.saturating_sub(total_quote_owed)
697697
.max(QUOTE_PRECISION_I128);
698698
} else if total_quote_owed < 0 {
699-
aum = aum.saturating_add((-total_quote_owed) as i128);
699+
aum = aum.saturating_add(-total_quote_owed);
700700
}
701701

702702
let aum_u128 = aum.max(0).cast::<u128>()?;
@@ -739,7 +739,7 @@ impl SpotBalance for ConstituentSpotBalance {
739739
}
740740

741741
fn balance(&self) -> u128 {
742-
self.scaled_balance as u128
742+
self.scaled_balance
743743
}
744744

745745
fn increase_balance(&mut self, delta: u128) -> DriftResult {
@@ -883,15 +883,15 @@ impl Constituent {
883883
self.pubkey,
884884
self.spot_market_index
885885
);
886-
return Err(ErrorCode::InvalidConstituentOperation.into());
886+
Err(ErrorCode::InvalidConstituentOperation)
887887
} else if ConstituentLpOperation::is_operation_paused(self.paused_operations, operation) {
888888
msg!(
889889
"Constituent {:?}, spot market {}, is paused for operation {:?}",
890890
self.pubkey,
891891
self.spot_market_index,
892892
operation
893893
);
894-
return Err(ErrorCode::InvalidConstituentOperation.into());
894+
Err(ErrorCode::InvalidConstituentOperation)
895895
} else {
896896
Ok(())
897897
}
@@ -1175,7 +1175,7 @@ impl<'a> AccountZeroCopy<'a, TargetsDatum, ConstituentTargetBaseFixed> {
11751175

11761176
// TODO: validate spot market
11771177
let datum = self.get(constituent_index as u32);
1178-
let target_weight = calculate_target_weight(datum.target_base, &spot_market, price, aum)?;
1178+
let target_weight = calculate_target_weight(datum.target_base, spot_market, price, aum)?;
11791179
Ok(target_weight)
11801180
}
11811181
}
@@ -1197,7 +1197,7 @@ pub fn calculate_target_weight(
11971197
.safe_mul(PERCENTAGE_PRECISION_I128)?
11981198
.safe_div(lp_pool_aum.cast::<i128>()?)?
11991199
.cast::<i64>()?
1200-
.clamp(-1 * PERCENTAGE_PRECISION_I64, PERCENTAGE_PRECISION_I64);
1200+
.clamp(-PERCENTAGE_PRECISION_I64, PERCENTAGE_PRECISION_I64);
12011201

12021202
Ok(target_weight)
12031203
}
@@ -1268,11 +1268,10 @@ impl<'a> AccountZeroCopyMut<'a, TargetsDatum, ConstituentTargetBaseFixed> {
12681268
mapping_index = j;
12691269

12701270
let cell = self.get_mut(i as u32);
1271-
let target_base = target_notional
1271+
let target_base = -target_notional
12721272
.safe_div(PERCENTAGE_PRECISION_I128)?
12731273
.safe_mul(10_i128.pow(decimals as u32))?
1274-
.safe_div(price as i128)?
1275-
* -1; // Want to target opposite sign of total scaled notional inventory
1274+
.safe_div(price as i128)?; // Want to target opposite sign of total scaled notional inventory
12761275

12771276
msg!(
12781277
"updating constituent index {} target base to {} from aggregated perp notional {}",
@@ -1295,7 +1294,7 @@ impl<'a> AccountZeroCopyMut<'a, AmmConstituentDatum, AmmConstituentMappingFixed>
12951294

12961295
let mut open_slot_index: Option<u32> = None;
12971296
for i in 0..len {
1298-
let cell = self.get(i as u32);
1297+
let cell = self.get(i);
12991298
if cell.constituent_index == datum.constituent_index
13001299
&& cell.perp_market_index == datum.perp_market_index
13011300
{
@@ -1305,7 +1304,7 @@ impl<'a> AccountZeroCopyMut<'a, AmmConstituentDatum, AmmConstituentMappingFixed>
13051304
open_slot_index = Some(i);
13061305
}
13071306
}
1308-
let open_slot = open_slot_index.ok_or_else(|| ErrorCode::DefaultError.into())?;
1307+
let open_slot = open_slot_index.ok_or(ErrorCode::DefaultError)?;
13091308

13101309
let cell = self.get_mut(open_slot);
13111310
*cell = datum;
@@ -1319,11 +1318,11 @@ impl<'a> AccountZeroCopyMut<'a, AmmConstituentDatum, AmmConstituentMappingFixed>
13191318
let len = self.len();
13201319
let mut data: Vec<AmmConstituentDatum> = Vec::with_capacity(len as usize);
13211320
for i in 0..len {
1322-
data.push(*self.get(i as u32));
1321+
data.push(*self.get(i));
13231322
}
13241323
data.sort_by_key(|datum| datum.constituent_index);
13251324
for i in 0..len {
1326-
let cell = self.get_mut(i as u32);
1325+
let cell = self.get_mut(i);
13271326
*cell = data[i as usize];
13281327
}
13291328
Ok(())
@@ -1482,8 +1481,8 @@ impl ConstituentCorrelations {
14821481
"ConstituentCorrelation correlations must be between 0 and PERCENTAGE_PRECISION"
14831482
)?;
14841483

1485-
self.correlations[(i as usize * num_constituents + j as usize) as usize] = corr;
1486-
self.correlations[(j as usize * num_constituents + i as usize) as usize] = corr;
1484+
self.correlations[(i as usize * num_constituents + j as usize)] = corr;
1485+
self.correlations[(j as usize * num_constituents + i as usize)] = corr;
14871486

14881487
self.validate()?;
14891488

@@ -1568,7 +1567,7 @@ pub fn update_constituent_target_base_for_derivatives(
15681567
constituent_target_base: &mut AccountZeroCopyMut<'_, TargetsDatum, ConstituentTargetBaseFixed>,
15691568
) -> DriftResult<()> {
15701569
for (parent_index, constituent_indexes) in derivative_groups.iter() {
1571-
let parent_constituent = constituent_map.get_ref(&(parent_index))?;
1570+
let parent_constituent = constituent_map.get_ref(parent_index)?;
15721571
let parent_target_base = constituent_target_base
15731572
.get(*parent_index as u32)
15741573
.target_base;

0 commit comments

Comments
 (0)