@drift-labs/sdk 2.76.0-beta.9 → 2.77.0-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 CHANGED
@@ -1 +1 @@
1
- 2.76.0-beta.9
1
+ 2.77.0-beta.0
@@ -11,6 +11,8 @@ export declare class AdminClient extends DriftClient {
11
11
  getInitializeSpotMarketIx(mint: PublicKey, optimalUtilization: number, optimalRate: number, maxRate: number, oracle: PublicKey, oracleSource: OracleSource, initialAssetWeight: number, maintenanceAssetWeight: number, initialLiabilityWeight: number, maintenanceLiabilityWeight: number, imfFactor?: number, liquidatorFee?: number, ifLiquidationFee?: number, activeStatus?: boolean, assetTier?: {
12
12
  collateral: {};
13
13
  }, scaleInitialAssetWeightStart?: BN, withdrawGuardThreshold?: BN, orderTickSize?: BN, orderStepSize?: BN, ifTotalFactor?: number, name?: string): Promise<TransactionInstruction>;
14
+ deleteInitializedSpotMarket(marketIndex: number): Promise<TransactionSignature>;
15
+ getDeleteInitializedSpotMarketIx(marketIndex: number): Promise<TransactionInstruction>;
14
16
  initializeSerumFulfillmentConfig(marketIndex: number, serumMarket: PublicKey, serumProgram: PublicKey): Promise<TransactionSignature>;
15
17
  getInitializeSerumFulfillmentConfigIx(marketIndex: number, serumMarket: PublicKey, serumProgram: PublicKey): Promise<TransactionInstruction>;
16
18
  initializePhoenixFulfillmentConfig(marketIndex: number, phoenixMarket: PublicKey): Promise<TransactionSignature>;
@@ -100,6 +100,30 @@ class AdminClient extends driftClient_1.DriftClient {
100
100
  });
101
101
  return initializeIx;
102
102
  }
103
+ async deleteInitializedSpotMarket(marketIndex) {
104
+ const deleteInitializeMarketIx = await this.getDeleteInitializedSpotMarketIx(marketIndex);
105
+ const tx = await this.buildTransaction(deleteInitializeMarketIx);
106
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
107
+ return txSig;
108
+ }
109
+ async getDeleteInitializedSpotMarketIx(marketIndex) {
110
+ const spotMarketPublicKey = await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex);
111
+ const spotMarketVaultPublicKey = await (0, pda_1.getSpotMarketVaultPublicKey)(this.program.programId, marketIndex);
112
+ const insuranceFundVaultPublicKey = await (0, pda_1.getInsuranceFundVaultPublicKey)(this.program.programId, marketIndex);
113
+ return await this.program.instruction.deleteInitializedSpotMarket(marketIndex, {
114
+ accounts: {
115
+ state: await this.getStatePublicKey(),
116
+ admin: this.isSubscribed
117
+ ? this.getStateAccount().admin
118
+ : this.wallet.publicKey,
119
+ spotMarket: spotMarketPublicKey,
120
+ spotMarketVault: spotMarketVaultPublicKey,
121
+ insuranceFundVault: insuranceFundVaultPublicKey,
122
+ driftSigner: this.getSignerPublicKey(),
123
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
124
+ },
125
+ });
126
+ }
103
127
  async initializeSerumFulfillmentConfig(marketIndex, serumMarket, serumProgram) {
104
128
  const initializeIx = await this.getInitializeSerumFulfillmentConfigIx(marketIndex, serumMarket, serumProgram);
105
129
  const tx = await this.buildTransaction(initializeIx);
@@ -546,6 +546,16 @@ exports.MainnetPerpMarkets = [
546
546
  launchTs: 1712240681000,
547
547
  oracleSource: __1.OracleSource.Prelaunch,
548
548
  },
549
+ {
550
+ fullName: 'Tensor',
551
+ category: ['NFT', 'Solana'],
552
+ symbol: 'TNSR-PERP',
553
+ baseAssetSymbol: 'TNSR',
554
+ marketIndex: 29,
555
+ oracle: new web3_js_1.PublicKey('7Cfyymx49ipGsgEsCA2XygAB2DUsan4C6Cyb5c8oR5st'),
556
+ launchTs: 1712593532000,
557
+ oracleSource: __1.OracleSource.SWITCHBOARD,
558
+ },
549
559
  ];
