Skip to content

Commit 4baaa3b

Browse files
committed
merge dlp
2 parents b78695b + 00139c4 commit 4baaa3b

File tree

17 files changed

+338
-266
lines changed

17 files changed

+338
-266
lines changed

programs/drift/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ pub enum ErrorCode {
692692
Unauthorized,
693693
#[msg("Invalid Isolated Perp Market")]
694694
InvalidIsolatedPerpMarket,
695+
#[msg("Invalid Lp Pool Id for Operation")]
696+
InvalidLpPoolId,
695697
}
696698

697699
#[macro_export]

programs/drift/src/instructions/admin.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ pub fn handle_initialize_perp_market(
714714
curve_update_intensity: u8,
715715
amm_jit_intensity: u8,
716716
name: [u8; 32],
717+
lp_pool_id: u8,
717718
) -> Result<()> {
718719
msg!("perp market {}", market_index);
719720
let perp_market_pubkey = ctx.accounts.perp_market.to_account_info().key;
@@ -1001,7 +1002,8 @@ pub fn handle_initialize_perp_market(
10011002
lp_exchange_fee_excluscion_scalar: 0,
10021003
lp_paused_operations: 0,
10031004
last_fill_price: 0,
1004-
padding: [0; 24],
1005+
lp_pool_id,
1006+
padding: [0; 23],
10051007
amm: AMM {
10061008
oracle: *ctx.accounts.oracle.key,
10071009
oracle_source,
@@ -2829,6 +2831,25 @@ pub fn handle_update_perp_liquidation_fee(
28292831
Ok(())
28302832
}
28312833

2834+
#[access_control(
2835+
perp_market_valid(&ctx.accounts.perp_market)
2836+
)]
2837+
pub fn handle_update_perp_lp_pool_id(
2838+
ctx: Context<AdminUpdatePerpMarket>,
2839+
lp_pool_id: u8,
2840+
) -> Result<()> {
2841+
let perp_market = &mut load_mut!(ctx.accounts.perp_market)?;
2842+
2843+
msg!(
2844+
"updating perp market {} lp pool id: {} -> {}",
2845+
perp_market.market_index,
2846+
perp_market.lp_pool_id,
2847+
lp_pool_id
2848+
);
2849+
perp_market.lp_pool_id = lp_pool_id;
2850+
Ok(())
2851+
}
2852+
28322853
#[access_control(
28332854
spot_market_valid(&ctx.accounts.spot_market)
28342855
)]

programs/drift/src/instructions/keeper.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,6 +3409,16 @@ pub fn handle_settle_perp_to_lp_pool<'c: 'info, 'info>(
34093409

