@drift-labs/sdk 2.49.0-beta.4 → 2.49.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -727,8 +727,8 @@ class DriftClient {
727
727
  getUserAccountAndSlot(subAccountId) {
728
728
  return this.getUser(subAccountId).getUserAccountAndSlot();
729
729
  }
730
- getSpotPosition(marketIndex) {
731
- return this.getUserAccount().spotPositions.find((spotPosition) => spotPosition.marketIndex === marketIndex);
730
+ getSpotPosition(marketIndex, subAccountId) {
731
+ return this.getUserAccount(subAccountId).spotPositions.find((spotPosition) => spotPosition.marketIndex === marketIndex);
732
732
  }
733
733
  getQuoteAssetTokenAmount() {
734
734
  return this.getTokenAmount(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
@@ -902,13 +902,13 @@ class DriftClient {
902
902
  perpMarketAccountMap,
903
903
  };
904
904
  }
905
- getOrder(orderId) {
905
+ getOrder(orderId, subAccountId) {
906
906
  var _a;
907
- return (_a = this.getUserAccount()) === null || _a === void 0 ? void 0 : _a.orders.find((order) => order.orderId === orderId);
907
+ return (_a = this.getUserAccount(subAccountId)) === null || _a === void 0 ? void 0 : _a.orders.find((order) => order.orderId === orderId);
908
908
  }
909
- getOrderByUserId(userOrderId) {
909
+ getOrderByUserId(userOrderId, subAccountId) {
910
910
  var _a;
911
- return (_a = this.getUserAccount()) === null || _a === void 0 ? void 0 : _a.orders.find((order) => order.userOrderId === userOrderId);
911
+ return (_a = this.getUserAccount(subAccountId)) === null || _a === void 0 ? void 0 : _a.orders.find((order) => order.userOrderId === userOrderId);
912
912
  }
913
913
  /**
914
914
  * Get the associated token address for the given spot market
@@ -944,11 +944,11 @@ class DriftClient {
944
944
  /**
945
945
  * Deposit funds into the given spot market
946
946
  *
947
- * @param amount
948
- * @param marketIndex
947
+ * @param amount to deposit
948
+ * @param marketIndex spot market index to deposit into
949
949
  * @param associatedTokenAccount can be the wallet public key if using native sol
950
- * @param subAccountId
951
- * @param reduceOnly
950
+ * @param subAccountId subaccountId to deposit
951
+ * @param reduceOnly if true, deposit must not increase account risk
952
952
  */
953
953
  async deposit(amount, marketIndex, associatedTokenAccount, subAccountId, reduceOnly = false) {
954
954
  const additionalSigners = [];
@@ -975,7 +975,7 @@ class DriftClient {
975
975
  return txSig;
976
976
  }
977
977
  async getDepositInstruction(amount, marketIndex, userTokenAccount, subAccountId, reduceOnly = false, userInitialized = true) {
978
- const userAccountPublicKey = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, subAccountId !== null && subAccountId !== void 0 ? subAccountId : this.activeSubAccountId);
978
+ const userAccountPublicKey = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, subAccountId);
979
979
  let remainingAccounts = [];
980
980
  if (userInitialized) {
981
981
  remainingAccounts = this.getRemainingAccounts({
@@ -1124,7 +1124,7 @@ class DriftClient {
1124
1124
  * @param associatedTokenAddress - the token account to withdraw to. can be the wallet public key if using native sol
1125
1125
  * @param reduceOnly
1126
1126
  */
1127
- async withdraw(amount, marketIndex, associatedTokenAddress, reduceOnly = false) {
1127
+ async withdraw(amount, marketIndex, associatedTokenAddress, reduceOnly = false, subAccountId) {
1128
1128
  const withdrawIxs = [];
1129
1129
  const additionalSigners = [];
1130
1130
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
@@ -1143,7 +1143,7 @@ class DriftClient {
1143
1143
  withdrawIxs.push(createAssociatedTokenAccountIx);
1144
1144
  }
1145
1145
  }
1146
- const withdrawCollateralIx = await this.getWithdrawIx(amount, spotMarketAccount.marketIndex, associatedTokenAddress, reduceOnly);
1146
+ const withdrawCollateralIx = await this.getWithdrawIx(amount, spotMarketAccount.marketIndex, associatedTokenAddress, reduceOnly, subAccountId);
1147
1147
  withdrawIxs.push(withdrawCollateralIx);
1148
1148
  // Close the wrapped sol account at the end of the transaction
1149
1149
  if (createWSOLTokenAccount) {
@@ -1156,10 +1156,10 @@ class DriftClient {
1156
1156
  this.spotMarketLastSlotCache.set(marketIndex, slot);
1157
1157
  return txSig;
1158
1158
  }
1159
- async getWithdrawIx(amount, marketIndex, userTokenAccount, reduceOnly = false) {
1160
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1159
+ async getWithdrawIx(amount, marketIndex, userTokenAccount, reduceOnly = false, subAccountId) {
1160
+ const user = await this.getUserAccountPublicKey(subAccountId);
1161
1161
  const remainingAccounts = this.getRemainingAccounts({
1162
- userAccounts: [this.getUserAccount()],
1162
+ userAccounts: [this.getUserAccount(subAccountId)],
1163
1163
  useMarketLastSlotCache: true,
1164
1164
  writableSpotMarketIndexes: [marketIndex],
1165
1165
  readableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
@@ -1171,7 +1171,7 @@ class DriftClient {
1171
1171
  spotMarket: spotMarketAccount.pubkey,
1172
1172
  spotMarketVault: spotMarketAccount.vault,
1173
1173
  driftSigner: this.getSignerPublicKey(),
1174
- user: userAccountPublicKey,
1174
+ user,
1175
1175
  userStats: this.getUserStatsAccountPublicKey(),
1176
1176
  userTokenAccount: userTokenAccount,
1177
1177
  authority: this.wallet.publicKey,
@@ -1262,8 +1262,8 @@ class DriftClient {
1262
1262
  remainingAccounts: remainingAccounts,
1263
1263
  });
1264
1264
  }
1265
- async removePerpLpShares(marketIndex, sharesToBurn, txParams) {
1266
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getRemovePerpLpSharesIx(marketIndex, sharesToBurn), txParams), [], this.opts);
1265
+ async removePerpLpShares(marketIndex, sharesToBurn, txParams, subAccountId) {
1266
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getRemovePerpLpSharesIx(marketIndex, sharesToBurn, subAccountId), txParams), [], this.opts);
1267
1267
  return txSig;
1268
1268
  }
1269
1269
  async removePerpLpSharesInExpiringMarket(marketIndex, userAccountPublicKey, sharesToBurn, txParams) {
@@ -1290,15 +1290,15 @@ class DriftClient {
1290
1290
  remainingAccounts: remainingAccounts,
1291
1291
  });
1292
1292
  }
1293
- async getRemovePerpLpSharesIx(marketIndex, sharesToBurn) {
1294
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1293
+ async getRemovePerpLpSharesIx(marketIndex, sharesToBurn, subAccountId) {
1294
+ const user = await this.getUserAccountPublicKey(subAccountId);
1295
1295
  const remainingAccounts = this.getRemainingAccounts({
1296
- userAccounts: [this.getUserAccount()],
1296
+ userAccounts: [this.getUserAccount(subAccountId)],
1297
1297
  useMarketLastSlotCache: true,
1298
1298
  writablePerpMarketIndexes: [marketIndex],
1299
1299
  });
1300
1300
  if (sharesToBurn == undefined) {
1301
- const userAccount = this.getUserAccount();
1301
+ const userAccount = this.getUserAccount(subAccountId);
1302
1302
  const perpPosition = userAccount.perpPositions.filter((position) => position.marketIndex === marketIndex)[0];
1303
1303
  sharesToBurn = perpPosition.lpShares;
1304
1304
  console.log('burning lp shares:', sharesToBurn.toString());
@@ -1306,28 +1306,28 @@ class DriftClient {
1306
1306
  return this.program.instruction.removePerpLpShares(sharesToBurn, marketIndex, {
1307
1307
  accounts: {
1308
1308
  state: await this.getStatePublicKey(),
1309
- user: userAccountPublicKey,
1309
+ user,
1310
1310
  authority: this.wallet.publicKey,
1311
1311
  },
1312
1312
  remainingAccounts: remainingAccounts,
1313
1313
  });
1314
1314
  }
1315
- async addPerpLpShares(amount, marketIndex, txParams) {
1316
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getAddPerpLpSharesIx(amount, marketIndex), txParams), [], this.opts);
1315
+ async addPerpLpShares(amount, marketIndex, txParams, subAccountId) {
1316
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getAddPerpLpSharesIx(amount, marketIndex, subAccountId), txParams), [], this.opts);
1317
1317
  this.perpMarketLastSlotCache.set(marketIndex, slot);
1318
1318
  return txSig;
1319
1319
  }
1320
- async getAddPerpLpSharesIx(amount, marketIndex) {
1321
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1320
+ async getAddPerpLpSharesIx(amount, marketIndex, subAccountId) {
1321
+ const user = await this.getUserAccountPublicKey(subAccountId);
1322
1322
  const remainingAccounts = this.getRemainingAccounts({
1323
- userAccounts: [this.getUserAccount()],
1323
+ userAccounts: [this.getUserAccount(subAccountId)],
1324
1324
  useMarketLastSlotCache: true,
1325
1325
  writablePerpMarketIndexes: [marketIndex],
1326
1326
  });
1327
1327
  return this.program.instruction.addPerpLpShares(amount, marketIndex, {
1328
1328
  accounts: {
1329
1329
  state: await this.getStatePublicKey(),
1330
- user: userAccountPublicKey,
1330
+ user,
1331
1331
  authority: this.wallet.publicKey,
1332
1332
  },
1333
1333
  remainingAccounts: remainingAccounts,
@@ -1349,14 +1349,14 @@ class DriftClient {
1349
1349
  /**
1350
1350
  * @deprecated use {@link placePerpOrder} or {@link placeAndTakePerpOrder} instead
1351
1351
  */
1352
- async openPosition(direction, amount, marketIndex, limitPrice) {
1352
+ async openPosition(direction, amount, marketIndex, limitPrice, subAccountId) {
1353
1353
  return await this.placeAndTakePerpOrder({
1354
1354
  orderType: types_1.OrderType.MARKET,
1355
1355
  marketIndex,
1356
1356
  direction,
1357
1357
  baseAssetAmount: amount,
1358
1358
  price: limitPrice,
1359
- });
1359
+ }, undefined, undefined, undefined, subAccountId);
1360
1360
  }
1361
1361
  async sendSignedTx(tx) {
1362
1362
  const { txSig } = await this.sendTransaction(tx, undefined, this.opts, true);
@@ -1377,15 +1377,15 @@ class DriftClient {
1377
1377
  const marketIndex = orderParams.marketIndex;
1378
1378
  const orderId = userAccount.nextOrderId;
1379
1379
  const bracketOrderIxs = [];
1380
- const placePerpOrderIx = await this.getPlacePerpOrderIx(orderParams);
1380
+ const placePerpOrderIx = await this.getPlacePerpOrderIx(orderParams, userAccount.subAccountId);
1381
1381
  for (const bracketOrderParams of bracketOrdersParams) {
1382
- const placeBracketOrderIx = await this.getPlacePerpOrderIx(bracketOrderParams);
1382
+ const placeBracketOrderIx = await this.getPlacePerpOrderIx(bracketOrderParams, userAccount.subAccountId);
1383
1383
  bracketOrderIxs.push(placeBracketOrderIx);
1384
1384
  }
1385
1385
  let cancelOrdersIx;
1386
1386
  let cancelExistingOrdersTx;
1387
1387
  if (cancelExistingOrders && (0, types_1.isVariant)(orderParams.marketType, 'perp')) {
1388
- cancelOrdersIx = await this.getCancelOrdersIx(orderParams.marketType, orderParams.marketIndex, null);
1388
+ cancelOrdersIx = await this.getCancelOrdersIx(orderParams.marketType, orderParams.marketIndex, null, userAccount.subAccountId);
1389
1389
  //@ts-ignore
1390
1390
  cancelExistingOrdersTx = await this.buildTransaction([cancelOrdersIx], txParams, this.txVersion);
1391
1391
  }
@@ -1395,7 +1395,7 @@ class DriftClient {
1395
1395
  const fillPerpOrderIx = await this.getFillPerpOrderIx(userAccountPublicKey, userAccount, {
1396
1396
  orderId,
1397
1397
  marketIndex,
1398
- }, makerInfo, referrerInfo);
1398
+ }, makerInfo, referrerInfo, userAccount.subAccountId);
1399
1399
  const versionedFillTx = await this.buildTransaction([fillPerpOrderIx], txParams, 0);
1400
1400
  const [signedVersionedMarketOrderTx, signedVersionedFillTx, signedCancelExistingOrdersTx,] = await this.provider.wallet.signAllTransactions([
1401
1401
  versionedMarketOrderTx,
@@ -1431,23 +1431,23 @@ class DriftClient {
1431
1431
  return { txSig, signedFillTx: undefined, signedCancelExistingOrdersTx };
1432
1432
  }
1433
1433
  }
1434
- async placePerpOrder(orderParams, txParams) {
1435
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlacePerpOrderIx(orderParams), txParams), [], this.opts);
1434
+ async placePerpOrder(orderParams, txParams, subAccountId) {
1435
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlacePerpOrderIx(orderParams, subAccountId), txParams), [], this.opts);
1436
1436
  this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
1437
1437
  return txSig;
1438
1438
  }
1439
- async getPlacePerpOrderIx(orderParams) {
1439
+ async getPlacePerpOrderIx(orderParams, subAccountId) {
1440
1440
  orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.PERP });
1441
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1441
+ const user = await this.getUserAccountPublicKey(subAccountId);
1442
1442
  const remainingAccounts = this.getRemainingAccounts({
1443
- userAccounts: [this.getUserAccount()],
1443
+ userAccounts: [this.getUserAccount(subAccountId)],
1444
1444
  useMarketLastSlotCache: true,
1445
1445
  readablePerpMarketIndex: orderParams.marketIndex,
1446
1446
  });
1447
1447
  return await this.program.instruction.placePerpOrder(orderParams, {
1448
1448
  accounts: {
1449
1449
  state: await this.getStatePublicKey(),
1450
- user: userAccountPublicKey,
1450
+ user,
1451
1451
  userStats: this.getUserStatsAccountPublicKey(),
1452
1452
  authority: this.wallet.publicKey,
1453
1453
  },
@@ -1537,72 +1537,72 @@ class DriftClient {
1537
1537
  const { txSig } = await this.sendTransaction(await this.buildTransaction(ix, txParams), [], this.opts);
1538
1538
  return txSig;
1539
1539
  }
1540
- async cancelOrder(orderId, txParams) {
1541
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getCancelOrderIx(orderId), txParams), [], this.opts);
1540
+ async cancelOrder(orderId, txParams, subAccountId) {
1541
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getCancelOrderIx(orderId, subAccountId), txParams), [], this.opts);
1542
1542
  return txSig;
1543
1543
  }
1544
- async getCancelOrderIx(orderId) {
1545
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1544
+ async getCancelOrderIx(orderId, subAccountId) {
1545
+ const user = await this.getUserAccountPublicKey(subAccountId);
1546
1546
  const remainingAccounts = this.getRemainingAccounts({
1547
- userAccounts: [this.getUserAccount()],
1547
+ userAccounts: [this.getUserAccount(subAccountId)],
1548
1548
  useMarketLastSlotCache: true,
1549
1549
  });
1550
1550
  return await this.program.instruction.cancelOrder(orderId !== null && orderId !== void 0 ? orderId : null, {
1551
1551
  accounts: {
1552
1552
  state: await this.getStatePublicKey(),
1553
- user: userAccountPublicKey,
1553
+ user,
1554
1554
  authority: this.wallet.publicKey,
1555
1555
  },
1556
1556
  remainingAccounts,
1557
1557
  });
1558
1558
  }
1559
- async cancelOrderByUserId(userOrderId, txParams) {
1560
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getCancelOrderByUserIdIx(userOrderId), txParams), [], this.opts);
1559
+ async cancelOrderByUserId(userOrderId, txParams, subAccountId) {
1560
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getCancelOrderByUserIdIx(userOrderId, subAccountId), txParams), [], this.opts);
1561
1561
  return txSig;
1562
1562
  }
1563
- async getCancelOrderByUserIdIx(userOrderId) {
1564
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1563
+ async getCancelOrderByUserIdIx(userOrderId, subAccountId) {
1564
+ const user = await this.getUserAccountPublicKey(subAccountId);
1565
1565
  const order = this.getOrderByUserId(userOrderId);
1566
1566
  const oracle = this.getPerpMarketAccount(order.marketIndex).amm.oracle;
1567
1567
  const remainingAccounts = this.getRemainingAccounts({
1568
- userAccounts: [this.getUserAccount()],
1568
+ userAccounts: [this.getUserAccount(subAccountId)],
1569
1569
  useMarketLastSlotCache: true,
1570
1570
  });
1571
1571
  return await this.program.instruction.cancelOrderByUserId(userOrderId, {
1572
1572
  accounts: {
1573
1573
  state: await this.getStatePublicKey(),
1574
- user: userAccountPublicKey,
1574
+ user,
1575
1575
  authority: this.wallet.publicKey,
1576
1576
  oracle,
1577
1577
  },
1578
1578
  remainingAccounts,
1579
1579
  });
1580
1580
  }
1581
- async cancelOrdersByIds(orderIds, txParams) {
1582
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getCancelOrdersByIdsIx(orderIds), txParams), [], this.opts);
1581
+ async cancelOrdersByIds(orderIds, txParams, subAccountId) {
1582
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getCancelOrdersByIdsIx(orderIds, subAccountId), txParams), [], this.opts);
1583
1583
  return txSig;
1584
1584
  }
1585
- async getCancelOrdersByIdsIx(orderIds) {
1586
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1585
+ async getCancelOrdersByIdsIx(orderIds, subAccountId) {
1586
+ const user = await this.getUserAccountPublicKey(subAccountId);
1587
1587
  const remainingAccounts = this.getRemainingAccounts({
1588
- userAccounts: [this.getUserAccount()],
1588
+ userAccounts: [this.getUserAccount(subAccountId)],
1589
1589
  useMarketLastSlotCache: true,
1590
1590
  });
1591
1591
  return await this.program.instruction.cancelOrdersByIds(orderIds, {
1592
1592
  accounts: {
1593
1593
  state: await this.getStatePublicKey(),
1594
- user: userAccountPublicKey,
1594
+ user,
1595
1595
  authority: this.wallet.publicKey,
1596
1596
  },
1597
1597
  remainingAccounts,
1598
1598
  });
1599
1599
  }
1600
- async cancelOrders(marketType, marketIndex, direction, txParams) {
1601
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getCancelOrdersIx(marketType, marketIndex, direction), txParams), [], this.opts);
1600
+ async cancelOrders(marketType, marketIndex, direction, txParams, subAccountId) {
1601
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getCancelOrdersIx(marketType, marketIndex, direction, subAccountId), txParams), [], this.opts);
1602
1602
  return txSig;
1603
1603
  }
1604
- async getCancelOrdersIx(marketType, marketIndex, direction) {
1605
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1604
+ async getCancelOrdersIx(marketType, marketIndex, direction, subAccountId) {
1605
+ const user = await this.getUserAccountPublicKey(subAccountId);
1606
1606
  let readablePerpMarketIndex = undefined;
1607
1607
  let readableSpotMarketIndexes = undefined;
1608
1608
  if (typeof marketIndex === 'number') {
@@ -1614,7 +1614,7 @@ class DriftClient {
1614
1614
  }
1615
1615
  }
1616
1616
  const remainingAccounts = this.getRemainingAccounts({
1617
- userAccounts: [this.getUserAccount()],
1617
+ userAccounts: [this.getUserAccount(subAccountId)],
1618
1618
  readablePerpMarketIndex,
1619
1619
  readableSpotMarketIndexes,
1620
1620
  useMarketLastSlotCache: true,
@@ -1622,27 +1622,27 @@ class DriftClient {
1622
1622
  return await this.program.instruction.cancelOrders(marketType !== null && marketType !== void 0 ? marketType : null, marketIndex !== null && marketIndex !== void 0 ? marketIndex : null, direction !== null && direction !== void 0 ? direction : null, {
1623
1623
  accounts: {
1624
1624
  state: await this.getStatePublicKey(),
1625
- user: userAccountPublicKey,
1625
+ user,
1626
1626
  authority: this.wallet.publicKey,
1627
1627
  },
1628
1628
  remainingAccounts,
1629
1629
  });
1630
1630
  }
1631
- async cancelAndPlaceOrders(cancelOrderParams, placeOrderParams, txParams) {
1631
+ async cancelAndPlaceOrders(cancelOrderParams, placeOrderParams, txParams, subAccountId) {
1632
1632
  const ixs = [
1633
- await this.getCancelOrdersIx(cancelOrderParams.marketType, cancelOrderParams.marketIndex, cancelOrderParams.direction),
1634
- await this.getPlaceOrdersIx(placeOrderParams),
1633
+ await this.getCancelOrdersIx(cancelOrderParams.marketType, cancelOrderParams.marketIndex, cancelOrderParams.direction, subAccountId),
1634
+ await this.getPlaceOrdersIx(placeOrderParams, subAccountId),
1635
1635
  ];
1636
1636
  const tx = await this.buildTransaction(ixs, txParams);
1637
1637
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
1638
1638
  return txSig;
1639
1639
  }
1640
- async placeOrders(params, txParams) {
1641
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceOrdersIx(params), txParams), [], this.opts);
1640
+ async placeOrders(params, txParams, subAccountId) {
1641
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceOrdersIx(params, subAccountId), txParams), [], this.opts);
1642
1642
  return txSig;
1643
1643
  }
1644
- async getPlaceOrdersIx(params) {
1645
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1644
+ async getPlaceOrdersIx(params, subAccountId) {
1645
+ const user = await this.getUserAccountPublicKey(subAccountId);
1646
1646
  const readablePerpMarketIndex = [];
1647
1647
  const readableSpotMarketIndexes = [];
1648
1648
  for (const param of params) {
@@ -1657,7 +1657,7 @@ class DriftClient {
1657
1657
  }
1658
1658
  }
1659
1659
  const remainingAccounts = this.getRemainingAccounts({
1660
- userAccounts: [this.getUserAccount()],
1660
+ userAccounts: [this.getUserAccount(subAccountId)],
1661
1661
  readablePerpMarketIndex,
1662
1662
  readableSpotMarketIndexes,
1663
1663
  useMarketLastSlotCache: true,
@@ -1665,20 +1665,20 @@ class DriftClient {
1665
1665
  return await this.program.instruction.placeOrders(params, {
1666
1666
  accounts: {
1667
1667
  state: await this.getStatePublicKey(),
1668
- user: userAccountPublicKey,
1668
+ user,
1669
1669
  userStats: this.getUserStatsAccountPublicKey(),
1670
1670
  authority: this.wallet.publicKey,
1671
1671
  },
1672
1672
  remainingAccounts,
1673
1673
  });
1674
1674
  }
1675
- async fillPerpOrder(userAccountPublicKey, user, order, makerInfo, referrerInfo, txParams) {
1676
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getFillPerpOrderIx(userAccountPublicKey, user, order, makerInfo, referrerInfo), txParams), [], this.opts);
1675
+ async fillPerpOrder(userAccountPublicKey, user, order, makerInfo, referrerInfo, txParams, fillerPublicKey) {
1676
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getFillPerpOrderIx(userAccountPublicKey, user, order, makerInfo, referrerInfo, fillerPublicKey), txParams), [], this.opts);
1677
1677
  return txSig;
1678
1678
  }
1679
- async getFillPerpOrderIx(userAccountPublicKey, userAccount, order, makerInfo, referrerInfo) {
1679
+ async getFillPerpOrderIx(userAccountPublicKey, userAccount, order, makerInfo, referrerInfo, fillerSubAccountId) {
1680
1680
  const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
1681
- const fillerPublicKey = await this.getUserAccountPublicKey();
1681
+ const filler = await this.getUserAccountPublicKey(fillerSubAccountId);
1682
1682
  const fillerStatsPublicKey = this.getUserStatsAccountPublicKey();
1683
1683
  const marketIndex = order
1684
1684
  ? order.marketIndex
@@ -1728,7 +1728,7 @@ class DriftClient {
1728
1728
  return await this.program.instruction.fillPerpOrder(orderId, null, {
1729
1729
  accounts: {
1730
1730
  state: await this.getStatePublicKey(),
1731
- filler: fillerPublicKey,
1731
+ filler,
1732
1732
  fillerStats: fillerStatsPublicKey,
1733
1733
  user: userAccountPublicKey,
1734
1734
  userStats: userStatsPublicKey,
@@ -1737,29 +1737,29 @@ class DriftClient {
1737
1737
  remainingAccounts,
1738
1738
  });
1739
1739
  }
1740
- async getRevertFillIx() {
1741
- const fillerPublicKey = await this.getUserAccountPublicKey();
1740
+ async getRevertFillIx(fillerPublicKey) {
1741
+ const filler = fillerPublicKey !== null && fillerPublicKey !== void 0 ? fillerPublicKey : (await this.getUserAccountPublicKey());
1742
1742
  const fillerStatsPublicKey = this.getUserStatsAccountPublicKey();
1743
1743
  return this.program.instruction.revertFill({
1744
1744
  accounts: {
1745
1745
  state: await this.getStatePublicKey(),
1746
- filler: fillerPublicKey,
1746
+ filler,
1747
1747
  fillerStats: fillerStatsPublicKey,
1748
1748
  authority: this.wallet.publicKey,
1749
1749
  },
1750
1750
  });
1751
1751
  }
1752
- async placeSpotOrder(orderParams, txParams) {
1753
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceSpotOrderIx(orderParams), txParams), [], this.opts);
1752
+ async placeSpotOrder(orderParams, txParams, subAccountId) {
1753
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceSpotOrderIx(orderParams, subAccountId), txParams), [], this.opts);
1754
1754
  this.spotMarketLastSlotCache.set(orderParams.marketIndex, slot);
1755
1755
  this.spotMarketLastSlotCache.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX, slot);
1756
1756
  return txSig;
1757
1757
  }
1758
- async getPlaceSpotOrderIx(orderParams) {
1758
+ async getPlaceSpotOrderIx(orderParams, subAccountId) {
1759
1759
  orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.SPOT });
1760
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1760
+ const userAccountPublicKey = await this.getUserAccountPublicKey(subAccountId);
1761
1761
  const remainingAccounts = this.getRemainingAccounts({
1762
- userAccounts: [this.getUserAccount()],
1762
+ userAccounts: [this.getUserAccount(subAccountId)],
1763
1763
  useMarketLastSlotCache: true,
1764
1764
  readableSpotMarketIndexes: [
1765
1765
  orderParams.marketIndex,
@@ -1780,9 +1780,9 @@ class DriftClient {
1780
1780
  const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getFillSpotOrderIx(userAccountPublicKey, user, order, fulfillmentConfig, makerInfo, referrerInfo), txParams), [], this.opts);
1781
1781
  return txSig;
1782
1782
  }
1783
- async getFillSpotOrderIx(userAccountPublicKey, userAccount, order, fulfillmentConfig, makerInfo, referrerInfo) {
1783
+ async getFillSpotOrderIx(userAccountPublicKey, userAccount, order, fulfillmentConfig, makerInfo, referrerInfo, fillerPublicKey) {
1784
1784
  const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
1785
- const fillerPublicKey = await this.getUserAccountPublicKey();
1785
+ const filler = fillerPublicKey !== null && fillerPublicKey !== void 0 ? fillerPublicKey : (await this.getUserAccountPublicKey());
1786
1786
  const fillerStatsPublicKey = this.getUserStatsAccountPublicKey();
1787
1787
  const marketIndex = order
1788
1788
  ? order.marketIndex
@@ -1825,7 +1825,7 @@ class DriftClient {
1825
1825
  return await this.program.instruction.fillSpotOrder(orderId, fulfillmentConfig ? fulfillmentConfig.fulfillmentType : null, makerOrderId, {
1826
1826
  accounts: {
1827
1827
  state: await this.getStatePublicKey(),
1828
- filler: fillerPublicKey,
1828
+ filler,
1829
1829
  fillerStats: fillerStatsPublicKey,
1830
1830
  user: userAccountPublicKey,
1831
1831
  userStats: userStatsPublicKey,
@@ -2266,12 +2266,12 @@ class DriftClient {
2266
2266
  ixs.push(beginSwapIx, closeWSOLIx, depositIx, createWSOLIx, endSwapIx);
2267
2267
  return ixs;
2268
2268
  }
2269
- async triggerOrder(userAccountPublicKey, user, order, txParams) {
2270
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getTriggerOrderIx(userAccountPublicKey, user, order), txParams), [], this.opts);
2269
+ async triggerOrder(userAccountPublicKey, user, order, txParams, fillerPublicKey) {
2270
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getTriggerOrderIx(userAccountPublicKey, user, order, fillerPublicKey), txParams), [], this.opts);
2271
2271
  return txSig;
2272
2272
  }
2273
- async getTriggerOrderIx(userAccountPublicKey, userAccount, order) {
2274
- const fillerPublicKey = await this.getUserAccountPublicKey();
2273
+ async getTriggerOrderIx(userAccountPublicKey, userAccount, order, fillerPublicKey) {
2274
+ const filler = fillerPublicKey !== null && fillerPublicKey !== void 0 ? fillerPublicKey : (await this.getUserAccountPublicKey());
2275
2275
  let remainingAccountsParams;
2276
2276
  if ((0, types_1.isVariant)(order.marketType, 'perp')) {
2277
2277
  remainingAccountsParams = {
@@ -2290,19 +2290,19 @@ class DriftClient {
2290
2290
  return await this.program.instruction.triggerOrder(orderId, {
2291
2291
  accounts: {
2292
2292
  state: await this.getStatePublicKey(),
2293
- filler: fillerPublicKey,
2293
+ filler,
2294
2294
  user: userAccountPublicKey,
2295
2295
  authority: this.wallet.publicKey,
2296
2296
  },
2297
2297
  remainingAccounts,
2298
2298
  });
2299
2299
  }
2300
- async forceCancelOrders(userAccountPublicKey, user, txParams) {
2301
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getForceCancelOrdersIx(userAccountPublicKey, user), txParams), [], this.opts);
2300
+ async forceCancelOrders(userAccountPublicKey, user, txParams, fillerPublicKey) {
2301
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getForceCancelOrdersIx(userAccountPublicKey, user, fillerPublicKey), txParams), [], this.opts);
2302
2302
  return txSig;
2303
2303
  }
2304
- async getForceCancelOrdersIx(userAccountPublicKey, userAccount) {
2305
- const fillerPublicKey = await this.getUserAccountPublicKey();
2304
+ async getForceCancelOrdersIx(userAccountPublicKey, userAccount, fillerPublicKey) {
2305
+ const filler = fillerPublicKey !== null && fillerPublicKey !== void 0 ? fillerPublicKey : (await this.getUserAccountPublicKey());
2306
2306
  const remainingAccounts = this.getRemainingAccounts({
2307
2307
  userAccounts: [userAccount],
2308
2308
  writableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
@@ -2310,66 +2310,66 @@ class DriftClient {
2310
2310
  return await this.program.instruction.forceCancelOrders({
2311
2311
  accounts: {
2312
2312
  state: await this.getStatePublicKey(),
2313
- filler: fillerPublicKey,
2313
+ filler,
2314
2314
  user: userAccountPublicKey,
2315
2315
  authority: this.wallet.publicKey,
2316
2316
  },
2317
2317
  remainingAccounts,
2318
2318
  });
2319
2319
  }
2320
- async updateUserIdle(userAccountPublicKey, user, txParams) {
2321
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getUpdateUserIdleIx(userAccountPublicKey, user), txParams), [], this.opts);
2320
+ async updateUserIdle(userAccountPublicKey, user, txParams, fillerPublicKey) {
2321
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getUpdateUserIdleIx(userAccountPublicKey, user, fillerPublicKey), txParams), [], this.opts);
2322
2322
  return txSig;
2323
2323
  }
2324
- async getUpdateUserIdleIx(userAccountPublicKey, userAccount) {
2325
- const fillerPublicKey = await this.getUserAccountPublicKey();
2324
+ async getUpdateUserIdleIx(userAccountPublicKey, userAccount, fillerPublicKey) {
2325
+ const filler = fillerPublicKey !== null && fillerPublicKey !== void 0 ? fillerPublicKey : (await this.getUserAccountPublicKey());
2326
2326
  const remainingAccounts = this.getRemainingAccounts({
2327
2327
  userAccounts: [userAccount],
2328
2328
  });
2329
2329
  return await this.program.instruction.updateUserIdle({
2330
2330
  accounts: {
2331
2331
  state: await this.getStatePublicKey(),
2332
- filler: fillerPublicKey,
2332
+ filler,
2333
2333
  user: userAccountPublicKey,
2334
2334
  authority: this.wallet.publicKey,
2335
2335
  },
2336
2336
  remainingAccounts,
2337
2337
  });
2338
2338
  }
2339
- async updateUserOpenOrdersCount(userAccountPublicKey, user, txParams) {
2340
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getUpdateUserOpenOrdersCountIx(userAccountPublicKey, user), txParams), [], this.opts);
2339
+ async updateUserOpenOrdersCount(userAccountPublicKey, user, txParams, fillerPublicKey) {
2340
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getUpdateUserOpenOrdersCountIx(userAccountPublicKey, user, fillerPublicKey), txParams), [], this.opts);
2341
2341
  return txSig;
2342
2342
  }
2343
- async getUpdateUserOpenOrdersCountIx(userAccountPublicKey, userAccount) {
2344
- const fillerPublicKey = await this.getUserAccountPublicKey();
2343
+ async getUpdateUserOpenOrdersCountIx(userAccountPublicKey, userAccount, fillerPublicKey) {
2344
+ const filler = fillerPublicKey !== null && fillerPublicKey !== void 0 ? fillerPublicKey : (await this.getUserAccountPublicKey());
2345
2345
  const remainingAccounts = this.getRemainingAccounts({
2346
2346
  userAccounts: [userAccount],
2347
2347
  });
2348
2348
  return await this.program.instruction.updateUserOpenOrdersCount({
2349
2349
  accounts: {
2350
2350
  state: await this.getStatePublicKey(),
2351
- filler: fillerPublicKey,
2351
+ filler,
2352
2352
  user: userAccountPublicKey,
2353
2353
  authority: this.wallet.publicKey,
2354
2354
  },
2355
2355
  remainingAccounts,
2356
2356
  });
2357
2357
  }
2358
- async placeAndTakePerpOrder(orderParams, makerInfo, referrerInfo, txParams) {
2359
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo), txParams), [], this.opts);
2358
+ async placeAndTakePerpOrder(orderParams, makerInfo, referrerInfo, txParams, subAccountId) {
2359
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, subAccountId), txParams), [], this.opts);
2360
2360
  this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
2361
2361
  return txSig;
2362
2362
  }
2363
- async getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo) {
2363
+ async getPlaceAndTakePerpOrderIx(orderParams, makerInfo, referrerInfo, subAccountId) {
2364
2364
  orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.PERP });
2365
2365
  const userStatsPublicKey = await this.getUserStatsAccountPublicKey();
2366
- const userAccountPublicKey = await this.getUserAccountPublicKey();
2366
+ const user = await this.getUserAccountPublicKey(subAccountId);
2367
2367
  makerInfo = Array.isArray(makerInfo)
2368
2368
  ? makerInfo
2369
2369
  : makerInfo
2370
2370
  ? [makerInfo]
2371
2371
  : [];
2372
- const userAccounts = [this.getUserAccount()];
2372
+ const userAccounts = [this.getUserAccount(subAccountId)];
2373
2373
  for (const maker of makerInfo) {
2374
2374
  userAccounts.push(maker.makerUserAccount);
2375
2375
  }
@@ -2409,24 +2409,27 @@ class DriftClient {
2409
2409
  return await this.program.instruction.placeAndTakePerpOrder(orderParams, null, {
2410
2410
  accounts: {
2411
2411
  state: await this.getStatePublicKey(),
2412
- user: userAccountPublicKey,
2412
+ user,
2413
2413
  userStats: userStatsPublicKey,
2414
2414
  authority: this.wallet.publicKey,
2415
2415
  },
2416
2416
  remainingAccounts,
2417
2417
  });
2418
2418
  }
2419
- async placeAndMakePerpOrder(orderParams, takerInfo, referrerInfo, txParams) {
2420
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndMakePerpOrderIx(orderParams, takerInfo, referrerInfo), txParams), [], this.opts);
2419
+ async placeAndMakePerpOrder(orderParams, takerInfo, referrerInfo, txParams, subAccountId) {
2420
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndMakePerpOrderIx(orderParams, takerInfo, referrerInfo, subAccountId), txParams), [], this.opts);
2421
2421
  this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
2422
2422
  return txSig;
2423
2423
  }
2424
- async getPlaceAndMakePerpOrderIx(orderParams, takerInfo, referrerInfo) {
2424
+ async getPlaceAndMakePerpOrderIx(orderParams, takerInfo, referrerInfo, subAccountId) {
2425
2425
  orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.PERP });
2426
2426
  const userStatsPublicKey = this.getUserStatsAccountPublicKey();
2427
- const userAccountPublicKey = await this.getUserAccountPublicKey();
2427
+ const user = await this.getUserAccountPublicKey(subAccountId);
2428
2428
  const remainingAccounts = this.getRemainingAccounts({
2429
- userAccounts: [this.getUserAccount(), takerInfo.takerUserAccount],
2429
+ userAccounts: [
2430
+ this.getUserAccount(subAccountId),
2431
+ takerInfo.takerUserAccount,
2432
+ ],
2430
2433
  useMarketLastSlotCache: true,
2431
2434
  writablePerpMarketIndexes: [orderParams.marketIndex],
2432
2435
  });
@@ -2446,7 +2449,7 @@ class DriftClient {
2446
2449
  return await this.program.instruction.placeAndMakePerpOrder(orderParams, takerOrderId, {
2447
2450
  accounts: {
2448
2451
  state: await this.getStatePublicKey(),
2449
- user: userAccountPublicKey,
2452
+ user,
2450
2453
  userStats: userStatsPublicKey,
2451
2454
  taker: takerInfo.taker,
2452
2455
  takerStats: takerInfo.takerStats,
@@ -2455,17 +2458,17 @@ class DriftClient {
2455
2458
  remainingAccounts,
2456
2459
  });
2457
2460
  }
2458
- async placeAndTakeSpotOrder(orderParams, fulfillmentConfig, makerInfo, referrerInfo, txParams) {
2459
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndTakeSpotOrderIx(orderParams, fulfillmentConfig, makerInfo, referrerInfo), txParams), [], this.opts);
2461
+ async placeAndTakeSpotOrder(orderParams, fulfillmentConfig, makerInfo, referrerInfo, txParams, subAccountId) {
2462
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndTakeSpotOrderIx(orderParams, fulfillmentConfig, makerInfo, referrerInfo, subAccountId), txParams), [], this.opts);
2460
2463
  this.spotMarketLastSlotCache.set(orderParams.marketIndex, slot);
2461
2464
  this.spotMarketLastSlotCache.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX, slot);
2462
2465
  return txSig;
2463
2466
  }
2464
- async getPlaceAndTakeSpotOrderIx(orderParams, fulfillmentConfig, makerInfo, referrerInfo) {
2467
+ async getPlaceAndTakeSpotOrderIx(orderParams, fulfillmentConfig, makerInfo, referrerInfo, subAccountId) {
2465
2468
  orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.SPOT });
2466
- const userStatsPublicKey = await this.getUserStatsAccountPublicKey();
2467
- const userAccountPublicKey = await this.getUserAccountPublicKey();
2468
- const userAccounts = [this.getUserAccount()];
2469
+ const userStatsPublicKey = this.getUserStatsAccountPublicKey();
2470
+ const user = await this.getUserAccountPublicKey(subAccountId);
2471
+ const userAccounts = [this.getUserAccount(subAccountId)];
2469
2472
  if (makerInfo !== undefined) {
2470
2473
  userAccounts.push(makerInfo.makerUserAccount);
2471
2474
  }
@@ -2507,25 +2510,28 @@ class DriftClient {
2507
2510
  return await this.program.instruction.placeAndTakeSpotOrder(orderParams, fulfillmentConfig ? fulfillmentConfig.fulfillmentType : null, makerOrderId, {
2508
2511
  accounts: {
2509
2512
  state: await this.getStatePublicKey(),
2510
- user: userAccountPublicKey,
2513
+ user,
2511
2514
  userStats: userStatsPublicKey,
2512
2515
  authority: this.wallet.publicKey,
2513
2516
  },
2514
2517
  remainingAccounts,
2515
2518
  });
2516
2519
  }
2517
- async placeAndMakeSpotOrder(orderParams, takerInfo, fulfillmentConfig, referrerInfo, txParams) {
2518
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndMakeSpotOrderIx(orderParams, takerInfo, fulfillmentConfig, referrerInfo), txParams), [], this.opts);
2520
+ async placeAndMakeSpotOrder(orderParams, takerInfo, fulfillmentConfig, referrerInfo, txParams, subAccountId) {
2521
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getPlaceAndMakeSpotOrderIx(orderParams, takerInfo, fulfillmentConfig, referrerInfo, subAccountId), txParams), [], this.opts);
2519
2522
  this.spotMarketLastSlotCache.set(orderParams.marketIndex, slot);
2520
2523
  this.spotMarketLastSlotCache.set(numericConstants_1.QUOTE_SPOT_MARKET_INDEX, slot);
2521
2524
  return txSig;
2522
2525
  }
2523
- async getPlaceAndMakeSpotOrderIx(orderParams, takerInfo, fulfillmentConfig, referrerInfo) {
2526
+ async getPlaceAndMakeSpotOrderIx(orderParams, takerInfo, fulfillmentConfig, referrerInfo, subAccountId) {
2524
2527
  orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.SPOT });
2525
2528
  const userStatsPublicKey = this.getUserStatsAccountPublicKey();
2526
- const userAccountPublicKey = await this.getUserAccountPublicKey();
2529
+ const user = await this.getUserAccountPublicKey(subAccountId);
2527
2530
  const remainingAccounts = this.getRemainingAccounts({
2528
- userAccounts: [this.getUserAccount(), takerInfo.takerUserAccount],
2531
+ userAccounts: [
2532
+ this.getUserAccount(subAccountId),
2533
+ takerInfo.takerUserAccount,
2534
+ ],
2529
2535
  useMarketLastSlotCache: true,
2530
2536
  writableSpotMarketIndexes: [
2531
2537
  orderParams.marketIndex,
@@ -2549,7 +2555,7 @@ class DriftClient {
2549
2555
  return await this.program.instruction.placeAndMakeSpotOrder(orderParams, takerOrderId, fulfillmentConfig ? fulfillmentConfig.fulfillmentType : null, {
2550
2556
  accounts: {
2551
2557
  state: await this.getStatePublicKey(),
2552
- user: userAccountPublicKey,
2558
+ user,
2553
2559
  userStats: userStatsPublicKey,
2554
2560
  taker: takerInfo.taker,
2555
2561
  takerStats: takerInfo.takerStats,
@@ -2561,8 +2567,8 @@ class DriftClient {
2561
2567
  /**
2562
2568
  * @deprecated use {@link placePerpOrder} or {@link placeAndTakePerpOrder} instead
2563
2569
  */
2564
- async closePosition(marketIndex, limitPrice) {
2565
- const userPosition = this.getUser().getPerpPosition(marketIndex);
2570
+ async closePosition(marketIndex, limitPrice, subAccountId) {
2571
+ const userPosition = this.getUser(subAccountId).getPerpPosition(marketIndex);
2566
2572
  if (!userPosition) {
2567
2573
  throw Error(`No position in market ${marketIndex.toString()}`);
2568
2574
  }
@@ -2573,7 +2579,7 @@ class DriftClient {
2573
2579
  baseAssetAmount: userPosition.baseAssetAmount.abs(),
2574
2580
  reduceOnly: true,
2575
2581
  price: limitPrice,
2576
- });
2582
+ }, undefined, undefined, undefined, subAccountId);
2577
2583
  }
2578
2584
  /**
2579
2585
  * Modifies an open order by closing it and replacing it with a new order.
@@ -2627,14 +2633,14 @@ class DriftClient {
2627
2633
  * @param orderParams.maxTs:
2628
2634
  * @returns
2629
2635
  */
2630
- async modifyOrder(orderParams, txParams) {
2631
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getModifyOrderIx(orderParams), txParams), [], this.opts);
2636
+ async modifyOrder(orderParams, txParams, subAccountId) {
2637
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getModifyOrderIx(orderParams, subAccountId), txParams), [], this.opts);
2632
2638
  return txSig;
2633
2639
  }
2634
- async getModifyOrderIx({ orderId, newDirection, newBaseAmount, newLimitPrice, newOraclePriceOffset, newTriggerPrice, newTriggerCondition, auctionDuration, auctionStartPrice, auctionEndPrice, reduceOnly, postOnly, immediateOrCancel, maxTs, policy, }) {
2635
- const userAccountPublicKey = await this.getUserAccountPublicKey();
2640
+ async getModifyOrderIx({ orderId, newDirection, newBaseAmount, newLimitPrice, newOraclePriceOffset, newTriggerPrice, newTriggerCondition, auctionDuration, auctionStartPrice, auctionEndPrice, reduceOnly, postOnly, immediateOrCancel, maxTs, policy, }, subAccountId) {
2641
+ const user = await this.getUserAccountPublicKey(subAccountId);
2636
2642
  const remainingAccounts = this.getRemainingAccounts({
2637
- userAccounts: [this.getUserAccount()],
2643
+ userAccounts: [this.getUserAccount(subAccountId)],
2638
2644
  useMarketLastSlotCache: true,
2639
2645
  });
2640
2646
  const orderParams = {
@@ -2656,7 +2662,7 @@ class DriftClient {
2656
2662
  return await this.program.instruction.modifyOrder(orderId, orderParams, {
2657
2663
  accounts: {
2658
2664
  state: await this.getStatePublicKey(),
2659
- user: userAccountPublicKey,
2665
+ user,
2660
2666
  userStats: this.getUserStatsAccountPublicKey(),
2661
2667
  authority: this.wallet.publicKey,
2662
2668
  },
@@ -2681,14 +2687,14 @@ class DriftClient {
2681
2687
  * @param orderParams.maxTs:
2682
2688
  * @returns
2683
2689
  */
2684
- async modifyOrderByUserOrderId(orderParams, txParams) {
2685
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getModifyOrderByUserIdIx(orderParams), txParams), [], this.opts);
2690
+ async modifyOrderByUserOrderId(orderParams, txParams, subAccountId) {
2691
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getModifyOrderByUserIdIx(orderParams, subAccountId), txParams), [], this.opts);
2686
2692
  return txSig;
2687
2693
  }
2688
- async getModifyOrderByUserIdIx({ userOrderId, newDirection, newBaseAmount, newLimitPrice, newOraclePriceOffset, newTriggerPrice, newTriggerCondition, auctionDuration, auctionStartPrice, auctionEndPrice, reduceOnly, postOnly, immediateOrCancel, maxTs, policy, }) {
2689
- const userAccountPublicKey = await this.getUserAccountPublicKey();
2694
+ async getModifyOrderByUserIdIx({ userOrderId, newDirection, newBaseAmount, newLimitPrice, newOraclePriceOffset, newTriggerPrice, newTriggerCondition, auctionDuration, auctionStartPrice, auctionEndPrice, reduceOnly, postOnly, immediateOrCancel, maxTs, policy, }, subAccountId) {
2695
+ const user = await this.getUserAccountPublicKey(subAccountId);
2690
2696
  const remainingAccounts = this.getRemainingAccounts({
2691
- userAccounts: [this.getUserAccount()],
2697
+ userAccounts: [this.getUserAccount(subAccountId)],
2692
2698
  useMarketLastSlotCache: true,
2693
2699
  });
2694
2700
  const orderParams = {
@@ -2710,7 +2716,7 @@ class DriftClient {
2710
2716
  return await this.program.instruction.modifyOrderByUserId(userOrderId, orderParams, {
2711
2717
  accounts: {
2712
2718
  state: await this.getStatePublicKey(),
2713
- user: userAccountPublicKey,
2719
+ user,
2714
2720
  userStats: this.getUserStatsAccountPublicKey(),
2715
2721
  authority: this.wallet.publicKey,
2716
2722
  },
@@ -2752,17 +2758,17 @@ class DriftClient {
2752
2758
  remainingAccounts: remainingAccounts,
2753
2759
  });
2754
2760
  }
2755
- async liquidatePerp(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount, limitPrice, txParams) {
2756
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidatePerpIx(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount, limitPrice), txParams), [], this.opts);
2761
+ async liquidatePerp(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount, limitPrice, txParams, liquidatorSubAccountId) {
2762
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidatePerpIx(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount, limitPrice, liquidatorSubAccountId), txParams), [], this.opts);
2757
2763
  this.perpMarketLastSlotCache.set(marketIndex, slot);
2758
2764
  return txSig;
2759
2765
  }
2760
- async getLiquidatePerpIx(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount, limitPrice) {
2766
+ async getLiquidatePerpIx(userAccountPublicKey, userAccount, marketIndex, maxBaseAssetAmount, limitPrice, liquidatorSubAccountId) {
2761
2767
  const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
2762
- const liquidatorPublicKey = await this.getUserAccountPublicKey();
2768
+ const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
2763
2769
  const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
2764
2770
  const remainingAccounts = this.getRemainingAccounts({
2765
- userAccounts: [this.getUserAccount(), userAccount],
2771
+ userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
2766
2772
  useMarketLastSlotCache: true,
2767
2773
  writablePerpMarketIndexes: [marketIndex],
2768
2774
  });
@@ -2772,24 +2778,24 @@ class DriftClient {
2772
2778
  authority: this.wallet.publicKey,
2773
2779
  user: userAccountPublicKey,
2774
2780
  userStats: userStatsPublicKey,
2775
- liquidator: liquidatorPublicKey,
2781
+ liquidator,
2776
2782
  liquidatorStats: liquidatorStatsPublicKey,
2777
2783
  },
2778
2784
  remainingAccounts: remainingAccounts,
2779
2785
  });
2780
2786
  }
2781
- async liquidateSpot(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, txParams) {
2782
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidateSpotIx(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice), txParams), [], this.opts);
2787
+ async liquidateSpot(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, txParams, liquidatorSubAccountId) {
2788
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidateSpotIx(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, liquidatorSubAccountId), txParams), [], this.opts);
2783
2789
  this.spotMarketLastSlotCache.set(assetMarketIndex, slot);
2784
2790
  this.spotMarketLastSlotCache.set(liabilityMarketIndex, slot);
2785
2791
  return txSig;
2786
2792
  }
2787
- async getLiquidateSpotIx(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice) {
2793
+ async getLiquidateSpotIx(userAccountPublicKey, userAccount, assetMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, liquidatorSubAccountId) {
2788
2794
  const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
2789
- const liquidatorPublicKey = await this.getUserAccountPublicKey();
2790
- const liquidatorStatsPublicKey = await this.getUserStatsAccountPublicKey();
2795
+ const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
2796
+ const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
2791
2797
  const remainingAccounts = this.getRemainingAccounts({
2792
- userAccounts: [this.getUserAccount(), userAccount],
2798
+ userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
2793
2799
  useMarketLastSlotCache: true,
2794
2800
  writableSpotMarketIndexes: [liabilityMarketIndex, assetMarketIndex],
2795
2801
  });
@@ -2799,24 +2805,24 @@ class DriftClient {
2799
2805
  authority: this.wallet.publicKey,
2800
2806
  user: userAccountPublicKey,
2801
2807
  userStats: userStatsPublicKey,
2802
- liquidator: liquidatorPublicKey,
2808
+ liquidator,
2803
2809
  liquidatorStats: liquidatorStatsPublicKey,
2804
2810
  },
2805
2811
  remainingAccounts: remainingAccounts,
2806
2812
  });
2807
2813
  }
2808
- async liquidateBorrowForPerpPnl(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, txParams) {
2809
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice), txParams), [], this.opts);
2814
+ async liquidateBorrowForPerpPnl(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, txParams, liquidatorSubAccountId) {
2815
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, liquidatorSubAccountId), txParams), [], this.opts);
2810
2816
  this.perpMarketLastSlotCache.set(perpMarketIndex, slot);
