@drift-labs/sdk 2.82.0-beta.1 → 2.82.0-beta.10
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
package/lib/tx/types.js
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ConfirmationStrategy = void 0;
|
|
3
|
+
exports.TxSendError = exports.ConfirmationStrategy = void 0;
|
|
4
4
|
var ConfirmationStrategy;
|
|
5
5
|
(function (ConfirmationStrategy) {
|
|
6
6
|
ConfirmationStrategy["WebSocket"] = "websocket";
|
|
7
7
|
ConfirmationStrategy["Polling"] = "polling";
|
|
8
8
|
ConfirmationStrategy["Combo"] = "combo";
|
|
9
9
|
})(ConfirmationStrategy = exports.ConfirmationStrategy || (exports.ConfirmationStrategy = {}));
|
|
10
|
+
class TxSendError extends Error {
|
|
11
|
+
constructor(message, code) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.message = message;
|
|
14
|
+
this.code = code;
|
|
15
|
+
if (Error.captureStackTrace) {
|
|
16
|
+
Error.captureStackTrace(this, TxSendError);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.TxSendError = TxSendError;
|
package/lib/tx/utils.js
CHANGED
|
@@ -30,7 +30,11 @@ async function getSignedTransactionMap(wallet, txsToSign, keys) {
|
|
|
30
30
|
keysWithTx.push(keys[index]);
|
|
31
31
|
}
|
|
32
32
|
});
|
|
33
|
-
const signedTxs = await wallet.signAllTransactions(txsToSign
|
|
33
|
+
const signedTxs = await wallet.signAllTransactions(txsToSign
|
|
34
|
+
.map((tx) => {
|
|
35
|
+
return tx;
|
|
36
|
+
})
|
|
37
|
+
.filter((tx) => tx !== undefined));
|
|
34
38
|
signedTxs.forEach((signedTx, index) => {
|
|
35
39
|
signedTxMap[keysWithTx[index]] = signedTx;
|
|
36
40
|
});
|
package/lib/user.d.ts
CHANGED
|
@@ -346,16 +346,6 @@ export declare class User {
|
|
|
346
346
|
*/
|
|
347
347
|
accountLeverageRatioAfterTrade(targetMarketIndex: number, targetMarketType: MarketType, tradeQuoteAmount: BN, tradeSide: PositionDirection, includeOpenOrders?: boolean): BN;
|
|
348
348
|
getUserFeeTier(marketType: MarketType, now?: BN): import("./types").FeeTier;
|
|
349
|
-
/**
|
|
350
|
-
* Calculates taker / maker fee (as a percentage, e.g. .001 = 10 basis points) for particular marketType
|
|
351
|
-
* @param marketType
|
|
352
|
-
* @param positionMarketIndex
|
|
353
|
-
* @returns : {takerFee: number, makerFee: number} Precision None
|
|
354
|
-
*/
|
|
355
|
-
getMarketFees(marketType: MarketType, marketIndex?: number): {
|
|
356
|
-
takerFee: number;
|
|
357
|
-
makerFee: number;
|
|
358
|
-
};
|
|
359
349
|
/**
|
|
360
350
|
* Calculates how much perp fee will be taken for a given sized trade
|
|
361
351
|
* @param quoteAmount
|
package/lib/user.js
CHANGED
|
@@ -22,7 +22,7 @@ class User {
|
|
|
22
22
|
this._isSubscribed = val;
|
|
23
23
|
}
|
|
24
24
|
constructor(config) {
|
|
25
|
-
var _a, _b, _c, _d;
|
|
25
|
+
var _a, _b, _c, _d, _e;
|
|
26
26
|
this._isSubscribed = false;
|
|
27
27
|
this.driftClient = config.driftClient;
|
|
28
28
|
this.userAccountPublicKey = config.userAccountPublicKey;
|
|
@@ -33,7 +33,10 @@ class User {
|
|
|
33
33
|
this.accountSubscriber = config.accountSubscription.userAccountSubscriber;
|
|
34
34
|
}
|
|
35
35
|
else {
|
|
36
|
-
this.accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(config.driftClient.program, config.userAccountPublicKey,
|
|
36
|
+
this.accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(config.driftClient.program, config.userAccountPublicKey, {
|
|
37
|
+
resubTimeoutMs: (_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.resubTimeoutMs,
|
|
38
|
+
logResubMessages: (_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.logResubMessages,
|
|
39
|
+
}, (_e = config.accountSubscription) === null || _e === void 0 ? void 0 : _e.commitment);
|
|
37
40
|
}
|
|
38
41
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
39
42
|
}
|
|
@@ -1709,32 +1712,6 @@ class User {
|
|
|
1709
1712
|
}
|
|
1710
1713
|
return state.spotFeeStructure.feeTiers[feeTierIndex];
|
|
1711
1714
|
}
|
|
1712
|
-
/**
|
|
1713
|
-
* Calculates taker / maker fee (as a percentage, e.g. .001 = 10 basis points) for particular marketType
|
|
1714
|
-
* @param marketType
|
|
1715
|
-
* @param positionMarketIndex
|
|
1716
|
-
* @returns : {takerFee: number, makerFee: number} Precision None
|
|
1717
|
-
*/
|
|
1718
|
-
getMarketFees(marketType, marketIndex) {
|
|
1719
|
-
const feeTier = this.getUserFeeTier(marketType);
|
|
1720
|
-
let takerFee = feeTier.feeNumerator / feeTier.feeDenominator;
|
|
1721
|
-
let makerFee = feeTier.makerRebateNumerator / feeTier.makerRebateDenominator;
|
|
1722
|
-
if (marketIndex !== undefined) {
|
|
1723
|
-
let marketAccount = null;
|
|
1724
|
-
if ((0, types_1.isVariant)(marketType, 'perp')) {
|
|
1725
|
-
marketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
1726
|
-
}
|
|
1727
|
-
else {
|
|
1728
|
-
marketAccount = this.driftClient.getSpotMarketAccount(marketIndex);
|
|
1729
|
-
}
|
|
1730
|
-
takerFee += (takerFee * marketAccount.feeAdjustment) / 100;
|
|
1731
|
-
makerFee += (makerFee * marketAccount.feeAdjustment) / 100;
|
|
1732
|
-
}
|
|
1733
|
-
return {
|
|
1734
|
-
takerFee,
|
|
1735
|
-
makerFee,
|
|
1736
|
-
};
|
|
1737
|
-
}
|
|
1738
1715
|
/**
|
|
1739
1716
|
* Calculates how much perp fee will be taken for a given sized trade
|
|
1740
1717
|
* @param quoteAmount
|
|
@@ -1742,7 +1719,7 @@ class User {
|
|
|
1742
1719
|
*/
|
|
1743
1720
|
calculateFeeForQuoteAmount(quoteAmount, marketIndex) {
|
|
1744
1721
|
if (marketIndex !== undefined) {
|
|
1745
|
-
const takerFeeMultiplier = this.getMarketFees(_1.MarketType.PERP, marketIndex).takerFee;
|
|
1722
|
+
const takerFeeMultiplier = this.driftClient.getMarketFees(_1.MarketType.PERP, marketIndex, this).takerFee;
|
|
1746
1723
|
const feeAmountNum = _1.BigNum.from(quoteAmount, numericConstants_1.QUOTE_PRECISION_EXP).toNum() *
|
|
1747
1724
|
takerFeeMultiplier;
|
|
1748
1725
|
return _1.BigNum.fromPrint(feeAmountNum.toString(), numericConstants_1.QUOTE_PRECISION_EXP).val;
|
package/lib/userConfig.d.ts
CHANGED
|
@@ -2,19 +2,20 @@
|
|
|
2
2
|
import { UserMap } from './userMap';
|
|
3
3
|
import { UserAccount } from '../types';
|
|
4
4
|
import { Commitment } from '@solana/web3.js';
|
|
5
|
+
import { ResubOpts } from '../accounts/types';
|
|
5
6
|
export declare class WebsocketSubscription {
|
|
6
7
|
private userMap;
|
|
7
8
|
private commitment;
|
|
8
9
|
private skipInitialLoad;
|
|
9
|
-
private
|
|
10
|
+
private resubOpts?;
|
|
10
11
|
private includeIdle?;
|
|
11
12
|
private decodeFn;
|
|
12
13
|
private subscriber;
|
|
13
|
-
constructor({ userMap, commitment, skipInitialLoad,
|
|
14
|
+
constructor({ userMap, commitment, skipInitialLoad, resubOpts, includeIdle, decodeFn, }: {
|
|
14
15
|
userMap: UserMap;
|
|
15
16
|
commitment: Commitment;
|
|
16
17
|
skipInitialLoad?: boolean;
|
|
17
|
-
|
|
18
|
+
resubOpts?: ResubOpts;
|
|
18
19
|
includeIdle?: boolean;
|
|
19
20
|
decodeFn: (name: string, data: Buffer) => UserAccount;
|
|
20
21
|
});
|
|
@@ -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({ userMap, commitment, skipInitialLoad = false,
|
|
7
|
+
constructor({ userMap, commitment, skipInitialLoad = false, resubOpts, includeIdle = false, decodeFn, }) {
|
|
8
8
|
this.userMap = userMap;
|
|
9
9
|
this.commitment = commitment;
|
|
10
10
|
this.skipInitialLoad = skipInitialLoad;
|
|
11
|
-
this.
|
|
11
|
+
this.resubOpts = resubOpts;
|
|
12
12
|
this.includeIdle = includeIdle || false;
|
|
13
13
|
this.decodeFn = decodeFn;
|
|
14
14
|
}
|
|
@@ -21,7 +21,7 @@ class WebsocketSubscription {
|
|
|
21
21
|
this.subscriber = new webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber('UserMap', 'User', this.userMap.driftClient.program, this.decodeFn, {
|
|
22
22
|
filters,
|
|
23
23
|
commitment: this.commitment,
|
|
24
|
-
}, this.
|
|
24
|
+
}, this.resubOpts);
|
|
25
25
|
}
|
|
26
26
|
await this.subscriber.subscribe((accountId, account, context) => {
|
|
27
27
|
const userKey = accountId.toBase58();
|
package/lib/userMap/userMap.js
CHANGED
|
@@ -56,7 +56,10 @@ class UserMap {
|
|
|
56
56
|
this.subscription = new WebsocketSubscription_1.WebsocketSubscription({
|
|
57
57
|
userMap: this,
|
|
58
58
|
commitment: this.commitment,
|
|
59
|
-
|
|
59
|
+
resubOpts: {
|
|
60
|
+
resubTimeoutMs: config.subscriptionConfig.resubTimeoutMs,
|
|
61
|
+
logResubMessages: config.subscriptionConfig.logResubMessages,
|
|
62
|
+
},
|
|
60
63
|
skipInitialLoad: config.skipInitialLoad,
|
|
61
64
|
decodeFn,
|
|
62
65
|
});
|
package/lib/userStats.js
CHANGED
|
@@ -7,17 +7,20 @@ const webSocketUserStatsAccountSubsriber_1 = require("./accounts/webSocketUserSt
|
|
|
7
7
|
const pda_1 = require("./addresses/pda");
|
|
8
8
|
class UserStats {
|
|
9
9
|
constructor(config) {
|
|
10
|
-
var _a, _b, _c;
|
|
10
|
+
var _a, _b, _c, _d, _e;
|
|
11
11
|
this.driftClient = config.driftClient;
|
|
12
12
|
this.userStatsAccountPublicKey = config.userStatsAccountPublicKey;
|
|
13
13
|
if (((_a = config.accountSubscription) === null || _a === void 0 ? void 0 : _a.type) === 'polling') {
|
|
14
14
|
this.accountSubscriber = new pollingUserStatsAccountSubscriber_1.PollingUserStatsAccountSubscriber(config.driftClient.program, config.userStatsAccountPublicKey, config.accountSubscription.accountLoader);
|
|
15
15
|
}
|
|
16
16
|
else if (((_b = config.accountSubscription) === null || _b === void 0 ? void 0 : _b.type) === 'websocket') {
|
|
17
|
-
this.accountSubscriber = new webSocketUserStatsAccountSubsriber_1.WebSocketUserStatsAccountSubscriber(config.driftClient.program, config.userStatsAccountPublicKey,
|
|
17
|
+
this.accountSubscriber = new webSocketUserStatsAccountSubsriber_1.WebSocketUserStatsAccountSubscriber(config.driftClient.program, config.userStatsAccountPublicKey, {
|
|
18
|
+
resubTimeoutMs: (_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.resubTimeoutMs,
|
|
19
|
+
logResubMessages: (_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.logResubMessages,
|
|
20
|
+
}, config.accountSubscription.commitment);
|
|
18
21
|
}
|
|
19
22
|
else {
|
|
20
|
-
throw new Error(`Unknown user stats account subscription type: ${(
|
|
23
|
+
throw new Error(`Unknown user stats account subscription type: ${(_e = config.accountSubscription) === null || _e === void 0 ? void 0 : _e.type}`);
|
|
21
24
|
}
|
|
22
25
|
}
|
|
23
26
|
async subscribe(userStatsAccount) {
|
package/lib/userStatsConfig.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drift-labs/sdk",
|
|
3
|
-
"version": "2.82.0-beta.
|
|
3
|
+
"version": "2.82.0-beta.10",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"author": "crispheaney",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"@project-serum/serum": "^0.13.38",
|
|
40
40
|
"@pythnetwork/client": "2.5.3",
|
|
41
41
|
"@solana/spl-token": "^0.3.7",
|
|
42
|
-
"@solana/web3.js": "1.
|
|
42
|
+
"@solana/web3.js": "1.91.7",
|
|
43
43
|
"strict-event-emitter-types": "^2.0.0",
|
|
44
44
|
"uuid": "^8.3.2",
|
|
45
45
|
"zstddec": "^0.1.0"
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
},
|
|
67
67
|
"description": "SDK for Drift Protocol",
|
|
68
68
|
"engines": {
|
|
69
|
-
"node": ">=
|
|
69
|
+
"node": ">=18"
|
|
70
70
|
}
|
|
71
71
|
}
|
package/src/accounts/types.ts
CHANGED
|
@@ -184,6 +184,11 @@ export type DataAndSlot<T> = {
|
|
|
184
184
|
slot: number;
|
|
185
185
|
};
|
|
186
186
|
|
|
187
|
+
export type ResubOpts = {
|
|
188
|
+
resubTimeoutMs?: number;
|
|
189
|
+
logResubMessages?: boolean;
|
|
190
|
+
};
|
|
191
|
+
|
|
187
192
|
export interface UserStatsAccountEvents {
|
|
188
193
|
userStatsAccountUpdate: (payload: UserStatsAccount) => void;
|
|
189
194
|
update: void;
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
DataAndSlot,
|
|
3
|
+
BufferAndSlot,
|
|
4
|
+
AccountSubscriber,
|
|
5
|
+
ResubOpts,
|
|
6
|
+
} from './types';
|
|
2
7
|
import { AnchorProvider, Program } from '@coral-xyz/anchor';
|
|
3
8
|
import { AccountInfo, Commitment, Context, PublicKey } from '@solana/web3.js';
|
|
4
9
|
import { capitalize } from './utils';
|
|
@@ -13,7 +18,9 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
13
18
|
decodeBufferFn: (buffer: Buffer) => T;
|
|
14
19
|
onChange: (data: T) => void;
|
|
15
20
|
listenerId?: number;
|
|
16
|
-
|
|
21
|
+
|
|
22
|
+
resubOpts?: ResubOpts;
|
|
23
|
+
|
|
17
24
|
commitment?: Commitment;
|
|
18
25
|
isUnsubscribing = false;
|
|
19
26
|
|
|
@@ -26,15 +33,15 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
26
33
|
program: Program,
|
|
27
34
|
accountPublicKey: PublicKey,
|
|
28
35
|
decodeBuffer?: (buffer: Buffer) => T,
|
|
29
|
-
|
|
36
|
+
resubOpts?: ResubOpts,
|
|
30
37
|
commitment?: Commitment
|
|
31
38
|
) {
|
|
32
39
|
this.accountName = accountName;
|
|
33
40
|
this.program = program;
|
|
34
41
|
this.accountPublicKey = accountPublicKey;
|
|
35
42
|
this.decodeBufferFn = decodeBuffer;
|
|
36
|
-
this.
|
|
37
|
-
if (this.resubTimeoutMs < 1000) {
|
|
43
|
+
this.resubOpts = resubOpts;
|
|
44
|
+
if (this.resubOpts?.resubTimeoutMs < 1000) {
|
|
38
45
|
console.log(
|
|
39
46
|
'resubTimeoutMs should be at least 1000ms to avoid spamming resub'
|
|
40
47
|
);
|
|
@@ -57,7 +64,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
57
64
|
this.listenerId = this.program.provider.connection.onAccountChange(
|
|
58
65
|
this.accountPublicKey,
|
|
59
66
|
(accountInfo, context) => {
|
|
60
|
-
if (this.resubTimeoutMs) {
|
|
67
|
+
if (this.resubOpts?.resubTimeoutMs) {
|
|
61
68
|
this.receivingData = true;
|
|
62
69
|
clearTimeout(this.timeoutId);
|
|
63
70
|
this.handleRpcResponse(context, accountInfo);
|
|
@@ -69,7 +76,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
69
76
|
this.commitment
|
|
70
77
|
);
|
|
71
78
|
|
|
72
|
-
if (this.resubTimeoutMs) {
|
|
79
|
+
if (this.resubOpts?.resubTimeoutMs) {
|
|
73
80
|
this.receivingData = true;
|
|
74
81
|
this.setTimeout();
|
|
75
82
|
}
|
|
@@ -91,21 +98,26 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
91
98
|
if (!this.onChange) {
|
|
92
99
|
throw new Error('onChange callback function must be set');
|
|
93
100
|
}
|
|
94
|
-
this.timeoutId = setTimeout(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
101
|
+
this.timeoutId = setTimeout(
|
|
102
|
+
async () => {
|
|
103
|
+
if (this.isUnsubscribing) {
|
|
104
|
+
// If we are in the process of unsubscribing, do not attempt to resubscribe
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
99
107
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
108
|
+
if (this.receivingData) {
|
|
109
|
+
if (this.resubOpts?.logResubMessages) {
|
|
110
|
+
console.log(
|
|
111
|
+
`No ws data from ${this.accountName} in ${this.resubOpts.resubTimeoutMs}ms, resubscribing`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
await this.unsubscribe(true);
|
|
115
|
+
this.receivingData = false;
|
|
116
|
+
await this.subscribe(this.onChange);
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
this.resubOpts?.resubTimeoutMs
|
|
120
|
+
);
|
|
109
121
|
}
|
|
110
122
|
|
|
111
123
|
async fetch(): Promise<void> {
|
|
@@ -172,7 +184,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
172
184
|
|
|
173
185
|
unsubscribe(onResub = false): Promise<void> {
|
|
174
186
|
if (!onResub) {
|
|
175
|
-
this.resubTimeoutMs = undefined;
|
|
187
|
+
this.resubOpts.resubTimeoutMs = undefined;
|
|
176
188
|
}
|
|
177
189
|
this.isUnsubscribing = true;
|
|
178
190
|
clearTimeout(this.timeoutId);
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
DriftClientAccountSubscriber,
|
|
3
3
|
DriftClientAccountEvents,
|
|
4
4
|
DataAndSlot,
|
|
5
|
+
ResubOpts,
|
|
5
6
|
} from './types';
|
|
6
7
|
import { AccountSubscriber, NotSubscribedError } from './types';
|
|
7
8
|
import { SpotMarketAccount, PerpMarketAccount, StateAccount } from '../types';
|
|
@@ -32,7 +33,7 @@ export class WebSocketDriftClientAccountSubscriber
|
|
|
32
33
|
oracleInfos: OracleInfo[];
|
|
33
34
|
oracleClientCache = new OracleClientCache();
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
resubOpts?: ResubOpts;
|
|
36
37
|
shouldFindAllMarketsAndOracles: boolean;
|
|
37
38
|
|
|
38
39
|
eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
|
|
@@ -59,7 +60,7 @@ export class WebSocketDriftClientAccountSubscriber
|
|
|
59
60
|
spotMarketIndexes: number[],
|
|
60
61
|
oracleInfos: OracleInfo[],
|
|
61
62
|
shouldFindAllMarketsAndOracles: boolean,
|
|
62
|
-
|
|
63
|
+
resubOpts?: ResubOpts,
|
|
63
64
|
commitment?: Commitment
|
|
64
65
|
) {
|
|
65
66
|
this.isSubscribed = false;
|
|
@@ -69,7 +70,7 @@ export class WebSocketDriftClientAccountSubscriber
|
|
|
69
70
|
this.spotMarketIndexes = spotMarketIndexes;
|
|
70
71
|
this.oracleInfos = oracleInfos;
|
|
71
72
|
this.shouldFindAllMarketsAndOracles = shouldFindAllMarketsAndOracles;
|
|
72
|
-
this.
|
|
73
|
+
this.resubOpts = resubOpts;
|
|
73
74
|
this.commitment = commitment;
|
|
74
75
|
}
|
|
75
76
|
|
|
@@ -152,7 +153,7 @@ export class WebSocketDriftClientAccountSubscriber
|
|
|
152
153
|
this.program,
|
|
153
154
|
perpMarketPublicKey,
|
|
154
155
|
undefined,
|
|
155
|
-
this.
|
|
156
|
+
this.resubOpts,
|
|
156
157
|
this.commitment
|
|
157
158
|
);
|
|
158
159
|
await accountSubscriber.subscribe((data: PerpMarketAccount) => {
|
|
@@ -180,7 +181,7 @@ export class WebSocketDriftClientAccountSubscriber
|
|
|
180
181
|
this.program,
|
|
181
182
|
marketPublicKey,
|
|
182
183
|
undefined,
|
|
183
|
-
this.
|
|
184
|
+
this.resubOpts,
|
|
184
185
|
this.commitment
|
|
185
186
|
);
|
|
186
187
|
await accountSubscriber.subscribe((data: SpotMarketAccount) => {
|
|
@@ -214,7 +215,7 @@ export class WebSocketDriftClientAccountSubscriber
|
|
|
214
215
|
(buffer: Buffer) => {
|
|
215
216
|
return client.getOraclePriceDataFromBuffer(buffer);
|
|
216
217
|
},
|
|
217
|
-
this.
|
|
218
|
+
this.resubOpts,
|
|
218
219
|
this.commitment
|
|
219
220
|
);
|
|
220
221
|
|
|
@@ -16,7 +16,7 @@ export class WebSocketInsuranceFundStakeAccountSubscriber
|
|
|
16
16
|
implements InsuranceFundStakeAccountSubscriber
|
|
17
17
|
{
|
|
18
18
|
isSubscribed: boolean;
|
|
19
|
-
|
|
19
|
+
resubTimeoutMs?: number;
|
|
20
20
|
commitment?: Commitment;
|
|
21
21
|
program: Program;
|
|
22
22
|
eventEmitter: StrictEventEmitter<
|
|
@@ -30,7 +30,7 @@ export class WebSocketInsuranceFundStakeAccountSubscriber
|
|
|
30
30
|
public constructor(
|
|
31
31
|
program: Program,
|
|
32
32
|
insuranceFundStakeAccountPublicKey: PublicKey,
|
|
33
|
-
|
|
33
|
+
resubTimeoutMs?: number,
|
|
34
34
|
commitment?: Commitment
|
|
35
35
|
) {
|
|
36
36
|
this.isSubscribed = false;
|
|
@@ -38,7 +38,7 @@ export class WebSocketInsuranceFundStakeAccountSubscriber
|
|
|
38
38
|
this.insuranceFundStakeAccountPublicKey =
|
|
39
39
|
insuranceFundStakeAccountPublicKey;
|
|
40
40
|
this.eventEmitter = new EventEmitter();
|
|
41
|
-
this.
|
|
41
|
+
this.resubTimeoutMs = resubTimeoutMs;
|
|
42
42
|
this.commitment = commitment;
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -55,7 +55,9 @@ export class WebSocketInsuranceFundStakeAccountSubscriber
|
|
|
55
55
|
this.program,
|
|
56
56
|
this.insuranceFundStakeAccountPublicKey,
|
|
57
57
|
undefined,
|
|
58
|
-
|
|
58
|
+
{
|
|
59
|
+
resubTimeoutMs: this.resubTimeoutMs,
|
|
60
|
+
},
|
|
59
61
|
this.commitment
|
|
60
62
|
);
|
|
61
63
|
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
DataAndSlot,
|
|
3
|
+
BufferAndSlot,
|
|
4
|
+
ProgramAccountSubscriber,
|
|
5
|
+
ResubOpts,
|
|
6
|
+
} from './types';
|
|
2
7
|
import { AnchorProvider, Program } from '@coral-xyz/anchor';
|
|
3
8
|
import {
|
|
4
9
|
Commitment,
|
|
@@ -25,7 +30,7 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
25
30
|
buffer: Buffer
|
|
26
31
|
) => void;
|
|
27
32
|
listenerId?: number;
|
|
28
|
-
|
|
33
|
+
resubOpts?: ResubOpts;
|
|
29
34
|
isUnsubscribing = false;
|
|
30
35
|
timeoutId?: NodeJS.Timeout;
|
|
31
36
|
options: { filters: MemcmpFilter[]; commitment?: Commitment };
|
|
@@ -40,14 +45,14 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
40
45
|
options: { filters: MemcmpFilter[]; commitment?: Commitment } = {
|
|
41
46
|
filters: [],
|
|
42
47
|
},
|
|
43
|
-
|
|
48
|
+
resubOpts?: ResubOpts
|
|
44
49
|
) {
|
|
45
50
|
this.subscriptionName = subscriptionName;
|
|
46
51
|
this.accountDiscriminator = accountDiscriminator;
|
|
47
52
|
this.program = program;
|
|
48
53
|
this.decodeBuffer = decodeBufferFn;
|
|
49
|
-
this.
|
|
50
|
-
if (this.resubTimeoutMs < 1000) {
|
|
54
|
+
this.resubOpts = resubOpts;
|
|
55
|
+
if (this.resubOpts?.resubTimeoutMs < 1000) {
|
|
51
56
|
console.log(
|
|
52
57
|
'resubTimeoutMs should be at least 1000ms to avoid spamming resub'
|
|
53
58
|
);
|
|
@@ -73,7 +78,7 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
73
78
|
this.listenerId = this.program.provider.connection.onProgramAccountChange(
|
|
74
79
|
this.program.programId,
|
|
75
80
|
(keyedAccountInfo, context) => {
|
|
76
|
-
if (this.resubTimeoutMs) {
|
|
81
|
+
if (this.resubOpts?.resubTimeoutMs) {
|
|
77
82
|
this.receivingData = true;
|
|
78
83
|
clearTimeout(this.timeoutId);
|
|
79
84
|
this.handleRpcResponse(context, keyedAccountInfo);
|
|
@@ -87,7 +92,7 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
87
92
|
this.options.filters
|
|
88
93
|
);
|
|
89
94
|
|
|
90
|
-
if (this.resubTimeoutMs) {
|
|
95
|
+
if (this.resubOpts?.resubTimeoutMs) {
|
|
91
96
|
this.receivingData = true;
|
|
92
97
|
this.setTimeout();
|
|
93
98
|
}
|
|
@@ -97,21 +102,26 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
97
102
|
if (!this.onChange) {
|
|
98
103
|
throw new Error('onChange callback function must be set');
|
|
99
104
|
}
|
|
100
|
-
this.timeoutId = setTimeout(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
+
this.timeoutId = setTimeout(
|
|
106
|
+
async () => {
|
|
107
|
+
if (this.isUnsubscribing) {
|
|
108
|
+
// If we are in the process of unsubscribing, do not attempt to resubscribe
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
105
111
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
112
|
+
if (this.receivingData) {
|
|
113
|
+
if (this.resubOpts?.logResubMessages) {
|
|
114
|
+
console.log(
|
|
115
|
+
`No ws data from ${this.subscriptionName} in ${this.resubOpts?.resubTimeoutMs}ms, resubscribing`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
await this.unsubscribe(true);
|
|
119
|
+
this.receivingData = false;
|
|
120
|
+
await this.subscribe(this.onChange);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
this.resubOpts?.resubTimeoutMs
|
|
124
|
+
);
|
|
115
125
|
}
|
|
116
126
|
|
|
117
127
|
handleRpcResponse(
|
|
@@ -163,7 +173,7 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
163
173
|
|
|
164
174
|
unsubscribe(onResub = false): Promise<void> {
|
|
165
175
|
if (!onResub) {
|
|
166
|
-
this.resubTimeoutMs = undefined;
|
|
176
|
+
this.resubOpts.resubTimeoutMs = undefined;
|
|
167
177
|
}
|
|
168
178
|
this.isUnsubscribing = true;
|
|
169
179
|
clearTimeout(this.timeoutId);
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
NotSubscribedError,
|
|
5
5
|
UserAccountEvents,
|
|
6
6
|
UserAccountSubscriber,
|
|
7
|
+
ResubOpts,
|
|
7
8
|
} from './types';
|
|
8
9
|
import { Program } from '@coral-xyz/anchor';
|
|
9
10
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
@@ -14,7 +15,7 @@ import { UserAccount } from '../types';
|
|
|
14
15
|
|
|
15
16
|
export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
|
|
16
17
|
isSubscribed: boolean;
|
|
17
|
-
|
|
18
|
+
resubOpts?: ResubOpts;
|
|
18
19
|
commitment?: Commitment;
|
|
19
20
|
program: Program;
|
|
20
21
|
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
@@ -25,14 +26,14 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
25
26
|
public constructor(
|
|
26
27
|
program: Program,
|
|
27
28
|
userAccountPublicKey: PublicKey,
|
|
28
|
-
|
|
29
|
+
resubOpts?: ResubOpts,
|
|
29
30
|
commitment?: Commitment
|
|
30
31
|
) {
|
|
31
32
|
this.isSubscribed = false;
|
|
32
33
|
this.program = program;
|
|
34
|
+
this.resubOpts = resubOpts;
|
|
33
35
|
this.userAccountPublicKey = userAccountPublicKey;
|
|
34
36
|
this.eventEmitter = new EventEmitter();
|
|
35
|
-
this.reconnectTimeoutMs = reconnectTimeoutMs;
|
|
36
37
|
this.commitment = commitment;
|
|
37
38
|
}
|
|
38
39
|
|
|
@@ -46,7 +47,7 @@ export class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
46
47
|
this.program,
|
|
47
48
|
this.userAccountPublicKey,
|
|
48
49
|
undefined,
|
|
49
|
-
this.
|
|
50
|
+
this.resubOpts,
|
|
50
51
|
this.commitment
|
|
51
52
|
);
|
|
52
53
|
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
NotSubscribedError,
|
|
5
5
|
UserStatsAccountSubscriber,
|
|
6
6
|
UserStatsAccountEvents,
|
|
7
|
+
ResubOpts,
|
|
7
8
|
} from './types';
|
|
8
9
|
import { Program } from '@coral-xyz/anchor';
|
|
9
10
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
@@ -16,7 +17,7 @@ export class WebSocketUserStatsAccountSubscriber
|
|
|
16
17
|
implements UserStatsAccountSubscriber
|
|
17
18
|
{
|
|
18
19
|
isSubscribed: boolean;
|
|
19
|
-
|
|
20
|
+
resubOpts?: ResubOpts;
|
|
20
21
|
commitment?: Commitment;
|
|
21
22
|
program: Program;
|
|
22
23
|
eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
|
|
@@ -27,14 +28,14 @@ export class WebSocketUserStatsAccountSubscriber
|
|
|
27
28
|
public constructor(
|
|
28
29
|
program: Program,
|
|
29
30
|
userStatsAccountPublicKey: PublicKey,
|
|
30
|
-
|
|
31
|
+
resubOpts?: ResubOpts,
|
|
31
32
|
commitment?: Commitment
|
|
32
33
|
) {
|
|
33
34
|
this.isSubscribed = false;
|
|
34
35
|
this.program = program;
|
|
35
36
|
this.userStatsAccountPublicKey = userStatsAccountPublicKey;
|
|
36
37
|
this.eventEmitter = new EventEmitter();
|
|
37
|
-
this.
|
|
38
|
+
this.resubOpts = resubOpts;
|
|
38
39
|
this.commitment = commitment;
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -48,7 +49,7 @@ export class WebSocketUserStatsAccountSubscriber
|
|
|
48
49
|
this.program,
|
|
49
50
|
this.userStatsAccountPublicKey,
|
|
50
51
|
undefined,
|
|
51
|
-
this.
|
|
52
|
+
this.resubOpts,
|
|
52
53
|
this.commitment
|
|
53
54
|
);
|
|
54
55
|
|