@drift-labs/sdk 2.82.0-beta.1 → 2.82.0-beta.11
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/README.md +65 -47
- package/VERSION +1 -1
- package/lib/accounts/types.d.ts +4 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +3 -3
- package/lib/accounts/webSocketAccountSubscriber.js +15 -8
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +3 -3
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +5 -5
- package/lib/accounts/webSocketInsuranceFundStakeAccountSubscriber.d.ts +2 -2
- package/lib/accounts/webSocketInsuranceFundStakeAccountSubscriber.js +5 -3
- package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +3 -3
- package/lib/accounts/webSocketProgramAccountSubscriber.js +15 -9
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +3 -3
- package/lib/accounts/webSocketUserAccountSubscriber.js +3 -3
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +3 -3
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +3 -3
- package/lib/auctionSubscriber/auctionSubscriber.d.ts +2 -2
- package/lib/auctionSubscriber/auctionSubscriber.js +3 -3
- package/lib/auctionSubscriber/types.d.ts +1 -0
- package/lib/constants/perpMarkets.js +2 -2
- package/lib/driftClient.d.ts +12 -1
- package/lib/driftClient.js +59 -16
- package/lib/driftClientConfig.d.ts +1 -0
- package/lib/orderSubscriber/OrderSubscriber.js +6 -3
- package/lib/orderSubscriber/WebsocketSubscription.d.ts +4 -3
- package/lib/orderSubscriber/WebsocketSubscription.js +3 -3
- package/lib/orderSubscriber/types.d.ts +1 -0
- 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/lib/tx/baseTxSender.js +3 -2
- package/lib/tx/types.d.ts +5 -0
- package/lib/tx/types.js +12 -1
- package/lib/tx/utils.js +5 -1
- package/lib/user.d.ts +0 -10
- package/lib/user.js +6 -29
- package/lib/userConfig.d.ts +1 -0
- package/lib/userMap/WebsocketSubscription.d.ts +4 -3
- package/lib/userMap/WebsocketSubscription.js +3 -3
- package/lib/userMap/userMap.js +4 -1
- package/lib/userMap/userMapConfig.d.ts +1 -0
- package/lib/userStats.js +6 -3
- package/lib/userStatsConfig.d.ts +1 -0
- package/package.json +3 -3
- package/src/accounts/types.ts +5 -0
- package/src/accounts/webSocketAccountSubscriber.ts +34 -22
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +7 -6
- package/src/accounts/webSocketInsuranceFundStakeAccountSubscriber.ts +6 -4
- package/src/accounts/webSocketProgramAccountSubscriber.ts +32 -22
- package/src/accounts/webSocketUserAccountSubscriber.ts +5 -4
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +5 -4
- package/src/auctionSubscriber/auctionSubscriber.ts +10 -4
- package/src/auctionSubscriber/types.ts +1 -0
- package/src/blockhashSubscriber/types.ts +4 -0
- package/src/constants/perpMarkets.ts +2 -2
- package/src/driftClient.ts +70 -12
- package/src/driftClientConfig.ts +1 -0
- package/src/orderSubscriber/OrderSubscriber.ts +4 -1
- package/src/orderSubscriber/WebsocketSubscription.ts +6 -5
- package/src/orderSubscriber/types.ts +1 -0
- 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/src/tx/baseTxSender.ts +8 -4
- package/src/tx/types.ts +12 -0
- package/src/tx/utils.ts +5 -1
- package/src/user.ts +7 -32
- package/src/userConfig.ts +1 -0
- package/src/userMap/WebsocketSubscription.ts +6 -5
- package/src/userMap/userMap.ts +4 -1
- package/src/userMap/userMapConfig.ts +1 -0
- package/src/userStats.ts +4 -1
- package/src/userStatsConfig.ts +1 -0
- package/tests/dlob/helpers.ts +4 -0
|
@@ -5,10 +5,10 @@ import { EventEmitter } from 'events';
|
|
|
5
5
|
export declare class AuctionSubscriber {
|
|
6
6
|
private driftClient;
|
|
7
7
|
private opts;
|
|
8
|
-
private
|
|
8
|
+
private resubOpts?;
|
|
9
9
|
eventEmitter: StrictEventEmitter<EventEmitter, AuctionSubscriberEvents>;
|
|
10
10
|
private subscriber;
|
|
11
|
-
constructor({ driftClient, opts, resubTimeoutMs }: AuctionSubscriberConfig);
|
|
11
|
+
constructor({ driftClient, opts, resubTimeoutMs, logResubMessages, }: AuctionSubscriberConfig);
|
|
12
12
|
subscribe(): Promise<void>;
|
|
13
13
|
unsubscribe(): Promise<void>;
|
|
14
14
|
}
|
|
@@ -5,18 +5,18 @@ const memcmp_1 = require("../memcmp");
|
|
|
5
5
|
const events_1 = require("events");
|
|
6
6
|
const webSocketProgramAccountSubscriber_1 = require("../accounts/webSocketProgramAccountSubscriber");
|
|
7
7
|
class AuctionSubscriber {
|
|
8
|
-
constructor({ driftClient, opts, resubTimeoutMs }) {
|
|
8
|
+
constructor({ driftClient, opts, resubTimeoutMs, logResubMessages, }) {
|
|
9
9
|
this.driftClient = driftClient;
|
|
10
10
|
this.opts = opts || this.driftClient.opts;
|
|
11
11
|
this.eventEmitter = new events_1.EventEmitter();
|
|
12
|
-
this.
|
|
12
|
+
this.resubOpts = { resubTimeoutMs, logResubMessages };
|
|
13
13
|
}
|
|
14
14
|
async subscribe() {
|
|
15
15
|
if (!this.subscriber) {
|
|
16
16
|
this.subscriber = new webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber('AuctionSubscriber', 'User', this.driftClient.program, this.driftClient.program.account.user.coder.accounts.decode.bind(this.driftClient.program.account.user.coder.accounts), {
|
|
17
17
|
filters: [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getUserWithAuctionFilter)()],
|
|
18
18
|
commitment: this.opts.commitment,
|
|
19
|
-
}, this.
|
|
19
|
+
}, this.resubOpts);
|
|
20
20
|
}
|
|
21
21
|
await this.subscriber.subscribe((accountId, data, context) => {
|
|
22
22
|
this.eventEmitter.emit('onAccountUpdate', data, accountId, context.slot);
|
|
@@ -5,6 +5,7 @@ export type AuctionSubscriberConfig = {
|
|
|
5
5
|
driftClient: DriftClient;
|
|
6
6
|
opts?: ConfirmOptions;
|
|
7
7
|
resubTimeoutMs?: number;
|
|
8
|
+
logResubMessages?: boolean;
|
|
8
9
|
};
|
|
9
10
|
export interface AuctionSubscriberEvents {
|
|
10
11
|
onAccountUpdate: (account: UserAccount, pubkey: PublicKey, slot: number) => void;
|
|
@@ -542,9 +542,9 @@ exports.MainnetPerpMarkets = [
|
|
|
542
542
|
symbol: 'KMNO-PERP',
|
|
543
543
|
baseAssetSymbol: 'KMNO',
|
|
544
544
|
marketIndex: 28,
|
|
545
|
-
oracle: new web3_js_1.PublicKey('
|
|
545
|
+
oracle: new web3_js_1.PublicKey('6ynsvjkE2UoiRScbDx7ZxbBsyn7wyvg5P1vENvhtkG1C'),
|
|
546
546
|
launchTs: 1712240681000,
|
|
547
|
-
oracleSource: __1.OracleSource.
|
|
547
|
+
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
548
548
|
},
|
|
549
549
|
{
|
|
550
550
|
fullName: 'Tensor',
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -650,12 +650,23 @@ export declare class DriftClient {
|
|
|
650
650
|
requestRemoveInsuranceFundStake(marketIndex: number, amount: BN, txParams?: TxParams): Promise<TransactionSignature>;
|
|
651
651
|
cancelRequestRemoveInsuranceFundStake(marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
652
652
|
removeInsuranceFundStake(marketIndex: number, collateralAccountPublicKey: PublicKey, txParams?: TxParams): Promise<TransactionSignature>;
|
|
653
|
-
settleRevenueToInsuranceFund(
|
|
653
|
+
settleRevenueToInsuranceFund(spotMarketIndex: number, subAccountId?: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
654
|
+
getSettleRevenueToInsuranceFundIx(spotMarketIndex: number, subAccountId?: number): Promise<TransactionInstruction>;
|
|
654
655
|
resolvePerpPnlDeficit(spotMarketIndex: number, perpMarketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
655
656
|
getResolvePerpPnlDeficitIx(spotMarketIndex: number, perpMarketIndex: number): Promise<TransactionInstruction>;
|
|
656
657
|
getDepositIntoSpotMarketRevenuePoolIx(marketIndex: number, amount: BN, userTokenAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
|
|
657
658
|
depositIntoSpotMarketRevenuePool(marketIndex: number, amount: BN, userTokenAccountPublicKey: PublicKey): Promise<TransactionSignature>;
|
|
658
659
|
getPerpMarketExtendedInfo(marketIndex: number): PerpMarketExtendedInfo;
|
|
660
|
+
/**
|
|
661
|
+
* Calculates taker / maker fee (as a percentage, e.g. .001 = 10 basis points) for particular marketType
|
|
662
|
+
* @param marketType
|
|
663
|
+
* @param positionMarketIndex
|
|
664
|
+
* @returns : {takerFee: number, makerFee: number} Precision None
|
|
665
|
+
*/
|
|
666
|
+
getMarketFees(marketType: MarketType, marketIndex?: number, user?: User): {
|
|
667
|
+
takerFee: number;
|
|
668
|
+
makerFee: number;
|
|
669
|
+
};
|
|
659
670
|
/**
|
|
660
671
|
* Returns the market index and type for a given market name
|
|
661
672
|
* E.g. "SOL-PERP" -> { marketIndex: 0, marketType: MarketType.PERP }
|
package/lib/driftClient.js
CHANGED
|
@@ -70,7 +70,7 @@ class DriftClient {
|
|
|
70
70
|
this._isSubscribed = val;
|
|
71
71
|
}
|
|
72
72
|
constructor(config) {
|
|
73
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2;
|
|
73
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5;
|
|
74
74
|
this.users = new Map();
|
|
75
75
|
this._isSubscribed = false;
|
|
76
76
|
this.perpMarketLastSlotCache = new Map();
|
|
@@ -125,12 +125,14 @@ class DriftClient {
|
|
|
125
125
|
this.userAccountSubscriptionConfig = {
|
|
126
126
|
type: 'websocket',
|
|
127
127
|
resubTimeoutMs: (_p = config.accountSubscription) === null || _p === void 0 ? void 0 : _p.resubTimeoutMs,
|
|
128
|
-
|
|
128
|
+
logResubMessages: (_q = config.accountSubscription) === null || _q === void 0 ? void 0 : _q.logResubMessages,
|
|
129
|
+
commitment: (_r = config.accountSubscription) === null || _r === void 0 ? void 0 : _r.commitment,
|
|
129
130
|
};
|
|
130
131
|
this.userStatsAccountSubscriptionConfig = {
|
|
131
132
|
type: 'websocket',
|
|
132
|
-
resubTimeoutMs: (
|
|
133
|
-
|
|
133
|
+
resubTimeoutMs: (_s = config.accountSubscription) === null || _s === void 0 ? void 0 : _s.resubTimeoutMs,
|
|
134
|
+
logResubMessages: (_t = config.accountSubscription) === null || _t === void 0 ? void 0 : _t.logResubMessages,
|
|
135
|
+
commitment: (_u = config.accountSubscription) === null || _u === void 0 ? void 0 : _u.commitment,
|
|
134
136
|
};
|
|
135
137
|
}
|
|
136
138
|
if (config.userStats) {
|
|
@@ -147,11 +149,14 @@ class DriftClient {
|
|
|
147
149
|
const noMarketsAndOraclesSpecified = config.perpMarketIndexes === undefined &&
|
|
148
150
|
config.spotMarketIndexes === undefined &&
|
|
149
151
|
config.oracleInfos === undefined;
|
|
150
|
-
if (((
|
|
151
|
-
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (
|
|
152
|
+
if (((_v = config.accountSubscription) === null || _v === void 0 ? void 0 : _v.type) === 'polling') {
|
|
153
|
+
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_w = config.perpMarketIndexes) !== null && _w !== void 0 ? _w : [], (_x = config.spotMarketIndexes) !== null && _x !== void 0 ? _x : [], (_y = config.oracleInfos) !== null && _y !== void 0 ? _y : [], noMarketsAndOraclesSpecified);
|
|
152
154
|
}
|
|
153
155
|
else {
|
|
154
|
-
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (
|
|
156
|
+
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_z = config.perpMarketIndexes) !== null && _z !== void 0 ? _z : [], (_0 = config.spotMarketIndexes) !== null && _0 !== void 0 ? _0 : [], (_1 = config.oracleInfos) !== null && _1 !== void 0 ? _1 : [], noMarketsAndOraclesSpecified, {
|
|
157
|
+
resubTimeoutMs: (_2 = config.accountSubscription) === null || _2 === void 0 ? void 0 : _2.resubTimeoutMs,
|
|
158
|
+
logResubMessages: (_3 = config.accountSubscription) === null || _3 === void 0 ? void 0 : _3.logResubMessages,
|
|
159
|
+
}, (_4 = config.accountSubscription) === null || _4 === void 0 ? void 0 : _4.commitment);
|
|
155
160
|
}
|
|
156
161
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
157
162
|
if (config.enableMetricsEvents) {
|
|
@@ -159,7 +164,7 @@ class DriftClient {
|
|
|
159
164
|
this.metricsEventEmitter = new events_1.EventEmitter();
|
|
160
165
|
}
|
|
161
166
|
this.txSender =
|
|
162
|
-
(
|
|
167
|
+
(_5 = config.txSender) !== null && _5 !== void 0 ? _5 : new retryTxSender_1.RetryTxSender({
|
|
163
168
|
connection: this.connection,
|
|
164
169
|
wallet: this.wallet,
|
|
165
170
|
opts: this.opts,
|
|
@@ -3465,14 +3470,19 @@ class DriftClient {
|
|
|
3465
3470
|
const { txSig } = await this.sendTransaction(tx, additionalSigners, this.opts);
|
|
3466
3471
|
return txSig;
|
|
3467
3472
|
}
|
|
3468
|
-
async settleRevenueToInsuranceFund(
|
|
3469
|
-
const
|
|
3473
|
+
async settleRevenueToInsuranceFund(spotMarketIndex, subAccountId, txParams) {
|
|
3474
|
+
const tx = await this.buildTransaction(await this.getSettleRevenueToInsuranceFundIx(spotMarketIndex, subAccountId), txParams);
|
|
3475
|
+
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
3476
|
+
return txSig;
|
|
3477
|
+
}
|
|
3478
|
+
async getSettleRevenueToInsuranceFundIx(spotMarketIndex, subAccountId) {
|
|
3479
|
+
const spotMarketAccount = this.getSpotMarketAccount(spotMarketIndex);
|
|
3470
3480
|
const remainingAccounts = this.getRemainingAccounts({
|
|
3471
|
-
userAccounts: [this.getUserAccount()],
|
|
3481
|
+
userAccounts: [this.getUserAccount(subAccountId)],
|
|
3472
3482
|
useMarketLastSlotCache: true,
|
|
3473
|
-
writableSpotMarketIndexes: [
|
|
3483
|
+
writableSpotMarketIndexes: [spotMarketIndex],
|
|
3474
3484
|
});
|
|
3475
|
-
const ix = await this.program.instruction.settleRevenueToInsuranceFund(
|
|
3485
|
+
const ix = await this.program.instruction.settleRevenueToInsuranceFund(spotMarketIndex, {
|
|
3476
3486
|
accounts: {
|
|
3477
3487
|
state: await this.getStatePublicKey(),
|
|
3478
3488
|
spotMarket: spotMarketAccount.pubkey,
|
|
@@ -3483,9 +3493,7 @@ class DriftClient {
|
|
|
3483
3493
|
},
|
|
3484
3494
|
remainingAccounts,
|
|
3485
3495
|
});
|
|
3486
|
-
|
|
3487
|
-
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
3488
|
-
return txSig;
|
|
3496
|
+
return ix;
|
|
3489
3497
|
}
|
|
3490
3498
|
async resolvePerpPnlDeficit(spotMarketIndex, perpMarketIndex, txParams) {
|
|
3491
3499
|
const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.getResolvePerpPnlDeficitIx(spotMarketIndex, perpMarketIndex), txParams), [], this.opts);
|
|
@@ -3545,6 +3553,41 @@ class DriftClient {
|
|
|
3545
3553
|
};
|
|
3546
3554
|
return extendedInfo;
|
|
3547
3555
|
}
|
|
3556
|
+
/**
|
|
3557
|
+
* Calculates taker / maker fee (as a percentage, e.g. .001 = 10 basis points) for particular marketType
|
|
3558
|
+
* @param marketType
|
|
3559
|
+
* @param positionMarketIndex
|
|
3560
|
+
* @returns : {takerFee: number, makerFee: number} Precision None
|
|
3561
|
+
*/
|
|
3562
|
+
getMarketFees(marketType, marketIndex, user) {
|
|
3563
|
+
let feeTier;
|
|
3564
|
+
if (user) {
|
|
3565
|
+
feeTier = user.getUserFeeTier(marketType);
|
|
3566
|
+
}
|
|
3567
|
+
else {
|
|
3568
|
+
const state = this.getStateAccount();
|
|
3569
|
+
feeTier = (0, types_1.isVariant)(marketType, 'perp')
|
|
3570
|
+
? state.perpFeeStructure.feeTiers[0]
|
|
3571
|
+
: state.spotFeeStructure.feeTiers[0];
|
|
3572
|
+
}
|
|
3573
|
+
let takerFee = feeTier.feeNumerator / feeTier.feeDenominator;
|
|
3574
|
+
let makerFee = feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;
|
|
3575
|
+
if (marketIndex !== undefined) {
|
|
3576
|
+
let marketAccount = null;
|
|
3577
|
+
if ((0, types_1.isVariant)(marketType, 'perp')) {
|
|
3578
|
+
marketAccount = this.getPerpMarketAccount(marketIndex);
|
|
3579
|
+
}
|
|
3580
|
+
else {
|
|
3581
|
+
marketAccount = this.getSpotMarketAccount(marketIndex);
|
|
3582
|
+
}
|
|
3583
|
+
takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
|
|
3584
|
+
makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
|
|
3585
|
+
}
|
|
3586
|
+
return {
|
|
3587
|
+
takerFee,
|
|
3588
|
+
makerFee,
|
|
3589
|
+
};
|
|
3590
|
+
}
|
|
3548
3591
|
/**
|
|
3549
3592
|
* Returns the market index and type for a given market name
|
|
3550
3593
|
* E.g. "SOL-PERP" -> { marketIndex: 0, marketType: MarketType.PERP }
|
|
@@ -12,7 +12,7 @@ const index_1 = require("../index");
|
|
|
12
12
|
const user_1 = require("../decode/user");
|
|
13
13
|
class OrderSubscriber {
|
|
14
14
|
constructor(config) {
|
|
15
|
-
var _a;
|
|
15
|
+
var _a, _b, _c;
|
|
16
16
|
this.usersAccounts = new Map();
|
|
17
17
|
this.driftClient = config.driftClient;
|
|
18
18
|
this.commitment = config.subscriptionConfig.commitment || 'processed';
|
|
@@ -27,12 +27,15 @@ class OrderSubscriber {
|
|
|
27
27
|
orderSubscriber: this,
|
|
28
28
|
commitment: this.commitment,
|
|
29
29
|
skipInitialLoad: config.subscriptionConfig.skipInitialLoad,
|
|
30
|
-
|
|
30
|
+
resubOpts: {
|
|
31
|
+
resubTimeoutMs: (_a = config.subscriptionConfig) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs,
|
|
32
|
+
logResubMessages: (_b = config.subscriptionConfig) === null || _b === void 0 ? void 0 : _b.logResubMessages,
|
|
33
|
+
},
|
|
31
34
|
resyncIntervalMs: config.subscriptionConfig.resyncIntervalMs,
|
|
32
35
|
decoded: config.decodeData,
|
|
33
36
|
});
|
|
34
37
|
}
|
|
35
|
-
if ((
|
|
38
|
+
if ((_c = config.fastDecode) !== null && _c !== void 0 ? _c : true) {
|
|
36
39
|
this.decodeFn = (name, data) => (0, user_1.decodeUser)(data);
|
|
37
40
|
}
|
|
38
41
|
else {
|
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { OrderSubscriber } from './OrderSubscriber';
|
|
2
2
|
import { Commitment } from '@solana/web3.js';
|
|
3
|
+
import { ResubOpts } from '../accounts/types';
|
|
3
4
|
export declare class WebsocketSubscription {
|
|
4
5
|
private orderSubscriber;
|
|
5
6
|
private commitment;
|
|
6
7
|
private skipInitialLoad;
|
|
7
|
-
private
|
|
8
|
+
private resubOpts?;
|
|
8
9
|
private resyncIntervalMs?;
|
|
9
10
|
private subscriber?;
|
|
10
11
|
private resyncTimeoutId?;
|
|
11
12
|
private decoded?;
|
|
12
|
-
constructor({ orderSubscriber, commitment, skipInitialLoad,
|
|
13
|
+
constructor({ orderSubscriber, commitment, skipInitialLoad, resubOpts, resyncIntervalMs, decoded, }: {
|
|
13
14
|
orderSubscriber: OrderSubscriber;
|
|
14
15
|
commitment: Commitment;
|
|
15
16
|
skipInitialLoad?: boolean;
|
|
16
|
-
|
|
17
|
+
resubOpts?: ResubOpts;
|
|
17
18
|
resyncIntervalMs?: number;
|
|
18
19
|
decoded?: boolean;
|
|
19
20
|
});
|
|
@@ -4,11 +4,11 @@ exports.WebsocketSubscription = void 0;
|
|
|
4
4
|
const memcmp_1 = require("../memcmp");
|
|
5
5
|
const webSocketProgramAccountSubscriber_1 = require("../accounts/webSocketProgramAccountSubscriber");
|
|
6
6
|
class WebsocketSubscription {
|
|
7
|
-
constructor({ orderSubscriber, commitment, skipInitialLoad = false,
|
|
7
|
+
constructor({ orderSubscriber, commitment, skipInitialLoad = false, resubOpts, resyncIntervalMs, decoded = true, }) {
|
|
8
8
|
this.orderSubscriber = orderSubscriber;
|
|
9
9
|
this.commitment = commitment;
|
|
10
10
|
this.skipInitialLoad = skipInitialLoad;
|
|
11
|
-
this.
|
|
11
|
+
this.resubOpts = resubOpts;
|
|
12
12
|
this.resyncIntervalMs = resyncIntervalMs;
|
|
13
13
|
this.decoded = decoded;
|
|
14
14
|
}
|
|
@@ -19,7 +19,7 @@ class WebsocketSubscription {
|
|
|
19
19
|
this.subscriber = new webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber('OrderSubscriber', 'User', this.orderSubscriber.driftClient.program, this.orderSubscriber.decodeFn, {
|
|
20
20
|
filters: [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getNonIdleUserFilter)()],
|
|
21
21
|
commitment: this.commitment,
|
|
22
|
-
}, this.
|
|
22
|
+
}, this.resubOpts);
|
|
23
23
|
await this.subscriber.subscribe((accountId, account, context, buffer) => {
|
|
24
24
|
var _a;
|
|
25
25
|
const userKey = accountId.toBase58();
|
|
@@ -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/lib/tx/baseTxSender.js
CHANGED
|
@@ -10,6 +10,7 @@ const anchor_1 = require("@coral-xyz/anchor");
|
|
|
10
10
|
const assert_1 = __importDefault(require("assert"));
|
|
11
11
|
const bs58_1 = __importDefault(require("bs58"));
|
|
12
12
|
const DEFAULT_TIMEOUT = 35000;
|
|
13
|
+
const NOT_CONFIRMED_ERROR_CODE = -1001;
|
|
13
14
|
class BaseTxSender {
|
|
14
15
|
constructor({ connection, wallet, opts = anchor_1.AnchorProvider.defaultOptions(), timeout = DEFAULT_TIMEOUT, additionalConnections = new Array(), confirmationStrategy = types_1.ConfirmationStrategy.Combo, additionalTxSenderCallbacks, }) {
|
|
15
16
|
this.timeoutCount = 0;
|
|
@@ -181,7 +182,7 @@ class BaseTxSender {
|
|
|
181
182
|
}
|
|
182
183
|
this.timeoutCount += 1;
|
|
183
184
|
const duration = (Date.now() - start) / 1000;
|
|
184
|
-
throw new
|
|
185
|
+
throw new types_1.TxSendError(`Transaction was not confirmed in ${duration.toFixed(2)} seconds. It is unknown if it succeeded or failed. Check signature ${signature} using the Solana Explorer or CLI tools.`, NOT_CONFIRMED_ERROR_CODE);
|
|
185
186
|
}
|
|
186
187
|
return response;
|
|
187
188
|
}
|
|
@@ -203,7 +204,7 @@ class BaseTxSender {
|
|
|
203
204
|
// Transaction not confirmed within 30 seconds
|
|
204
205
|
this.timeoutCount += 1;
|
|
205
206
|
const duration = (Date.now() - start) / 1000;
|
|
206
|
-
throw new
|
|
207
|
+
throw new types_1.TxSendError(`Transaction was not confirmed in ${duration.toFixed(2)} seconds. It is unknown if it succeeded or failed. Check signature ${signature} using the Solana Explorer or CLI tools.`, NOT_CONFIRMED_ERROR_CODE);
|
|
207
208
|
}
|
|
208
209
|
async confirmTransaction(signature, commitment) {
|
|
209
210
|
if (this.confirmationStrategy === types_1.ConfirmationStrategy.WebSocket ||
|
package/lib/tx/types.d.ts
CHANGED
|
@@ -22,3 +22,8 @@ export interface TxSender {
|
|
|
22
22
|
simulateTransaction(tx: VersionedTransaction): Promise<boolean>;
|
|
23
23
|
getTimeoutCount(): number;
|
|
24
24
|
}
|
|
25
|
+
export declare class TxSendError extends Error {
|
|
26
|
+
message: string;
|
|
27
|
+
code: number;
|
|
28
|
+
constructor(message: string, code: number);
|
|
29
|
+
}
|