Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
"@project-serum/common": "0.0.1-beta.3",
"@project-serum/serum": "0.13.65",
"@pythnetwork/client": "2.21.0",
"@solana/spl-token": "0.4.13",
"@solana/spl-token": "0.3.7",
"@solana/web3.js": "1.73.2",
"@solana/spl-token-metadata": "0.1.6",
"@types/bn.js": "5.1.6",
"@types/chai": "5.0.0",
"@types/mocha": "8.2.3",
Expand All @@ -31,7 +30,8 @@
"dependencies": {
"@ellipsis-labs/phoenix-sdk": "1.4.2",
"@pythnetwork/pyth-solana-receiver": "0.8.0",
"@switchboard-xyz/on-demand": "2.3.2",
"@switchboard-xyz/on-demand": "2.4.1",
"@switchboard-xyz/common": "3.0.14",
"anchor-bankrun": "0.3.0",
"chai-bn": "0.2.2",
"csvtojson": "2.0.10",
Expand Down
68 changes: 68 additions & 0 deletions programs/drift/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4455,6 +4455,7 @@ pub fn handle_initialize_constituent<'info>(
max_weight_deviation: i64,
swap_fee_min: i64,
swap_fee_max: i64,
oracle_staleness_threshold: u64,
) -> Result<()> {
let mut constituent = ctx.accounts.constituent.load_init()?;
let mut lp_pool = ctx.accounts.lp_pool.load_mut()?;
Expand All @@ -4472,12 +4473,67 @@ pub fn handle_initialize_constituent<'info>(
constituent.max_weight_deviation = max_weight_deviation;
constituent.swap_fee_min = swap_fee_min;
constituent.swap_fee_max = swap_fee_max;
constituent.oracle_staleness_threshold = oracle_staleness_threshold;
constituent.pubkey = ctx.accounts.constituent.key();
constituent.constituent_index = (constituent_target_weights.weights.len() - 1) as u16;
lp_pool.constituents += 1;

Ok(())
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
pub struct ConstituentParams {
pub max_weight_deviation: Option<i64>,
pub swap_fee_min: Option<i64>,
pub swap_fee_max: Option<i64>,
pub oracle_staleness_threshold: Option<u64>,
}

pub fn handle_update_constituent_params<'info>(
ctx: Context<UpdateConstituentParams>,
constituent_params: ConstituentParams,
) -> Result<()> {
let mut constituent = ctx.accounts.constituent.load_mut()?;
if constituent_params.max_weight_deviation.is_some() {
msg!(
"max_weight_deviation: {:?} -> {:?}",
constituent.max_weight_deviation,
constituent_params.max_weight_deviation
);
constituent.max_weight_deviation = constituent_params.max_weight_deviation.unwrap();
}

if constituent_params.swap_fee_min.is_some() {
msg!(
"swap_fee_min: {:?} -> {:?}",
constituent.swap_fee_min,
constituent_params.swap_fee_min
);
constituent.swap_fee_min = constituent_params.swap_fee_min.unwrap();
}

if constituent_params.swap_fee_max.is_some() {
msg!(
"swap_fee_max: {:?} -> {:?}",
constituent.swap_fee_max,
constituent_params.swap_fee_max
);
constituent.swap_fee_max = constituent_params.swap_fee_max.unwrap();
}

if constituent_params.oracle_staleness_threshold.is_some() {
msg!(
"oracle_staleness_threshold: {:?} -> {:?}",
constituent.oracle_staleness_threshold,
constituent_params.oracle_staleness_threshold
);
constituent.oracle_staleness_threshold =
constituent_params.oracle_staleness_threshold.unwrap();
}

Ok(())
}

pub fn handle_update_amm_constituent_mapping_data<'info>(
ctx: Context<UpdateAmmConstituentMappingData>,
amm_constituent_mapping_data: Vec<AddAmmConstituentMappingDatum>,
Expand Down Expand Up @@ -5433,6 +5489,18 @@ pub struct InitializeConstituent<'info> {
pub token_program: Interface<'info, TokenInterface>,
}

