@drift-labs/sdk 2.77.0-beta.0 → 2.77.0-beta.2

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.
@@ -0,0 +1,126 @@
1
+ import {
2
+ AddressLookupTableAccount,
3
+ Connection,
4
+ RpcResponseAndContext,
5
+ SimulatedTransactionResponse,
6
+ TransactionInstruction,
7
+ TransactionVersion,
8
+ VersionedTransaction,
9
+ } from '@solana/web3.js';
10
+ import { BaseTxParams, ProcessingTxParams } from '..';
11
+
12
+ const COMPUTE_UNIT_BUFFER_FACTOR = 1.2;
13
+
14
+ const TEST_SIMS_ALWAYS_FAIL = false;
15
+
16
+ type TransactionProps = {
17
+ instructions: TransactionInstruction | TransactionInstruction[];
18
+ txParams?: BaseTxParams;
19
+ txVersion?: TransactionVersion;
20
+ lookupTables?: AddressLookupTableAccount[];
21
+ forceVersionedTransaction?: boolean;
22
+ };
23
+
24
+ /**
25
+ * This class is responsible for running through a "processing" pipeline for a base transaction, to adjust the standard transaction parameters based on a given configuration.
26
+ */
27
+ export class TransactionProcessor {
28
+ private static async getComputeUnitsFromSim(
29
+ txSim: RpcResponseAndContext<SimulatedTransactionResponse>
30
+ ) {
31
+ if (txSim?.value?.unitsConsumed) {
32
+ return txSim?.value?.unitsConsumed;
33
+ }
34
+
35
+ return undefined;
36
+ }
37
+
38
+ public static async getTxSimComputeUnits(
39
+ tx: VersionedTransaction,
40
+ connection: Connection
41
+ ): Promise<{ success: boolean; computeUnits: number }> {
42
+ try {
43
+ if (TEST_SIMS_ALWAYS_FAIL)
44
+ throw new Error('Test Error::SIMS_ALWAYS_FAIL');
45
+
46
+ const simTxResult = await connection.simulateTransaction(tx, {
47
+ replaceRecentBlockhash: true, // This is important to ensure that the blockhash is not too new.. Otherwise we will very often receive a "blockHashNotFound" error
48
+ });
49
+
50
+ if (simTxResult?.value?.err) {
51
+ throw new Error(simTxResult?.value?.err?.toString());
52
+ }
53
+
54
+ const computeUnits = await this.getComputeUnitsFromSim(simTxResult);
55
+
56
+ return {
57
+ success: true,
58
+ computeUnits: computeUnits,
59
+ };
60
+ } catch (e) {
61
+ console.warn(
62
+ `Failed to get Simulated Compute Units for txParamProcessor`,
63
+ e
64
+ );
65
+
66
+ return {
67
+ success: false,
68
+ computeUnits: undefined,
69
+ };
70
+ }
71
+ }
72
+
73
+ static async process(props: {
74
+ txProps: TransactionProps;
75
+ txBuilder: (
76
+ baseTransactionProps: TransactionProps
77
+ ) => Promise<VersionedTransaction>;
78
+ processConfig: ProcessingTxParams;
79
+ processParams: {
80
+ connection: Connection;
81
+ };
82
+ }): Promise<BaseTxParams> {
83
+ // # Exit early if no process config is provided
84
+ if (!props.processConfig || Object.keys(props.processConfig).length === 0) {
85
+ return props.txProps.txParams;
86
+ }
87
+
88
+ // # Setup
89
+ const {
90
+ txProps: txProps,
91
+ txBuilder: txBuilder,
92
+ processConfig,
93
+ processParams: processProps,
94
+ } = props;
95
+
96
+ const baseTransaction = await txBuilder(txProps);
97
+
98
+ const finalTxProps = {
99
+ ...txProps,
100
+ };
101
+
102
+ // # Run Processes
103
+ if (processConfig.useSimulatedComputeUnits) {
104
+ const txSimComputeUnitsResult = await this.getTxSimComputeUnits(
105
+ baseTransaction,
106
+ processProps.connection
107
+ );
108
+
109
+ if (txSimComputeUnitsResult.success) {
110
+ const bufferedComputeUnits =
111
+ txSimComputeUnitsResult.computeUnits *
112
+ (processConfig?.computeUnitsBufferMultiplier ??
113
+ COMPUTE_UNIT_BUFFER_FACTOR);
114
+
115
+ // Adjust the transaction based on the simulated compute units
116
+ finalTxProps.txParams = {
117
+ ...txProps.txParams,
118
+ computeUnits: bufferedComputeUnits,
119
+ };
120
+ }
121
+ }
122
+
123
+ // # Return Final Tx Params
124
+ return finalTxProps.txParams;
125
+ }
126
+ }
@@ -15,6 +15,7 @@ import { BaseTxSender } from './baseTxSender';
15
15
  import bs58 from 'bs58';