2811
2817
  this.spotMarketLastSlotCache.set(liabilityMarketIndex, slot);
2812
2818
  return txSig;
2813
2819
  }
2814
- async getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice) {
2820
+ async getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilityMarketIndex, maxLiabilityTransfer, limitPrice, liquidatorSubAccountId) {
2815
2821
  const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
2816
- const liquidatorPublicKey = await this.getUserAccountPublicKey();
2817
- const liquidatorStatsPublicKey = await this.getUserStatsAccountPublicKey();
2822
+ const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
2823
+ const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
2818
2824
  const remainingAccounts = this.getRemainingAccounts({
2819
- userAccounts: [this.getUserAccount(), userAccount],
2825
+ userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
2820
2826
  writablePerpMarketIndexes: [perpMarketIndex],
2821
2827
  writableSpotMarketIndexes: [liabilityMarketIndex],
2822
2828
  });
@@ -2826,24 +2832,24 @@ class DriftClient {
2826
2832
  authority: this.wallet.publicKey,
2827
2833
  user: userAccountPublicKey,
2828
2834
  userStats: userStatsPublicKey,
2829
- liquidator: liquidatorPublicKey,
2835
+ liquidator,
2830
2836
  liquidatorStats: liquidatorStatsPublicKey,
2831
2837
  },
2832
2838
  remainingAccounts: remainingAccounts,
2833
2839
  });
