@drift-labs/sdk 2.82.0-beta.2 → 2.82.0-beta.20

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.
Files changed (127) hide show
  1. package/README.md +65 -47
  2. package/VERSION +1 -1
  3. package/lib/accounts/types.d.ts +4 -3
  4. package/lib/accounts/webSocketAccountSubscriber.d.ts +3 -3
  5. package/lib/accounts/webSocketAccountSubscriber.js +16 -9
  6. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +3 -3
  7. package/lib/accounts/webSocketDriftClientAccountSubscriber.js +5 -5
  8. package/lib/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +2 -2
  9. package/lib/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +5 -3
  10. package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +3 -3
  11. package/lib/accounts/webSocketProgramAccountSubscriber.js +15 -9
  12. package/lib/accounts/webSocketUserAccountSubscriber.d.ts +3 -3
  13. package/lib/accounts/webSocketUserAccountSubscriber.js +3 -3
  14. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +3 -3
  15. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +3 -3
  16. package/lib/auctionSubscriber/auctionSubscriber.d.ts +2 -2
  17. package/lib/auctionSubscriber/auctionSubscriber.js +3 -3
  18. package/lib/auctionSubscriber/types.d.ts +1 -0
  19. package/lib/clock/clockSubscriber.d.ts +29 -0
  20. package/lib/clock/clockSubscriber.js +74 -0
  21. package/lib/constants/perpMarkets.js +2 -2
  22. package/lib/constants/spotMarkets.js +11 -0
  23. package/lib/dlob/DLOB.js +2 -2
  24. package/lib/dlob/orderBookLevels.js +1 -0
  25. package/lib/driftClient.d.ts +21 -14
  26. package/lib/driftClient.js +192 -255
  27. package/lib/driftClientConfig.d.ts +3 -0
  28. package/lib/index.d.ts +2 -0
  29. package/lib/index.js +2 -0
  30. package/lib/jupiter/jupiterClient.d.ts +2 -1
  31. package/lib/jupiter/jupiterClient.js +10 -6
  32. package/lib/math/exchangeStatus.d.ts +2 -2
  33. package/lib/math/orders.d.ts +1 -1
  34. package/lib/math/orders.js +2 -2
  35. package/lib/orderSubscriber/OrderSubscriber.js +6 -3
  36. package/lib/orderSubscriber/WebsocketSubscription.d.ts +4 -3
  37. package/lib/orderSubscriber/WebsocketSubscription.js +3 -3
  38. package/lib/orderSubscriber/types.d.ts +1 -0
  39. package/lib/priorityFee/driftPriorityFeeMethod.d.ts +13 -3
  40. package/lib/priorityFee/driftPriorityFeeMethod.js +2 -2
  41. package/lib/priorityFee/index.d.ts +2 -0
  42. package/lib/priorityFee/index.js +2 -0
  43. package/lib/priorityFee/priorityFeeSubscriber.d.ts +1 -4
  44. package/lib/priorityFee/priorityFeeSubscriber.js +5 -4
  45. package/lib/priorityFee/priorityFeeSubscriberMap.d.ts +48 -0
  46. package/lib/priorityFee/priorityFeeSubscriberMap.js +88 -0
  47. package/lib/priorityFee/types.d.ts +8 -3
  48. package/lib/priorityFee/types.js +2 -1
  49. package/lib/tx/baseTxSender.d.ts +8 -6
  50. package/lib/tx/baseTxSender.js +9 -51
  51. package/lib/tx/fastSingleTxSender.d.ts +6 -6
  52. package/lib/tx/fastSingleTxSender.js +3 -31
  53. package/lib/tx/forwardOnlyTxSender.d.ts +4 -2
  54. package/lib/tx/forwardOnlyTxSender.js +2 -1
  55. package/lib/tx/retryTxSender.d.ts +4 -2
  56. package/lib/tx/retryTxSender.js +2 -1
  57. package/lib/tx/txHandler.d.ts +138 -0
  58. package/lib/tx/txHandler.js +396 -0
  59. package/lib/tx/txParamProcessor.d.ts +6 -10
  60. package/lib/tx/txParamProcessor.js +13 -17
  61. package/lib/tx/types.d.ts +8 -7
  62. package/lib/tx/types.js +12 -1
  63. package/lib/tx/whileValidTxSender.d.ts +7 -6
  64. package/lib/tx/whileValidTxSender.js +7 -28
  65. package/lib/types.d.ts +24 -4
  66. package/lib/types.js +10 -1
  67. package/lib/user.d.ts +0 -10
  68. package/lib/user.js +6 -29
  69. package/lib/userConfig.d.ts +1 -0
  70. package/lib/userMap/WebsocketSubscription.d.ts +4 -3
  71. package/lib/userMap/WebsocketSubscription.js +3 -3
  72. package/lib/userMap/userMap.js +4 -1
  73. package/lib/userMap/userMapConfig.d.ts +1 -0
  74. package/lib/userStats.js +6 -3
  75. package/lib/userStatsConfig.d.ts +1 -0
  76. package/lib/util/chainClock.d.ts +17 -0
  77. package/lib/util/chainClock.js +29 -0
  78. package/package.json +3 -3
  79. package/src/accounts/types.ts +5 -4
  80. package/src/accounts/webSocketAccountSubscriber.ts +35 -23
  81. package/src/accounts/webSocketDriftClientAccountSubscriber.ts +7 -6
  82. package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +6 -4
  83. package/src/accounts/webSocketProgramAccountSubscriber.ts +32 -22
  84. package/src/accounts/webSocketUserAccountSubscriber.ts +5 -4
  85. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +5 -4
  86. package/src/auctionSubscriber/auctionSubscriber.ts +10 -4
  87. package/src/auctionSubscriber/types.ts +1 -0
  88. package/src/clock/clockSubscriber.ts +113 -0
  89. package/src/constants/perpMarkets.ts +2 -2
  90. package/src/constants/spotMarkets.ts +13 -0
  91. package/src/dlob/DLOB.ts +2 -2
  92. package/src/dlob/orderBookLevels.ts +2 -0
  93. package/src/driftClient.ts +295 -386
  94. package/src/driftClientConfig.ts +3 -0
  95. package/src/index.ts +2 -0
  96. package/src/jupiter/jupiterClient.ts +15 -6
  97. package/src/math/exchangeStatus.ts +2 -1
  98. package/src/math/orders.ts +3 -2
  99. package/src/orderSubscriber/OrderSubscriber.ts +4 -1
  100. package/src/orderSubscriber/WebsocketSubscription.ts +6 -5
  101. package/src/orderSubscriber/types.ts +1 -0
  102. package/src/priorityFee/driftPriorityFeeMethod.ts +16 -4
  103. package/src/priorityFee/index.ts +2 -0
  104. package/src/priorityFee/priorityFeeSubscriber.ts +7 -7
  105. package/src/priorityFee/priorityFeeSubscriberMap.ts +112 -0
  106. package/src/priorityFee/types.ts +16 -3
  107. package/src/tx/baseTxSender.ts +29 -79
  108. package/src/tx/fastSingleTxSender.ts +10 -55
  109. package/src/tx/forwardOnlyTxSender.ts +5 -1
  110. package/src/tx/retryTxSender.ts +5 -1
  111. package/src/tx/txHandler.ts +625 -0
  112. package/src/tx/txParamProcessor.ts +16 -28
  113. package/src/tx/types.ts +14 -18
  114. package/src/tx/whileValidTxSender.ts +24 -48
  115. package/src/types.ts +26 -2
  116. package/src/user.ts +7 -32
  117. package/src/userConfig.ts +1 -0
  118. package/src/userMap/WebsocketSubscription.ts +6 -5
  119. package/src/userMap/userMap.ts +4 -1
  120. package/src/userMap/userMapConfig.ts +1 -0
  121. package/src/userStats.ts +4 -1
  122. package/src/userStatsConfig.ts +1 -0
  123. package/src/util/chainClock.ts +41 -0
  124. package/tests/dlob/helpers.ts +3 -0
  125. package/lib/tx/utils.d.ts +0 -6
  126. package/lib/tx/utils.js +0 -39
  127. package/src/tx/utils.ts +0 -64
