Skip to content

Commit 420b85f

Browse files
authored
Merge pull request #1598 from drift-labs/nour/crank-aum-improvements
Nour/crank aum improvements
2 parents 3923458 + a7830f4 commit 420b85f

File tree

13 files changed

+859
-690
lines changed

13 files changed

+859
-690
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
"@project-serum/common": "0.0.1-beta.3",
1111
"@project-serum/serum": "0.13.65",
1212
"@pythnetwork/client": "2.21.0",
13-
"@solana/spl-token": "0.4.13",
13+
"@solana/spl-token": "0.3.7",
1414
"@solana/web3.js": "1.73.2",
15-
"@solana/spl-token-metadata": "0.1.6",
1615
"@types/bn.js": "5.1.6",
1716
"@types/chai": "5.0.0",
1817
"@types/mocha": "8.2.3",
@@ -31,7 +30,8 @@
3130
"dependencies": {
3231
"@ellipsis-labs/phoenix-sdk": "1.4.2",
3332
"@pythnetwork/pyth-solana-receiver": "0.8.0",
34-
"@switchboard-xyz/on-demand": "2.3.2",
33+
"@switchboard-xyz/on-demand": "2.4.1",
34+
"@switchboard-xyz/common": "3.0.14",
3535
"anchor-bankrun": "0.3.0",
3636
"chai-bn": "0.2.2",
3737
"csvtojson": "2.0.10",

programs/drift/src/instructions/admin.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4455,6 +4455,7 @@ pub fn handle_initialize_constituent<'info>(
44554455
max_weight_deviation: i64,
44564456
swap_fee_min: i64,
44574457
swap_fee_max: i64,
4458+
oracle_staleness_threshold: u64,
44584459
) -> Result<()> {
44594460
let mut constituent = ctx.accounts.constituent.load_init()?;
44604461
let mut lp_pool = ctx.accounts.lp_pool.load_mut()?;
@@ -4472,12 +4473,67 @@ pub fn handle_initialize_constituent<'info>(
44724473
constituent.max_weight_deviation = max_weight_deviation;
44734474
constituent.swap_fee_min = swap_fee_min;
44744475
constituent.swap_fee_max = swap_fee_max;
4476+
constituent.oracle_staleness_threshold = oracle_staleness_threshold;
44754477
constituent.pubkey = ctx.accounts.constituent.key();
4478+
constituent.constituent_index = (constituent_target_weights.weights.len() - 1) as u16;
44764479
lp_pool.constituents += 1;
44774480

44784481
Ok(())
44794482
}
44804483

4484+
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
4485+
pub struct ConstituentParams {
4486+
pub max_weight_deviation: Option<i64>,
4487+
pub swap_fee_min: Option<i64>,
4488+
pub swap_fee_max: Option<i64>,
4489+
pub oracle_staleness_threshold: Option<u64>,
4490+
}
4491+
4492+
pub fn handle_update_constituent_params<'info>(
4493+
ctx: Context<UpdateConstituentParams>,
4494+
constituent_params: ConstituentParams,
4495+
) -> Result<()> {
4496+
let mut constituent = ctx.accounts.constituent.load_mut()?;
4497+
if constituent_params.max_weight_deviation.is_some() {
4498+
msg!(
4499+
"max_weight_deviation: {:?} -> {:?}",
4500+
constituent.max_weight_deviation,
4501+
constituent_params.max_weight_deviation
4502+
);
4503+
constituent.max_weight_deviation = constituent_params.max_weight_deviation.unwrap();
4504+
}
4505+
4506+
if constituent_params.swap_fee_min.is_some() {
4507+
msg!(
4508+
"swap_fee_min: {:?} -> {:?}",
4509+
constituent.swap_fee_min,
4510+
constituent_params.swap_fee_min
4511+
);
4512+
constituent.swap_fee_min = constituent_params.swap_fee_min.unwrap();
4513+
}
4514+
4515+
if constituent_params.swap_fee_max.is_some() {
4516+
msg!(
4517+
"swap_fee_max: {:?} -> {:?}",
4518+
constituent.swap_fee_max,
4519+
constituent_params.swap_fee_max
4520+
);
4521+
constituent.swap_fee_max = constituent_params.swap_fee_max.unwrap();
4522+
}
4523+
4524+
if constituent_params.oracle_staleness_threshold.is_some() {
4525+
msg!(
4526+
"oracle_staleness_threshold: {:?} -> {:?}",
4527+
constituent.oracle_staleness_threshold,
4528+
constituent_params.oracle_staleness_threshold
4529+
);
4530+
constituent.oracle_staleness_threshold =
4531+
constituent_params.oracle_staleness_threshold.unwrap();
4532+
}
4533+
4534+
Ok(())
4535+
}
4536+
44814537
pub fn handle_update_amm_constituent_mapping_data<'info>(
44824538
ctx: Context<UpdateAmmConstituentMappingData>,
44834539
amm_constituent_mapping_data: Vec<AddAmmConstituentMappingDatum>,
@@ -5433,6 +5489,18 @@ pub struct InitializeConstituent<'info> {
54335489
pub token_program: Interface<'info, TokenInterface>,
54345490
}
54355491