2834
2840
  }
2835
- async liquidatePerpPnlForDeposit(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer, limitPrice, txParams) {
2836
- const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidatePerpPnlForDepositIx(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer, limitPrice), txParams), [], this.opts);
2841
+ async liquidatePerpPnlForDeposit(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer, limitPrice, txParams, liquidatorSubAccountId) {
2842
+ const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getLiquidatePerpPnlForDepositIx(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer, limitPrice, liquidatorSubAccountId), txParams), [], this.opts);
2837
2843
  this.perpMarketLastSlotCache.set(perpMarketIndex, slot);
2838
2844
  this.spotMarketLastSlotCache.set(assetMarketIndex, slot);
2839
2845
  return txSig;
2840
2846
  }
2841
- async getLiquidatePerpPnlForDepositIx(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer, limitPrice) {
2847
+ async getLiquidatePerpPnlForDepositIx(userAccountPublicKey, userAccount, perpMarketIndex, assetMarketIndex, maxPnlTransfer, limitPrice, liquidatorSubAccountId) {
2842
2848
  const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
2843
- const liquidatorPublicKey = await this.getUserAccountPublicKey();
2844
- const liquidatorStatsPublicKey = await this.getUserStatsAccountPublicKey();
2849
+ const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
2850
+ const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
2845
2851
  const remainingAccounts = this.getRemainingAccounts({
2846
- userAccounts: [this.getUserAccount(), userAccount],
2852
+ userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
2847
2853
  writablePerpMarketIndexes: [perpMarketIndex],
2848
2854
  writableSpotMarketIndexes: [assetMarketIndex],
2849
2855
  });
@@ -2853,22 +2859,22 @@ class DriftClient {
2853
2859
  authority: this.wallet.publicKey,
2854
2860
  user: userAccountPublicKey,
2855
2861
  userStats: userStatsPublicKey,
2856
- liquidator: liquidatorPublicKey,
2862
+ liquidator,
2857
2863
  liquidatorStats: liquidatorStatsPublicKey,
2858
2864
  },
2859
2865
  remainingAccounts: remainingAccounts,
2860
2866
  });
