@drift-labs/sdk 2.38.1-beta.11 → 2.38.1-beta.13

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.38.1-beta.11
1
+ 2.38.1-beta.13
package/lib/index.d.ts CHANGED
@@ -65,6 +65,7 @@ export * from './priorityFee/priorityFeeSubscriber';
65
65
  export * from './phoenix/phoenixFulfillmentConfigMap';
66
66
  export * from './tx/fastSingleTxSender';
67
67
  export * from './tx/retryTxSender';
68
+ export * from './tx/priorityFeeCalculator';
68
69
  export * from './tx/types';
69
70
  export * from './util/computeUnits';
70
71
  export * from './util/tps';
package/lib/index.js CHANGED
@@ -88,6 +88,7 @@ __exportStar(require("./priorityFee/priorityFeeSubscriber"), exports);
88
88
  __exportStar(require("./phoenix/phoenixFulfillmentConfigMap"), exports);
89
89
  __exportStar(require("./tx/fastSingleTxSender"), exports);
90
90
  __exportStar(require("./tx/retryTxSender"), exports);
91
+ __exportStar(require("./tx/priorityFeeCalculator"), exports);
91
92
  __exportStar(require("./tx/types"), exports);
92
93
  __exportStar(require("./util/computeUnits"), exports);
93
94
  __exportStar(require("./util/tps"), exports);
