@drift-labs/sdk 2.45.0-beta.4 → 2.45.0-beta.6

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.
package/README.md CHANGED
@@ -57,7 +57,7 @@ The Drift SDK uses some common precisions, which are available as constants to i
57
57
 
58
58
  **Important Note for BigNum division**
59
59
 
60
- Because BN only supports integers, you need to be conscious of the numbers you are using when dividing. BN will return the floor when using the regular division function; if you want to get the exact divison, you need to add the modulus of the two numbers as well. There is a helper function `convertToNumber` in the SDK which will do this for you.
60
+ Because BN only supports integers, you need to be conscious of the numbers you are using when dividing. BN will return the floor when using the regular division function; if you want to get the exact division, you need to add the modulus of the two numbers as well. There is a helper function `convertToNumber` in the SDK which will do this for you.
61
61
 
62
62
  ```typescript
63
63
  import {convertToNumber} from @drift-labs/sdk
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.45.0-beta.4
1
+ 2.45.0-beta.6
@@ -36,6 +36,7 @@ export declare class AdminClient extends DriftClient {
36
36
  updateLiquidationMarginBufferRatio(updateLiquidationMarginBufferRatio: number): Promise<TransactionSignature>;
37
37
  updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
38
38
  updateStateSettlementDuration(settlementDuration: number): Promise<TransactionSignature>;
39
+ updateStateMaxNumberOfSubAccounts(maxNumberOfSubAccounts: number): Promise<TransactionSignature>;
39
40
  updateWithdrawGuardThreshold(spotMarketIndex: number, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
40
41
  updateSpotMarketIfFactor(spotMarketIndex: number, userIfFactor: BN, totalIfFactor: BN): Promise<TransactionSignature>;
41
42
  updateSpotMarketRevenueSettlePeriod(spotMarketIndex: number, revenueSettlePeriod: BN): Promise<TransactionSignature>;
@@ -447,6 +447,14 @@ class AdminClient extends driftClient_1.DriftClient {
447
447
  },
448
448
  });
449
449
  }
450
+ async updateStateMaxNumberOfSubAccounts(maxNumberOfSubAccounts) {
451
+ return await this.program.rpc.updateStateMaxNumberOfSubAccounts(maxNumberOfSubAccounts, {
452
+ accounts: {
453
+ admin: this.wallet.publicKey,
454
+ state: await this.getStatePublicKey(),
455
+ },
456
+ });
457
+ }
450
458
  async updateWithdrawGuardThreshold(spotMarketIndex, withdrawGuardThreshold) {
451
459
  const tx = await this.program.transaction.updateWithdrawGuardThreshold(withdrawGuardThreshold, {
452
460
  accounts: {
@@ -280,7 +280,7 @@ export declare class DriftClient {
280
280
  */
281
281
  sendMarketOrderAndGetSignedFillTx(orderParams: OptionalOrderParams, userAccountPublicKey: PublicKey, userAccount: UserAccount, makerInfo?: MakerInfo | MakerInfo[], txParams?: TxParams, bracketOrdersParams?: OptionalOrderParams[], referrerInfo?: ReferrerInfo, cancelExistingOrders?: boolean): Promise<{
282
282
  txSig: TransactionSignature;
283
- signedFillTx: Transaction;
283
+ signedFillTx?: Transaction;
284
284
  signedCancelExistingOrdersTx?: Transaction;
285
285
  }>;
286
286
  placePerpOrder(orderParams: OptionalOrderParams, txParams?: TxParams): Promise<TransactionSignature>;
@@ -1378,10 +1378,6 @@ class DriftClient {
1378
1378
  const placeBracketOrderIx = await this.getPlacePerpOrderIx(bracketOrderParams);
1379
1379
  bracketOrderIxs.push(placeBracketOrderIx);
1380
1380
  }
1381
- const fillPerpOrderIx = await this.getFillPerpOrderIx(userAccountPublicKey, userAccount, {
1382
- orderId,
1383
- marketIndex,
1384
- }, makerInfo, referrerInfo);
1385
1381
  let cancelOrdersIx;
1386
1382
  let cancelExistingOrdersTx;
1387
1383
  if (cancelExistingOrders && (0, types_1.isVariant)(orderParams.marketType, 'perp')) {
@@ -1392,6 +1388,10 @@ class DriftClient {
1392
1388
  // use versioned transactions if there is a lookup table account and wallet is compatible
1393
1389
  if (this.txVersion === 0) {
1394
1390
  const versionedMarketOrderTx = await this.buildTransaction([placePerpOrderIx].concat(bracketOrderIxs), txParams, 0);
1391
+ const fillPerpOrderIx = await this.getFillPerpOrderIx(userAccountPublicKey, userAccount, {
1392
+ orderId,
1393
+ marketIndex,
1394
+ }, makerInfo, referrerInfo);
1395
1395
  const versionedFillTx = await this.buildTransaction([fillPerpOrderIx], txParams, 0);
1396
1396
  const [signedVersionedMarketOrderTx, signedVersionedFillTx, signedCancelExistingOrdersTx,] = await this.provider.wallet.signAllTransactions([
1397
1397
  versionedMarketOrderTx,
@@ -1413,17 +1413,18 @@ class DriftClient {
1413
1413
  if (bracketOrderIxs.length > 0) {
1414
1414
  marketOrderTx.add(...bracketOrderIxs);
1415
1415
  }
1416
- const fillTx = (0, utils_1.wrapInTx)(fillPerpOrderIx, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits, txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice);
1417
1416
  // Apply the latest blockhash to the txs so that we can sign before sending them
1418
1417
  const currentBlockHash = (await this.connection.getLatestBlockhash('finalized')).blockhash;
1419
1418
  marketOrderTx.recentBlockhash = currentBlockHash;
1420
- fillTx.recentBlockhash = currentBlockHash;
1421
1419
  marketOrderTx.feePayer = userAccount.authority;
1422
- fillTx.feePayer = userAccount.authority;
1423
- const [signedMarketOrderTx, signedFillTx, signedCancelExistingOrdersTx] = await this.provider.wallet.signAllTransactions([marketOrderTx, fillTx, cancelExistingOrdersTx].filter((tx) => tx !== undefined));
1420
+ if (cancelExistingOrdersTx) {
1421
+ cancelExistingOrdersTx.recentBlockhash = currentBlockHash;
1422
+ cancelExistingOrdersTx.feePayer = userAccount.authority;
1423
+ }
1424
+ const [signedMarketOrderTx, signedCancelExistingOrdersTx] = await this.provider.wallet.signAllTransactions([marketOrderTx, cancelExistingOrdersTx].filter((tx) => tx !== undefined));
1424
1425
  const { txSig, slot } = await this.sendTransaction(signedMarketOrderTx, [], this.opts, true);
1425
1426
  this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
1426
- return { txSig, signedFillTx, signedCancelExistingOrdersTx };
1427
+ return { txSig, signedFillTx: undefined, signedCancelExistingOrdersTx };
1427
1428
  }
1428
1429
  }
1429
1430
  async placePerpOrder(orderParams, txParams) {
@@ -4163,6 +4163,27 @@
4163
4163
  }
4164
4164
  ]
4165
4165
  },
4166
+ {
4167
+ "name": "updateStateMaxNumberOfSubAccounts",
4168
+ "accounts": [
4169
+ {
4170
+ "name": "admin",
4171
+ "isMut": false,
4172
+ "isSigner": true
4173
+ },
4174
+ {
4175
+ "name": "state",
4176
+ "isMut": true,
4177
+ "isSigner": false
4178
+ }
4179
+ ],
4180
+ "args": [
4181
+ {
4182
+ "name": "maxNumberOfSubAccounts",
4183
+ "type": "u16"
4184
+ }
4185
+ ]
4186
+ },
4166
4187
  {
4167
4188
  "name": "updatePerpMarketOracle",
4168
4189
  "accounts": [
@@ -5722,12 +5743,16 @@
5722
5743
  "name": "initialPctToLiquidate",
5723
5744
  "type": "u16"
5724
5745
  },
5746
+ {
5747
+ "name": "maxNumberOfSubAccounts",
5748
+ "type": "u16"
5749
+ },
5725
5750
  {
5726
5751
  "name": "padding",
5727
5752
  "type": {
5728
5753
  "array": [
5729
5754
  "u8",
5730
- 14
5755
+ 12
5731
5756
  ]
5732
5757
  }
5733
5758
  }
@@ -10898,7 +10923,7 @@
10898
10923
  {
10899
10924
  "code": 6221,
10900
10925
  "name": "SpotFulfillmentConfigDisabled",
10901
- "msg": "Spot Fulfullment Config Disabled"
10926
+ "msg": "Spot Fulfillment Config Disabled"
10902
10927
  },
10903
10928
  {
10904
10929
  "code": 6222,
@@ -204,7 +204,21 @@ function isTriggered(order) {
204
204
  }
205
205
  exports.isTriggered = isTriggered;
206
206
  function isRestingLimitOrder(order, slot) {
207
- return (isLimitOrder(order) && (order.postOnly || (0, auction_1.isAuctionComplete)(order, slot)));
207
+ if (!isLimitOrder(order)) {
208
+ return false;
209
+ }
210
+ if ((0, types_1.isVariant)(order.orderType, 'triggerLimit')) {
211
+ if ((0, types_1.isVariant)(order.direction, 'long') &&
212
+ order.triggerPrice.lt(order.price)) {
213
+ return false;
214
+ }
215
+ else if ((0, types_1.isVariant)(order.direction, 'short') &&
216
+ order.triggerPrice.gt(order.price)) {
217
+ return false;
218
+ }
219
+ return (0, auction_1.isAuctionComplete)(order, slot);
220
+ }
221
+ return order.postOnly || (0, auction_1.isAuctionComplete)(order, slot);
208
222
  }
209
223
  exports.isRestingLimitOrder = isRestingLimitOrder;
210
224
  function isTakingOrder(order, slot) {
package/lib/types.d.ts CHANGED
@@ -620,6 +620,7 @@ export type StateAccount = {
620
620
  defaultSpotAuctionDuration: number;
621
621
  liquidationMarginBufferRatio: number;
622
622
  settlementDuration: number;
623
+ maxNumberOfSubAccounts: number;
623
624
  signer: PublicKey;
624
625
  signerNonce: number;
625
626
  srmVault: PublicKey;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.45.0-beta.4",
3
+ "version": "2.45.0-beta.6",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -832,6 +832,20 @@ export class AdminClient extends DriftClient {
832
832
  );
833
833
  }
834
834
 
835
+ public async updateStateMaxNumberOfSubAccounts(
836
+ maxNumberOfSubAccounts: number
837
+ ): Promise<TransactionSignature> {
838
+ return await this.program.rpc.updateStateMaxNumberOfSubAccounts(
839
+ maxNumberOfSubAccounts,
840
+ {
841
+ accounts: {
842
+ admin: this.wallet.publicKey,
843
+ state: await this.getStatePublicKey(),
844
+ },
845
+ }
846
+ );
847
+ }
848
+
835
849
  public async updateWithdrawGuardThreshold(
836
850
  spotMarketIndex: number,
837
851
  withdrawGuardThreshold: BN
@@ -2462,7 +2462,7 @@ export class DriftClient {
2462
2462
  cancelExistingOrders?: boolean
2463
2463
  ): Promise<{
2464
2464
  txSig: TransactionSignature;
2465
- signedFillTx: Transaction;
2465
+ signedFillTx?: Transaction;
2466
2466
  signedCancelExistingOrdersTx?: Transaction;
2467
2467
  }> {
2468
2468
  const marketIndex = orderParams.marketIndex;
@@ -2478,17 +2478,6 @@ export class DriftClient {
2478
2478
  bracketOrderIxs.push(placeBracketOrderIx);
2479
2479
  }
2480
2480
 
2481
- const fillPerpOrderIx = await this.getFillPerpOrderIx(
2482
- userAccountPublicKey,
2483
- userAccount,
2484
- {
2485
- orderId,
2486
- marketIndex,
2487
- },
2488
- makerInfo,
2489
- referrerInfo
2490
- );
2491
-
2492
2481
  let cancelOrdersIx: TransactionInstruction;
2493
2482
  let cancelExistingOrdersTx: Transaction;
2494
2483
  if (cancelExistingOrders && isVariant(orderParams.marketType, 'perp')) {
@@ -2513,6 +2502,18 @@ export class DriftClient {
2513
2502
  txParams,
2514
2503
  0
2515
2504
  );
2505
+
2506
+ const fillPerpOrderIx = await this.getFillPerpOrderIx(
2507
+ userAccountPublicKey,
2508
+ userAccount,
2509
+ {
2510
+ orderId,
2511
+ marketIndex,
2512
+ },
2513
+ makerInfo,
2514
+ referrerInfo
2515
+ );
2516
+
2516
2517
  const versionedFillTx = await this.buildTransaction(
2517
2518
  [fillPerpOrderIx],
2518
2519
  txParams,
@@ -2554,25 +2555,22 @@ export class DriftClient {
2554
2555
  marketOrderTx.add(...bracketOrderIxs);
2555
2556
  }
2556
2557
 
2557
- const fillTx = wrapInTx(
2558
- fillPerpOrderIx,
2559
- txParams?.computeUnits,
2560
- txParams?.computeUnitsPrice
2561
- );
2562
-
2563
2558
  // Apply the latest blockhash to the txs so that we can sign before sending them
2564
2559
  const currentBlockHash = (
2565
2560
  await this.connection.getLatestBlockhash('finalized')
2566
2561
  ).blockhash;
2567
2562
  marketOrderTx.recentBlockhash = currentBlockHash;
2568
- fillTx.recentBlockhash = currentBlockHash;
2569
2563
 
2570
2564
  marketOrderTx.feePayer = userAccount.authority;
2571
- fillTx.feePayer = userAccount.authority;
2572
2565
 
2573
- const [signedMarketOrderTx, signedFillTx, signedCancelExistingOrdersTx] =
2566
+ if (cancelExistingOrdersTx) {
2567
+ cancelExistingOrdersTx.recentBlockhash = currentBlockHash;
2568
+ cancelExistingOrdersTx.feePayer = userAccount.authority;
2569
+ }
2570
+
2571
+ const [signedMarketOrderTx, signedCancelExistingOrdersTx] =
2574
2572
  await this.provider.wallet.signAllTransactions(
2575
- [marketOrderTx, fillTx, cancelExistingOrdersTx].filter(
2573
+ [marketOrderTx, cancelExistingOrdersTx].filter(
2576
2574
  (tx) => tx !== undefined
2577
2575
  )
2578
2576
  );
@@ -2584,7 +2582,7 @@ export class DriftClient {
2584
2582
  );
2585
2583
  this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
2586
2584
 
2587
- return { txSig, signedFillTx, signedCancelExistingOrdersTx };
2585
+ return { txSig, signedFillTx: undefined, signedCancelExistingOrdersTx };
2588
2586
  }
2589
2587
  }
2590
2588
 
@@ -4163,6 +4163,27 @@
4163
4163
  }
4164
4164
  ]
4165
4165
  },
4166
+ {
4167
+ "name": "updateStateMaxNumberOfSubAccounts",
4168
+ "accounts": [
4169
+ {
4170
+ "name": "admin",
4171
+ "isMut": false,
4172
+ "isSigner": true
4173
+ },
4174
+ {
4175
+ "name": "state",
4176
+ "isMut": true,
4177
+ "isSigner": false
4178
+ }
4179
+ ],
4180
+ "args": [
4181
+ {
4182
+ "name": "maxNumberOfSubAccounts",
4183
+ "type": "u16"
4184
+ }
4185
+ ]
4186
+ },
4166
4187
  {
4167
4188
  "name": "updatePerpMarketOracle",
4168
4189
  "accounts": [
@@ -5722,12 +5743,16 @@
5722
5743
  "name": "initialPctToLiquidate",
5723
5744
  "type": "u16"
5724
5745
  },
5746
+ {
5747
+ "name": "maxNumberOfSubAccounts",
5748
+ "type": "u16"
5749
+ },
5725
5750
  {
5726
5751
  "name": "padding",
5727
5752
  "type": {
5728
5753
  "array": [
5729
5754
  "u8",
5730
- 14
5755
+ 12
5731
5756
  ]
5732
5757
  }
5733
5758
  }
@@ -10898,7 +10923,7 @@
10898
10923
  {
10899
10924
  "code": 6221,
10900
10925
  "name": "SpotFulfillmentConfigDisabled",
10901
- "msg": "Spot Fulfullment Config Disabled"
10926
+ "msg": "Spot Fulfillment Config Disabled"
10902
10927
  },
10903
10928
  {
10904
10929
  "code": 6222,
@@ -11071,4 +11096,4 @@
11071
11096
  "msg": "InvalidMarginCalculation"
11072
11097
  }
11073
11098
  ]
11074
- }
11099
+ }
@@ -322,9 +322,27 @@ export function isTriggered(order: Order): boolean {
322
322
  }
323
323
 
324
324
  export function isRestingLimitOrder(order: Order, slot: number): boolean {
325
- return (
326
- isLimitOrder(order) && (order.postOnly || isAuctionComplete(order, slot))
327
- );
325
+ if (!isLimitOrder(order)) {
326
+ return false;
327
+ }
328
+
329
+ if (isVariant(order.orderType, 'triggerLimit')) {
330
+ if (
331
+ isVariant(order.direction, 'long') &&
332
+ order.triggerPrice.lt(order.price)
333
+ ) {
334
+ return false;
335
+ } else if (
336
+ isVariant(order.direction, 'short') &&
337
+ order.triggerPrice.gt(order.price)
338
+ ) {
339
+ return false;
340
+ }
341
+
342
+ return isAuctionComplete(order, slot);
343
+ }
344
+
345
+ return order.postOnly || isAuctionComplete(order, slot);
328
346
  }
329
347
 
330
348
  export function isTakingOrder(order: Order, slot: number): boolean {
package/src/types.ts CHANGED
@@ -543,6 +543,7 @@ export type StateAccount = {
543
543
  defaultSpotAuctionDuration: number;
544
544
  liquidationMarginBufferRatio: number;
545
545
  settlementDuration: number;
546
+ maxNumberOfSubAccounts: number;
546
547
  signer: PublicKey;
547
548
  signerNonce: number;
548
549
  srmVault: PublicKey;
@@ -584,6 +584,7 @@ export const mockStateAccount: StateAccount = {
584
584
  },
585
585
  srmVault: PublicKey.default,
586
586
  whitelistMint: PublicKey.default,
587
+ maxNumberOfSubAccounts: 0,
587
588
  };
588
589
 
589
590
  export class MockUserMap implements UserMapInterface {
@@ -27,6 +27,7 @@ import {
27
27
 
28
28
  import { mockPerpMarkets, mockSpotMarkets, mockStateAccount } from './helpers';
29
29
  import { DLOBOrdersCoder } from '../../src/dlob/DLOBOrders';
30
+ import {isAuctionComplete, isRestingLimitOrder} from "../../lib";
30
31
 
31
32
  function insertOrderToDLOB(
32
33
  dlob: DLOB,
@@ -2504,6 +2505,52 @@ describe('DLOB Perp Tests', () => {
2504
2505
  }
2505
2506
  });
2506
2507
 
2508
+ it('Test trigger limit isnt maker', () => {
2509
+ const vAsk = new BN(15);
2510
+ const vBid = new BN(8);
2511
+
2512
+ const user0 = Keypair.generate();
2513
+
2514
+ const dlob = new DLOB();
2515
+ const marketIndex = 0;
2516
+
2517
+ const slot = 20;
2518
+ const oracle = {
2519
+ price: vBid.add(vAsk).div(new BN(2)),
2520
+ slot: new BN(slot),
2521
+ confidence: new BN(1),
2522
+ hasSufficientNumberOfDataPoints: true,
2523
+ };
2524
+
2525
+ // should trigger limit buy with above condition
2526
+ insertTriggerOrderToDLOB(
2527
+ dlob,
2528
+ user0.publicKey,
2529
+ OrderType.TRIGGER_LIMIT,
2530
+ MarketType.PERP,
2531
+ 1, //orderId
2532
+ marketIndex, // marketIndex
2533
+ oracle.price.add(new BN(1)), // price
2534
+ BASE_PRECISION, // baseAssetAmount: BN,
2535
+ PositionDirection.LONG,
2536
+ oracle.price.sub(new BN(1)), // triggerPrice: BN,
2537
+ OrderTriggerCondition.TRIGGERED_ABOVE, // triggerCondition: OrderTriggerCondition,
2538
+ vBid,
2539
+ vAsk,
2540
+ new BN(1), // slot
2541
+ );
2542
+
2543
+ const restingLimitBids = Array.from(dlob.getRestingLimitBids(marketIndex, slot, MarketType.PERP, oracle));
2544
+ expect(restingLimitBids.length).to.equal(0);
2545
+
2546
+ const takingBids = Array.from(dlob.getTakingBids(marketIndex, MarketType.PERP, slot, oracle));
2547
+ expect(takingBids.length).to.equal(1);
2548
+ const triggerLimitBid = takingBids[0];
2549
+ expect(isAuctionComplete(triggerLimitBid.order, slot)).to.equal(true);
2550
+ expect(isRestingLimitOrder(triggerLimitBid.order, slot)).to.equal(false);
2551
+
2552
+ });
2553
+
2507
2554
  it('Test will return expired market orders to fill', () => {
2508
2555
  const vAsk = new BN(15);
2509
2556
  const vBid = new BN(8);