34103410
for (_, perp_market_loader) in perp_market_map.0.iter() {
34113411
let mut perp_market = perp_market_loader.load_mut()?;
3412+
if lp_pool.lp_pool_id != perp_market.lp_pool_id {
3413+
msg!(
3414+
"Perp market {} does not have the same lp pool id as the lp pool being settled to: {} != {}",
3415+
perp_market.market_index,
3416+
perp_market.lp_pool_id,
3417+
lp_pool.lp_pool_id
3418+
);
3419+
return Err(ErrorCode::InvalidLpPoolId.into());
3420+
}
3421+
34123422
if perp_market.lp_status == 0
34133423
|| PerpLpOperation::is_operation_paused(
34143424
perp_market.lp_paused_operations,

programs/drift/src/instructions/lp_admin.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use super::optional_accounts::get_token_interface;
3636

3737
pub fn handle_initialize_lp_pool(
3838
ctx: Context<InitializeLpPool>,
39-
name: [u8; 32],
39+
lp_pool_id: u8,
4040
min_mint_fee: i64,
4141
max_aum: u128,
4242
max_settle_quote_amount_per_market: u64,
@@ -59,7 +59,6 @@ pub fn handle_initialize_lp_pool(
5959
)?;
6060

6161
*lp_pool = LPPool {
62-
name,
6362
pubkey: ctx.accounts.lp_pool.key(),
6463
mint: mint.key(),
6564
constituent_target_base: ctx.accounts.constituent_target_base.key(),
@@ -84,7 +83,8 @@ pub fn handle_initialize_lp_pool(
8483
xi: 2,
8584
target_oracle_delay_fee_bps_per_10_slots: 0,
8685
target_position_delay_fee_bps_per_10_slots: 0,
87-
padding: [0u8; 15],
86+
lp_pool_id,
87+
padding: [0u8; 14],
8888
whitelist_mint,
8989
};
9090

@@ -598,11 +598,14 @@ pub fn handle_begin_lp_swap<'c: 'info, 'info>(
598598
// Check admin
599599
let admin = &ctx.accounts.admin;
600600
#[cfg(feature = "anchor-test")]
601-
validate!(
602-
admin.key() == admin_hot_wallet::id() || admin.key() == ctx.accounts.state.key(),
603-
ErrorCode::Unauthorized,
604-
"Wrong signer for lp taker swap"
605-
)?;
601+
{
602+
let state = &ctx.accounts.state;
603+
validate!(
604+
admin.key() == admin_hot_wallet::id() || admin.key() == state.admin,
605+
ErrorCode::Unauthorized,
606+
"Wrong signer for lp taker swap"
607+
)?;
608+
}
606609
#[cfg(not(feature = "anchor-test"))]
607610
validate!(
608611
admin.key() == lp_pool_swap_wallet::id(),
@@ -1008,14 +1011,14 @@ pub fn handle_override_amm_cache_info<'c: 'info, 'info>(
10081011

10091012
#[derive(Accounts)]
10101013
#[instruction(
1011-
name: [u8; 32],
1014+
id: u8,
10121015
)]
10131016
pub struct InitializeLpPool<'info> {
10141017
#[account(mut)]
10151018
pub admin: Signer<'info>,
10161019
#[account(
10171020
init,
1018-
seeds = [b"lp_pool", name.as_ref()],
1021+
seeds = [b"lp_pool", id.to_le_bytes().as_ref()],
10191022
space = LPPool::SIZE,
10201023
bump,
10211024
payer = admin

programs/drift/src/instructions/lp_pool.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,10 @@ pub fn handle_lp_pool_add_liquidity<'c: 'info, 'info>(
775775
in_constituent.record_swap_fees(in_fee_amount)?;
776776
lp_pool.record_mint_redeem_fees(lp_fee_amount)?;
777777

778-
let lp_name = lp_pool.name;
778+
let lp_pool_id = lp_pool.lp_pool_id;
779779
let lp_bump = lp_pool.bump;
780780

781-
let lp_vault_signer_seeds = LPPool::get_lp_pool_signer_seeds(&lp_name, &lp_bump);
781+
let lp_vault_signer_seeds = LPPool::get_lp_pool_signer_seeds(&lp_pool_id, &lp_bump);
782782

783783
drop(lp_pool);
784784

@@ -1173,10 +1173,10 @@ pub fn handle_lp_pool_remove_liquidity<'c: 'info, 'info>(
11731173
out_constituent.record_swap_fees(out_fee_amount)?;
11741174
lp_pool.record_mint_redeem_fees(lp_fee_amount)?;
11751175

1176-
let lp_name = lp_pool.name;
1176+
let lp_pool_id = lp_pool.lp_pool_id;
11771177
let lp_bump = lp_pool.bump;
11781178

1179-
let lp_vault_signer_seeds = LPPool::get_lp_pool_signer_seeds(&lp_name, &lp_bump);
1179+
let lp_vault_signer_seeds = LPPool::get_lp_pool_signer_seeds(&lp_pool_id, &lp_bump);
11801180

11811181
drop(lp_pool);
11821182

programs/drift/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,7 @@ pub mod drift {
10231023
curve_update_intensity: u8,
10241024
amm_jit_intensity: u8,
10251025
name: [u8; 32],
1026+
lp_pool_id: u8,
10261027
) -> Result<()> {
10271028
handle_initialize_perp_market(
10281029
ctx,
@@ -1051,6 +1052,7 @@ pub mod drift {
10511052
curve_update_intensity,
10521053
amm_jit_intensity,
10531054
name,
1055+
lp_pool_id,
10541056
)
10551057
}
10561058

@@ -1242,6 +1244,13 @@ pub mod drift {
12421244
handle_update_perp_liquidation_fee(ctx, liquidator_fee, if_liquidation_fee)
12431245
}
12441246

1247+
pub fn update_perp_market_lp_pool_id(
1248+
ctx: Context<AdminUpdatePerpMarket>,
1249+
lp_pool_id: u8,
1250+
) -> Result<()> {
1251+
handle_update_perp_lp_pool_id(ctx, lp_pool_id)
1252+
}
1253+
12451254
pub fn update_insurance_fund_unstaking_period(
12461255
ctx: Context<AdminUpdateSpotMarket>,
12471256
insurance_fund_unstaking_period: i64,
@@ -1837,15 +1846,15 @@ pub mod drift {
18371846

18381847
pub fn initialize_lp_pool(
18391848
ctx: Context<InitializeLpPool>,
1840-
name: [u8; 32],
1849+
lp_pool_id: u8,
18411850
min_mint_fee: i64,
18421851
max_aum: u128,
18431852
max_settle_quote_amount_per_market: u64,
18441853
whitelist_mint: Pubkey,
18451854
) -> Result<()> {
18461855
handle_initialize_lp_pool(
18471856
ctx,
1848-
name,
1857+
lp_pool_id,
18491858
min_mint_fee,
18501859
max_aum,
18511860
max_settle_quote_amount_per_market,

programs/drift/src/state/lp_pool.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ mod tests;
6464
#[derive(Default, Debug)]
6565
#[repr(C)]
6666
pub struct LPPool {
67-
/// name of vault, TODO: check type + size
68-
pub name: [u8; 32],
6967
/// address of the vault.
7068
pub pubkey: Pubkey,
7169
// vault token mint
@@ -128,11 +126,13 @@ pub struct LPPool {
128126
pub target_oracle_delay_fee_bps_per_10_slots: u8,
129127
pub target_position_delay_fee_bps_per_10_slots: u8,
130128

131-
pub padding: [u8; 15],
129+
pub lp_pool_id: u8,
130+
131+
pub padding: [u8; 14],
132132
}
133133

134134
impl Size for LPPool {
135-
const SIZE: usize = 376;
135+
const SIZE: usize = 344;
136136
}
137137

138138
impl LPPool {
@@ -805,8 +805,12 @@ impl LPPool {
805805
Ok((aum_u128, crypto_delta, derivative_groups))
806806
}
807807

808-
pub fn get_lp_pool_signer_seeds<'a>(name: &'a [u8; 32], bump: &'a u8) -> [&'a [u8]; 3] {
809-
[LP_POOL_PDA_SEED.as_ref(), name, bytemuck::bytes_of(bump)]
808+
pub fn get_lp_pool_signer_seeds<'a>(lp_pool_id: &'a u8, bump: &'a u8) -> [&'a [u8]; 3] {
809+
[
810+
LP_POOL_PDA_SEED.as_ref(),
811+
bytemuck::bytes_of(lp_pool_id),
812+
bytemuck::bytes_of(bump),
813+
]
810814
}
811815
}
812816

programs/drift/src/state/perp_market.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ pub struct PerpMarket {
251251
pub lp_paused_operations: u8,
252252
pub lp_exchange_fee_excluscion_scalar: u8,
253253
pub last_fill_price: u64,
254-
pub padding: [u8; 24],
254+
pub lp_pool_id: u8,
255+
pub padding: [u8; 23],
255256
}
256257

257258
impl Default for PerpMarket {
@@ -298,7 +299,8 @@ impl Default for PerpMarket {
298299
lp_exchange_fee_excluscion_scalar: 0,
299300
lp_paused_operations: 0,
300301
last_fill_price: 0,
301-
padding: [0; 24],
302+
lp_pool_id: 0,
303+
padding: [0; 23],
302304
}
303305
}
304306
}

sdk/src/addresses/pda.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,12 @@ export function getRevenueShareEscrowAccountPublicKey(
427427

428428
export function getLpPoolPublicKey(
429429
programId: PublicKey,
430-
nameBuffer: number[]
430+
lpPoolId: number
431431
): PublicKey {
432432
return PublicKey.findProgramAddressSync(
433433
[
434434
Buffer.from(anchor.utils.bytes.utf8.encode('lp_pool')),
435-
Buffer.from(nameBuffer),
435+
new anchor.BN(lpPoolId).toArrayLike(Buffer, 'le', 1),
436436
],
437437
programId
438438
)[0];

0 commit comments

Comments
 (0)