@drift-labs/sdk 2.43.0-beta.15 → 2.43.0-beta.17

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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.43.0-beta.15
1
+ 2.43.0-beta.17
@@ -33,6 +33,7 @@ export declare class AdminClient extends DriftClient {
33
33
  updateSpotFeeStructure(feeStructure: FeeStructure): Promise<TransactionSignature>;
34
34
  updateInitialPctToLiquidate(initialPctToLiquidate: number): Promise<TransactionSignature>;
35
35
  updateLiquidationDuration(liquidationDuration: number): Promise<TransactionSignature>;
36
+ updateLiquidationMarginBufferRatio(updateLiquidationMarginBufferRatio: number): Promise<TransactionSignature>;
36
37
  updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
37
38
  updateStateSettlementDuration(settlementDuration: number): Promise<TransactionSignature>;
38
39
  updateWithdrawGuardThreshold(spotMarketIndex: number, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
@@ -421,6 +421,14 @@ class AdminClient extends driftClient_1.DriftClient {
421
421
  },
422
422
  });
423
423
  }
424
+ async updateLiquidationMarginBufferRatio(updateLiquidationMarginBufferRatio) {
425
+ return await this.program.rpc.updateLiquidationMarginBufferRatio(updateLiquidationMarginBufferRatio, {
426
+ accounts: {
427
+ admin: this.wallet.publicKey,
428
+ state: await this.getStatePublicKey(),
429
+ },
430
+ });
431
+ }
424
432
  async updateOracleGuardRails(oracleGuardRails) {
425
433
  const tx = await this.program.transaction.updateOracleGuardRails(oracleGuardRails, {
426
434
  accounts: {
@@ -275,7 +275,7 @@ export declare class DriftClient {
275
275
  * @param makerInfo
276
276
  * @param txParams
277
277
  * @param bracketOrdersParams
278
- * @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same market. Intended use is to auto-cancel TP/SL orders when closing a position
278
+ * @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same perp market. Intended use is to auto-cancel TP/SL orders when closing a position. Ignored if orderParams.marketType is not MarketType.PERP
279
279
  * @returns
280
280
  */
281
281
  sendMarketOrderAndGetSignedFillTx(orderParams: OptionalOrderParams, userAccountPublicKey: PublicKey, userAccount: UserAccount, makerInfo?: MakerInfo | MakerInfo[], txParams?: TxParams, bracketOrdersParams?: OptionalOrderParams[], referrerInfo?: ReferrerInfo, cancelExistingOrders?: boolean): Promise<{
@@ -1366,7 +1366,7 @@ class DriftClient {
1366
1366
  * @param makerInfo
1367
1367
  * @param txParams
1368
1368
  * @param bracketOrdersParams
1369
- * @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same market. Intended use is to auto-cancel TP/SL orders when closing a position
1369
+ * @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same perp market. Intended use is to auto-cancel TP/SL orders when closing a position. Ignored if orderParams.marketType is not MarketType.PERP
1370
1370
  * @returns
1371
1371
  */
1372
1372
  async sendMarketOrderAndGetSignedFillTx(orderParams, userAccountPublicKey, userAccount, makerInfo, txParams, bracketOrdersParams = new Array(), referrerInfo, cancelExistingOrders) {
@@ -1384,7 +1384,7 @@ class DriftClient {
1384
1384
  }, makerInfo, referrerInfo);
1385
1385
  let cancelOrdersIx;
1386
1386
  let cancelExistingOrdersTx;
1387
- if (cancelExistingOrders) {
1387
+ if (cancelExistingOrders && (0, types_1.isVariant)(orderParams.marketType, 'perp')) {
1388
1388
  cancelOrdersIx = await this.getCancelOrdersIx(orderParams.marketType, orderParams.marketIndex, null);
1389
1389
  //@ts-ignore
1390
1390
  cancelExistingOrdersTx = await this.buildTransaction([cancelOrdersIx], txParams, this.txVersion);
@@ -1600,7 +1600,7 @@ class DriftClient {
1600
1600
  const userAccountPublicKey = await this.getUserAccountPublicKey();
1601
1601
  let readablePerpMarketIndex = undefined;
1602
1602
  let readableSpotMarketIndexes = undefined;
1603
- if (marketIndex) {
1603
+ if (typeof marketIndex === 'number') {
1604
1604
  if (marketType && (0, types_1.isVariant)(marketType, 'perp')) {
1605
1605
  readablePerpMarketIndex = marketIndex;
1606
1606
  }
@@ -2120,6 +2120,9 @@ class DriftClient {
2120
2120
  });
2121
2121
  quote = fetchedQuote;
2122
2122
  }
2123
+ if (!quote) {
2124
+ throw new Error("Could not fetch Jupiter's quote. Please try again.");
2125
+ }
2123
2126
  const transaction = await jupiterClient.getSwap({
2124
2127
  quote,
2125
2128
  userPublicKey: this.provider.wallet.publicKey,
@@ -4098,6 +4098,27 @@
4098
4098
  }
4099
4099
  ]
4100
4100
  },
4101
+ {
4102
+ "name": "updateLiquidationMarginBufferRatio",
4103
+ "accounts": [
4104
+ {
4105
+ "name": "admin",
4106
+ "isMut": false,
4107
+ "isSigner": true
4108
+ },
4109
+ {
4110
+ "name": "state",
4111
+ "isMut": true,
4112
+ "isSigner": false
4113
+ }
4114
+ ],
4115
+ "args": [
4116
+ {
4117
+ "name": "liquidationMarginBufferRatio",
4118
+ "type": "u32"
4119
+ }
4120
+ ]
4121
+ },
4101
4122
  {
4102
4123
  "name": "updateOracleGuardRails",
4103
4124
  "accounts": [
@@ -65,6 +65,9 @@ class JupiterClient {
65
65
  * @param slippageBps the slippage tolerance in basis points
66
66
  */
67
67
  async getSwap({ quote, userPublicKey, slippageBps = 50, }) {
68
+ if (!quote) {
69
+ throw new Error('Jupiter swap quote not provided. Please try again.');
70
+ }
68
71
  const resp = await (await (0, node_fetch_1.default)(`${this.url}/v6/swap`, {
69
72
  method: 'POST',
70
73
  headers: {
@@ -77,8 +80,13 @@ class JupiterClient {
77
80
  }),
78
81
  })).json();
79
82
  const { swapTransaction } = resp;
80
- const swapTransactionBuf = Buffer.from(swapTransaction, 'base64');
81
- return web3_js_1.VersionedTransaction.deserialize(swapTransactionBuf);
83
+ try {
84
+ const swapTransactionBuf = Buffer.from(swapTransaction, 'base64');
85
+ return web3_js_1.VersionedTransaction.deserialize(swapTransactionBuf);
86
+ }
87
+ catch (err) {
88
+ throw new Error('Something went wrong with creating the Jupiter swap transaction. Please try again.');
89
+ }
82
90
  }
83
91
  /**
84
92
  * ** @deprecated - use getSwap
package/lib/math/amm.js CHANGED
@@ -410,7 +410,8 @@ function calculateSpreadReserves(amm, oraclePriceData, now) {
410
410
  quoteAssetReserve: amm.quoteAssetReserve,
411
411
  };
412
412
  }
413
- const quoteAssetReserveDelta = amm.quoteAssetReserve.div(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(new anchor_1.BN(spread / 2)));
413
+ const spreadFraction = anchor_1.BN.max(new anchor_1.BN(spread / 2), numericConstants_1.ONE);
414
+ const quoteAssetReserveDelta = amm.quoteAssetReserve.div(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(spreadFraction));
414
415
  let quoteAssetReserve;
415
416
  if ((0, types_1.isVariant)(direction, 'long')) {
416
417
  quoteAssetReserve = amm.quoteAssetReserve.add(quoteAssetReserveDelta);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.43.0-beta.15",
3
+ "version": "2.43.0-beta.17",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -787,6 +787,20 @@ export class AdminClient extends DriftClient {
787
787
  );
788
788
  }
789
789
 
790
+ public async updateLiquidationMarginBufferRatio(
791
+ updateLiquidationMarginBufferRatio: number
792
+ ): Promise<TransactionSignature> {
793
+ return await this.program.rpc.updateLiquidationMarginBufferRatio(
794
+ updateLiquidationMarginBufferRatio,
795
+ {
796
+ accounts: {
797
+ admin: this.wallet.publicKey,
798
+ state: await this.getStatePublicKey(),
799
+ },
800
+ }
801
+ );
802
+ }
803
+
790
804
  public async updateOracleGuardRails(
791
805
  oracleGuardRails: OracleGuardRails
792
806
  ): Promise<TransactionSignature> {
@@ -2448,7 +2448,7 @@ export class DriftClient {
2448
2448
  * @param makerInfo
2449
2449
  * @param txParams
2450
2450
  * @param bracketOrdersParams
2451
- * @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same market. Intended use is to auto-cancel TP/SL orders when closing a position
2451
+ * @param cancelExistingOrders - Builds and returns an extra transaciton to cancel the existing orders in the same perp market. Intended use is to auto-cancel TP/SL orders when closing a position. Ignored if orderParams.marketType is not MarketType.PERP
2452
2452
  * @returns
2453
2453
  */
2454
2454
  public async sendMarketOrderAndGetSignedFillTx(
@@ -2491,7 +2491,7 @@ export class DriftClient {
2491
2491
 
2492
2492
  let cancelOrdersIx: TransactionInstruction;
2493
2493
  let cancelExistingOrdersTx: Transaction;
2494
- if (cancelExistingOrders) {
2494
+ if (cancelExistingOrders && isVariant(orderParams.marketType, 'perp')) {
2495
2495
  cancelOrdersIx = await this.getCancelOrdersIx(
2496
2496
  orderParams.marketType,
2497
2497
  orderParams.marketIndex,
@@ -2896,7 +2896,8 @@ export class DriftClient {
2896
2896
 
2897
2897
  let readablePerpMarketIndex = undefined;
2898
2898
  let readableSpotMarketIndexes = undefined;
2899
- if (marketIndex) {
2899
+
2900
+ if (typeof marketIndex === 'number') {
2900
2901
  if (marketType && isVariant(marketType, 'perp')) {
2901
2902
  readablePerpMarketIndex = marketIndex;
2902
2903
  } else if (marketType && isVariant(marketType, 'spot')) {
@@ -3733,6 +3734,10 @@ export class DriftClient {
3733
3734
  quote = fetchedQuote;
3734
3735
  }
3735
3736
 
3737
+ if (!quote) {
3738
+ throw new Error("Could not fetch Jupiter's quote. Please try again.");
3739
+ }
3740
+
3736
3741
  const transaction = await jupiterClient.getSwap({
3737
3742
  quote,
3738
3743
  userPublicKey: this.provider.wallet.publicKey,
@@ -4098,6 +4098,27 @@
4098
4098
  }
4099
4099
  ]
4100
4100
  },
4101
+ {
4102
+ "name": "updateLiquidationMarginBufferRatio",
4103
+ "accounts": [
4104
+ {
4105
+ "name": "admin",
4106
+ "isMut": false,
4107
+ "isSigner": true
4108
+ },
4109
+ {
4110
+ "name": "state",
4111
+ "isMut": true,
4112
+ "isSigner": false
4113
+ }
4114
+ ],
4115
+ "args": [
4116
+ {
4117
+ "name": "liquidationMarginBufferRatio",
4118
+ "type": "u32"
4119
+ }
4120
+ ]
4121
+ },
4101
4122
  {
4102
4123
  "name": "updateOracleGuardRails",
4103
4124
  "accounts": [
@@ -319,6 +319,10 @@ export class JupiterClient {
319
319
  userPublicKey: PublicKey;
320
320
  slippageBps?: number;
321
321
  }): Promise<VersionedTransaction> {
322
+ if (!quote) {
323
+ throw new Error('Jupiter swap quote not provided. Please try again.');
324
+ }
325
+
322
326
  const resp = await (
323
327
  await fetch(`${this.url}/v6/swap`, {
324
328
  method: 'POST',
@@ -334,8 +338,14 @@ export class JupiterClient {
334
338
  ).json();
335
339
  const { swapTransaction } = resp;
336
340
 
337
- const swapTransactionBuf = Buffer.from(swapTransaction, 'base64');
338
- return VersionedTransaction.deserialize(swapTransactionBuf);
341
+ try {
342
+ const swapTransactionBuf = Buffer.from(swapTransaction, 'base64');
343
+ return VersionedTransaction.deserialize(swapTransactionBuf);
344
+ } catch (err) {
345
+ throw new Error(
346
+ 'Something went wrong with creating the Jupiter swap transaction. Please try again.'
347
+ );
348
+ }
339
349
  }
340
350
 
341
351
  /**
package/src/math/amm.ts CHANGED
@@ -760,9 +760,9 @@ export function calculateSpreadReserves(
760
760
  quoteAssetReserve: amm.quoteAssetReserve,
761
761
  };
762
762
  }
763
-
763
+ const spreadFraction = BN.max(new BN(spread / 2), ONE);
764
764
  const quoteAssetReserveDelta = amm.quoteAssetReserve.div(
765
- BID_ASK_SPREAD_PRECISION.div(new BN(spread / 2))
765
+ BID_ASK_SPREAD_PRECISION.div(spreadFraction)
766
766
  );
767
767
 
768
768
  let quoteAssetReserve;