16
16
 
17
17
  const DEFAULT_RETRY = 2000;
18
+ const PLACEHOLDER_BLOCKHASH = 'Fdum64WVeej6DeL85REV9NvfSxEJNPZ74DBk7A8kTrKP';
18
19
 
19
20
  type ResolveReference = {
20
21
  resolve?: () => void;
@@ -103,11 +104,12 @@ export class WhileValidTxSender extends BaseTxSender {
103
104
  ixs: TransactionInstruction[],
104
105
  lookupTableAccounts: AddressLookupTableAccount[],
105
106
  _additionalSigners?: Array<Signer>,
106
- _opts?: ConfirmOptions
107
+ _opts?: ConfirmOptions,
108
+ blockhash?: string
107
109
  ): Promise<VersionedTransaction> {
108
110
  const message = new TransactionMessage({
109
111
  payerKey: this.wallet.publicKey,
110
- recentBlockhash: '', // set blank and reset in sendVersionTransaction
112
+ recentBlockhash: blockhash ?? PLACEHOLDER_BLOCKHASH, // set blank and reset in sendVersionTransaction
111
113
  instructions: ixs,
112
114
  }).compileToV0Message(lookupTableAccounts);
113
115
 
@@ -124,17 +126,18 @@ export class WhileValidTxSender extends BaseTxSender {
124
126
  extraConfirmationOptions?: ExtraConfirmationOptions
125
127
  ): Promise<TxSigAndSlot> {
126
128
  const latestBlockhash = await this.connection.getLatestBlockhash();
127
- tx.message.recentBlockhash = latestBlockhash.blockhash;
128
129
 
129
130
  let signedTx;
130
131
  if (preSigned) {
131
132
  signedTx = tx;
132
133
  // @ts-ignore
133
134
  } else if (this.wallet.payer) {
135
+ tx.message.recentBlockhash = latestBlockhash.blockhash;
134
136
  // @ts-ignore
135
137
  tx.sign((additionalSigners ?? []).concat(this.wallet.payer));
136
138
  signedTx = tx;
137
139
  } else {
140
+ tx.message.recentBlockhash = latestBlockhash.blockhash;
138
141
  additionalSigners
139
142
  ?.filter((s): s is Signer => s !== undefined)
140
143
  .forEach((kp) => {
package/src/types.ts CHANGED
@@ -999,11 +999,18 @@ export type ReferrerInfo = {
999
999
  referrerStats: PublicKey;
1000
1000
  };
1001
1001
 
1002
- export type TxParams = {
1002
+ export type BaseTxParams = {
1003
1003
  computeUnits?: number;
1004
1004
  computeUnitsPrice?: number;
1005
1005
  };
1006
1006
 
1007
+ export type ProcessingTxParams = {
1008
+ useSimulatedComputeUnits?: boolean;
1009
+ computeUnitsBufferMultiplier?: number;
1010
+ };
1011
+
1012
+ export type TxParams = BaseTxParams & ProcessingTxParams;
1013
+
1007
1014
  export class SwapReduceOnly {
1008
1015
  static readonly In = { in: {} };
1009
1016
  static readonly Out = { out: {} };
package/src/user.ts CHANGED
@@ -3123,8 +3123,13 @@ export class User {
3123
3123
  let makerFee =
3124
3124
  feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;
3125
3125
 
3126
- if (marketIndex !== undefined && isVariant(marketType, 'perp')) {
3127
- const marketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
3126
+ if (marketIndex !== undefined) {
3127
+ let marketAccount = null;
3128
+ if (isVariant(marketType, 'perp')) {
3129
+ marketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
3130
+ } else {
3131
+ marketAccount = this.driftClient.getSpotMarketAccount(marketIndex);
3132
+ }
3128
3133
  takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
3129
3134
  makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
3130
3135
  }