#[derive(Accounts)]
pub struct UpdateConstituentParams<'info> {
#[account(
mut,
constraint = admin.key() == admin_hot_wallet::id() || admin.key() == state.admin
)]
pub admin: Signer<'info>,
pub state: Box<Account<'info, State>>,
#[account(mut)]
pub constituent: AccountLoader<'info, Constituent>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
pub struct AddAmmConstituentMappingDatum {
pub constituent_index: u16,
Expand Down
32 changes: 28 additions & 4 deletions programs/drift/src/instructions/lp_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use anchor_lang::{prelude::*, Accounts, Key, Result};
use crate::{
error::ErrorCode,
math::{
casting::Cast,
constants::{
PRICE_PRECISION_I128, QUOTE_PRECISION, QUOTE_PRECISION_I128, SPOT_BALANCE_PRECISION,
SPOT_WEIGHT_PRECISION_I128,
},
oracle::{is_oracle_valid_for_action, DriftAction},
safe_math::SafeMath,
},
Expand Down Expand Up @@ -167,13 +172,16 @@ pub fn handle_update_lp_pool_aum<'c: 'info, 'info>(
spot_market.get_max_confidence_interval_multiplier()?,
)?;

let oracle_slot = slot - oracle_data.0.delay.max(0i64).cast::<u64>()?;
let oracle_price: Option<i64> = {
if !is_oracle_valid_for_action(oracle_data.1, Some(DriftAction::UpdateLpPoolAum))? {
msg!(
"Oracle data for spot market {} is invalid. Skipping update",
spot_market.market_index,
);
if slot - constituent.last_oracle_slot > 400 {
if slot.saturating_sub(constituent.last_oracle_slot)
>= constituent.oracle_staleness_threshold
{
None
} else {
Some(constituent.last_oracle_price)
Expand All @@ -184,18 +192,34 @@ pub fn handle_update_lp_pool_aum<'c: 'info, 'info>(
};

if oracle_price.is_none() {
msg!("hi");
return Err(ErrorCode::OracleTooStaleForLPAUMUpdate.into());
}

constituent.last_oracle_price = oracle_price.unwrap();
constituent.last_oracle_slot = slot;
constituent.last_oracle_slot = oracle_slot;

if oracle_slot < oldest_slot {
oldest_slot = oracle_slot;
}

let (numerator_scale, denominator_scale) = if spot_market.decimals > 6 {
(10_i128.pow(spot_market.decimals - 6), 1)
} else {
(1, 10_i128.pow(6 - spot_market.decimals))
};

let constituent_aum = constituent
.get_full_balance(&spot_market)?
.safe_mul(oracle_price.unwrap() as i128)?;
aum = aum.safe_add(constituent_aum as u128)?;
.safe_mul(numerator_scale)?
.safe_div(denominator_scale)?
.safe_mul(oracle_price.unwrap() as i128)?
.safe_div(PRICE_PRECISION_I128)?
.max(0);
aum = aum.safe_add(constituent_aum.cast()?)?;
}

lp_pool.oldest_oracle_slot = oldest_slot;
lp_pool.last_aum = aum;
lp_pool.last_aum_slot = slot;
lp_pool.last_aum_ts = Clock::get()?.unix_timestamp;
Expand Down
9 changes: 9 additions & 0 deletions programs/drift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,7 @@ pub mod drift {
max_weight_deviation: i64,
swap_fee_min: i64,
swap_fee_max: i64,
oracle_staleness_threshold: u64,
) -> Result<()> {
handle_initialize_constituent(
ctx,
Expand All @@ -1711,9 +1712,17 @@ pub mod drift {
max_weight_deviation,
swap_fee_min,
swap_fee_max,
oracle_staleness_threshold,
)
}

pub fn update_constituent_params(
ctx: Context<UpdateConstituentParams>,
constituent_params: ConstituentParams,
) -> Result<()> {
handle_update_constituent_params(ctx, constituent_params)
}

pub fn add_amm_constituent_mapping_data(
ctx: Context<AddAmmConstituentMappingData>,
lp_pool_name: [u8; 32],
Expand Down
6 changes: 4 additions & 2 deletions programs/drift/src/state/constituent_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use anchor_lang::Discriminator;
use arrayref::array_ref;

use crate::error::{DriftResult, ErrorCode};
use crate::state::user::PerpPositions;

use crate::math::safe_unwrap::SafeUnwrap;
use crate::msg;
Expand Down Expand Up @@ -96,6 +95,10 @@ impl<'a> ConstituentMap<'a> {

let constituent_discriminator: [u8; 8] = Constituent::discriminator();
while let Some(account_info) = account_info_iter.peek() {
if account_info.owner != &crate::ID {
break;
}

let data = account_info
.try_borrow_data()
.or(Err(ErrorCode::ConstituentCouldNotLoad))?;
Expand All @@ -122,7 +125,6 @@ impl<'a> ConstituentMap<'a> {

// constituent index 42 bytes from front of account
let constituent_index = u16::from_le_bytes(*array_ref![data, 42, 2]);

if constituent_map.0.contains_key(&constituent_index) {
msg!(
"Can not include same constituent index twice {}",
Expand Down
19 changes: 11 additions & 8 deletions programs/drift/src/state/lp_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ impl LPPool {
pub struct BLPosition {
/// The scaled balance of the position. To get the token amount, multiply by the cumulative deposit/borrow
/// interest of corresponding market.
/// precision: SPOT_BALANCE_PRECISION
pub scaled_balance: u64,
/// precision: token precision
pub scaled_balance: u128,
/// The cumulative deposits/borrows a user has made into a market
/// precision: token mint precision
pub cumulative_deposits: i64,
Expand All @@ -228,12 +228,12 @@ impl SpotBalance for BLPosition {
}

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

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

Expand All @@ -245,7 +245,7 @@ impl SpotBalance for BLPosition {

impl BLPosition {
pub fn get_token_amount(&self, spot_market: &SpotMarket) -> DriftResult<u128> {
get_token_amount(self.scaled_balance.cast()?, spot_market, &self.balance_type)
get_token_amount(self.scaled_balance, spot_market, &self.balance_type)
}
}

Expand Down Expand Up @@ -273,18 +273,21 @@ pub struct Constituent {
/// precision: PERCENTAGE_PRECISION
pub swap_fee_max: i64,

/// ata token balance in SPOT_BALANCE_PRECISION
pub token_balance: u64,
/// ata token balance in token precision
pub token_balance: u128,

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

pub last_oracle_price: i64,
pub last_oracle_slot: u64,

pub oracle_staleness_threshold: u64,
_padding2: [u8; 8],
}

impl Size for Constituent {
const SIZE: usize = 120;
const SIZE: usize = 152;
}

impl Constituent {
Expand Down
5 changes: 3 additions & 2 deletions sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@drift-labs/sdk",
"version": "2.118.0-beta.4",
"version": "2.121.0-beta.5",
"main": "lib/node/index.js",
"types": "lib/node/index.d.ts",
"browser": "./lib/browser/index.js",
Expand Down Expand Up @@ -47,7 +47,8 @@
"@pythnetwork/pyth-solana-receiver": "0.7.0",
"@solana/spl-token": "0.3.7",
"@solana/web3.js": "1.92.3",
"@switchboard-xyz/on-demand": "2.4.0",
"@switchboard-xyz/common": "3.0.14",
"@switchboard-xyz/on-demand": "2.4.1",
"@triton-one/yellowstone-grpc": "1.3.0",
"anchor-bankrun": "0.3.0",
"nanoid": "3.3.4",
Expand Down
Loading