@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.
- package/VERSION +1 -1
- package/lib/adminClient.d.ts +1 -0
- package/lib/adminClient.js +11 -0
- package/lib/driftClient.d.ts +22 -2
- package/lib/driftClient.js +94 -13
- package/lib/idl/drift.json +37 -2
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/math/spotBalance.d.ts +6 -5
- package/lib/math/spotBalance.js +29 -12
- package/lib/math/spotMarket.d.ts +1 -1
- package/lib/math/spotMarket.js +2 -2
- package/lib/math/spotPosition.d.ts +16 -3
- package/lib/math/spotPosition.js +53 -9
- package/lib/oracles/strictOraclePrice.d.ts +8 -0
- package/lib/oracles/strictOraclePrice.js +17 -0
- package/lib/tx/priorityFeeCalculator.d.ts +44 -0
- package/lib/tx/priorityFeeCalculator.js +85 -0
- package/lib/types.d.ts +1 -0
- package/lib/user.d.ts +5 -4
- package/lib/user.js +71 -105
- package/package.json +1 -1
- package/src/adminClient.ts +23 -0
- package/src/driftClient.ts +170 -13
- package/src/idl/drift.json +37 -2
- package/src/index.ts +3 -0
- package/src/math/spotBalance.ts +38 -12
- package/src/math/spotMarket.ts +7 -1
- package/src/math/spotPosition.ts +133 -18
- package/src/oracles/strictOraclePrice.ts +19 -0
- package/src/tx/priorityFeeCalculator.ts +117 -0
- package/src/types.ts +1 -0
- package/src/user.ts +171 -228
- package/tests/dlob/helpers.ts +10 -7
- package/tests/tx/priorityFeeCalculator.ts +77 -0
- package/tests/user/test.ts +77 -4
|
@@ -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
|
+
}
|