@drift-labs/sdk 2.31.1-beta.12 → 2.31.1-beta.14

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.31.1-beta.12
1
+ 2.31.1-beta.14
@@ -92,7 +92,7 @@ function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now, })
92
92
  pegMultiplier: updatedAmm.pegMultiplier,
93
93
  };
94
94
  const getL2Bids = function* () {
95
- while (numBids < numOrders) {
95
+ while (numBids < numOrders && baseSize.gt(__1.ZERO)) {
96
96
  const [afterSwapQuoteReserves, afterSwapBaseReserves] = (0, __1.calculateAmmReservesAfterSwap)(bidAmm, 'base', baseSize, __1.SwapDirection.ADD);
97
97
  const quoteSwapped = (0, __1.calculateQuoteAssetAmountSwapped)(bidAmm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(), bidAmm.pegMultiplier, __1.SwapDirection.ADD);
98
98
  const price = quoteSwapped.mul(__1.BASE_PRECISION).div(baseSize);
@@ -115,7 +115,7 @@ function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now, })
115
115
  pegMultiplier: updatedAmm.pegMultiplier,
116
116
  };
117
117
  const getL2Asks = function* () {
118
- while (numAsks < numOrders) {
118
+ while (numAsks < numOrders && askSize.gt(__1.ZERO)) {
119
119
  const [afterSwapQuoteReserves, afterSwapBaseReserves] = (0, __1.calculateAmmReservesAfterSwap)(askAmm, 'base', askSize, __1.SwapDirection.REMOVE);
120
120
  const quoteSwapped = (0, __1.calculateQuoteAssetAmountSwapped)(askAmm.quoteAssetReserve.sub(afterSwapQuoteReserves).abs(), askAmm.pegMultiplier, __1.SwapDirection.REMOVE);
121
121
  const price = quoteSwapped.mul(__1.BASE_PRECISION).div(askSize);
@@ -116,6 +116,7 @@ export declare class DriftClient {
116
116
  initializeReferrerName(name: string): Promise<TransactionSignature>;
117
117
  updateUserName(name: string, subAccountId?: number): Promise<TransactionSignature>;
118
118
  updateUserCustomMarginRatio(marginRatio: number, subAccountId?: number): Promise<TransactionSignature>;
119
+ getUpdateUserMarginTradingEnabledIx(marginTradingEnabled: boolean, subAccountId?: number, userAccountPublicKey?: PublicKey): Promise<TransactionInstruction>;
119
120
  updateUserMarginTradingEnabled(marginTradingEnabled: boolean, subAccountId?: number): Promise<TransactionSignature>;
120
121
  updateUserDelegate(delegate: PublicKey, subAccountId?: number): Promise<TransactionSignature>;
121
122
  fetchAllUserAccounts(includeIdle?: boolean): Promise<ProgramAccount<UserAccount>[]>;
@@ -323,6 +324,22 @@ export declare class DriftClient {
323
324
  reduceOnly?: SwapReduceOnly;
324
325
  txParams?: TxParams;
325
326
  }): Promise<TransactionSignature>;
327
+ getJupiterSwapIx({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, userAccountPublicKey, }: {
328
+ jupiterClient: JupiterClient;
329
+ outMarketIndex: number;
330
+ inMarketIndex: number;
331
+ outAssociatedTokenAccount?: PublicKey;
332
+ inAssociatedTokenAccount?: PublicKey;
333
+ amount: BN;
334
+ slippageBps?: number;
335
+ swapMode?: SwapMode;
336
+ route?: Route;
337
+ reduceOnly?: SwapReduceOnly;
338
+ userAccountPublicKey?: PublicKey;
339
+ }): Promise<{
340
+ ixs: TransactionInstruction[];
341
+ lookupTables: AddressLookupTableAccount[];
342
+ }>;
326
343
  /**
327
344
  * Get the drift begin_swap and end_swap instructions
328
345
  *
@@ -332,8 +349,10 @@ export declare class DriftClient {
332
349
  * @param inTokenAccount the token account to move the tokens being sold
333
350
  * @param outTokenAccount the token account to receive the tokens being bought
334
351
  * @param limitPrice the limit price of the swap
352
+ * @param reduceOnly
353
+ * @param userAccountPublicKey optional, specify a custom userAccountPublicKey to use instead of getting the current user account; can be helpful if the account is being created within the current tx
335
354
  */
336
- getSwapIx({ outMarketIndex, inMarketIndex, amountIn, inTokenAccount, outTokenAccount, limitPrice, reduceOnly, }: {
355
+ getSwapIx({ outMarketIndex, inMarketIndex, amountIn, inTokenAccount, outTokenAccount, limitPrice, reduceOnly, userAccountPublicKey, }: {
337
356
  outMarketIndex: number;
338
357
  inMarketIndex: number;
339
358
  amountIn: BN;
@@ -341,6 +360,7 @@ export declare class DriftClient {
341
360
  outTokenAccount: PublicKey;
342
361
  limitPrice?: BN;
343
362
  reduceOnly?: SwapReduceOnly;
363
+ userAccountPublicKey?: PublicKey;
344
364
  }): Promise<{
345
365
  beginSwapIx: TransactionInstruction;
346
366
  endSwapIx: TransactionInstruction;
@@ -348,8 +368,9 @@ export declare class DriftClient {
348
368
  stakeForMSOL({ amount }: {
349
369
  amount: BN;
350
370
  }): Promise<TxSigAndSlot>;
351
- getStakeForMSOLIx({ amount, }: {
371
+ getStakeForMSOLIx({ amount, userAccountPublicKey, }: {
352
372
  amount: BN;
373
+ userAccountPublicKey?: PublicKey;
353
374
  }): Promise<TransactionInstruction[]>;
354
375
  triggerOrder(userAccountPublicKey: PublicKey, user: UserAccount, order: Order, txParams?: TxParams): Promise<TransactionSignature>;
355
376
  getTriggerOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order): Promise<TransactionInstruction>;
@@ -508,6 +508,27 @@ class DriftClient {
508
508
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
509
509
  return txSig;
510
510
  }
511
+ async getUpdateUserMarginTradingEnabledIx(marginTradingEnabled, subAccountId = 0, userAccountPublicKey) {
512
+ const userAccountPublicKeyToUse = userAccountPublicKey ||
513
+ (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, subAccountId);
514
+ await this.addUser(subAccountId, this.wallet.publicKey);
515
+ let remainingAccounts;
516
+ try {
517
+ remainingAccounts = this.getRemainingAccounts({
518
+ userAccounts: [this.getUserAccount(subAccountId)],
519
+ });
520
+ }
521
+ catch (err) {
522
+ remainingAccounts = [];
523
+ }
524
+ return await this.program.instruction.updateUserMarginTradingEnabled(subAccountId, marginTradingEnabled, {
525
+ accounts: {
526
+ user: userAccountPublicKeyToUse,
527
+ authority: this.wallet.publicKey,
528
+ },
529
+ remainingAccounts,
530
+ });
531
+ }
511
532
  async updateUserMarginTradingEnabled(marginTradingEnabled, subAccountId = 0) {
512
533
  const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, subAccountId);
513
534
  await this.addUser(subAccountId, this.wallet.publicKey);
@@ -1925,6 +1946,25 @@ class DriftClient {
1925
1946
  * @param txParams
1926
1947
  */
1927
1948
  async swap({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, txParams, }) {
1949
+ const { ixs, lookupTables } = await this.getJupiterSwapIx({
1950
+ jupiterClient,
1951
+ outMarketIndex,
1952
+ inMarketIndex,
1953
+ outAssociatedTokenAccount,
1954
+ inAssociatedTokenAccount,
1955
+ amount,
1956
+ slippageBps,
1957
+ swapMode,
1958
+ route,
1959
+ reduceOnly,
1960
+ });
1961
+ const tx = (await this.buildTransaction(ixs, txParams, 0, lookupTables));
1962
+ const { txSig, slot } = await this.sendTransaction(tx);
1963
+ this.spotMarketLastSlotCache.set(outMarketIndex, slot);
1964
+ this.spotMarketLastSlotCache.set(inMarketIndex, slot);
1965
+ return txSig;
1966
+ }
1967
+ async getJupiterSwapIx({ jupiterClient, outMarketIndex, inMarketIndex, outAssociatedTokenAccount, inAssociatedTokenAccount, amount, slippageBps, swapMode, route, reduceOnly, userAccountPublicKey, }) {
1928
1968
  const outMarket = this.getSpotMarketAccount(outMarketIndex);
1929
1969
  const inMarket = this.getSpotMarketAccount(inMarketIndex);
1930
1970
  if (!route) {
@@ -1975,18 +2015,15 @@ class DriftClient {
1975
2015
  inTokenAccount: inAssociatedTokenAccount,
1976
2016
  outTokenAccount: outAssociatedTokenAccount,
1977
2017
  reduceOnly,
2018
+ userAccountPublicKey,
1978
2019
  });
1979
- const instructions = [
2020
+ const ixs = [
1980
2021
  ...preInstructions,
1981
2022
  beginSwapIx,
1982
2023
  ...jupiterInstructions,
1983
2024
  endSwapIx,
1984
2025
  ];
1985
- const tx = (await this.buildTransaction(instructions, txParams, 0, lookupTables));
1986
- const { txSig, slot } = await this.sendTransaction(tx);
1987
- this.spotMarketLastSlotCache.set(outMarketIndex, slot);
1988
- this.spotMarketLastSlotCache.set(inMarketIndex, slot);
1989
- return txSig;
2026
+ return { ixs, lookupTables };
1990
2027
  }
1991
2028
  /**
1992
2029
  * Get the drift begin_swap and end_swap instructions
@@ -1997,11 +2034,14 @@ class DriftClient {
1997
2034
  * @param inTokenAccount the token account to move the tokens being sold
1998
2035
  * @param outTokenAccount the token account to receive the tokens being bought
1999
2036
  * @param limitPrice the limit price of the swap
2037
+ * @param reduceOnly
2038
+ * @param userAccountPublicKey optional, specify a custom userAccountPublicKey to use instead of getting the current user account; can be helpful if the account is being created within the current tx
2000
2039
  */
2001
- async getSwapIx({ outMarketIndex, inMarketIndex, amountIn, inTokenAccount, outTokenAccount, limitPrice, reduceOnly, }) {
2002
- const userAccountPublicKey = await this.getUserAccountPublicKey();
2040
+ async getSwapIx({ outMarketIndex, inMarketIndex, amountIn, inTokenAccount, outTokenAccount, limitPrice, reduceOnly, userAccountPublicKey, }) {
2041
+ const userAccountPublicKeyToUse = userAccountPublicKey || (await this.getUserAccountPublicKey());
2042
+ const userAccounts = this.hasUser() ? [this.getUserAccount()] : [];
2003
2043
  const remainingAccounts = this.getRemainingAccounts({
2004
- userAccounts: [this.getUserAccount()],
2044
+ userAccounts,
2005
2045
  writableSpotMarketIndexes: [outMarketIndex, inMarketIndex],
2006
2046
  });
2007
2047
  const outSpotMarket = this.getSpotMarketAccount(outMarketIndex);
@@ -2009,7 +2049,7 @@ class DriftClient {
2009
2049
  const beginSwapIx = await this.program.instruction.beginSwap(inMarketIndex, outMarketIndex, amountIn, {
2010
2050
  accounts: {
2011
2051
  state: await this.getStatePublicKey(),
2012
- user: userAccountPublicKey,
2052
+ user: userAccountPublicKeyToUse,
2013
2053
  userStats: this.getUserStatsAccountPublicKey(),
2014
2054
  authority: this.authority,
2015
2055
  outSpotMarketVault: outSpotMarket.vault,
@@ -2025,7 +2065,7 @@ class DriftClient {
2025
2065
  const endSwapIx = await this.program.instruction.endSwap(inMarketIndex, outMarketIndex, limitPrice !== null && limitPrice !== void 0 ? limitPrice : null, reduceOnly !== null && reduceOnly !== void 0 ? reduceOnly : null, {
2026
2066
  accounts: {
2027
2067
  state: await this.getStatePublicKey(),
2028
- user: userAccountPublicKey,
2068
+ user: userAccountPublicKeyToUse,
2029
2069
  userStats: this.getUserStatsAccountPublicKey(),
2030
2070
  authority: this.authority,
2031
2071
  outSpotMarketVault: outSpotMarket.vault,
@@ -2045,7 +2085,7 @@ class DriftClient {
2045
2085
  const tx = await this.buildTransaction(ixs);
2046
2086
  return this.sendTransaction(tx);
2047
2087
  }
2048
- async getStakeForMSOLIx({ amount, }) {
2088
+ async getStakeForMSOLIx({ amount, userAccountPublicKey, }) {
2049
2089
  const wSOLMint = this.getSpotMarketAccount(1).mint;
2050
2090
  const mSOLAccount = await this.getAssociatedTokenAccount(2);
2051
2091
  const wSOLAccount = await this.getAssociatedTokenAccount(1, false);
@@ -2058,6 +2098,7 @@ class DriftClient {
2058
2098
  amountIn: amount,
2059
2099
  inTokenAccount: wSOLAccount,
2060
2100
  outTokenAccount: mSOLAccount,
2101
+ userAccountPublicKey,
2061
2102
  });
2062
2103
  const program = (0, marinade_1.getMarinadeFinanceProgram)(this.provider);
2063
2104
  const depositIx = await (0, marinade_1.getMarinadeDepositIx)({
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.31.1-beta.11",
2
+ "version": "2.31.1-beta.13",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -0,0 +1,15 @@
1
+ import { AddressLookupTableAccount, PublicKey, TransactionInstruction } from '@solana/web3.js';
2
+ import { JupiterClient } from '../jupiter/jupiterClient';
3
+ import { DriftClient } from '../driftClient';
4
+ import { BN } from '@coral-xyz/anchor';
5
+ export declare function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userAccountPublicKey, }: {
6
+ amount: BN;
7
+ jupiterClient: JupiterClient;
8
+ driftClient: DriftClient;
9
+ userAccountPublicKey?: PublicKey;
10
+ }): Promise<{
11
+ ixs: TransactionInstruction[];
12
+ lookupTables: AddressLookupTableAccount[];
13
+ method: 'jupiter' | 'marinade';
14
+ price: number;
15
+ }>;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.findBestSuperStakeIxs = void 0;
4
+ const marinade_1 = require("../marinade");
5
+ async function findBestSuperStakeIxs({ amount, jupiterClient, driftClient, userAccountPublicKey, }) {
6
+ const marinadeProgram = (0, marinade_1.getMarinadeFinanceProgram)(driftClient.provider);
7
+ const marinadePrice = await (0, marinade_1.getMarinadeMSolPrice)(marinadeProgram);
8
+ const solMint = driftClient.getSpotMarketAccount(1).mint;
9
+ const mSOLMint = driftClient.getSpotMarketAccount(2).mint;
10
+ const jupiterRoutes = await jupiterClient.getRoutes({
11
+ inputMint: solMint,
12
+ outputMint: mSOLMint,
13
+ amount,
14
+ });
15
+ const bestRoute = jupiterRoutes[0];
16
+ const jupiterPrice = bestRoute.inAmount / bestRoute.outAmount;
17
+ if (marinadePrice <= jupiterPrice) {
18
+ const ixs = await driftClient.getStakeForMSOLIx({ amount });
19
+ return {
20
+ method: 'marinade',
21
+ ixs,
22
+ lookupTables: [],
23
+ price: marinadePrice,
24
+ };
25
+ }
26
+ else {
27
+ const { ixs, lookupTables } = await driftClient.getJupiterSwapIx({
28
+ inMarketIndex: 1,
29
+ outMarketIndex: 2,
30
+ route: bestRoute,
31
+ jupiterClient,
32
+ amount,
33
+ userAccountPublicKey,
34
+ });
35
+ return {
36
+ method: 'jupiter',
37
+ ixs,
38
+ lookupTables,
39
+ price: jupiterPrice,
40
+ };
41
+ }
42
+ }
43
+ exports.findBestSuperStakeIxs = findBestSuperStakeIxs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.31.1-beta.12",
3
+ "version": "2.31.1-beta.14",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -12,6 +12,7 @@ import {
12
12
  PositionDirection,
13
13
  standardizePrice,
14
14
  SwapDirection,
15
+ ZERO,
15
16
  } from '..';
16
17
  import { PublicKey } from '@solana/web3.js';
17
18
 
@@ -169,7 +170,7 @@ export function getVammL2Generator({
169
170
  pegMultiplier: updatedAmm.pegMultiplier,
170
171
  };
171
172
  const getL2Bids = function* () {
172
- while (numBids < numOrders) {
173
+ while (numBids < numOrders && baseSize.gt(ZERO)) {
173
174
  const [afterSwapQuoteReserves, afterSwapBaseReserves] =
174
175
  calculateAmmReservesAfterSwap(
175
176
  bidAmm,
@@ -208,7 +209,7 @@ export function getVammL2Generator({
208
209
  pegMultiplier: updatedAmm.pegMultiplier,
209
210
  };
210
211
  const getL2Asks = function* () {
211
- while (numAsks < numOrders) {
212
+ while (numAsks < numOrders && askSize.gt(ZERO)) {
212
213
  const [afterSwapQuoteReserves, afterSwapBaseReserves] =
213
214
  calculateAmmReservesAfterSwap(
214
215
  askAmm,
@@ -863,6 +863,43 @@ export class DriftClient {
863
863
  return txSig;
864
864
  }
865
865
 
866
+ public async getUpdateUserMarginTradingEnabledIx(
867
+ marginTradingEnabled: boolean,
868
+ subAccountId = 0,
869
+ userAccountPublicKey?: PublicKey
870
+ ): Promise<TransactionInstruction> {
871
+ const userAccountPublicKeyToUse =
872
+ userAccountPublicKey ||
873
+ getUserAccountPublicKeySync(
874
+ this.program.programId,
875
+ this.wallet.publicKey,
876
+ subAccountId
877
+ );
878
+
879
+ await this.addUser(subAccountId, this.wallet.publicKey);
880
+
881
+ let remainingAccounts;
882
+ try {
883
+ remainingAccounts = this.getRemainingAccounts({
884
+ userAccounts: [this.getUserAccount(subAccountId)],
885
+ });
886
+ } catch (err) {
887
+ remainingAccounts = [];
888
+ }
889
+
890
+ return await this.program.instruction.updateUserMarginTradingEnabled(
891
+ subAccountId,
892
+ marginTradingEnabled,
893
+ {
894
+ accounts: {
895
+ user: userAccountPublicKeyToUse,
896
+ authority: this.wallet.publicKey,
897
+ },
898
+ remainingAccounts,
899
+ }
900
+ );
901
+ }
902
+
866
903
  public async updateUserMarginTradingEnabled(
867
904
  marginTradingEnabled: boolean,
868
905
  subAccountId = 0
@@ -3351,6 +3388,61 @@ export class DriftClient {
3351
3388
  reduceOnly?: SwapReduceOnly;
3352
3389
  txParams?: TxParams;
3353
3390
  }): Promise<TransactionSignature> {
3391
+ const { ixs, lookupTables } = await this.getJupiterSwapIx({
3392
+ jupiterClient,
3393
+ outMarketIndex,
3394
+ inMarketIndex,
3395
+ outAssociatedTokenAccount,
3396
+ inAssociatedTokenAccount,
3397
+ amount,
3398
+ slippageBps,
3399
+ swapMode,
3400
+ route,
3401
+ reduceOnly,
3402
+ });
3403
+
3404
+ const tx = (await this.buildTransaction(
3405
+ ixs,
3406
+ txParams,
3407
+ 0,
3408
+ lookupTables
3409
+ )) as VersionedTransaction;
3410
+
3411
+ const { txSig, slot } = await this.sendTransaction(tx);
3412
+ this.spotMarketLastSlotCache.set(outMarketIndex, slot);
3413
+ this.spotMarketLastSlotCache.set(inMarketIndex, slot);
3414
+
3415
+ return txSig;
3416
+ }
3417
+
3418
+ public async getJupiterSwapIx({
3419
+ jupiterClient,
3420
+ outMarketIndex,
3421
+ inMarketIndex,
3422
+ outAssociatedTokenAccount,
3423
+ inAssociatedTokenAccount,
3424
+ amount,
3425
+ slippageBps,
3426
+ swapMode,
3427
+ route,
3428
+ reduceOnly,
3429
+ userAccountPublicKey,
3430
+ }: {
3431
+ jupiterClient: JupiterClient;
3432
+ outMarketIndex: number;
3433
+ inMarketIndex: number;
3434
+ outAssociatedTokenAccount?: PublicKey;
3435
+ inAssociatedTokenAccount?: PublicKey;
3436
+ amount: BN;
3437
+ slippageBps?: number;
3438
+ swapMode?: SwapMode;
3439
+ route?: Route;
3440
+ reduceOnly?: SwapReduceOnly;
3441
+ userAccountPublicKey?: PublicKey;
3442
+ }): Promise<{
3443
+ ixs: TransactionInstruction[];
3444
+ lookupTables: AddressLookupTableAccount[];
3445
+ }> {
3354
3446
  const outMarket = this.getSpotMarketAccount(outMarketIndex);
3355
3447
  const inMarket = this.getSpotMarketAccount(inMarketIndex);
3356
3448
 
@@ -3437,27 +3529,17 @@ export class DriftClient {
3437
3529
  inTokenAccount: inAssociatedTokenAccount,
3438
3530
  outTokenAccount: outAssociatedTokenAccount,
3439
3531
  reduceOnly,
3532
+ userAccountPublicKey,
3440
3533
  });
3441
3534
 
3442
- const instructions = [
3535
+ const ixs = [
3443
3536
  ...preInstructions,
3444
3537
  beginSwapIx,
3445
3538
  ...jupiterInstructions,
3446
3539
  endSwapIx,
3447
3540
  ];
3448
3541
 
3449
- const tx = (await this.buildTransaction(
3450
- instructions,
3451
- txParams,
3452
- 0,
3453
- lookupTables
3454
- )) as VersionedTransaction;
3455
-
3456
- const { txSig, slot } = await this.sendTransaction(tx);
3457
- this.spotMarketLastSlotCache.set(outMarketIndex, slot);
3458
- this.spotMarketLastSlotCache.set(inMarketIndex, slot);
3459
-
3460
- return txSig;
3542
+ return { ixs, lookupTables };
3461
3543
  }
3462
3544
 
3463
3545
  /**
@@ -3469,6 +3551,8 @@ export class DriftClient {
3469
3551
  * @param inTokenAccount the token account to move the tokens being sold
3470
3552
  * @param outTokenAccount the token account to receive the tokens being bought
3471
3553
  * @param limitPrice the limit price of the swap
3554
+ * @param reduceOnly
3555
+ * @param userAccountPublicKey optional, specify a custom userAccountPublicKey to use instead of getting the current user account; can be helpful if the account is being created within the current tx
3472
3556
  */
3473
3557
  public async getSwapIx({
3474
3558
  outMarketIndex,
@@ -3478,6 +3562,7 @@ export class DriftClient {
3478
3562
  outTokenAccount,
3479
3563
  limitPrice,
3480
3564
  reduceOnly,
3565
+ userAccountPublicKey,
3481
3566
  }: {
3482
3567
  outMarketIndex: number;
3483
3568
  inMarketIndex: number;
@@ -3486,14 +3571,17 @@ export class DriftClient {
3486
3571
  outTokenAccount: PublicKey;
3487
3572
  limitPrice?: BN;
3488
3573
  reduceOnly?: SwapReduceOnly;
3574
+ userAccountPublicKey?: PublicKey;
3489
3575
  }): Promise<{
3490
3576
  beginSwapIx: TransactionInstruction;
3491
3577
  endSwapIx: TransactionInstruction;
3492
3578
  }> {
3493
- const userAccountPublicKey = await this.getUserAccountPublicKey();
3579
+ const userAccountPublicKeyToUse =
3580
+ userAccountPublicKey || (await this.getUserAccountPublicKey());
3494
3581
 
3582
+ const userAccounts = this.hasUser() ? [this.getUserAccount()] : [];
3495
3583
  const remainingAccounts = this.getRemainingAccounts({
3496
- userAccounts: [this.getUserAccount()],
3584
+ userAccounts,
3497
3585
  writableSpotMarketIndexes: [outMarketIndex, inMarketIndex],
3498
3586
  });
3499
3587
 
@@ -3507,7 +3595,7 @@ export class DriftClient {
3507
3595
  {
3508
3596
  accounts: {
3509
3597
  state: await this.getStatePublicKey(),
3510
- user: userAccountPublicKey,
3598
+ user: userAccountPublicKeyToUse,
3511
3599
  userStats: this.getUserStatsAccountPublicKey(),
3512
3600
  authority: this.authority,
3513
3601
  outSpotMarketVault: outSpotMarket.vault,
@@ -3530,7 +3618,7 @@ export class DriftClient {
3530
3618
  {
3531
3619
  accounts: {
3532
3620
  state: await this.getStatePublicKey(),
3533
- user: userAccountPublicKey,
3621
+ user: userAccountPublicKeyToUse,
3534
3622
  userStats: this.getUserStatsAccountPublicKey(),
3535
3623
  authority: this.authority,
3536
3624
  outSpotMarketVault: outSpotMarket.vault,
@@ -3556,8 +3644,10 @@ export class DriftClient {
3556
3644
 
3557
3645
  public async getStakeForMSOLIx({
3558
3646
  amount,
3647
+ userAccountPublicKey,
3559
3648
  }: {
3560
3649
  amount: BN;
3650
+ userAccountPublicKey?: PublicKey;
3561
3651
  }): Promise<TransactionInstruction[]> {
3562
3652
  const wSOLMint = this.getSpotMarketAccount(1).mint;
3563
3653
  const mSOLAccount = await this.getAssociatedTokenAccount(2);
@@ -3585,6 +3675,7 @@ export class DriftClient {
3585
3675
  amountIn: amount,
3586
3676
  inTokenAccount: wSOLAccount,
3587
3677
  outTokenAccount: mSOLAccount,
3678
+ userAccountPublicKey,
3588
3679
  });
3589
3680
 
3590
3681
  const program = getMarinadeFinanceProgram(this.provider);
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.31.1-beta.12",
2
+ "version": "2.31.1-beta.14",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -0,0 +1,65 @@
1
+ import {
2
+ AddressLookupTableAccount,
3
+ PublicKey,
4
+ TransactionInstruction,
5
+ } from '@solana/web3.js';
6
+ import { JupiterClient } from '../jupiter/jupiterClient';
7
+ import { DriftClient } from '../driftClient';
8
+ import { getMarinadeFinanceProgram, getMarinadeMSolPrice } from '../marinade';
9
+ import { BN } from '@coral-xyz/anchor';
10
+
11
+ export async function findBestSuperStakeIxs({
12
+ amount,
13
+ jupiterClient,
14
+ driftClient,
15
+ userAccountPublicKey,
16
+ }: {
17
+ amount: BN;
18
+ jupiterClient: JupiterClient;
19
+ driftClient: DriftClient;
20
+ userAccountPublicKey?: PublicKey;
21
+ }): Promise<{
22
+ ixs: TransactionInstruction[];
23
+ lookupTables: AddressLookupTableAccount[];
24
+ method: 'jupiter' | 'marinade';
25
+ price: number;
26
+ }> {
27
+ const marinadeProgram = getMarinadeFinanceProgram(driftClient.provider);
28
+ const marinadePrice = await getMarinadeMSolPrice(marinadeProgram);
29
+
30
+ const solMint = driftClient.getSpotMarketAccount(1).mint;
31
+ const mSOLMint = driftClient.getSpotMarketAccount(2).mint;
32
+ const jupiterRoutes = await jupiterClient.getRoutes({
33
+ inputMint: solMint,
34
+ outputMint: mSOLMint,
35
+ amount,
36
+ });
37
+
38
+ const bestRoute = jupiterRoutes[0];
39
+ const jupiterPrice = bestRoute.inAmount / bestRoute.outAmount;
40
+
41
+ if (marinadePrice <= jupiterPrice) {
42
+ const ixs = await driftClient.getStakeForMSOLIx({ amount });
43
+ return {
44
+ method: 'marinade',
45
+ ixs,
46
+ lookupTables: [],
47
+ price: marinadePrice,
48
+ };
49
+ } else {
50
+ const { ixs, lookupTables } = await driftClient.getJupiterSwapIx({
51
+ inMarketIndex: 1,
52
+ outMarketIndex: 2,
53
+ route: bestRoute,
54
+ jupiterClient,
55
+ amount,
56
+ userAccountPublicKey,
57
+ });
58
+ return {
59
+ method: 'jupiter',
60
+ ixs,
61
+ lookupTables,
62
+ price: jupiterPrice,
63
+ };
64
+ }
65
+ }