@drift-labs/sdk 2.38.1-beta.8 → 2.39.1-beta.0

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,117 @@
1
+ import { ComputeBudgetProgram, TransactionInstruction } from '@solana/web3.js';
2
+
3
+ /**
4
+ * This class determines whether a priority fee needs to be included in a transaction based on
5
+ * a recent history of timed out transactions.
6
+ */
7
+ export class PriorityFeeCalculator {
8
+ lastTxTimeoutCount: number;
9
+ priorityFeeTriggered: boolean;
10
+ lastTxTimeoutCountTriggered: number;
11
+ priorityFeeLatchDurationMs: number; // how long to stay in triggered state before resetting
12
+
13
+ /**
14
+ * Constructor for the PriorityFeeCalculator class.
15
+ * @param currentTimeMs - The current time in milliseconds.
16
+ * @param priorityFeeLatchDurationMs - The duration for how long to stay in triggered state before resetting. Default value is 10 seconds.
17
+ */
18
+ constructor(
19
+ currentTimeMs: number,
20
+ priorityFeeLatchDurationMs: number = 10 * 1000
21
+ ) {
22
+ this.lastTxTimeoutCount = 0;
23
+ this.priorityFeeTriggered = false;
24
+ this.lastTxTimeoutCountTriggered = currentTimeMs;
25
+ this.priorityFeeLatchDurationMs = priorityFeeLatchDurationMs;
26
+ }
27
+
28
+ /**
29
+ * Update the priority fee state based on the current time and the current timeout count.
30
+ * @param currentTimeMs current time in milliseconds
31
+ * @returns true if priority fee should be included in the next transaction
32
+ */
33
+ public updatePriorityFee(
34
+ currentTimeMs: number,
35
+ txTimeoutCount: number
36
+ ): boolean {
37
+ let triggerPriorityFee = false;
38
+
39
+ if (txTimeoutCount > this.lastTxTimeoutCount) {
40
+ this.lastTxTimeoutCount = txTimeoutCount;
41
+ this.lastTxTimeoutCountTriggered = currentTimeMs;
42
+ triggerPriorityFee = true;
43
+ } else {
44
+ if (!this.priorityFeeTriggered) {
45
+ triggerPriorityFee = false;
46
+ } else if (
47
+ currentTimeMs - this.lastTxTimeoutCountTriggered <
48
+ this.priorityFeeLatchDurationMs
49
+ ) {
50
+ triggerPriorityFee = true;
51
+ }
52
+ }
53
+
54
+ this.priorityFeeTriggered = triggerPriorityFee;
55
+
56
+ return triggerPriorityFee;
57
+ }
58
+
59
+ /**
60
+ * This method returns a transaction instruction list that sets the compute limit on the ComputeBudget program.
61
+ * @param computeUnitLimit - The maximum number of compute units that can be used by the transaction.
62
+ * @returns An array of transaction instructions.
63
+ */
64
+ public generateComputeBudgetIxs(
65
+ computeUnitLimit: number
66
+ ): Array<TransactionInstruction> {
67
+ const ixs = [
68
+ ComputeBudgetProgram.setComputeUnitLimit({
69
+ units: computeUnitLimit,
70
+ }),
71
+ ];
72
+
73
+ return ixs;
74
+ }
75
+
76
+ /**
77
+ * Calculates the compute unit price to use based on the desired additional fee to pay and the compute unit limit.
78
+ * @param computeUnitLimit desired CU to use
79
+ * @param additionalFeeMicroLamports desired additional fee to pay, in micro lamports
80
+ * @returns the compute unit price to use, in micro lamports
81
+ */
82
+ public calculateComputeUnitPrice(
83
+ computeUnitLimit: number,
84
+ additionalFeeMicroLamports: number
85
+ ): number {
86
+ return additionalFeeMicroLamports / computeUnitLimit;
87
+ }
88
+
89
+ /**
90
+ * This method generates a list of transaction instructions for the ComputeBudget program, and includes a priority fee if it's required
91
+ * @param computeUnitLimit - The maximum number of compute units that can be used by the transaction.
92
+ * @param usePriorityFee - A boolean indicating whether to include a priority fee in the transaction, this should be from `this.updatePriorityFee()` or `this.priorityFeeTriggered()`.
93
+ * @param additionalFeeMicroLamports - The additional fee to be paid, in micro lamports, the actual price will be calculated.
94
+ * @returns An array of transaction instructions.
95
+ */
96
+ public generateComputeBudgetWithPriorityFeeIx(
97
+ computeUnitLimit: number,
98
+ usePriorityFee: boolean,
99
+ additionalFeeMicroLamports: number
100
+ ): Array<TransactionInstruction> {
101
+ const ixs = this.generateComputeBudgetIxs(computeUnitLimit);
102
+
103
+ if (usePriorityFee) {
104
+ const computeUnitPrice = this.calculateComputeUnitPrice(
105
+ computeUnitLimit,
106
+ additionalFeeMicroLamports
107
+ );
108
+ ixs.push(
109
+ ComputeBudgetProgram.setComputeUnitPrice({
110
+ microLamports: computeUnitPrice,
111
+ })
112
+ );
113
+ }
114
+
115
+ return ixs;
116
+ }
117
+ }
package/src/types.ts CHANGED
@@ -655,6 +655,7 @@ export type SpotMarketAccount = {
655
655
  maintenanceLiabilityWeight: number;
656
656
  liquidatorFee: number;
657
657
  imfFactor: number;
658
+ scaleInitialAssetWeightStart: BN;
658
659
 
659
660
  withdrawGuardThreshold: BN;
660
661
  depositTokenTwap: BN;