550
560
  exports.PerpMarkets = {
551
561
  devnet: exports.DevnetPerpMarkets,
@@ -186,6 +186,17 @@ exports.MainnetSpotMarkets = [
186
186
  phoenixMarket: new web3_js_1.PublicKey('8dFTCTAbtGuHsdDL8WEPrTU6pXFDrU1QSjBTutw8fwZk'),
187
187
  launchTs: 1712149014000,
188
188
  },
189
+ {
190
+ symbol: 'TNSR',
191
+ marketIndex: 14,
192
+ oracle: new web3_js_1.PublicKey('7Cfyymx49ipGsgEsCA2XygAB2DUsan4C6Cyb5c8oR5st'),
193
+ oracleSource: __1.OracleSource.SWITCHBOARD,
194
+ mint: new web3_js_1.PublicKey('TNSRxcUxoT9xBG3de7PiJyTDYu7kskLqcpddxnEJAS6'),
195
+ precision: new __1.BN(10).pow(numericConstants_1.NINE),
196
+ precisionExp: numericConstants_1.NINE,
197
+ phoenixMarket: new web3_js_1.PublicKey('AbJCZ9TAJiby5AY3cHcXS2gUdENC6mtsm6m7XpC2ZMvE'),
198
+ launchTs: 1712593532000,
199
+ },
189
200
  ];
190
201
  exports.SpotMarkets = {
191
202
  devnet: exports.DevnetSpotMarkets,
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.75.0",
2
+ "version": "2.76.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -2696,6 +2696,52 @@
2696
2696
  }
2697
2697
  ]
2698
2698
  },