2861
2867
  }
2862
- async resolvePerpBankruptcy(userAccountPublicKey, userAccount, marketIndex, txParams) {
2863
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getResolvePerpBankruptcyIx(userAccountPublicKey, userAccount, marketIndex), txParams), [], this.opts);
2868
+ async resolvePerpBankruptcy(userAccountPublicKey, userAccount, marketIndex, txParams, liquidatorSubAccountId) {
2869
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getResolvePerpBankruptcyIx(userAccountPublicKey, userAccount, marketIndex, liquidatorSubAccountId), txParams), [], this.opts);
2864
2870
  return txSig;
2865
2871
  }
2866
- async getResolvePerpBankruptcyIx(userAccountPublicKey, userAccount, marketIndex) {
2872
+ async getResolvePerpBankruptcyIx(userAccountPublicKey, userAccount, marketIndex, liquidatorSubAccountId) {
2867
2873
  const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
2868
- const liquidatorPublicKey = await this.getUserAccountPublicKey();
2869
- const liquidatorStatsPublicKey = await this.getUserStatsAccountPublicKey();
2874
+ const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
2875
+ const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
2870
2876
  const remainingAccounts = this.getRemainingAccounts({
2871
- userAccounts: [this.getUserAccount(), userAccount],
2877
+ userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
2872
2878
  writablePerpMarketIndexes: [marketIndex],
2873
2879
  writableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
2874
2880
  });
@@ -2879,7 +2885,7 @@ class DriftClient {
2879
2885
  authority: this.wallet.publicKey,
2880
2886
  user: userAccountPublicKey,
2881
2887
  userStats: userStatsPublicKey,
2882
- liquidator: liquidatorPublicKey,
2888
+ liquidator,
2883
2889
  liquidatorStats: liquidatorStatsPublicKey,
2884
2890
  spotMarketVault: spotMarket.vault,
2885
2891
  insuranceFundVault: spotMarket.insuranceFund.vault,
@@ -2889,16 +2895,16 @@ class DriftClient {
2889
2895
  remainingAccounts: remainingAccounts,
2890
2896
  });
2891
2897
  }
2892
- async resolveSpotBankruptcy(userAccountPublicKey, userAccount, marketIndex, txParams) {
2893
- const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getResolveSpotBankruptcyIx(userAccountPublicKey, userAccount, marketIndex), txParams), [], this.opts);
2898
+ async resolveSpotBankruptcy(userAccountPublicKey, userAccount, marketIndex, txParams, liquidatorSubAccountId) {
2899
+ const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getResolveSpotBankruptcyIx(userAccountPublicKey, userAccount, marketIndex, liquidatorSubAccountId), txParams), [], this.opts);
2894
2900
  return txSig;