5492+
#[derive(Accounts)]
5493+
pub struct UpdateConstituentParams<'info> {
5494+
#[account(
5495+
mut,
5496+
constraint = admin.key() == admin_hot_wallet::id() || admin.key() == state.admin
5497+
)]
5498+
pub admin: Signer<'info>,
5499+
pub state: Box<Account<'info, State>>,
5500+
#[account(mut)]
5501+
pub constituent: AccountLoader<'info, Constituent>,
5502+
}
5503+
54365504
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
54375505
pub struct AddAmmConstituentMappingDatum {
54385506
pub constituent_index: u16,

programs/drift/src/instructions/lp_pool.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ use anchor_lang::{prelude::*, Accounts, Key, Result};
33
use crate::{
44
error::ErrorCode,
55
math::{
6+
casting::Cast,
7+
constants::{
8+
PRICE_PRECISION_I128, QUOTE_PRECISION, QUOTE_PRECISION_I128, SPOT_BALANCE_PRECISION,
9+
SPOT_WEIGHT_PRECISION_I128,
10+
},
611
oracle::{is_oracle_valid_for_action, DriftAction},
712
safe_math::SafeMath,
813
},
@@ -167,13 +172,16 @@ pub fn handle_update_lp_pool_aum<'c: 'info, 'info>(
167172
spot_market.get_max_confidence_interval_multiplier()?,
168173
)?;
169174

175+
let oracle_slot = slot - oracle_data.0.delay.max(0i64).cast::<u64>()?;
170176
let oracle_price: Option<i64> = {
171177
if !is_oracle_valid_for_action(oracle_data.1, Some(DriftAction::UpdateLpPoolAum))? {
172178
msg!(
173179
"Oracle data for spot market {} is invalid. Skipping update",
174180
spot_market.market_index,
175181
);
176-
if slot - constituent.last_oracle_slot > 400 {
182+
if slot.saturating_sub(constituent.last_oracle_slot)
183+
>= constituent.oracle_staleness_threshold
184+
{
177185
None
178186
} else {
179187
Some(constituent.last_oracle_price)
@@ -184,18 +192,34 @@ pub fn handle_update_lp_pool_aum<'c: 'info, 'info>(
184192
};
185193

186194
if oracle_price.is_none() {
195+
msg!("hi");
187196
return Err(ErrorCode::OracleTooStaleForLPAUMUpdate.into());
188197
}
189198

190199
constituent.last_oracle_price = oracle_price.unwrap();
191-
constituent.last_oracle_slot = slot;
200+
constituent.last_oracle_slot = oracle_slot;
201+
202+
if oracle_slot < oldest_slot {
203+
oldest_slot = oracle_slot;
204+
}
205+
206+
let (numerator_scale, denominator_scale) = if spot_market.decimals > 6 {
207+
(10_i128.pow(spot_market.decimals - 6), 1)
208+
} else {
209+
(1, 10_i128.pow(6 - spot_market.decimals))
210+
};
192211

193212
let constituent_aum = constituent
194213
.get_full_balance(&spot_market)?
195-
.safe_mul(oracle_price.unwrap() as i128)?;
196-
aum = aum.safe_add(constituent_aum as u128)?;
214+
.safe_mul(numerator_scale)?
215+
.safe_div(denominator_scale)?
216+
.safe_mul(oracle_price.unwrap() as i128)?
217+
.safe_div(PRICE_PRECISION_I128)?
218+
.max(0);
219+
aum = aum.safe_add(constituent_aum.cast()?)?;
197220
}
198221

222+
lp_pool.oldest_oracle_slot = oldest_slot;
199223
lp_pool.last_aum = aum;
200224
lp_pool.last_aum_slot = slot;
201225
lp_pool.last_aum_ts = Clock::get()?.unix_timestamp;

programs/drift/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,7 @@ pub mod drift {
17031703
max_weight_deviation: i64,
17041704
swap_fee_min: i64,
17051705
swap_fee_max: i64,
1706+
oracle_staleness_threshold: u64,
17061707
) -> Result<()> {
17071708
handle_initialize_constituent(
17081709
ctx,
@@ -1711,9 +1712,17 @@ pub mod drift {
17111712
max_weight_deviation,
17121713
swap_fee_min,
17131714
swap_fee_max,
1715+
oracle_staleness_threshold,
17141716
)
17151717
}
17161718

1719+
pub fn update_constituent_params(
1720+
ctx: Context<UpdateConstituentParams>,
1721+
constituent_params: ConstituentParams,
1722+
) -> Result<()> {
1723+
handle_update_constituent_params(ctx, constituent_params)
1724+
}
1725+
17171726
pub fn add_amm_constituent_mapping_data(
17181727
ctx: Context<AddAmmConstituentMappingData>,
17191728
lp_pool_name: [u8; 32],

programs/drift/src/state/constituent_map.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use anchor_lang::Discriminator;
1010
use arrayref::array_ref;
1111

1212
use crate::error::{DriftResult, ErrorCode};
13-
use crate::state::user::PerpPositions;
1413

1514
use crate::math::safe_unwrap::SafeUnwrap;
1615
use crate::msg;
@@ -96,6 +95,10 @@ impl<'a> ConstituentMap<'a> {
9695

9796
let constituent_discriminator: [u8; 8] = Constituent::discriminator();
9897
while let Some(account_info) = account_info_iter.peek() {
98+
if account_info.owner != &crate::ID {
99+
break;
100+
}
101+
99102
let data = account_info
100103
.try_borrow_data()
101104
.or(Err(ErrorCode::ConstituentCouldNotLoad))?;
@@ -122,7 +125,6 @@ impl<'a> ConstituentMap<'a> {
122125

123126
// constituent index 42 bytes from front of account
124127
let constituent_index = u16::from_le_bytes(*array_ref![data, 42, 2]);
125-
126128
if constituent_map.0.contains_key(&constituent_index) {
127129
msg!(
128130
"Can not include same constituent index twice {}",

programs/drift/src/state/lp_pool.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ impl LPPool {
202202
pub struct BLPosition {
203203
/// The scaled balance of the position. To get the token amount, multiply by the cumulative deposit/borrow
204204
/// interest of corresponding market.
205-
/// precision: SPOT_BALANCE_PRECISION
206-
pub scaled_balance: u64,
205+
/// precision: token precision
206+
pub scaled_balance: u128,
207207
/// The cumulative deposits/borrows a user has made into a market
208208
/// precision: token mint precision
209209
pub cumulative_deposits: i64,
@@ -228,12 +228,12 @@ impl SpotBalance for BLPosition {
228228
}
229229

230230
fn increase_balance(&mut self, delta: u128) -> DriftResult {
231-
self.scaled_balance = self.scaled_balance.safe_add(delta.cast()?)?;
231+
self.scaled_balance = self.scaled_balance.safe_add(delta)?;
232232
Ok(())
233233
}
234234

235235
fn decrease_balance(&mut self, delta: u128) -> DriftResult {
236-
self.scaled_balance = self.scaled_balance.safe_sub(delta.cast()?)?;
236+
self.scaled_balance = self.scaled_balance.safe_sub(delta)?;
237237
Ok(())
238238
}
239239

@@ -245,7 +245,7 @@ impl SpotBalance for BLPosition {
245245

246246
impl BLPosition {
247247
pub fn get_token_amount(&self, spot_market: &SpotMarket) -> DriftResult<u128> {
248-
get_token_amount(self.scaled_balance.cast()?, spot_market, &self.balance_type)
248+
get_token_amount(self.scaled_balance, spot_market, &self.balance_type)
249249
}
250250
}
251251

@@ -273,18 +273,21 @@ pub struct Constituent {
273273
/// precision: PERCENTAGE_PRECISION
274274
pub swap_fee_max: i64,
275275

276-
/// ata token balance in SPOT_BALANCE_PRECISION
277-
pub token_balance: u64,
276+
/// ata token balance in token precision
277+
pub token_balance: u128,
278278

279279
/// spot borrow-lend balance for constituent
280280
pub spot_balance: BLPosition, // should be in constituent base asset
281281

282282
pub last_oracle_price: i64,
283283
pub last_oracle_slot: u64,
284+
285+
pub oracle_staleness_threshold: u64,
286+
_padding2: [u8; 8],
284287
}
285288

286289
impl Size for Constituent {
287-
const SIZE: usize = 120;
290+
const SIZE: usize = 152;
288291
}
289292

290293
impl Constituent {

sdk/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@drift-labs/sdk",
3-
"version": "2.118.0-beta.4",
3+
"version": "2.121.0-beta.5",
44
"main": "lib/node/index.js",
55
"types": "lib/node/index.d.ts",
66
"browser": "./lib/browser/index.js",
@@ -47,7 +47,8 @@
4747
"@pythnetwork/pyth-solana-receiver": "0.7.0",
4848
"@solana/spl-token": "0.3.7",
4949
"@solana/web3.js": "1.92.3",
50-
"@switchboard-xyz/on-demand": "2.4.0",
50+
"@switchboard-xyz/common": "3.0.14",
51+
"@switchboard-xyz/on-demand": "2.4.1",
5152
"@triton-one/yellowstone-grpc": "1.3.0",
5253
"anchor-bankrun": "0.3.0",
5354
"nanoid": "3.3.4",

0 commit comments

Comments
 (0)