2699
+ {
2700
+ "name": "deleteInitializedSpotMarket",
2701
+ "accounts": [
2702
+ {
2703
+ "name": "admin",
2704
+ "isMut": true,
2705
+ "isSigner": true
2706
+ },
2707
+ {
2708
+ "name": "state",
2709
+ "isMut": true,
2710
+ "isSigner": false
2711
+ },
2712
+ {
2713
+ "name": "spotMarket",
2714
+ "isMut": true,
2715
+ "isSigner": false
2716
+ },
2717
+ {
2718
+ "name": "spotMarketVault",
2719
+ "isMut": true,
2720
+ "isSigner": false
2721
+ },
2722
+ {
2723
+ "name": "insuranceFundVault",
2724
+ "isMut": true,
2725
+ "isSigner": false
2726
+ },
2727
+ {
2728
+ "name": "driftSigner",
2729
+ "isMut": false,
2730
+ "isSigner": false
2731
+ },
2732
+ {
2733
+ "name": "tokenProgram",
2734
+ "isMut": false,
2735
+ "isSigner": false
2736
+ }
2737
+ ],
2738
+ "args": [
2739
+ {
2740
+ "name": "marketIndex",
2741
+ "type": "u16"
2742
+ }
2743
+ ]
2744
+ },
2699
2745
  {
2700
2746
  "name": "initializeSerumFulfillmentConfig",
2701
2747
  "accounts": [
@@ -0,0 +1,3 @@
1
+ import { HeliusPriorityFeeLevels } from './heliusPriorityFeeMethod';
2
+ export type DriftPriorityFeeResponse = HeliusPriorityFeeLevels[];
3
+ export declare function fetchDriftPriorityFee(url: string, marketType: string, marketIndex: number): Promise<DriftPriorityFeeResponse>;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.fetchDriftPriorityFee = void 0;
7
+ const node_fetch_1 = __importDefault(require("node-fetch"));
8
+ async function fetchDriftPriorityFee(url, marketType, marketIndex) {
9
+ const response = await (0, node_fetch_1.default)(`${url}?marketType=${marketType}&marketIndex=${marketIndex}`);
10
+ return await response.json();
11
+ }
12
+ exports.fetchDriftPriorityFee = fetchDriftPriorityFee;
@@ -3,10 +3,16 @@ import { PriorityFeeMethod, PriorityFeeStrategy, PriorityFeeSubscriberConfig } f
3
3
  import { AverageOverSlotsStrategy } from './averageOverSlotsStrategy';
4
4
  import { MaxOverSlotsStrategy } from './maxOverSlotsStrategy';
5
5
  import { HeliusPriorityFeeLevels, HeliusPriorityLevel } from './heliusPriorityFeeMethod';
6
+ import { MarketType } from '..';
7
+ export type DriftMarketInfo = {
8
+ marketType: string;
9
+ marketIndex: number;
10
+ };
6
11
  export declare class PriorityFeeSubscriber {
7
12
  connection: Connection;
8
13
  frequencyMs: number;
9
14
  addresses: string[];
15
+ driftMarket?: DriftMarketInfo;
10
16
  customStrategy?: PriorityFeeStrategy;
11
17
  averageStrategy: AverageOverSlotsStrategy;
12
18
  maxStrategy: MaxOverSlotsStrategy;
@@ -14,6 +20,7 @@ export declare class PriorityFeeSubscriber {
14
20
  lookbackDistance: number;
15
21
  maxFeeMicroLamports?: number;
16
22
  priorityFeeMultiplier?: number;
23
+ driftPriorityFeeEndpoint?: string;
17
24
  heliusRpcUrl?: string;
18
25
  lastHeliusSample?: HeliusPriorityFeeLevels;
19
26
  intervalId?: ReturnType<typeof setTimeout>;
@@ -26,6 +33,7 @@ export declare class PriorityFeeSubscriber {
26
33
  subscribe(): Promise<void>;
27
34
  private loadForSolana;
28
35
  private loadForHelius;
36
+ private loadForDrift;
29
37
  getMaxPriorityFee(): number | undefined;
30
38
  updateMaxPriorityFee(newMaxFee: number | undefined): void;
31
39
  getPriorityFeeMultiplier(): number;
@@ -38,4 +46,5 @@ export declare class PriorityFeeSubscriber {
38
46
  load(): Promise<void>;
39
47
  unsubscribe(): Promise<void>;
40
48
  updateAddresses(addresses: PublicKey[]): void;
49
+ updateMarketTypeAndIndex(marketType: MarketType, marketIndex: number): void;
41
50
  }
@@ -6,6 +6,8 @@ const averageOverSlotsStrategy_1 = require("./averageOverSlotsStrategy");
6
6
  const maxOverSlotsStrategy_1 = require("./maxOverSlotsStrategy");
7
7
  const solanaPriorityFeeMethod_1 = require("./solanaPriorityFeeMethod");
8
8
  const heliusPriorityFeeMethod_1 = require("./heliusPriorityFeeMethod");
9
+ const driftPriorityFeeMethod_1 = require("./driftPriorityFeeMethod");
10
+ const __1 = require("..");
9
11
  class PriorityFeeSubscriber {
10
12
  constructor(config) {
11
13
  var _a, _b;
@@ -19,7 +21,10 @@ class PriorityFeeSubscriber {
19
21
  this.lastSlotSeen = 0;
20
22
  this.connection = config.connection;
21
23
  this.frequencyMs = config.frequencyMs;
22
- this.addresses = config.addresses.map((address) => address.toBase58());
24
+ this.addresses = config.addresses
25
+ ? config.addresses.map((address) => address.toBase58())
26
+ : [];
27
+ this.driftMarket = config.driftMarket;
23
28
  if (config.customStrategy) {
24
29
  this.customStrategy = config.customStrategy;
25
30
  }
@@ -42,6 +47,9 @@ class PriorityFeeSubscriber {
42
47
  this.heliusRpcUrl = config.heliusRpcUrl;
43
48
  }
44
49
  }
50
+ else if (this.priorityFeeMethod === types_1.PriorityFeeMethod.DRIFT) {
51
+ this.driftPriorityFeeEndpoint = config.driftPriorityFeeEndpoint;
52
+ }
45
53
  }
46
54
  if (this.priorityFeeMethod === types_1.PriorityFeeMethod.SOLANA) {
47
55
  if (this.connection === undefined) {
@@ -74,14 +82,34 @@ class PriorityFeeSubscriber {
74
82
  this.lastHeliusSample = (_b = (_a = sample === null || sample === void 0 ? void 0 : sample.result) === null || _a === void 0 ? void 0 : _a.priorityFeeLevels) !== null && _b !== void 0 ? _b : undefined;
75
83
  if (this.lastHeliusSample) {
76
84
  this.lastAvgStrategyResult =
77
- this.heliusRpcUrl[heliusPriorityFeeMethod_1.HeliusPriorityLevel.MEDIUM];
85
+ this.lastHeliusSample[heliusPriorityFeeMethod_1.HeliusPriorityLevel.MEDIUM];
78
86
  this.lastMaxStrategyResult =
79
- this.heliusRpcUrl[heliusPriorityFeeMethod_1.HeliusPriorityLevel.UNSAFE_MAX];
87
+ this.lastHeliusSample[heliusPriorityFeeMethod_1.HeliusPriorityLevel.UNSAFE_MAX];
80
88
  if (this.customStrategy) {
81
89
  this.lastCustomStrategyResult = this.customStrategy.calculate(sample);
82
90
  }
83
91
  }
84
92
  }
93
+ async loadForDrift() {
94
+ if (!this.driftMarket) {
95
+ return;
96
+ }
97
+ const sample = await (0, driftPriorityFeeMethod_1.fetchDriftPriorityFee)(this.driftPriorityFeeEndpoint, this.driftMarket.marketType, this.driftMarket.marketIndex);
98
+ if (sample.length > 0) {
99
+ this.lastAvgStrategyResult = sample[heliusPriorityFeeMethod_1.HeliusPriorityLevel.MEDIUM];
100
+ this.lastMaxStrategyResult = sample[heliusPriorityFeeMethod_1.HeliusPriorityLevel.UNSAFE_MAX];
101
+ if (this.customStrategy) {
102
+ // stay compatible with helius response
103
+ this.lastCustomStrategyResult = this.customStrategy.calculate({
104
+ jsonrpc: '',
105
+ result: {
106
+ priorityFeeLevels: sample[0],
107
+ },
108
+ id: '',
109
+ });
110
+ }
111
+ }
112
+ }
85
113
  getMaxPriorityFee() {
86
114
  return this.maxFeeMicroLamports;
87
115
  }
@@ -136,6 +164,9 @@ class PriorityFeeSubscriber {
136
164
  else if (this.priorityFeeMethod === types_1.PriorityFeeMethod.HELIUS) {
137
165
  await this.loadForHelius();
138
166
  }
167
+ else if (this.priorityFeeMethod === types_1.PriorityFeeMethod.DRIFT) {
168
+ await this.loadForDrift();
169
+ }
139
170
  else {
140
171
  throw new Error(`${this.priorityFeeMethod} load not implemented`);
141
172
  }
@@ -155,5 +186,8 @@ class PriorityFeeSubscriber {
155
186
  updateAddresses(addresses) {
156
187
  this.addresses = addresses.map((k) => k.toBase58());
157
188
  }
189
+ updateMarketTypeAndIndex(marketType, marketIndex) {
190
+ this.driftMarket = { marketType: (0, __1.getVariant)(marketType), marketIndex };
191
+ }
158
192
  }
159
193
  exports.PriorityFeeSubscriber = PriorityFeeSubscriber;
@@ -1,21 +1,25 @@
1
1
  import { Connection, PublicKey } from '@solana/web3.js';
2
2
  import { SolanaPriorityFeeResponse } from './solanaPriorityFeeMethod';
3
3
  import { HeliusPriorityFeeResponse } from './heliusPriorityFeeMethod';
4
+ import { DriftMarketInfo } from './priorityFeeSubscriber';
4
5
  export interface PriorityFeeStrategy {
5
6
  calculate(samples: SolanaPriorityFeeResponse[] | HeliusPriorityFeeResponse): number;
6
7
  }
7
8
  export declare enum PriorityFeeMethod {
8
9
  SOLANA = "solana",
9
- HELIUS = "helius"
10
+ HELIUS = "helius",
11
+ DRIFT = "drift"
10
12
  }
11
13
  export type PriorityFeeSubscriberConfig = {
12
14
  connection?: Connection;
13
15
  frequencyMs: number;
14
- addresses: PublicKey[];
16
+ addresses?: PublicKey[];
17
+ driftMarket?: DriftMarketInfo;
15
18
  customStrategy?: PriorityFeeStrategy;
16
19
  priorityFeeMethod?: PriorityFeeMethod;
17
20
  slotsToCheck?: number;
18
21
  heliusRpcUrl?: string;
22
+ driftPriorityFeeEndpoint?: string;
19
23
  maxFeeMicroLamports?: number;
20
24
  priorityFeeMultiplier?: number;
21
25
  };
@@ -5,4 +5,5 @@ var PriorityFeeMethod;
5
5
  (function (PriorityFeeMethod) {
6
6
  PriorityFeeMethod["SOLANA"] = "solana";
7
7
  PriorityFeeMethod["HELIUS"] = "helius";
8
+ PriorityFeeMethod["DRIFT"] = "drift";
8
9
  })(PriorityFeeMethod = exports.PriorityFeeMethod || (exports.PriorityFeeMethod = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.76.0-beta.9",
3
+ "version": "2.77.0-beta.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -227,6 +227,55 @@ export class AdminClient extends DriftClient {
227
227
  return initializeIx;
228
228
  }
229
229
 
230
+ public async deleteInitializedSpotMarket(
231
+ marketIndex: number
232
+ ): Promise<TransactionSignature> {
233
+ const deleteInitializeMarketIx =
234
+ await this.getDeleteInitializedSpotMarketIx(marketIndex);
235
+
236
+ const tx = await this.buildTransaction(deleteInitializeMarketIx);
237
+
238
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
239
+
240
+ return txSig;
241
+ }
242
+
243
+ public async getDeleteInitializedSpotMarketIx(
244
+ marketIndex: number
245
+ ): Promise<TransactionInstruction> {
246
+ const spotMarketPublicKey = await getSpotMarketPublicKey(
247
+ this.program.programId,
248
+ marketIndex
249
+ );
250
+
251
+ const spotMarketVaultPublicKey = await getSpotMarketVaultPublicKey(
252
+ this.program.programId,
253
+ marketIndex
254
+ );
255
+
256
+ const insuranceFundVaultPublicKey = await getInsuranceFundVaultPublicKey(
257
+ this.program.programId,
258
+ marketIndex
259
+ );
260
+
261
+ return await this.program.instruction.deleteInitializedSpotMarket(
262
+ marketIndex,
263
+ {
264
+ accounts: {
265
+ state: await this.getStatePublicKey(),
266
+ admin: this.isSubscribed
267
+ ? this.getStateAccount().admin
268
+ : this.wallet.publicKey,
269
+ spotMarket: spotMarketPublicKey,
270
+ spotMarketVault: spotMarketVaultPublicKey,
271
+ insuranceFundVault: insuranceFundVaultPublicKey,
272
+ driftSigner: this.getSignerPublicKey(),
273
+ tokenProgram: TOKEN_PROGRAM_ID,
274
+ },
275
+ }
276
+ );
277
+ }
278
+
230
279
  public async initializeSerumFulfillmentConfig(
231
280
  marketIndex: number,
232
281
  serumMarket: PublicKey,
@@ -557,6 +557,16 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
557
557
  launchTs: 1712240681000,
558
558
  oracleSource: OracleSource.Prelaunch,
559
559
  },
560
+ {
561
+ fullName: 'Tensor',
562
+ category: ['NFT', 'Solana'],
563
+ symbol: 'TNSR-PERP',
564
+ baseAssetSymbol: 'TNSR',
565
+ marketIndex: 29,
566
+ oracle: new PublicKey('7Cfyymx49ipGsgEsCA2XygAB2DUsan4C6Cyb5c8oR5st'),
567
+ launchTs: 1712593532000,
568
+ oracleSource: OracleSource.SWITCHBOARD,
569
+ },
560
570
  ];
561
571
 
562
572
  export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {
@@ -227,6 +227,19 @@ export const MainnetSpotMarkets: SpotMarketConfig[] = [
227
227
  ),
228
228
  launchTs: 1712149014000,
229
229
  },
230
+ {
231
+ symbol: 'TNSR',
232
+ marketIndex: 14,
233
+ oracle: new PublicKey('7Cfyymx49ipGsgEsCA2XygAB2DUsan4C6Cyb5c8oR5st'),
234
+ oracleSource: OracleSource.SWITCHBOARD,
235
+ mint: new PublicKey('TNSRxcUxoT9xBG3de7PiJyTDYu7kskLqcpddxnEJAS6'),
236
+ precision: new BN(10).pow(NINE),
237
+ precisionExp: NINE,
238
+ phoenixMarket: new PublicKey(
239
+ 'AbJCZ9TAJiby5AY3cHcXS2gUdENC6mtsm6m7XpC2ZMvE'
240
+ ),
241
+ launchTs: 1712593532000,
242
+ },
230
243
  ];
231
244
 
232
245
  export const SpotMarkets: { [key in DriftEnv]: SpotMarketConfig[] } = {
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.75.0",
2
+ "version": "2.76.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -2696,6 +2696,52 @@
2696
2696
  }
2697
2697
  ]
2698
2698
  },
2699
+ {
2700
+ "name": "deleteInitializedSpotMarket",
2701
+ "accounts": [
2702
+ {
2703
+ "name": "admin",
2704
+ "isMut": true,
2705
+ "isSigner": true
2706
+ },
2707
+ {
2708
+ "name": "state",
2709
+ "isMut": true,
2710
+ "isSigner": false
2711
+ },
2712
+ {
2713
+ "name": "spotMarket",
2714
+ "isMut": true,
2715
+ "isSigner": false
2716
+ },
2717
+ {
2718
+ "name": "spotMarketVault",
2719
+ "isMut": true,
2720
+ "isSigner": false
2721
+ },
2722
+ {
2723
+ "name": "insuranceFundVault",
2724
+ "isMut": true,
2725
+ "isSigner": false
2726
+ },
2727
+ {
2728
+ "name": "driftSigner",
2729
+ "isMut": false,
2730
+ "isSigner": false
2731
+ },
2732
+ {
2733
+ "name": "tokenProgram",
2734
+ "isMut": false,
2735
+ "isSigner": false
2736
+ }
2737
+ ],
2738
+ "args": [
2739
+ {
2740
+ "name": "marketIndex",
2741
+ "type": "u16"
2742
+ }
2743
+ ]
2744
+ },
2699
2745
  {
2700
2746
  "name": "initializeSerumFulfillmentConfig",
2701
2747
  "accounts": [
@@ -0,0 +1,15 @@
1
+ import fetch from 'node-fetch';
2
+ import { HeliusPriorityFeeLevels } from './heliusPriorityFeeMethod';
3
+
4
+ export type DriftPriorityFeeResponse = HeliusPriorityFeeLevels[];
5
+
6
+ export async function fetchDriftPriorityFee(
7
+ url: string,
8
+ marketType: string,
9
+ marketIndex: number
10
+ ): Promise<DriftPriorityFeeResponse> {
11
+ const response = await fetch(
12
+ `${url}?marketType=${marketType}&marketIndex=${marketIndex}`
13
+ );
14
+ return await response.json();
15
+ }
@@ -12,11 +12,19 @@ import {
12
12
  HeliusPriorityLevel,
13
13
  fetchHeliusPriorityFee,
14
14
  } from './heliusPriorityFeeMethod';
15
+ import { fetchDriftPriorityFee } from './driftPriorityFeeMethod';
16
+ import { getVariant, MarketType } from '..';
17
+
18
+ export type DriftMarketInfo = {
19
+ marketType: string;
20
+ marketIndex: number;
21
+ };
15
22
 
16
23
  export class PriorityFeeSubscriber {
17
24
  connection: Connection;
18
25
  frequencyMs: number;
19
26
  addresses: string[];
27
+ driftMarket?: DriftMarketInfo;
20
28
  customStrategy?: PriorityFeeStrategy;
21
29
  averageStrategy = new AverageOverSlotsStrategy();
22
30
  maxStrategy = new MaxOverSlotsStrategy();
@@ -25,6 +33,7 @@ export class PriorityFeeSubscriber {
25
33
  maxFeeMicroLamports?: number;
26
34
  priorityFeeMultiplier?: number;
27
35
 
36
+ driftPriorityFeeEndpoint?: string;
28
37
  heliusRpcUrl?: string;
29
38
  lastHeliusSample?: HeliusPriorityFeeLevels;
30
39
 
@@ -39,13 +48,18 @@ export class PriorityFeeSubscriber {
39
48
  public constructor(config: PriorityFeeSubscriberConfig) {
40
49
  this.connection = config.connection;
41
50
  this.frequencyMs = config.frequencyMs;
42
- this.addresses = config.addresses.map((address) => address.toBase58());
51
+ this.addresses = config.addresses
52
+ ? config.addresses.map((address) => address.toBase58())
53
+ : [];
54
+ this.driftMarket = config.driftMarket;
55
+
43
56
  if (config.customStrategy) {
44
57
  this.customStrategy = config.customStrategy;
45
58
  } else {
46
59
  this.customStrategy = this.averageStrategy;
47
60
  }
48
61
  this.lookbackDistance = config.slotsToCheck ?? 50;
62
+
49
63
  if (config.priorityFeeMethod) {
50
64
  this.priorityFeeMethod = config.priorityFeeMethod;
51
65
 
@@ -61,6 +75,8 @@ export class PriorityFeeSubscriber {
61
75
  } else {
62
76
  this.heliusRpcUrl = config.heliusRpcUrl;
63
77
  }
78
+ } else if (this.priorityFeeMethod === PriorityFeeMethod.DRIFT) {
79
+ this.driftPriorityFeeEndpoint = config.driftPriorityFeeEndpoint;
64
80
  }
65
81
  }
66
82
 
@@ -111,15 +127,40 @@ export class PriorityFeeSubscriber {
111
127
 
112
128
  if (this.lastHeliusSample) {
113
129
  this.lastAvgStrategyResult =
114
- this.heliusRpcUrl[HeliusPriorityLevel.MEDIUM];
130
+ this.lastHeliusSample[HeliusPriorityLevel.MEDIUM];
115
131
  this.lastMaxStrategyResult =
116
- this.heliusRpcUrl[HeliusPriorityLevel.UNSAFE_MAX];
132
+ this.lastHeliusSample[HeliusPriorityLevel.UNSAFE_MAX];
117
133
  if (this.customStrategy) {
118
134
  this.lastCustomStrategyResult = this.customStrategy.calculate(sample!);
119
135
  }
120
136
  }
121
137
  }
122
138
 
139
+ private async loadForDrift(): Promise<void> {
140
+ if (!this.driftMarket) {
141
+ return;
142
+ }
143
+ const sample = await fetchDriftPriorityFee(
144
+ this.driftPriorityFeeEndpoint!,
145
+ this.driftMarket.marketType,
146
+ this.driftMarket.marketIndex
147
+ );
148
+ if (sample.length > 0) {
149
+ this.lastAvgStrategyResult = sample[HeliusPriorityLevel.MEDIUM];
150
+ this.lastMaxStrategyResult = sample[HeliusPriorityLevel.UNSAFE_MAX];
151
+ if (this.customStrategy) {
152
+ // stay compatible with helius response
153
+ this.lastCustomStrategyResult = this.customStrategy.calculate({
154
+ jsonrpc: '',
155
+ result: {
156
+ priorityFeeLevels: sample[0],
157
+ },
158
+ id: '',
159
+ });
160
+ }
161
+ }
162
+ }
163
+
123
164
  public getMaxPriorityFee(): number | undefined {
124
165
  return this.maxFeeMicroLamports;
125
166
  }
@@ -183,6 +224,8 @@ export class PriorityFeeSubscriber {
183
224
  await this.loadForSolana();
184
225
  } else if (this.priorityFeeMethod === PriorityFeeMethod.HELIUS) {
185
226
  await this.loadForHelius();
227
+ } else if (this.priorityFeeMethod === PriorityFeeMethod.DRIFT) {
228
+ await this.loadForDrift();
186
229
  } else {
187
230
  throw new Error(`${this.priorityFeeMethod} load not implemented`);
188
231
  }
@@ -207,4 +250,8 @@ export class PriorityFeeSubscriber {
207
250
  public updateAddresses(addresses: PublicKey[]) {
208
251
  this.addresses = addresses.map((k) => k.toBase58());
209
252
  }
253
+
254
+ public updateMarketTypeAndIndex(marketType: MarketType, marketIndex: number) {
255
+ this.driftMarket = { marketType: getVariant(marketType), marketIndex };
256
+ }
210
257
  }
@@ -1,6 +1,7 @@
1
1
  import { Connection, PublicKey } from '@solana/web3.js';
2
2
  import { SolanaPriorityFeeResponse } from './solanaPriorityFeeMethod';
3
3
  import { HeliusPriorityFeeResponse } from './heliusPriorityFeeMethod';
4
+ import { DriftMarketInfo } from './priorityFeeSubscriber';
4
5
 
5
6
  export interface PriorityFeeStrategy {
6
7
  // calculate the priority fee for a given set of samples.
@@ -13,6 +14,7 @@ export interface PriorityFeeStrategy {
13
14
  export enum PriorityFeeMethod {
14
15
  SOLANA = 'solana',
15
16
  HELIUS = 'helius',
17
+ DRIFT = 'drift',
16
18
  }
17
19
 
18
20
  export type PriorityFeeSubscriberConfig = {
@@ -21,7 +23,9 @@ export type PriorityFeeSubscriberConfig = {
21
23
  /// frequency to make RPC calls to update priority fee samples, in milliseconds
22
24
  frequencyMs: number;
23
25
  /// addresses you plan to write lock, used to determine priority fees
24
- addresses: PublicKey[];
26
+ addresses?: PublicKey[];
27
+ /// drift market type and index, optionally provide at initialization time if using priorityFeeMethod.DRIFT
28
+ driftMarket?: DriftMarketInfo;
25
29
  /// custom strategy to calculate priority fees, defaults to AVERAGE
26
30
  customStrategy?: PriorityFeeStrategy;
27
31
  /// method for fetching priority fee samples
@@ -30,6 +34,8 @@ export type PriorityFeeSubscriberConfig = {
30
34
  slotsToCheck?: number;
31
35
  /// url for helius rpc, required if using priorityFeeMethod.HELIUS
32
36
  heliusRpcUrl?: string;
37
+ /// url for drift cached priority fee endpoint, required if using priorityFeeMethod.DRIFT
38
+ driftPriorityFeeEndpoint?: string;
33
39
  /// clamp any returned priority fee value to this value.
34
40
  maxFeeMicroLamports?: number;
35
41
  /// multiplier applied to priority fee before maxFeeMicroLamports, defaults to 1.0