2895
2901
  }
2896
- async getResolveSpotBankruptcyIx(userAccountPublicKey, userAccount, marketIndex) {
2902
+ async getResolveSpotBankruptcyIx(userAccountPublicKey, userAccount, marketIndex, liquidatorSubAccountId) {
2897
2903
  const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
2898
- const liquidatorPublicKey = await this.getUserAccountPublicKey();
2899
- const liquidatorStatsPublicKey = await this.getUserStatsAccountPublicKey();
2904
+ const liquidator = await this.getUserAccountPublicKey(liquidatorSubAccountId);
2905
+ const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
2900
2906
  const remainingAccounts = this.getRemainingAccounts({
2901
- userAccounts: [this.getUserAccount(), userAccount],
2907
+ userAccounts: [this.getUserAccount(liquidatorSubAccountId), userAccount],
2902
2908
  writableSpotMarketIndexes: [marketIndex],
2903
2909
  });
2904
2910
  const spotMarket = this.getSpotMarketAccount(marketIndex);
@@ -2909,7 +2915,7 @@ class DriftClient {
2909
2915
  user: userAccountPublicKey,
2910
2916
  userStats: userStatsPublicKey,
2911
2917
  liquidatorStats: liquidatorStatsPublicKey,
2912
- liquidator: liquidatorPublicKey,
2918
+ liquidator,
2913
2919
  spotMarketVault: spotMarket.vault,
2914
2920
  insuranceFundVault: spotMarket.insuranceFund.vault,
2915
2921
  driftSigner: this.getSignerPublicKey(),