@drift-labs/sdk 2.82.0-beta.2 → 2.82.0-beta.4
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/priorityFee/driftPriorityFeeMethod.d.ts +13 -3
- package/lib/priorityFee/driftPriorityFeeMethod.js +2 -2
- package/lib/priorityFee/index.d.ts +2 -0
- package/lib/priorityFee/index.js +2 -0
- package/lib/priorityFee/priorityFeeSubscriber.d.ts +1 -4
- package/lib/priorityFee/priorityFeeSubscriber.js +5 -4
- package/lib/priorityFee/priorityFeeSubscriberMap.d.ts +48 -0
- package/lib/priorityFee/priorityFeeSubscriberMap.js +88 -0
- package/lib/priorityFee/types.d.ts +8 -3
- package/lib/priorityFee/types.js +2 -1
- package/package.json +1 -1
- package/src/priorityFee/driftPriorityFeeMethod.ts +16 -4
- package/src/priorityFee/index.ts +2 -0
- package/src/priorityFee/priorityFeeSubscriber.ts +7 -7
- package/src/priorityFee/priorityFeeSubscriberMap.ts +112 -0
- package/src/priorityFee/types.ts +16 -3
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.82.0-beta.
|
|
1
|
+
2.82.0-beta.4
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export type
|
|
3
|
-
|
|
1
|
+
import { HeliusPriorityLevel } from './heliusPriorityFeeMethod';
|
|
2
|
+
export type DriftMarketInfo = {
|
|
3
|
+
marketType: string;
|
|
4
|
+
marketIndex: number;
|
|
5
|
+
};
|
|
6
|
+
export type DriftPriorityFeeLevels = {
|
|
7
|
+
[key in HeliusPriorityLevel]: number;
|
|
8
|
+
} & {
|
|
9
|
+
marketType: 'perp' | 'spot';
|
|
10
|
+
marketIndex: number;
|
|
11
|
+
};
|
|
12
|
+
export type DriftPriorityFeeResponse = DriftPriorityFeeLevels[];
|
|
13
|
+
export declare function fetchDriftPriorityFee(url: string, marketTypes: string[], marketIndexes: number[]): Promise<DriftPriorityFeeResponse>;
|
|
@@ -5,9 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.fetchDriftPriorityFee = void 0;
|
|
7
7
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
|
-
async function fetchDriftPriorityFee(url, marketTypes,
|
|
8
|
+
async function fetchDriftPriorityFee(url, marketTypes, marketIndexes) {
|
|
9
9
|
try {
|
|
10
|
-
const response = await (0, node_fetch_1.default)(`${url}/batchPriorityFees?marketType=${marketTypes.join(',')}&marketIndex=${
|
|
10
|
+
const response = await (0, node_fetch_1.default)(`${url}/batchPriorityFees?marketType=${marketTypes.join(',')}&marketIndex=${marketIndexes.join(',')}`);
|
|
11
11
|
if (!response.ok) {
|
|
12
12
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
13
13
|
}
|
|
@@ -4,6 +4,8 @@ export * from './ewmaStrategy';
|
|
|
4
4
|
export * from './maxOverSlotsStrategy';
|
|
5
5
|
export * from './maxStrategy';
|
|
6
6
|
export * from './priorityFeeSubscriber';
|
|
7
|
+
export * from './priorityFeeSubscriberMap';
|
|
7
8
|
export * from './solanaPriorityFeeMethod';
|
|
8
9
|
export * from './heliusPriorityFeeMethod';
|
|
10
|
+
export * from './driftPriorityFeeMethod';
|
|
9
11
|
export * from './types';
|
package/lib/priorityFee/index.js
CHANGED
|
@@ -20,6 +20,8 @@ __exportStar(require("./ewmaStrategy"), exports);
|
|
|
20
20
|
__exportStar(require("./maxOverSlotsStrategy"), exports);
|
|
21
21
|
__exportStar(require("./maxStrategy"), exports);
|
|
22
22
|
__exportStar(require("./priorityFeeSubscriber"), exports);
|
|
23
|
+
__exportStar(require("./priorityFeeSubscriberMap"), exports);
|
|
23
24
|
__exportStar(require("./solanaPriorityFeeMethod"), exports);
|
|
24
25
|
__exportStar(require("./heliusPriorityFeeMethod"), exports);
|
|
26
|
+
__exportStar(require("./driftPriorityFeeMethod"), exports);
|
|
25
27
|
__exportStar(require("./types"), exports);
|
|
@@ -3,10 +3,7 @@ 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
|
-
|
|
7
|
-
marketType: string;
|
|
8
|
-
marketIndex: number;
|
|
9
|
-
};
|
|
6
|
+
import { DriftMarketInfo } from './driftPriorityFeeMethod';
|
|
10
7
|
export declare class PriorityFeeSubscriber {
|
|
11
8
|
connection: Connection;
|
|
12
9
|
frequencyMs: number;
|
|
@@ -9,7 +9,7 @@ const heliusPriorityFeeMethod_1 = require("./heliusPriorityFeeMethod");
|
|
|
9
9
|
const driftPriorityFeeMethod_1 = require("./driftPriorityFeeMethod");
|
|
10
10
|
class PriorityFeeSubscriber {
|
|
11
11
|
constructor(config) {
|
|
12
|
-
var _a, _b;
|
|
12
|
+
var _a, _b, _c;
|
|
13
13
|
this.averageStrategy = new averageOverSlotsStrategy_1.AverageOverSlotsStrategy();
|
|
14
14
|
this.maxStrategy = new maxOverSlotsStrategy_1.MaxOverSlotsStrategy();
|
|
15
15
|
this.priorityFeeMethod = types_1.PriorityFeeMethod.SOLANA;
|
|
@@ -19,7 +19,8 @@ class PriorityFeeSubscriber {
|
|
|
19
19
|
this.lastMaxStrategyResult = 0;
|
|
20
20
|
this.lastSlotSeen = 0;
|
|
21
21
|
this.connection = config.connection;
|
|
22
|
-
this.frequencyMs =
|
|
22
|
+
this.frequencyMs =
|
|
23
|
+
(_a = config.frequencyMs) !== null && _a !== void 0 ? _a : types_1.DEFAULT_PRIORITY_FEE_MAP_FREQUENCY_MS;
|
|
23
24
|
this.addresses = config.addresses
|
|
24
25
|
? config.addresses.map((address) => address.toBase58())
|
|
25
26
|
: [];
|
|
@@ -30,7 +31,7 @@ class PriorityFeeSubscriber {
|
|
|
30
31
|
else {
|
|
31
32
|
this.customStrategy = this.averageStrategy;
|
|
32
33
|
}
|
|
33
|
-
this.lookbackDistance = (
|
|
34
|
+
this.lookbackDistance = (_b = config.slotsToCheck) !== null && _b !== void 0 ? _b : 50;
|
|
34
35
|
if (config.priorityFeeMethod) {
|
|
35
36
|
this.priorityFeeMethod = config.priorityFeeMethod;
|
|
36
37
|
if (this.priorityFeeMethod === types_1.PriorityFeeMethod.HELIUS) {
|
|
@@ -56,7 +57,7 @@ class PriorityFeeSubscriber {
|
|
|
56
57
|
}
|
|
57
58
|
}
|
|
58
59
|
this.maxFeeMicroLamports = config.maxFeeMicroLamports;
|
|
59
|
-
this.priorityFeeMultiplier = (
|
|
60
|
+
this.priorityFeeMultiplier = (_c = config.priorityFeeMultiplier) !== null && _c !== void 0 ? _c : 1.0;
|
|
60
61
|
}
|
|
61
62
|
async subscribe() {
|
|
62
63
|
if (this.intervalId) {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { DriftMarketInfo, DriftPriorityFeeLevels } from './driftPriorityFeeMethod';
|
|
2
|
+
import { PriorityFeeSubscriberMapConfig } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* takes advantage of /batchPriorityFees endpoint from drift hosted priority fee service
|
|
5
|
+
*/
|
|
6
|
+
export declare class PriorityFeeSubscriberMap {
|
|
7
|
+
frequencyMs: number;
|
|
8
|
+
intervalId?: ReturnType<typeof setTimeout>;
|
|
9
|
+
driftMarkets?: DriftMarketInfo[];
|
|
10
|
+
driftPriorityFeeEndpoint?: string;
|
|
11
|
+
feesMap: Map<string, Map<number, DriftPriorityFeeLevels>>;
|
|
12
|
+
constructor(config: PriorityFeeSubscriberMapConfig);
|
|
13
|
+
private updateFeesMap;
|
|
14
|
+
subscribe(): Promise<void>;
|
|
15
|
+
unsubscribe(): Promise<void>;
|
|
16
|
+
load(): Promise<void>;
|
|
17
|
+
updateMarketTypeAndIndex(driftMarkets: DriftMarketInfo[]): void;
|
|
18
|
+
getPriorityFees(marketType: string, marketIndex: number): DriftPriorityFeeLevels | undefined;
|
|
19
|
+
}
|
|
20
|
+
/** Example usage:
|
|
21
|
+
async function main() {
|
|
22
|
+
const driftMarkets: DriftMarketInfo[] = [
|
|
23
|
+
{ marketType: 'perp', marketIndex: 0 },
|
|
24
|
+
{ marketType: 'perp', marketIndex: 1 },
|
|
25
|
+
{ marketType: 'spot', marketIndex: 2 }
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
const subscriber = new PriorityFeeSubscriberMap({
|
|
29
|
+
driftPriorityFeeEndpoint: 'https://dlob.drift.trade',
|
|
30
|
+
frequencyMs: 5000,
|
|
31
|
+
driftMarkets
|
|
32
|
+
});
|
|
33
|
+
await subscriber.subscribe();
|
|
34
|
+
|
|
35
|
+
for (let i = 0; i < 20; i++) {
|
|
36
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
37
|
+
driftMarkets.forEach(market => {
|
|
38
|
+
const fees = subscriber.getPriorityFees(market.marketType, market.marketIndex);
|
|
39
|
+
console.log(`Priority fees for ${market.marketType} market ${market.marketIndex}:`, fees);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
await subscriber.unsubscribe();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
main().catch(console.error);
|
|
48
|
+
*/
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PriorityFeeSubscriberMap = void 0;
|
|
4
|
+
const driftPriorityFeeMethod_1 = require("./driftPriorityFeeMethod");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
/**
|
|
7
|
+
* takes advantage of /batchPriorityFees endpoint from drift hosted priority fee service
|
|
8
|
+
*/
|
|
9
|
+
class PriorityFeeSubscriberMap {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
var _a;
|
|
12
|
+
this.frequencyMs = config.frequencyMs;
|
|
13
|
+
this.frequencyMs =
|
|
14
|
+
(_a = config.frequencyMs) !== null && _a !== void 0 ? _a : types_1.DEFAULT_PRIORITY_FEE_MAP_FREQUENCY_MS;
|
|
15
|
+
this.driftPriorityFeeEndpoint = config.driftPriorityFeeEndpoint;
|
|
16
|
+
this.driftMarkets = config.driftMarkets;
|
|
17
|
+
this.feesMap = new Map();
|
|
18
|
+
this.feesMap.set('perp', new Map());
|
|
19
|
+
this.feesMap.set('spot', new Map());
|
|
20
|
+
}
|
|
21
|
+
updateFeesMap(driftPriorityFeeResponse) {
|
|
22
|
+
driftPriorityFeeResponse.forEach((fee) => {
|
|
23
|
+
this.feesMap.get(fee.marketType).set(fee.marketIndex, fee);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async subscribe() {
|
|
27
|
+
if (this.intervalId) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
await this.load();
|
|
31
|
+
this.intervalId = setInterval(this.load.bind(this), this.frequencyMs);
|
|
32
|
+
}
|
|
33
|
+
async unsubscribe() {
|
|
34
|
+
if (this.intervalId) {
|
|
35
|
+
clearInterval(this.intervalId);
|
|
36
|
+
this.intervalId = undefined;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async load() {
|
|
40
|
+
try {
|
|
41
|
+
if (!this.driftMarkets) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const fees = await (0, driftPriorityFeeMethod_1.fetchDriftPriorityFee)(this.driftPriorityFeeEndpoint, this.driftMarkets.map((m) => m.marketType), this.driftMarkets.map((m) => m.marketIndex));
|
|
45
|
+
this.updateFeesMap(fees);
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
console.error('Error fetching drift priority fees', e);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
updateMarketTypeAndIndex(driftMarkets) {
|
|
52
|
+
this.driftMarkets = driftMarkets;
|
|
53
|
+
}
|
|
54
|
+
getPriorityFees(marketType, marketIndex) {
|
|
55
|
+
var _a;
|
|
56
|
+
return (_a = this.feesMap.get(marketType)) === null || _a === void 0 ? void 0 : _a.get(marketIndex);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.PriorityFeeSubscriberMap = PriorityFeeSubscriberMap;
|
|
60
|
+
/** Example usage:
|
|
61
|
+
async function main() {
|
|
62
|
+
const driftMarkets: DriftMarketInfo[] = [
|
|
63
|
+
{ marketType: 'perp', marketIndex: 0 },
|
|
64
|
+
{ marketType: 'perp', marketIndex: 1 },
|
|
65
|
+
{ marketType: 'spot', marketIndex: 2 }
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
const subscriber = new PriorityFeeSubscriberMap({
|
|
69
|
+
driftPriorityFeeEndpoint: 'https://dlob.drift.trade',
|
|
70
|
+
frequencyMs: 5000,
|
|
71
|
+
driftMarkets
|
|
72
|
+
});
|
|
73
|
+
await subscriber.subscribe();
|
|
74
|
+
|
|
75
|
+
for (let i = 0; i < 20; i++) {
|
|
76
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
77
|
+
driftMarkets.forEach(market => {
|
|
78
|
+
const fees = subscriber.getPriorityFees(market.marketType, market.marketIndex);
|
|
79
|
+
console.log(`Priority fees for ${market.marketType} market ${market.marketIndex}:`, fees);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
await subscriber.unsubscribe();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
main().catch(console.error);
|
|
88
|
+
*/
|
|
@@ -1,8 +1,8 @@
|
|
|
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 './
|
|
5
|
-
|
|
4
|
+
import { DriftMarketInfo, DriftPriorityFeeResponse } from './driftPriorityFeeMethod';
|
|
5
|
+
export declare const DEFAULT_PRIORITY_FEE_MAP_FREQUENCY_MS = 10000;
|
|
6
6
|
export interface PriorityFeeStrategy {
|
|
7
7
|
calculate(samples: SolanaPriorityFeeResponse[] | HeliusPriorityFeeResponse | DriftPriorityFeeResponse): number;
|
|
8
8
|
}
|
|
@@ -13,7 +13,7 @@ export declare enum PriorityFeeMethod {
|
|
|
13
13
|
}
|
|
14
14
|
export type PriorityFeeSubscriberConfig = {
|
|
15
15
|
connection?: Connection;
|
|
16
|
-
frequencyMs
|
|
16
|
+
frequencyMs?: number;
|
|
17
17
|
addresses?: PublicKey[];
|
|
18
18
|
driftMarkets?: DriftMarketInfo[];
|
|
19
19
|
customStrategy?: PriorityFeeStrategy;
|
|
@@ -24,3 +24,8 @@ export type PriorityFeeSubscriberConfig = {
|
|
|
24
24
|
maxFeeMicroLamports?: number;
|
|
25
25
|
priorityFeeMultiplier?: number;
|
|
26
26
|
};
|
|
27
|
+
export type PriorityFeeSubscriberMapConfig = {
|
|
28
|
+
frequencyMs?: number;
|
|
29
|
+
driftMarkets?: DriftMarketInfo[];
|
|
30
|
+
driftPriorityFeeEndpoint: string;
|
|
31
|
+
};
|
package/lib/priorityFee/types.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PriorityFeeMethod = void 0;
|
|
3
|
+
exports.PriorityFeeMethod = exports.DEFAULT_PRIORITY_FEE_MAP_FREQUENCY_MS = void 0;
|
|
4
|
+
exports.DEFAULT_PRIORITY_FEE_MAP_FREQUENCY_MS = 10000;
|
|
4
5
|
var PriorityFeeMethod;
|
|
5
6
|
(function (PriorityFeeMethod) {
|
|
6
7
|
PriorityFeeMethod["SOLANA"] = "solana";
|
package/package.json
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
import fetch from 'node-fetch';
|
|
2
|
-
import {
|
|
2
|
+
import { HeliusPriorityLevel } from './heliusPriorityFeeMethod';
|
|
3
3
|
|
|
4
|
-
export type
|
|
4
|
+
export type DriftMarketInfo = {
|
|
5
|
+
marketType: string;
|
|
6
|
+
marketIndex: number;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type DriftPriorityFeeLevels = {
|
|
10
|
+
[key in HeliusPriorityLevel]: number;
|
|
11
|
+
} & {
|
|
12
|
+
marketType: 'perp' | 'spot';
|
|
13
|
+
marketIndex: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type DriftPriorityFeeResponse = DriftPriorityFeeLevels[];
|
|
5
17
|
|
|
6
18
|
export async function fetchDriftPriorityFee(
|
|
7
19
|
url: string,
|
|
8
20
|
marketTypes: string[],
|
|
9
|
-
|
|
21
|
+
marketIndexes: number[]
|
|
10
22
|
): Promise<DriftPriorityFeeResponse> {
|
|
11
23
|
try {
|
|
12
24
|
const response = await fetch(
|
|
13
25
|
`${url}/batchPriorityFees?marketType=${marketTypes.join(
|
|
14
26
|
','
|
|
15
|
-
)}&marketIndex=${
|
|
27
|
+
)}&marketIndex=${marketIndexes.join(',')}`
|
|
16
28
|
);
|
|
17
29
|
if (!response.ok) {
|
|
18
30
|
throw new Error(`HTTP error! status: ${response.status}`);
|
package/src/priorityFee/index.ts
CHANGED
|
@@ -4,6 +4,8 @@ export * from './ewmaStrategy';
|
|
|
4
4
|
export * from './maxOverSlotsStrategy';
|
|
5
5
|
export * from './maxStrategy';
|
|
6
6
|
export * from './priorityFeeSubscriber';
|
|
7
|
+
export * from './priorityFeeSubscriberMap';
|
|
7
8
|
export * from './solanaPriorityFeeMethod';
|
|
8
9
|
export * from './heliusPriorityFeeMethod';
|
|
10
|
+
export * from './driftPriorityFeeMethod';
|
|
9
11
|
export * from './types';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Connection, PublicKey } from '@solana/web3.js';
|
|
2
2
|
import {
|
|
3
|
+
DEFAULT_PRIORITY_FEE_MAP_FREQUENCY_MS,
|
|
3
4
|
PriorityFeeMethod,
|
|
4
5
|
PriorityFeeStrategy,
|
|
5
6
|
PriorityFeeSubscriberConfig,
|
|
@@ -12,12 +13,10 @@ import {
|
|
|
12
13
|
HeliusPriorityLevel,
|
|
13
14
|
fetchHeliusPriorityFee,
|
|
14
15
|
} from './heliusPriorityFeeMethod';
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
marketIndex: number;
|
|
20
|
-
};
|
|
16
|
+
import {
|
|
17
|
+
fetchDriftPriorityFee,
|
|
18
|
+
DriftMarketInfo,
|
|
19
|
+
} from './driftPriorityFeeMethod';
|
|
21
20
|
|
|
22
21
|
export class PriorityFeeSubscriber {
|
|
23
22
|
connection: Connection;
|
|
@@ -46,7 +45,8 @@ export class PriorityFeeSubscriber {
|
|
|
46
45
|
|
|
47
46
|
public constructor(config: PriorityFeeSubscriberConfig) {
|
|
48
47
|
this.connection = config.connection;
|
|
49
|
-
this.frequencyMs =
|
|
48
|
+
this.frequencyMs =
|
|
49
|
+
config.frequencyMs ?? DEFAULT_PRIORITY_FEE_MAP_FREQUENCY_MS;
|
|
50
50
|
this.addresses = config.addresses
|
|
51
51
|
? config.addresses.map((address) => address.toBase58())
|
|
52
52
|
: [];
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DriftMarketInfo,
|
|
3
|
+
DriftPriorityFeeLevels,
|
|
4
|
+
DriftPriorityFeeResponse,
|
|
5
|
+
fetchDriftPriorityFee,
|
|
6
|
+
} from './driftPriorityFeeMethod';
|
|
7
|
+
import {
|
|
8
|
+
DEFAULT_PRIORITY_FEE_MAP_FREQUENCY_MS,
|
|
9
|
+
PriorityFeeSubscriberMapConfig,
|
|
10
|
+
} from './types';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* takes advantage of /batchPriorityFees endpoint from drift hosted priority fee service
|
|
14
|
+
*/
|
|
15
|
+
export class PriorityFeeSubscriberMap {
|
|
16
|
+
frequencyMs: number;
|
|
17
|
+
intervalId?: ReturnType<typeof setTimeout>;
|
|
18
|
+
|
|
19
|
+
driftMarkets?: DriftMarketInfo[];
|
|
20
|
+
driftPriorityFeeEndpoint?: string;
|
|
21
|
+
feesMap: Map<string, Map<number, DriftPriorityFeeLevels>>; // marketType -> marketIndex -> priority fee
|
|
22
|
+
|
|
23
|
+
public constructor(config: PriorityFeeSubscriberMapConfig) {
|
|
24
|
+
this.frequencyMs = config.frequencyMs;
|
|
25
|
+
this.frequencyMs =
|
|
26
|
+
config.frequencyMs ?? DEFAULT_PRIORITY_FEE_MAP_FREQUENCY_MS;
|
|
27
|
+
this.driftPriorityFeeEndpoint = config.driftPriorityFeeEndpoint;
|
|
28
|
+
this.driftMarkets = config.driftMarkets;
|
|
29
|
+
this.feesMap = new Map<string, Map<number, DriftPriorityFeeLevels>>();
|
|
30
|
+
this.feesMap.set('perp', new Map<number, DriftPriorityFeeLevels>());
|
|
31
|
+
this.feesMap.set('spot', new Map<number, DriftPriorityFeeLevels>());
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private updateFeesMap(driftPriorityFeeResponse: DriftPriorityFeeResponse) {
|
|
35
|
+
driftPriorityFeeResponse.forEach((fee: DriftPriorityFeeLevels) => {
|
|
36
|
+
this.feesMap.get(fee.marketType)!.set(fee.marketIndex, fee);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public async subscribe(): Promise<void> {
|
|
41
|
+
if (this.intervalId) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
await this.load();
|
|
46
|
+
this.intervalId = setInterval(this.load.bind(this), this.frequencyMs);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public async unsubscribe(): Promise<void> {
|
|
50
|
+
if (this.intervalId) {
|
|
51
|
+
clearInterval(this.intervalId);
|
|
52
|
+
this.intervalId = undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public async load(): Promise<void> {
|
|
57
|
+
try {
|
|
58
|
+
if (!this.driftMarkets) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const fees = await fetchDriftPriorityFee(
|
|
62
|
+
this.driftPriorityFeeEndpoint!,
|
|
63
|
+
this.driftMarkets.map((m) => m.marketType),
|
|
64
|
+
this.driftMarkets.map((m) => m.marketIndex)
|
|
65
|
+
);
|
|
66
|
+
this.updateFeesMap(fees);
|
|
67
|
+
} catch (e) {
|
|
68
|
+
console.error('Error fetching drift priority fees', e);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public updateMarketTypeAndIndex(driftMarkets: DriftMarketInfo[]) {
|
|
73
|
+
this.driftMarkets = driftMarkets;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public getPriorityFees(
|
|
77
|
+
marketType: string,
|
|
78
|
+
marketIndex: number
|
|
79
|
+
): DriftPriorityFeeLevels | undefined {
|
|
80
|
+
return this.feesMap.get(marketType)?.get(marketIndex);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Example usage:
|
|
85
|
+
async function main() {
|
|
86
|
+
const driftMarkets: DriftMarketInfo[] = [
|
|
87
|
+
{ marketType: 'perp', marketIndex: 0 },
|
|
88
|
+
{ marketType: 'perp', marketIndex: 1 },
|
|
89
|
+
{ marketType: 'spot', marketIndex: 2 }
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
const subscriber = new PriorityFeeSubscriberMap({
|
|
93
|
+
driftPriorityFeeEndpoint: 'https://dlob.drift.trade',
|
|
94
|
+
frequencyMs: 5000,
|
|
95
|
+
driftMarkets
|
|
96
|
+
});
|
|
97
|
+
await subscriber.subscribe();
|
|
98
|
+
|
|
99
|
+
for (let i = 0; i < 20; i++) {
|
|
100
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
101
|
+
driftMarkets.forEach(market => {
|
|
102
|
+
const fees = subscriber.getPriorityFees(market.marketType, market.marketIndex);
|
|
103
|
+
console.log(`Priority fees for ${market.marketType} market ${market.marketIndex}:`, fees);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
await subscriber.unsubscribe();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
main().catch(console.error);
|
|
112
|
+
*/
|
package/src/priorityFee/types.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { Connection, PublicKey } from '@solana/web3.js';
|
|
2
2
|
import { SolanaPriorityFeeResponse } from './solanaPriorityFeeMethod';
|
|
3
3
|
import { HeliusPriorityFeeResponse } from './heliusPriorityFeeMethod';
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
DriftMarketInfo,
|
|
6
|
+
DriftPriorityFeeResponse,
|
|
7
|
+
} from './driftPriorityFeeMethod';
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_PRIORITY_FEE_MAP_FREQUENCY_MS = 10_000;
|
|
6
10
|
|
|
7
11
|
export interface PriorityFeeStrategy {
|
|
8
12
|
// calculate the priority fee for a given set of samples.
|
|
@@ -25,7 +29,7 @@ export type PriorityFeeSubscriberConfig = {
|
|
|
25
29
|
/// rpc connection, optional if using priorityFeeMethod.HELIUS
|
|
26
30
|
connection?: Connection;
|
|
27
31
|
/// frequency to make RPC calls to update priority fee samples, in milliseconds
|
|
28
|
-
frequencyMs
|
|
32
|
+
frequencyMs?: number;
|
|
29
33
|
/// addresses you plan to write lock, used to determine priority fees
|
|
30
34
|
addresses?: PublicKey[];
|
|
31
35
|
/// drift market type and index, optionally provide at initialization time if using priorityFeeMethod.DRIFT
|
|
@@ -45,3 +49,12 @@ export type PriorityFeeSubscriberConfig = {
|
|
|
45
49
|
/// multiplier applied to priority fee before maxFeeMicroLamports, defaults to 1.0
|
|
46
50
|
priorityFeeMultiplier?: number;
|
|
47
51
|
};
|
|
52
|
+
|
|
53
|
+
export type PriorityFeeSubscriberMapConfig = {
|
|
54
|
+
/// frequency to make RPC calls to update priority fee samples, in milliseconds
|
|
55
|
+
frequencyMs?: number;
|
|
56
|
+
/// drift market type and associated market index to query
|
|
57
|
+
driftMarkets?: DriftMarketInfo[];
|
|
58
|
+
/// url for drift cached priority fee endpoint
|
|
59
|
+
driftPriorityFeeEndpoint: string;
|
|
60
|
+
};
|