@drift-labs/sdk 2.82.0-beta.5 → 2.82.0-beta.6
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/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/driftClient.js +13 -8
- 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/tx/utils.js +5 -1
- package/lib/user.js +5 -2
- 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 +1 -1
- 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/driftClient.ts +6 -1
- 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/tx/utils.ts +5 -1
- package/src/user.ts +4 -1
- 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/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.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
|
}
|
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
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
|
|
|
@@ -6,20 +6,26 @@ import { EventEmitter } from 'events';
|
|
|
6
6
|
import { UserAccount } from '../types';
|
|
7
7
|
import { ConfirmOptions, Context, PublicKey } from '@solana/web3.js';
|
|
8
8
|
import { WebSocketProgramAccountSubscriber } from '../accounts/webSocketProgramAccountSubscriber';
|
|
9
|
+
import { ResubOpts } from '../accounts/types';
|
|
9
10
|
|
|
10
11
|
export class AuctionSubscriber {
|
|
11
12
|
private driftClient: DriftClient;
|
|
12
13
|
private opts: ConfirmOptions;
|
|
13
|
-
private
|
|
14
|
+
private resubOpts?: ResubOpts;
|
|
14
15
|
|
|
15
16
|
eventEmitter: StrictEventEmitter<EventEmitter, AuctionSubscriberEvents>;
|
|
16
17
|
private subscriber: WebSocketProgramAccountSubscriber<UserAccount>;
|
|
17
18
|
|
|
18
|
-
constructor({
|
|
19
|
+
constructor({
|
|
20
|
+
driftClient,
|
|
21
|
+
opts,
|
|
22
|
+
resubTimeoutMs,
|
|
23
|
+
logResubMessages,
|
|
24
|
+
}: AuctionSubscriberConfig) {
|
|
19
25
|
this.driftClient = driftClient;
|
|
20
26
|
this.opts = opts || this.driftClient.opts;
|
|
21
27
|
this.eventEmitter = new EventEmitter();
|
|
22
|
-
this.
|
|
28
|
+
this.resubOpts = { resubTimeoutMs, logResubMessages };
|
|
23
29
|
}
|
|
24
30
|
|
|
25
31
|
public async subscribe() {
|
|
@@ -35,7 +41,7 @@ export class AuctionSubscriber {
|
|
|
35
41
|
filters: [getUserFilter(), getUserWithAuctionFilter()],
|
|
36
42
|
commitment: this.opts.commitment,
|
|
37
43
|
},
|
|
38
|
-
this.
|
|
44
|
+
this.resubOpts
|
|
39
45
|
);
|
|
40
46
|
}
|
|
41
47
|
|
package/src/driftClient.ts
CHANGED
|
@@ -251,11 +251,13 @@ export class DriftClient {
|
|
|
251
251
|
this.userAccountSubscriptionConfig = {
|
|
252
252
|
type: 'websocket',
|
|
253
253
|
resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
|
|
254
|
+
logResubMessages: config.accountSubscription?.logResubMessages,
|
|
254
255
|
commitment: config.accountSubscription?.commitment,
|
|
255
256
|
};
|
|
256
257
|
this.userStatsAccountSubscriptionConfig = {
|
|
257
258
|
type: 'websocket',
|
|
258
259
|
resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
|
|
260
|
+
logResubMessages: config.accountSubscription?.logResubMessages,
|
|
259
261
|
commitment: config.accountSubscription?.commitment,
|
|
260
262
|
};
|
|
261
263
|
}
|
|
@@ -298,7 +300,10 @@ export class DriftClient {
|
|
|
298
300
|
config.spotMarketIndexes ?? [],
|
|
299
301
|
config.oracleInfos ?? [],
|
|
300
302
|
noMarketsAndOraclesSpecified,
|
|
301
|
-
|
|
303
|
+
{
|
|
304
|
+
resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
|
|
305
|
+
logResubMessages: config.accountSubscription?.logResubMessages,
|
|
306
|
+
},
|
|
302
307
|
config.accountSubscription?.commitment
|
|
303
308
|
);
|
|
304
309
|
}
|
package/src/driftClientConfig.ts
CHANGED
|
@@ -39,7 +39,10 @@ export class OrderSubscriber {
|
|
|
39
39
|
orderSubscriber: this,
|
|
40
40
|
commitment: this.commitment,
|
|
41
41
|
skipInitialLoad: config.subscriptionConfig.skipInitialLoad,
|
|
42
|
-
|
|
42
|
+
resubOpts: {
|
|
43
|
+
resubTimeoutMs: config.subscriptionConfig?.resubTimeoutMs,
|
|
44
|
+
logResubMessages: config.subscriptionConfig?.logResubMessages,
|
|
45
|
+
},
|
|
43
46
|
resyncIntervalMs: config.subscriptionConfig.resyncIntervalMs,
|
|
44
47
|
decoded: config.decodeData,
|
|
45
48
|
});
|