@@ -0,0 +1,44 @@
1
+ import { TransactionInstruction } from '@solana/web3.js';
2
+ /**
3
+ * This class determines whether a priority fee needs to be included in a transaction based on
4
+ * a recent history of timed out transactions.
5
+ */
6
+ export declare class PriorityFeeCalculator {
7
+ lastTxTimeoutCount: number;
8
+ priorityFeeTriggered: boolean;
9
+ lastTxTimeoutCountTriggered: number;
10
+ priorityFeeLatchDurationMs: number;
11
+ /**
12
+ * Constructor for the PriorityFeeCalculator class.
13
+ * @param currentTimeMs - The current time in milliseconds.
14
+ * @param priorityFeeLatchDurationMs - The duration for how long to stay in triggered state before resetting. Default value is 10 seconds.
15
+ */
16
+ constructor(currentTimeMs: number, priorityFeeLatchDurationMs?: number);
17
+ /**
18
+ * Update the priority fee state based on the current time and the current timeout count.
19
+ * @param currentTimeMs current time in milliseconds
20
+ * @returns true if priority fee should be included in the next transaction
21
+ */
22
+ updatePriorityFee(currentTimeMs: number, txTimeoutCount: number): boolean;
23
+ /**
24
+ * This method returns a transaction instruction list that sets the compute limit on the ComputeBudget program.
25
+ * @param computeUnitLimit - The maximum number of compute units that can be used by the transaction.
26
+ * @returns An array of transaction instructions.
27
+ */
28
+ generateComputeBudgetIxs(computeUnitLimit: number): Array<TransactionInstruction>;
29
+ /**
30
+ * Calculates the compute unit price to use based on the desired additional fee to pay and the compute unit limit.
31
+ * @param computeUnitLimit desired CU to use
32
+ * @param additionalFeeMicroLamports desired additional fee to pay, in micro lamports
33
+ * @returns the compute unit price to use, in micro lamports
34
+ */
35
+ calculateComputeUnitPrice(computeUnitLimit: number, additionalFeeMicroLamports: number): number;
36
+ /**
37
+ * This method generates a list of transaction instructions for the ComputeBudget program, and includes a priority fee if it's required
38
+ * @param computeUnitLimit - The maximum number of compute units that can be used by the transaction.
39
+ * @param usePriorityFee - A boolean indicating whether to include a priority fee in the transaction, this should be from `this.updatePriorityFee()` or `this.priorityFeeTriggered()`.
40
+ * @param additionalFeeMicroLamports - The additional fee to be paid, in micro lamports, the actual price will be calculated.
41
+ * @returns An array of transaction instructions.
42
+ */
43
+ generateComputeBudgetWithPriorityFeeIx(computeUnitLimit: number, usePriorityFee: boolean, additionalFeeMicroLamports: number): Array<TransactionInstruction>;
44
+ }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PriorityFeeCalculator = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ /**
6
+ * This class determines whether a priority fee needs to be included in a transaction based on
7
+ * a recent history of timed out transactions.
8
+ */
9
+ class PriorityFeeCalculator {
10
+ /**
11
+ * Constructor for the PriorityFeeCalculator class.
12
+ * @param currentTimeMs - The current time in milliseconds.
13
+ * @param priorityFeeLatchDurationMs - The duration for how long to stay in triggered state before resetting. Default value is 10 seconds.
14
+ */
15
+ constructor(currentTimeMs, priorityFeeLatchDurationMs = 10 * 1000) {
16
+ this.lastTxTimeoutCount = 0;
17
+ this.priorityFeeTriggered = false;
18
+ this.lastTxTimeoutCountTriggered = currentTimeMs;
19
+ this.priorityFeeLatchDurationMs = priorityFeeLatchDurationMs;
20
+ }
21
+ /**
22
+ * Update the priority fee state based on the current time and the current timeout count.
23
+ * @param currentTimeMs current time in milliseconds
24
+ * @returns true if priority fee should be included in the next transaction
25
+ */
26
+ updatePriorityFee(currentTimeMs, txTimeoutCount) {
27
+ let triggerPriorityFee = false;
28
+ if (txTimeoutCount > this.lastTxTimeoutCount) {
29
+ this.lastTxTimeoutCount = txTimeoutCount;
30
+ this.lastTxTimeoutCountTriggered = currentTimeMs;
31
+ triggerPriorityFee = true;
32
+ }
33
+ else {
34
+ if (!this.priorityFeeTriggered) {
35
+ triggerPriorityFee = false;
36
+ }
37
+ else if (currentTimeMs - this.lastTxTimeoutCountTriggered <
38
+ this.priorityFeeLatchDurationMs) {
39
+ triggerPriorityFee = true;
40
+ }
41
+ }
42
+ this.priorityFeeTriggered = triggerPriorityFee;
43
+ return triggerPriorityFee;
44
+ }
45
+ /**
46
+ * This method returns a transaction instruction list that sets the compute limit on the ComputeBudget program.
47
+ * @param computeUnitLimit - The maximum number of compute units that can be used by the transaction.
48
+ * @returns An array of transaction instructions.
49
+ */
50
+ generateComputeBudgetIxs(computeUnitLimit) {
51
+ const ixs = [
52
+ web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
53
+ units: computeUnitLimit,
54
+ }),
55
+ ];
56
+ return ixs;
57
+ }
58
+ /**
59
+ * Calculates the compute unit price to use based on the desired additional fee to pay and the compute unit limit.
60
+ * @param computeUnitLimit desired CU to use
61
+ * @param additionalFeeMicroLamports desired additional fee to pay, in micro lamports
62
+ * @returns the compute unit price to use, in micro lamports
63
+ */
64
+ calculateComputeUnitPrice(computeUnitLimit, additionalFeeMicroLamports) {
65
+ return additionalFeeMicroLamports / computeUnitLimit;
66
+ }
67
+ /**
68
+ * This method generates a list of transaction instructions for the ComputeBudget program, and includes a priority fee if it's required
69
+ * @param computeUnitLimit - The maximum number of compute units that can be used by the transaction.
70
+ * @param usePriorityFee - A boolean indicating whether to include a priority fee in the transaction, this should be from `this.updatePriorityFee()` or `this.priorityFeeTriggered()`.
71
+ * @param additionalFeeMicroLamports - The additional fee to be paid, in micro lamports, the actual price will be calculated.
72
+ * @returns An array of transaction instructions.
73
+ */
74
+ generateComputeBudgetWithPriorityFeeIx(computeUnitLimit, usePriorityFee, additionalFeeMicroLamports) {
75
+ const ixs = this.generateComputeBudgetIxs(computeUnitLimit);
76
+ if (usePriorityFee) {
77
+ const computeUnitPrice = this.calculateComputeUnitPrice(computeUnitLimit, additionalFeeMicroLamports);
78
+ ixs.push(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
79
+ microLamports: computeUnitPrice,
80
+ }));
81
+ }
82
+ return ixs;
83
+ }
84
+ }
85
+ exports.PriorityFeeCalculator = PriorityFeeCalculator;
package/lib/user.js CHANGED
@@ -1834,7 +1834,7 @@ class User {
1834
1834
  const spotMarketAccount = this.driftClient.getQuoteSpotMarketAccount();
1835
1835
  const oraclePriceData = this.getOracleDataForSpotMarket(numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
1836
1836
  const baseAssetValue = (0, _1.getTokenValue)(netQuoteValue, spotMarketAccount.decimals, oraclePriceData);
1837
- const { weight, weightedTokenValue } = (0, spotPosition_1.calculateWeightedTokenValue)(netQuoteValue, baseAssetValue, oraclePriceData, spotMarketAccount, marginCategory);
1837
+ const { weight, weightedTokenValue } = (0, spotPosition_1.calculateWeightedTokenValue)(netQuoteValue, baseAssetValue, oraclePriceData.price, spotMarketAccount, marginCategory);
1838
1838
  if (netQuoteValue.lt(numericConstants_1.ZERO)) {
1839
1839
  healthComponents.borrows.push({
1840
1840
  marketIndex: spotMarketAccount.marketIndex,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.38.1-beta.11",
3
+ "version": "2.38.1-beta.13",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
package/src/index.ts CHANGED
@@ -66,6 +66,7 @@ export * from './priorityFee/priorityFeeSubscriber';
66
66
  export * from './phoenix/phoenixFulfillmentConfigMap';
67
67
  export * from './tx/fastSingleTxSender';
68
68
  export * from './tx/retryTxSender';
69
+ export * from './tx/priorityFeeCalculator';
69
70
  export * from './tx/types';
70
71
  export * from './util/computeUnits';
71
72
  export * from './util/tps';
@@ -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/user.ts CHANGED
@@ -3320,7 +3320,7 @@ export class User {
3320
3320
  const { weight, weightedTokenValue } = calculateWeightedTokenValue(
3321
3321
  netQuoteValue,
3322
3322
  baseAssetValue,
3323
- oraclePriceData,
3323
+ oraclePriceData.price,
3324
3324
  spotMarketAccount,
3325
3325
  marginCategory
3326
3326
  );
@@ -0,0 +1,77 @@
1
+ import { expect } from 'chai';
2
+ import { PriorityFeeCalculator } from '../../src/tx/priorityFeeCalculator';
3
+
4
+ describe('PriorityFeeCalculator', () => {
5
+ let priorityFeeCalculator: PriorityFeeCalculator;
6
+
7
+ const startTime = 1000000;
8
+ const latch_duration = 10_000;
9
+
10
+ beforeEach(() => {
11
+ priorityFeeCalculator = new PriorityFeeCalculator(
12
+ startTime,
13
+ latch_duration
14
+ );
15
+ });
16
+
17
+ it('should trigger priority fee when timeout count increases', () => {
18
+ const timeoutCount = 1;
19
+ expect(priorityFeeCalculator.updatePriorityFee(startTime, timeoutCount)).to
20
+ .be.true;
21
+ expect(
22
+ priorityFeeCalculator.updatePriorityFee(
23
+ startTime + latch_duration,
24
+ timeoutCount + 1
25
+ )
26
+ ).to.be.true;
27
+ expect(
28
+ priorityFeeCalculator.updatePriorityFee(
29
+ startTime + latch_duration,
30
+ timeoutCount + 2
31
+ )
32
+ ).to.be.true;
33
+ });
34
+
35
+ it('should trigger priority fee when timeout count increases, and stay latched until latch duration', () => {
36
+ const timeoutCount = 1;
37
+ expect(priorityFeeCalculator.updatePriorityFee(startTime, timeoutCount)).to
38
+ .be.true;
39
+ expect(
40
+ priorityFeeCalculator.updatePriorityFee(
41
+ startTime + latch_duration / 2,
42
+ timeoutCount
43
+ )
44
+ ).to.be.true;
45
+ expect(
46
+ priorityFeeCalculator.updatePriorityFee(
47
+ startTime + latch_duration - 1,
48
+ timeoutCount
49
+ )
50
+ ).to.be.true;
51
+ expect(
52
+ priorityFeeCalculator.updatePriorityFee(
53
+ startTime + latch_duration * 2,
54
+ timeoutCount
55
+ )
56
+ ).to.be.false;
57
+ });
58
+
59
+ it('should not trigger priority fee when timeout count does not increase', () => {
60
+ const timeoutCount = 0;
61
+ expect(priorityFeeCalculator.updatePriorityFee(startTime, timeoutCount)).to
62
+ .be.false;
63
+ });
64
+
65
+ it('should correctly calculate compute unit price', () => {
66
+ const computeUnitLimit = 1_000_000;
67
+ const additionalFeeMicroLamports = 1_000_000_000; // 1000 lamports
68
+ const actualComputeUnitPrice =
69
+ priorityFeeCalculator.calculateComputeUnitPrice(
70
+ computeUnitLimit,
71
+ additionalFeeMicroLamports
72
+ );
73
+ expect(actualComputeUnitPrice * computeUnitLimit).to.equal(
74
+ additionalFeeMicroLamports
75
+ );
76
+ });
77
+ });