Skip to content

Commit 6b5142a

Browse files
committed
dlp taker discovered bug fixes and sdk changes
1 parent abc450e commit 6b5142a

File tree

8 files changed

+70
-16
lines changed

8 files changed

+70
-16
lines changed

programs/drift/src/instructions/admin.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4798,7 +4798,7 @@ pub fn handle_initialize_lp_pool(
47984798
last_aum_slot: 0,
47994799
last_aum_ts: 0,
48004800
max_settle_quote_amount: max_settle_quote_amount_per_market,
4801-
last_revenue_rebalance_ts: 0,
4801+
last_hedge_ts: 0,
48024802
total_fees_received: 0,
48034803
total_fees_paid: 0,
48044804
total_mint_redeem_fees_paid: 0,
@@ -5693,6 +5693,9 @@ pub fn handle_begin_lp_swap<'c: 'info, 'info>(
56935693
in_constituent.flash_loan_initial_token_amount = ctx.accounts.signer_in_token_account.amount;
56945694
out_constituent.flash_loan_initial_token_amount = ctx.accounts.signer_out_token_account.amount;
56955695

5696+
drop(in_constituent);
5697+
drop(out_constituent);
5698+
56965699
send_from_program_vault(
56975700
&ctx.accounts.token_program,
56985701
constituent_in_token_account,

programs/drift/src/instructions/lp_pool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub fn handle_update_constituent_target_base<'c: 'info, 'info>(
176176
validate!(
177177
!exists_invalid_constituent_index,
178178
ErrorCode::InvalidUpdateConstituentTargetBaseArgument,
179-
"Constituent index larger than number of constituent target weights"
179+
"Constituent index larger than numr of constituent target weights"
180180
)?;
181181

182182
constituent_target_base.update_target_base(

programs/drift/src/state/lp_pool.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub struct LPPool {
8484
pub max_settle_quote_amount: u64,
8585

8686
/// timestamp of last vAMM revenue rebalance
87-
pub last_revenue_rebalance_ts: u64, // 8, 168
87+
pub last_hedge_ts: u64, // 8, 168
8888
pub revenue_rebalance_period: u64,
8989

9090
/// Every mint/redeem has a monotonically increasing id. This is the next id to use
@@ -619,8 +619,7 @@ impl LPPool {
619619

620620
/// Returns the fee to charge for a mint or redeem in PERCENTAGE_PRECISION
621621
pub fn get_mint_redeem_fee(&self, now: i64, is_minting: bool) -> DriftResult<i64> {
622-
let time_since_last_rebalance =
623-
now.safe_sub(self.last_revenue_rebalance_ts.cast::<i64>()?)?;
622+
let time_since_last_rebalance = now.safe_sub(self.last_hedge_ts.cast::<i64>()?)?;
624623
if is_minting {
625624
// mint fee
626625
self.min_mint_fee.safe_add(

programs/drift/src/state/lp_pool/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ mod swap_tests {
855855

856856
fn get_mint_redeem_fee_scenario(now: i64, is_mint: bool, expected_fee: i64) {
857857
let lp_pool = LPPool {
858-
last_revenue_rebalance_ts: 0,
858+
last_hedge_ts: 0,
859859
revenue_rebalance_period: 3600, // hourly
860860
max_mint_fee_premium: 2000, // 20 bps
861861
min_mint_fee: 100, // 1 bps
@@ -912,7 +912,7 @@ mod swap_tests {
912912
) {
913913
let lp_pool = LPPool {
914914
last_aum,
915-
last_revenue_rebalance_ts: 0,
915+
last_hedge_ts: 0,
916916
revenue_rebalance_period: 3600,
917917
max_mint_fee_premium: 0,
918918
min_mint_fee: 0,
@@ -1095,7 +1095,7 @@ mod swap_tests {
10951095
) {
10961096
let lp_pool = LPPool {
10971097
last_aum,
1098-
last_revenue_rebalance_ts: 0,
1098+
last_hedge_ts: 0,
10991099
revenue_rebalance_period: 3600,
11001100
max_mint_fee_premium: 2000, // 20 bps
11011101
min_mint_fee: 100, // 1 bps

sdk/src/adminClient.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5867,12 +5867,10 @@ export class AdminClient extends DriftClient {
58675867
depositMarketIndex: number,
58685868
amountToDeposit: BN
58695869
): Promise<TransactionSignature> {
5870-
const { depositIx } = await this.getDepositWithdrawToProgramVaultIxs(
5870+
const depositIx = await this.getDepositToProgramVaultIx(
58715871
lpPoolName,
58725872
depositMarketIndex,
5873-
depositMarketIndex,
58745873
amountToDeposit,
5875-
new BN(0)
58765874
);
58775875

58785876
const tx = await this.buildTransaction([depositIx]);
@@ -5885,19 +5883,46 @@ export class AdminClient extends DriftClient {
58855883
borrowMarketIndex: number,
58865884
amountToWithdraw: BN
58875885
): Promise<TransactionSignature> {
5888-
const { withdrawIx } = await this.getDepositWithdrawToProgramVaultIxs(
5886+
const withdrawIx = await this.getWithdrawFromProgramVaultIx(
58895887
lpPoolName,
58905888
borrowMarketIndex,
5891-
borrowMarketIndex,
5892-
new BN(0),
58935889
amountToWithdraw
58945890
);
5895-
58965891
const tx = await this.buildTransaction([withdrawIx]);
58975892
const { txSig } = await this.sendTransaction(tx, [], this.opts);
58985893
return txSig;
58995894
}
59005895

5896+
public async getDepositToProgramVaultIx(
5897+
lpPoolName: number[],
5898+
depositMarketIndex: number,
5899+
amountToDeposit: BN
5900+
): Promise<TransactionInstruction> {
5901+
const { depositIx } = await this.getDepositWithdrawToProgramVaultIxs(
5902+
lpPoolName,
5903+
depositMarketIndex,
5904+
depositMarketIndex,
5905+
amountToDeposit,
5906+
new BN(0)
5907+
);
5908+
return depositIx;
5909+
}
5910+
5911+
public async getWithdrawFromProgramVaultIx(
5912+
lpPoolName: number[],
5913+
borrowMarketIndex: number,
5914+
amountToWithdraw: BN
5915+
): Promise<TransactionInstruction> {
5916+
const { withdrawIx } = await this.getDepositWithdrawToProgramVaultIxs(
5917+
lpPoolName,
5918+
borrowMarketIndex,
5919+
borrowMarketIndex,
5920+
new BN(0),
5921+
amountToWithdraw
5922+
);
5923+
return withdrawIx;
5924+
}
5925+
59015926
public async updatePerpMarketLpPoolFeeTransferScalar(
59025927
marketIndex: number,
59035928
lpFeeTransferScalar: number

sdk/src/constituentMap/constituentMap.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export interface ConstituentMapInterface {
3939
unsubscribe(): Promise<void>;
4040
has(key: string): boolean;
4141
get(key: string): ConstituentAccount | undefined;
42+
getFromSpotMarketIndex(
43+
spotMarketIndex: number): ConstituentAccount | undefined;
44+
getFromConstituentIndex(
45+
constituentIndex: number): ConstituentAccount | undefined;
46+
4247
getWithSlot(key: string): DataAndSlot<ConstituentAccount> | undefined;
4348
mustGet(key: string): Promise<ConstituentAccount>;
4449
mustGetWithSlot(key: string): Promise<DataAndSlot<ConstituentAccount>>;
@@ -52,6 +57,9 @@ export class ConstituentMap implements ConstituentMapInterface {
5257
private commitment?: Commitment;
5358
private connection?: Connection;
5459

60+
private constituentIndexToKeyMap = new Map<number, string>();
61+
private spotMarketIndexToKeyMap = new Map<number, string>();
62+
5563
constructor(config: ConstituentMapConfig) {
5664
this.driftClient = config.driftClient;
5765
this.additionalFilters = config.additionalFilters;
@@ -172,6 +180,16 @@ export class ConstituentMap implements ConstituentMapInterface {
172180
return this.constituentMap.get(key)?.data;
173181
}
174182

183+
public getFromConstituentIndex(constituentIndex: number): ConstituentAccount | undefined {
184+
const key = this.constituentIndexToKeyMap.get(constituentIndex);
185+
return key ? this.get(key) : undefined;
186+
}
187+
188+
public getFromSpotMarketIndex(spotMarketIndex: number): ConstituentAccount | undefined {
189+
const key = this.spotMarketIndexToKeyMap.get(spotMarketIndex);
190+
return key ? this.get(key) : undefined;
191+
}
192+
175193
public getWithSlot(key: string): DataAndSlot<ConstituentAccount> | undefined {
176194
return this.constituentMap.get(key);
177195
}
@@ -245,5 +263,13 @@ export class ConstituentMap implements ConstituentMapInterface {
245263
slot,
246264
});
247265
}
266+
this.constituentIndexToKeyMap.set(
267+
constituentAccount.constituentIndex,
268+
key
269+
);
270+
this.spotMarketIndexToKeyMap.set(
271+
constituentAccount.spotMarketIndex,
272+
key
273+
);
248274
}
249275
}

sdk/src/idl/drift.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9700,7 +9700,7 @@
97009700
"type": "u64"
97019701
},
97029702
{
9703-
"name": "lastRevenueRebalanceTs",
9703+
"name": "lastHedgeTs",
97049704
"docs": [
97059705
"timestamp of last vAMM revenue rebalance"
97069706
],

sdk/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,7 @@ export type LPPoolAccount = {
16581658
lastAum: BN;
16591659
lastAumSlot: BN;
16601660
lastAumTs: BN;
1661+
lastHedgeTs: BN;
16611662
bump: number;
16621663
oldestOracleSlot: BN;
16631664
lastRevenueRebalanceTs: BN;

0 commit comments

Comments
 (0)