Skip to content

Commit 00139c4

Browse files
authored
Use lp pool id (#1992)
* add lp pool id and replace lp pool name * add settle perp market enforcement with lp pool id * constituent map fix
1 parent 70c4a4d commit 00139c4

File tree

17 files changed

+353
-296
lines changed

17 files changed

+353
-296
lines changed

programs/drift/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ pub enum ErrorCode {
690690
InvalidConstituentOperation,
691691
#[msg("Unauthorized for operation")]
692692
Unauthorized,
693+
#[msg("Invalid Lp Pool Id for Operation")]
694+
InvalidLpPoolId,
693695
}
694696

695697
#[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
@@ -3356,6 +3356,16 @@ pub fn handle_settle_perp_to_lp_pool<'c: 'info, 'info>(
33563356

33573357
for (_, perp_market_loader) in perp_market_map.0.iter() {
33583358
let mut perp_market = perp_market_loader.load_mut()?;
3359+
if lp_pool.lp_pool_id != perp_market.lp_pool_id {
3360+
msg!(
3361+
"Perp market {} does not have the same lp pool id as the lp pool being settled to: {} != {}",
3362+
perp_market.market_index,
3363+
perp_market.lp_pool_id,
3364+
lp_pool.lp_pool_id
3365+
);
3366+
return Err(ErrorCode::InvalidLpPoolId.into());
3367+
}
3368+
33593369
if perp_market.lp_status == 0
33603370
|| PerpLpOperation::is_operation_paused(
33613371
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() == state.admin,
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
@@ -770,10 +770,10 @@ pub fn handle_lp_pool_add_liquidity<'c: 'info, 'info>(
770770
in_constituent.record_swap_fees(in_fee_amount)?;
771771
lp_pool.record_mint_redeem_fees(lp_fee_amount)?;
772772

773-
let lp_name = lp_pool.name;
773+
let lp_pool_id = lp_pool.lp_pool_id;
774774
let lp_bump = lp_pool.bump;
775775

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

778778
drop(lp_pool);
779779

@@ -1166,10 +1166,10 @@ pub fn handle_lp_pool_remove_liquidity<'c: 'info, 'info>(
11661166
out_constituent.record_swap_fees(out_fee_amount)?;
11671167
lp_pool.record_mint_redeem_fees(lp_fee_amount)?;
11681168

1169-
let lp_name = lp_pool.name;
1169+
let lp_pool_id = lp_pool.lp_pool_id;
11701170
let lp_bump = lp_pool.bump;
11711171

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

11741174
drop(lp_pool);
11751175

programs/drift/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@ pub mod drift {
981981
curve_update_intensity: u8,
982982
amm_jit_intensity: u8,
983983
name: [u8; 32],
984+
lp_pool_id: u8,
984985
) -> Result<()> {
985986
handle_initialize_perp_market(
986987
ctx,
@@ -1009,6 +1010,7 @@ pub mod drift {
10091010
curve_update_intensity,
10101011
amm_jit_intensity,
10111012
name,
1013+
lp_pool_id,
10121014
)
10131015
}
10141016

@@ -1200,6 +1202,13 @@ pub mod drift {
12001202
handle_update_perp_liquidation_fee(ctx, liquidator_fee, if_liquidation_fee)
12011203
}
12021204

1205+
pub fn update_perp_market_lp_pool_id(
1206+
ctx: Context<AdminUpdatePerpMarket>,
1207+
lp_pool_id: u8,
1208+
) -> Result<()> {
1209+
handle_update_perp_lp_pool_id(ctx, lp_pool_id)
1210+
}
1211+
12031212
pub fn update_insurance_fund_unstaking_period(
12041213
ctx: Context<AdminUpdateSpotMarket>,
12051214
insurance_fund_unstaking_period: i64,
@@ -1795,15 +1804,15 @@ pub mod drift {
17951804

17961805
pub fn initialize_lp_pool(
17971806
ctx: Context<InitializeLpPool>,
1798-
name: [u8; 32],
1807+
lp_pool_id: u8,
17991808
min_mint_fee: i64,
18001809
max_aum: u128,
18011810
max_settle_quote_amount_per_market: u64,
18021811
whitelist_mint: Pubkey,
18031812
) -> Result<()> {
18041813
handle_initialize_lp_pool(
18051814
ctx,
1806-
name,
1815+
lp_pool_id,
18071816
min_mint_fee,
18081817
max_aum,
18091818
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
@@ -63,8 +63,6 @@ mod tests;
6363
#[derive(Default, Debug)]
6464
#[repr(C)]
6565
pub struct LPPool {
66-
/// name of vault, TODO: check type + size
67-
pub name: [u8; 32],
6866
/// address of the vault.
6967
pub pubkey: Pubkey,
7068
// vault token mint
@@ -127,11 +125,13 @@ pub struct LPPool {
127125
pub target_oracle_delay_fee_bps_per_10_slots: u8,
128126
pub target_position_delay_fee_bps_per_10_slots: u8,
129127

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

133133
impl Size for LPPool {
134-
const SIZE: usize = 376;
134+
const SIZE: usize = 344;
135135
}
136136

137137
impl LPPool {
@@ -803,8 +803,12 @@ impl LPPool {
803803
Ok((aum_u128, crypto_delta, derivative_groups))
804804
}
805805

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

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)