@@ -4018,7 +4018,8 @@ export class DriftClient {
40184018 bracketOrdersParams = new Array < OptionalOrderParams > ( ) ,
40194019 referrerInfo ?: ReferrerInfo ,
40204020 cancelExistingOrders ?: boolean ,
4021- settlePnl ?: boolean
4021+ settlePnl ?: boolean ,
4022+ positionMaxLev ?: number
40224023 ) : Promise < {
40234024 cancelExistingOrdersTx ?: Transaction | VersionedTransaction ;
40244025 settlePnlTx ?: Transaction | VersionedTransaction ;
@@ -4034,7 +4035,7 @@ export class DriftClient {
40344035 const marketIndex = orderParams . marketIndex ;
40354036 const orderId = userAccount . nextOrderId ;
40364037
4037- const ixPromisesForTxs : Record < TxKeys , Promise < TransactionInstruction > > = {
4038+ const ixPromisesForTxs : Record < TxKeys , Promise < TransactionInstruction | TransactionInstruction [ ] > > = {
40384039 cancelExistingOrdersTx : undefined ,
40394040 settlePnlTx : undefined ,
40404041 fillTx : undefined ,
@@ -4043,11 +4044,17 @@ export class DriftClient {
40434044
40444045 const txKeys = Object . keys ( ixPromisesForTxs ) ;
40454046
4046- ixPromisesForTxs . marketOrderTx = this . getPlaceOrdersIx (
4047+ const marketOrderTxIxs = positionMaxLev ? this . getPlaceOrdersAndSetPositionMaxLevIx (
4048+ [ orderParams , ...bracketOrdersParams ] ,
4049+ positionMaxLev ,
4050+ userAccount . subAccountId
4051+ ) : this . getPlaceOrdersIx (
40474052 [ orderParams , ...bracketOrdersParams ] ,
40484053 userAccount . subAccountId
40494054 ) ;
40504055
4056+ ixPromisesForTxs . marketOrderTx = marketOrderTxIxs ;
4057+
40514058 /* Cancel open orders in market if requested */
40524059 if ( cancelExistingOrders && isVariant ( orderParams . marketType , 'perp' ) ) {
40534060 ixPromisesForTxs . cancelExistingOrdersTx = this . getCancelOrdersIx (
@@ -4087,7 +4094,7 @@ export class DriftClient {
40874094 const ixsMap = ixs . reduce ( ( acc , ix , i ) => {
40884095 acc [ txKeys [ i ] ] = ix ;
40894096 return acc ;
4090- } , { } ) as MappedRecord < typeof ixPromisesForTxs , TransactionInstruction > ;
4097+ } , { } ) as MappedRecord < typeof ixPromisesForTxs , TransactionInstruction | TransactionInstruction [ ] > ;
40914098
40924099 const txsMap = ( await this . buildTransactionsMap (
40934100 ixsMap ,
@@ -4671,6 +4678,65 @@ export class DriftClient {
46714678 } ) ;
46724679 }
46734680
4681+ public async getPlaceOrdersAndSetPositionMaxLevIx (
4682+ params : OptionalOrderParams [ ] ,
4683+ positionMaxLev : number ,
4684+ subAccountId ?: number
4685+ ) : Promise < TransactionInstruction [ ] > {
4686+ const user = await this . getUserAccountPublicKey ( subAccountId ) ;
4687+
4688+ const readablePerpMarketIndex : number [ ] = [ ] ;
4689+ const readableSpotMarketIndexes : number [ ] = [ ] ;
4690+ for ( const param of params ) {
4691+ if ( ! param . marketType ) {
4692+ throw new Error ( 'must set param.marketType' ) ;
4693+ }
4694+ if ( isVariant ( param . marketType , 'perp' ) ) {
4695+ readablePerpMarketIndex . push ( param . marketIndex ) ;
4696+ } else {
4697+ readableSpotMarketIndexes . push ( param . marketIndex ) ;
4698+ }
4699+ }
4700+
4701+ const remainingAccounts = this . getRemainingAccounts ( {
4702+ userAccounts : [ this . getUserAccount ( subAccountId ) ] ,
4703+ readablePerpMarketIndex,
4704+ readableSpotMarketIndexes,
4705+ useMarketLastSlotCache : true ,
4706+ } ) ;
4707+
4708+ for ( const param of params ) {
4709+ if ( isUpdateHighLeverageMode ( param . bitFlags ) ) {
4710+ remainingAccounts . push ( {
4711+ pubkey : getHighLeverageModeConfigPublicKey ( this . program . programId ) ,
4712+ isWritable : true ,
4713+ isSigner : false ,
4714+ } ) ;
4715+ }
4716+ }
4717+
4718+ const formattedParams = params . map ( ( item ) => getOrderParams ( item ) ) ;
4719+
4720+ const placeOrdersIxs = await this . program . instruction . placeOrders ( formattedParams , {
4721+ accounts : {
4722+ state : await this . getStatePublicKey ( ) ,
4723+ user,
4724+ userStats : this . getUserStatsAccountPublicKey ( ) ,
4725+ authority : this . wallet . publicKey ,
4726+ } ,
4727+ remainingAccounts,
4728+ } ) ;
4729+
4730+ // TODO: Handle multiple markets?
4731+ const setPositionMaxLevIxs = await this . getUpdateUserPerpPositionCustomMarginRatioIx (
4732+ readablePerpMarketIndex [ 0 ] ,
4733+ 1 / positionMaxLev ,
4734+ subAccountId
4735+ ) ;
4736+
4737+ return [ placeOrdersIxs , setPositionMaxLevIxs ] ;
4738+ }
4739+
46744740 public async fillPerpOrder (
46754741 userAccountPublicKey : PublicKey ,
46764742 user : UserAccount ,
0 commit comments