@@ -1,10 +1,7 @@
1
1
  import {
2
- AddressLookupTableAccount,
3
2
  Connection,
4
3
  RpcResponseAndContext,
5
4
  SimulatedTransactionResponse,
6
- TransactionInstruction,
7
- TransactionVersion,
8
5
  VersionedTransaction,
9
6
  } from '@solana/web3.js';
10
7
  import { BaseTxParams, ProcessingTxParams } from '..';
@@ -13,18 +10,14 @@ const COMPUTE_UNIT_BUFFER_FACTOR = 1.2;
13
10
 
14
11
  const TEST_SIMS_ALWAYS_FAIL = false;
15
12
 
16
- type TransactionProps = {
17
- instructions: TransactionInstruction | TransactionInstruction[];
18
- txParams?: BaseTxParams;
19
- txVersion?: TransactionVersion;
20
- lookupTables?: AddressLookupTableAccount[];
21
- forceVersionedTransaction?: boolean;
13
+ type TransactionBuildingProps = {
14
+ txParams: BaseTxParams;
22
15
  };
23
16
 
24
17
  /**
25
18
  * 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
19
  */
27
- export class TransactionProcessor {
20
+ export class TransactionParamProcessor {
28
21
  private static async getComputeUnitsFromSim(
29
22
  txSim: RpcResponseAndContext<SimulatedTransactionResponse>
30
23
  ) {
@@ -71,37 +64,35 @@ export class TransactionProcessor {
71
64
  }
72
65
 
73
66
  static async process(props: {
74
- txProps: TransactionProps;
75
- txBuilder: (
76
- baseTransactionProps: TransactionProps
77
- ) => Promise<VersionedTransaction>;
67
+ baseTxParams: BaseTxParams;
78
68
  processConfig: ProcessingTxParams;
79
69
  processParams: {
80
70
  connection: Connection;
81
71
  };
72
+ txBuilder: (
73
+ baseTransactionProps: TransactionBuildingProps
74
+ ) => Promise<VersionedTransaction>;
82
75
  }): Promise<BaseTxParams> {
83
76
  // # Exit early if no process config is provided
84
77
  if (!props.processConfig || Object.keys(props.processConfig).length === 0) {
85
- return props.txProps.txParams;
78
+ return props.baseTxParams;
86
79
  }
87
80
 
88
81
  // # Setup
89
82
  const {
90
- txProps: txProps,
91
83
  txBuilder: txBuilder,
92
84
  processConfig,
93
85
  processParams: processProps,
94
86
  } = props;
95
87
 
96
- const finalTxProps = {
97
- ...txProps,
88
+ const finalTxParams: BaseTxParams = {
89
+ ...props.baseTxParams,
98
90
  };
99
91
 
100
92
  // # Run Processes
101
93
  if (processConfig.useSimulatedComputeUnits) {
102
94
  const txToSim = await txBuilder({
103
- ...txProps,
104
- txParams: { ...txProps.txParams, computeUnits: 1_400_000 },
95
+ txParams: { ...finalTxParams, computeUnits: 1_400_000 },
105
96
  });
106
97
 
107
98
  const txSimComputeUnitsResult = await this.getTxSimComputeUnits(
@@ -116,10 +107,7 @@ export class TransactionProcessor {
116
107
  COMPUTE_UNIT_BUFFER_FACTOR);
117
108
 
118
109
  // Adjust the transaction based on the simulated compute units
119
- finalTxProps.txParams = {
120
- ...txProps.txParams,
121
- computeUnits: Math.ceil(bufferedComputeUnits), // Round the compute units to a whole number
122
- };
110
+ finalTxParams.computeUnits = Math.ceil(bufferedComputeUnits); // Round the compute units to a whole number
123
111
  }
124
112
  }
125
113
 
@@ -135,20 +123,20 @@ export class TransactionProcessor {
135
123
  );
136
124
  }
137
125
 
138
- const simulatedComputeUnits = finalTxProps.txParams.computeUnits;
126
+ const simulatedComputeUnits = finalTxParams.computeUnits;
139
127
 
140
128
  const computeUnitPrice = processConfig.getCUPriceFromComputeUnits(
141
129
  simulatedComputeUnits
142
130
  );
143
131
 
144
132
  console.debug(
145
- `🔧:: Adjusting compute unit price for simulated compute unit budget :: ${finalTxProps.txParams.computeUnitsPrice}=>${computeUnitPrice}`
133
+ `🔧:: Adjusting compute unit price for simulated compute unit budget :: ${finalTxParams.computeUnitsPrice}=>${computeUnitPrice}`
146
134
  );
147
135
 
148
- finalTxProps.txParams.computeUnitsPrice = computeUnitPrice;
136
+ finalTxParams.computeUnitsPrice = computeUnitPrice;
149
137
  }
150
138
 
151
139
  // # Return Final Tx Params
152
- return finalTxProps.txParams;
140
+ return finalTxParams;
153
141
  }
154
142
  }
package/src/tx/types.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  import {
2
- AddressLookupTableAccount,
3
2
  ConfirmOptions,
4
3
  Signer,
5
4
  Transaction,
6
- TransactionInstruction,
7
5
  TransactionSignature,
8
6
  VersionedTransaction,
9
7
  } from '@solana/web3.js';
@@ -20,10 +18,6 @@ export type TxSigAndSlot = {
20
18
  slot: number;
21
19
  };
22
20
 
23
- export type ExtraConfirmationOptions = {
24
- onSignedCb: () => void;
25
- };
26
-
27
21
  export interface TxSender {
28
22
  wallet: IWallet;
29
23
 
@@ -31,26 +25,16 @@ export interface TxSender {
31
25
  tx: Transaction,
32
26
  additionalSigners?: Array<Signer>,
33
27
  opts?: ConfirmOptions,
34
- preSigned?: boolean,
35
- extraConfirmationOptions?: ExtraConfirmationOptions
28
+ preSigned?: boolean
36
29
  ): Promise<TxSigAndSlot>;
37
30
 
38
31
  sendVersionedTransaction(
39
32
  tx: VersionedTransaction,
40
33
  additionalSigners?: Array<Signer>,
41
34
  opts?: ConfirmOptions,
42
- preSigned?: boolean,
43
- extraConfirmationOptions?: ExtraConfirmationOptions
35
+ preSigned?: boolean
44
36
  ): Promise<TxSigAndSlot>;
45
37
 
46
- getVersionedTransaction(
47
- ixs: TransactionInstruction[],
48
- lookupTableAccounts: AddressLookupTableAccount[],
49
- additionalSigners?: Array<Signer>,
50
- opts?: ConfirmOptions,
51
- blockhash?: string
52
- ): Promise<VersionedTransaction>;
53
-
54
38
  sendRawTransaction(
55
39
  rawTransaction: Buffer | Uint8Array,
56
40
  opts: ConfirmOptions
@@ -60,3 +44,15 @@ export interface TxSender {
60
44
 
61
45
  getTimeoutCount(): number;
62
46
  }
47
+
48
+ export class TxSendError extends Error {
49
+ constructor(
50
+ public message: string,
51
+ public code: number
52
+ ) {
53
+ super(message);
54
+ if (Error.captureStackTrace) {
55
+ Error.captureStackTrace(this, TxSendError);
56
+ }
57
+ }
58
+ }
@@ -1,22 +1,19 @@
1
- import { ExtraConfirmationOptions, TxSigAndSlot } from './types';
1
+ import { TxSigAndSlot } from './types';
2
2
  import {
3
- AddressLookupTableAccount,
4
3
  Commitment,
5
4
  ConfirmOptions,
6
5
  Connection,
7
6
  Signer,
8
7
  Transaction,
9
- TransactionInstruction,
10
- TransactionMessage,
11
8
  VersionedTransaction,
12
9
  } from '@solana/web3.js';
13
10
  import { AnchorProvider } from '@coral-xyz/anchor';
14
- import { IWallet } from '../types';
15
11
  import { BaseTxSender } from './baseTxSender';
16
12
  import bs58 from 'bs58';
13
+ import { TxHandler } from './txHandler';
14
+ import { IWallet } from '../types';
17
15
 
18
16
  const DEFAULT_RETRY = 2000;
19
- const PLACEHOLDER_BLOCKHASH = 'Fdum64WVeej6DeL85REV9NvfSxEJNPZ74DBk7A8kTrKP';
20
17
 
21
18
  type ResolveReference = {
22
19
  resolve?: () => void;
@@ -44,6 +41,7 @@ export class WhileValidTxSender extends BaseTxSender {
44
41
  additionalConnections = new Array<Connection>(),
45
42
  additionalTxSenderCallbacks = [],
46
43
  blockhashCommitment = 'finalized',
44
+ txHandler,
47
45
  }: {
48
46
  connection: Connection;
49
47
  wallet: IWallet;
@@ -52,6 +50,7 @@ export class WhileValidTxSender extends BaseTxSender {
52
50
  additionalConnections?;
53
51
  additionalTxSenderCallbacks?: ((base58EncodedTx: string) => void)[];
54
52
  blockhashCommitment?: Commitment;
53
+ txHandler: TxHandler;
55
54
  }) {
56
55
  super({
57
56
  connection,
@@ -59,6 +58,7 @@ export class WhileValidTxSender extends BaseTxSender {
59
58
  opts,
60
59
  additionalConnections,
61
60
  additionalTxSenderCallbacks,
61
+ txHandler,
62
62
  });
63
63
  this.retrySleep = retrySleep;
64
64
  this.blockhashCommitment = blockhashCommitment;
@@ -77,23 +77,20 @@ export class WhileValidTxSender extends BaseTxSender {
77
77
  opts: ConfirmOptions,
78
78
  preSigned?: boolean
79
79
  ): Promise<Transaction> {
80
- const latestBlockhash = await this.connection.getLatestBlockhash(
81
- this.blockhashCommitment
82
- );
80
+ const latestBlockhash =
81
+ await this.txHandler.getLatestBlockhashForTransaction();
83
82
 
84
83
  // handle tx
85
84
  let signedTx = tx;
86
85
  if (!preSigned) {
87
- tx.feePayer = this.wallet.publicKey;
88
- tx.recentBlockhash = latestBlockhash.blockhash;
89
-
90
- additionalSigners
91
- .filter((s): s is Signer => s !== undefined)
92
- .forEach((kp) => {
93
- tx.partialSign(kp);
94
- });
95
-
96
- signedTx = await this.wallet.signTransaction(tx);
86
+ signedTx = await this.txHandler.prepareTx(
87
+ tx,
88
+ additionalSigners,
89
+ undefined,
90
+ opts,
91
+ false,
92
+ latestBlockhash
93
+ );
97
94
  }
98
95
 
99
96
  // handle subclass-specific side effects
@@ -105,34 +102,14 @@ export class WhileValidTxSender extends BaseTxSender {
105
102
  return signedTx;
106
103
  }
107
104
 
108
- async getVersionedTransaction(
109
- ixs: TransactionInstruction[],
110
- lookupTableAccounts: AddressLookupTableAccount[],
111
- _additionalSigners?: Array<Signer>,
112
- _opts?: ConfirmOptions,
113
- blockhash?: string
114
- ): Promise<VersionedTransaction> {
115
- const message = new TransactionMessage({
116
- payerKey: this.wallet.publicKey,
117
- recentBlockhash: blockhash ?? PLACEHOLDER_BLOCKHASH, // set blank and reset in sendVersionTransaction
118
- instructions: ixs,
119
- }).compileToV0Message(lookupTableAccounts);
120
-
121
- const tx = new VersionedTransaction(message);
122
-
123
- return tx;
124
- }
125
-
126
105
  async sendVersionedTransaction(
127
106
  tx: VersionedTransaction,
128
107
  additionalSigners?: Array<Signer>,
129
108
  opts?: ConfirmOptions,
130
- preSigned?: boolean,
131
- extraConfirmationOptions?: ExtraConfirmationOptions
109
+ preSigned?: boolean
132
110
  ): Promise<TxSigAndSlot> {
133
- const latestBlockhash = await this.connection.getLatestBlockhash(
134
- this.blockhashCommitment
135
- );
111
+ const latestBlockhash =
112
+ await this.txHandler.getLatestBlockhashForTransaction();
136
113
 
137
114
  let signedTx;
138
115
  if (preSigned) {
@@ -150,12 +127,11 @@ export class WhileValidTxSender extends BaseTxSender {
150
127
  .forEach((kp) => {
151
128
  tx.sign([kp]);
152
129
  });
153
- // @ts-ignore
154
- signedTx = await this.wallet.signTransaction(tx);
155
- }
156
-
157
- if (extraConfirmationOptions?.onSignedCb) {
158
- extraConfirmationOptions.onSignedCb();
130
+ signedTx = await this.txHandler.signVersionedTx(
131
+ tx,
132
+ additionalSigners,
133
+ latestBlockhash
134
+ );
159
135
  }
160
136
 
161
137
  if (opts === undefined) {
package/src/types.ts CHANGED
@@ -33,12 +33,21 @@ export enum PerpOperation {
33
33
  FILL = 4,
34
34
  SETTLE_PNL = 8,
35
35
  SETTLE_PNL_WITH_POSITION = 16,
36
+ LIQUIDATION = 32,
36
37
  }
37
38
 
38
39
  export enum SpotOperation {
39
40
  UPDATE_CUMULATIVE_INTEREST = 1,
40
41
  FILL = 2,
41
42
  WITHDRAW = 4,
43
+ LIQUIDATION = 8,
44
+ }
45
+
46
+ export enum InsuranceFundOperation {
47
+ INIT = 1,
48
+ ADD = 2,
49
+ REQUEST_REMOVE = 4,
50
+ REMOVE = 8,
42
51
  }
43
52
 
44
53
  export enum UserStatus {
@@ -706,6 +715,8 @@ export type SpotMarketAccount = {
706
715
  ordersEnabled: boolean;
707
716
 
708
717
  pausedOperations: number;
718
+
719
+ ifPausedOperations: number;
709
720
  };
710
721
 
711
722
  export type PoolBalance = {
@@ -1002,10 +1013,12 @@ export type ReferrerInfo = {
1002
1013
  referrerStats: PublicKey;
1003
1014
  };
1004
1015
 
1005
- export type BaseTxParams = {
1016
+ type ExactType<T> = Pick<T, keyof T>;
1017
+
1018
+ export type BaseTxParams = ExactType<{
1006
1019
  computeUnits?: number;
1007
1020
  computeUnitsPrice?: number;
1008
- };
1021
+ }>;
1009
1022
 
1010
1023
  export type ProcessingTxParams = {
1011
1024
  useSimulatedComputeUnits?: boolean;
@@ -1171,3 +1184,14 @@ export type HealthComponent = {
1171
1184
  weight: BN;
1172
1185
  weightedValue: BN;
1173
1186
  };
1187
+
1188
+ export interface DriftClientMetricsEvents {
1189
+ txSigned: SignedTxData[];
1190
+ }
1191
+
1192
+ export type SignedTxData = {
1193
+ txSig: string;
1194
+ signedTx: Transaction | VersionedTransaction;
1195
+ lastValidBlockHeight?: number;
1196
+ blockHash: string;
1197
+ };
package/src/user.ts CHANGED
@@ -118,7 +118,10 @@ export class User {
118
118
  this.accountSubscriber = new WebSocketUserAccountSubscriber(
119
119
  config.driftClient.program,
120
120
  config.userAccountPublicKey,
121
- config.accountSubscription?.resubTimeoutMs,
121
+ {
122
+ resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
123
+ logResubMessages: config.accountSubscription?.logResubMessages,
124
+ },
122
125
  config.accountSubscription?.commitment
123
126
  );
124
127
  }
@@ -3162,35 +3165,6 @@ export class User {
3162
3165
  return state.spotFeeStructure.feeTiers[feeTierIndex];
3163
3166
  }
3164
3167
 
3165
- /**
3166
- * Calculates taker / maker fee (as a percentage, e.g. .001 = 10 basis points) for particular marketType
3167
- * @param marketType
3168
- * @param positionMarketIndex
3169
- * @returns : {takerFee: number, makerFee: number} Precision None
3170
- */
3171
- public getMarketFees(marketType: MarketType, marketIndex?: number) {
3172
- const feeTier = this.getUserFeeTier(marketType);
3173
- let takerFee = feeTier.feeNumerator / feeTier.feeDenominator;
3174
- let makerFee =
3175
- feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;
3176
-
3177
- if (marketIndex !== undefined) {
3178
- let marketAccount = null;
3179
- if (isVariant(marketType, 'perp')) {
3180
- marketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
3181
- } else {
3182
- marketAccount = this.driftClient.getSpotMarketAccount(marketIndex);
3183
- }
3184
- takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
3185
- makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
3186
- }
3187
-
3188
- return {
3189
- takerFee,
3190
- makerFee,
3191
- };
3192
- }
3193
-
3194
3168
  /**
3195
3169
  * Calculates how much perp fee will be taken for a given sized trade
3196
3170
  * @param quoteAmount
@@ -3198,9 +3172,10 @@ export class User {
3198
3172
  */
3199
3173
  public calculateFeeForQuoteAmount(quoteAmount: BN, marketIndex?: number): BN {
3200
3174
  if (marketIndex !== undefined) {
3201
- const takerFeeMultiplier = this.getMarketFees(
3175
+ const takerFeeMultiplier = this.driftClient.getMarketFees(
3202
3176
  MarketType.PERP,
3203
- marketIndex
3177
+ marketIndex,
3178
+ this
3204
3179
  ).takerFee;
3205
3180
  const feeAmountNum =
3206
3181
  BigNum.from(quoteAmount, QUOTE_PRECISION_EXP).toNum() *
package/src/userConfig.ts CHANGED
@@ -13,6 +13,7 @@ export type UserSubscriptionConfig =
13
13
  | {
14
14
  type: 'websocket';
15
15
  resubTimeoutMs?: number;
16
+ logResubMessages?: boolean;
16
17
  commitment?: Commitment;
17
18
  }
18
19
  | {
@@ -3,12 +3,13 @@ import { getNonIdleUserFilter, getUserFilter } from '../memcmp';
3
3
  import { WebSocketProgramAccountSubscriber } from '../accounts/webSocketProgramAccountSubscriber';
4
4
  import { UserAccount } from '../types';
5
5
  import { Commitment, Context, PublicKey } from '@solana/web3.js';
6
+ import { ResubOpts } from '../accounts/types';
6
7
 
7
8
  export class WebsocketSubscription {
8
9
  private userMap: UserMap;
9
10
  private commitment: Commitment;
10
11
  private skipInitialLoad: boolean;
11
- private resubTimeoutMs?: number;
12
+ private resubOpts?: ResubOpts;
12
13
  private includeIdle?: boolean;
13
14
  private decodeFn: (name: string, data: Buffer) => UserAccount;
14
15
 
@@ -18,21 +19,21 @@ export class WebsocketSubscription {
18
19
  userMap,
19
20
  commitment,
20
21
  skipInitialLoad = false,
21
- resubTimeoutMs,
22
+ resubOpts,
22
23
  includeIdle = false,
23
24
  decodeFn,
24
25
  }: {
25
26
  userMap: UserMap;
26
27
  commitment: Commitment;
27
28
  skipInitialLoad?: boolean;
28
- resubTimeoutMs?: number;
29
+ resubOpts?: ResubOpts;
29
30
  includeIdle?: boolean;
30
31
  decodeFn: (name: string, data: Buffer) => UserAccount;
31
32
  }) {
32
33
  this.userMap = userMap;
33
34
  this.commitment = commitment;
34
35
  this.skipInitialLoad = skipInitialLoad;
35
- this.resubTimeoutMs = resubTimeoutMs;
36
+ this.resubOpts = resubOpts;
36
37
  this.includeIdle = includeIdle || false;
37
38
  this.decodeFn = decodeFn;
38
39
  }
@@ -52,7 +53,7 @@ export class WebsocketSubscription {
52
53
  filters,
53
54
  commitment: this.commitment,
54
55
  },
55
- this.resubTimeoutMs
56
+ this.resubOpts
56
57
  );
57
58
  }
58
59
 
@@ -124,7 +124,10 @@ export class UserMap implements UserMapInterface {
124
124
  this.subscription = new WebsocketSubscription({
125
125
  userMap: this,
126
126
  commitment: this.commitment,
127
- resubTimeoutMs: config.subscriptionConfig.resubTimeoutMs,
127
+ resubOpts: {
128
+ resubTimeoutMs: config.subscriptionConfig.resubTimeoutMs,
129
+ logResubMessages: config.subscriptionConfig.logResubMessages,
130
+ },
128
131
  skipInitialLoad: config.skipInitialLoad,
129
132
  decodeFn,
130
133
  });
@@ -20,6 +20,7 @@ export type UserMapConfig = {
20
20
  | {
21
21
  type: 'websocket';
22
22
  resubTimeoutMs?: number;
23
+ logResubMessages?: boolean;
23
24
  commitment?: Commitment;
24
25
  };
25
26
 
package/src/userStats.ts CHANGED
@@ -29,7 +29,10 @@ export class UserStats {
29
29
  this.accountSubscriber = new WebSocketUserStatsAccountSubscriber(
30
30
  config.driftClient.program,
31
31
  config.userStatsAccountPublicKey,
32
- config.accountSubscription.resubTimeoutMs,
32
+ {
33
+ resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
34
+ logResubMessages: config.accountSubscription?.logResubMessages,
35
+ },
33
36
  config.accountSubscription.commitment
34
37
  );
35
38
  } else {
@@ -12,6 +12,7 @@ export type UserStatsSubscriptionConfig =
12
12
  | {
13
13
  type: 'websocket';
14
14
  resubTimeoutMs?: number;
15
+ logResubMessages?: boolean;
15
16
  commitment?: Commitment;
16
17
  }
17
18
  | {
@@ -0,0 +1,41 @@
1
+ import { Commitment } from '@solana/web3.js';
2
+
3
+ export type ChainClockProgress = {
4
+ blockHeight?: number;
5
+ slot?: number;
6
+ ts?: number;
7
+ };
8
+
9
+ export type ChainClockUpdateProps = {
10
+ commitment: Commitment;
11
+ } & ChainClockProgress;
12
+
13
+ export type ChainClockState = Map<Commitment, ChainClockProgress>;
14
+
15
+ export type ChainClickInitialisationProps = ChainClockUpdateProps[];
16
+
17
+ export class ChainClock {
18
+ private _state: ChainClockState;
19
+
20
+ constructor(props: ChainClickInitialisationProps) {
21
+ this._state = new Map<Commitment, ChainClockUpdateProps>();
22
+ props.forEach((prop) => {
23
+ this._state.set(prop.commitment, prop);
24
+ });
25
+ }
26
+
27
+ update(props: ChainClockUpdateProps): void {
28
+ const state = this._state.get(props.commitment);
29
+ if (state) {
30
+ if (props.blockHeight) state.blockHeight = props.blockHeight;
31
+ if (props.slot) state.slot = props.slot;
32
+ if (props.ts) state.ts = props.ts;
33
+ } else {
34
+ this._state.set(props.commitment, props);
35
+ }
36
+ }
37
+
38
+ public getState(commitment: Commitment): ChainClockProgress {
39
+ return this._state.get(commitment);
40
+ }
41
+ }
@@ -350,6 +350,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
350
350
  lastIndexPriceTwapTs: new BN(0),
351
351
  },
352
352
  pausedOperations: 0,
353
+ ifPausedOperations: 0,
353
354
  },
354
355
  {
355
356
  status: MarketStatus.ACTIVE,
@@ -432,6 +433,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
432
433
  lastIndexPriceTwapTs: new BN(0),
433
434
  },
434
435
  pausedOperations: 0,
436
+ ifPausedOperations: 0,
435
437
  },
436
438
  {
437
439
  status: MarketStatus.ACTIVE,
@@ -514,6 +516,7 @@ export const mockSpotMarkets: Array<SpotMarketAccount> = [
514
516
  lastIndexPriceTwapTs: new BN(0),
515
517
  },
516
518
  pausedOperations: 0,
519
+ ifPausedOperations: 0,
517
520
  },
518
521
  ];
519
522
 
package/lib/tx/utils.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { Wallet } from '@coral-xyz/anchor';
2
- import { Transaction, TransactionInstruction, VersionedTransaction } from '@solana/web3.js';
3
- export declare function wrapInTx(instruction: TransactionInstruction, computeUnits?: number, computeUnitsPrice?: number): Transaction;
4
- export declare function getSignedTransactionMap(wallet: Wallet, txsToSign: (Transaction | VersionedTransaction | undefined)[], keys: string[]): Promise<{
5
- [key: string]: Transaction | VersionedTransaction | undefined;
6
- }>;
package/lib/tx/utils.js DELETED
@@ -1,39 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSignedTransactionMap = exports.wrapInTx = void 0;
4
- const web3_js_1 = require("@solana/web3.js");
5
- const COMPUTE_UNITS_DEFAULT = 200000;
6
- function wrapInTx(instruction, computeUnits = 600000, computeUnitsPrice = 0) {
7
- const tx = new web3_js_1.Transaction();
8
- if (computeUnits != COMPUTE_UNITS_DEFAULT) {
9
- tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
10
- units: computeUnits,
11
- }));
12
- }
13
- if (computeUnitsPrice != 0) {
14
- tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
15
- microLamports: computeUnitsPrice,
16
- }));
17
- }
18
- return tx.add(instruction);
19
- }
20
- exports.wrapInTx = wrapInTx;
21
- /* Helper function for signing multiple transactions where some may be undefined and mapping the output */
22
- async function getSignedTransactionMap(wallet, txsToSign, keys) {
23
- const signedTxMap = {};
24
- const keysWithTx = [];
25
- txsToSign.forEach((tx, index) => {
26
- if (tx == undefined) {
27
- signedTxMap[keys[index]] = undefined;
28
- }
29
- else {
30
- keysWithTx.push(keys[index]);
31
- }
32
- });
33
- const signedTxs = await wallet.signAllTransactions(txsToSign.filter((tx) => tx !== undefined));
34
- signedTxs.forEach((signedTx, index) => {
35
- signedTxMap[keysWithTx[index]] = signedTx;
36
- });
37
- return signedTxMap;
38
- }
39
- exports.getSignedTransactionMap = getSignedTransactionMap;