@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/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.82.0-beta.
|
|
1
|
+
2.82.0-beta.6
|
package/lib/accounts/types.d.ts
CHANGED
|
@@ -126,6 +126,10 @@ export type DataAndSlot<T> = {
|
|
|
126
126
|
data: T;
|
|
127
127
|
slot: number;
|
|
128
128
|
};
|
|
129
|
+
export type ResubOpts = {
|
|
130
|
+
resubTimeoutMs?: number;
|
|
131
|
+
logResubMessages?: boolean;
|
|
132
|
+
};
|
|
129
133
|
export interface UserStatsAccountEvents {
|
|
130
134
|
userStatsAccountUpdate: (payload: UserStatsAccount) => void;
|
|
131
135
|
update: void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
-
import { DataAndSlot, BufferAndSlot, AccountSubscriber } from './types';
|
|
3
|
+
import { DataAndSlot, BufferAndSlot, AccountSubscriber, ResubOpts } from './types';
|
|
4
4
|
import { Program } from '@coral-xyz/anchor';
|
|
5
5
|
import { AccountInfo, Commitment, Context, PublicKey } from '@solana/web3.js';
|
|
6
6
|
export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
@@ -12,12 +12,12 @@ export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<
|
|
|
12
12
|
decodeBufferFn: (buffer: Buffer) => T;
|
|
13
13
|
onChange: (data: T) => void;
|
|
14
14
|
listenerId?: number;
|
|
15
|
-
|
|
15
|
+
resubOpts?: ResubOpts;
|
|
16
16
|
commitment?: Commitment;
|
|
17
17
|
isUnsubscribing: boolean;
|
|
18
18
|
timeoutId?: NodeJS.Timeout;
|
|
19
19
|
receivingData: boolean;
|
|
20
|
-
constructor(accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => T,
|
|
20
|
+
constructor(accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => T, resubOpts?: ResubOpts, commitment?: Commitment);
|
|
21
21
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
22
22
|
setData(data: T, slot?: number): void;
|
|
23
23
|
private setTimeout;
|
|
@@ -3,14 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.WebSocketAccountSubscriber = void 0;
|
|
4
4
|
const utils_1 = require("./utils");
|
|
5
5
|
class WebSocketAccountSubscriber {
|
|
6
|
-
constructor(accountName, program, accountPublicKey, decodeBuffer,
|
|
6
|
+
constructor(accountName, program, accountPublicKey, decodeBuffer, resubOpts, commitment) {
|
|
7
|
+
var _a;
|
|
7
8
|
this.isUnsubscribing = false;
|
|
8
9
|
this.accountName = accountName;
|
|
9
10
|
this.program = program;
|
|
10
11
|
this.accountPublicKey = accountPublicKey;
|
|
11
12
|
this.decodeBufferFn = decodeBuffer;
|
|
12
|
-
this.
|
|
13
|
-
if (this.resubTimeoutMs < 1000) {
|
|
13
|
+
this.resubOpts = resubOpts;
|
|
14
|
+
if (((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) < 1000) {
|
|
14
15
|
console.log('resubTimeoutMs should be at least 1000ms to avoid spamming resub');
|
|
15
16
|
}
|
|
16
17
|
this.receivingData = false;
|
|
@@ -18,6 +19,7 @@ class WebSocketAccountSubscriber {
|
|
|
18
19
|
commitment !== null && commitment !== void 0 ? commitment : this.program.provider.opts.commitment;
|
|
19
20
|
}
|
|
20
21
|
async subscribe(onChange) {
|
|
22
|
+
var _a;
|
|
21
23
|
if (this.listenerId != null || this.isUnsubscribing) {
|
|
22
24
|
return;
|
|
23
25
|
}
|
|
@@ -26,7 +28,8 @@ class WebSocketAccountSubscriber {
|
|
|
26
28
|
await this.fetch();
|
|
27
29
|
}
|
|
28
30
|
this.listenerId = this.program.provider.connection.onAccountChange(this.accountPublicKey, (accountInfo, context) => {
|
|
29
|
-
|
|
31
|
+
var _a;
|
|
32
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
|
|
30
33
|
this.receivingData = true;
|
|
31
34
|
clearTimeout(this.timeoutId);
|
|
32
35
|
this.handleRpcResponse(context, accountInfo);
|
|
@@ -36,7 +39,7 @@ class WebSocketAccountSubscriber {
|
|
|
36
39
|
this.handleRpcResponse(context, accountInfo);
|
|
37
40
|
}
|
|
38
41
|
}, this.commitment);
|
|
39
|
-
if (this.resubTimeoutMs) {
|
|
42
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
|
|
40
43
|
this.receivingData = true;
|
|
41
44
|
this.setTimeout();
|
|
42
45
|
}
|
|
@@ -52,21 +55,25 @@ class WebSocketAccountSubscriber {
|
|
|
52
55
|
};
|
|
53
56
|
}
|
|
54
57
|
setTimeout() {
|
|
58
|
+
var _a;
|
|
55
59
|
if (!this.onChange) {
|
|
56
60
|
throw new Error('onChange callback function must be set');
|
|
57
61
|
}
|
|
58
62
|
this.timeoutId = setTimeout(async () => {
|
|
63
|
+
var _a;
|
|
59
64
|
if (this.isUnsubscribing) {
|
|
60
65
|
// If we are in the process of unsubscribing, do not attempt to resubscribe
|
|
61
66
|
return;
|
|
62
67
|
}
|
|
63
68
|
if (this.receivingData) {
|
|
64
|
-
|
|
69
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
|
|
70
|
+
console.log(`No ws data from ${this.accountName} in ${this.resubOpts.resubTimeoutMs}ms, resubscribing`);
|
|
71
|
+
}
|
|
65
72
|
await this.unsubscribe(true);
|
|
66
73
|
this.receivingData = false;
|
|
67
74
|
await this.subscribe(this.onChange);
|
|
68
75
|
}
|
|
69
|
-
}, this.resubTimeoutMs);
|
|
76
|
+
}, (_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs);
|
|
70
77
|
}
|
|
71
78
|
async fetch() {
|
|
72
79
|
const rpcResponse = await this.program.provider.connection.getAccountInfoAndContext(this.accountPublicKey, this.program.provider.opts.commitment);
|
|
@@ -120,7 +127,7 @@ class WebSocketAccountSubscriber {
|
|
|
120
127
|
}
|
|
121
128
|
unsubscribe(onResub = false) {
|
|
122
129
|
if (!onResub) {
|
|
123
|
-
this.resubTimeoutMs = undefined;
|
|
130
|
+
this.resubOpts.resubTimeoutMs = undefined;
|
|
124
131
|
}
|
|
125
132
|
this.isUnsubscribing = true;
|
|
126
133
|
clearTimeout(this.timeoutId);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { DriftClientAccountSubscriber, DriftClientAccountEvents, DataAndSlot } from './types';
|
|
2
|
+
import { DriftClientAccountSubscriber, DriftClientAccountEvents, DataAndSlot, ResubOpts } from './types';
|
|
3
3
|
import { AccountSubscriber } from './types';
|
|
4
4
|
import { SpotMarketAccount, PerpMarketAccount, StateAccount } from '../types';
|
|
5
5
|
import { Program } from '@coral-xyz/anchor';
|
|
@@ -16,7 +16,7 @@ export declare class WebSocketDriftClientAccountSubscriber implements DriftClien
|
|
|
16
16
|
spotMarketIndexes: number[];
|
|
17
17
|
oracleInfos: OracleInfo[];
|
|
18
18
|
oracleClientCache: OracleClientCache;
|
|
19
|
-
|
|
19
|
+
resubOpts?: ResubOpts;
|
|
20
20
|
shouldFindAllMarketsAndOracles: boolean;
|
|
21
21
|
eventEmitter: StrictEventEmitter<EventEmitter, DriftClientAccountEvents>;
|
|
22
22
|
stateAccountSubscriber?: AccountSubscriber<StateAccount>;
|
|
@@ -28,7 +28,7 @@ export declare class WebSocketDriftClientAccountSubscriber implements DriftClien
|
|
|
28
28
|
private isSubscribing;
|
|
29
29
|
private subscriptionPromise;
|
|
30
30
|
private subscriptionPromiseResolver;
|
|
31
|
-
constructor(program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean,
|
|
31
|
+
constructor(program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, resubOpts?: ResubOpts, commitment?: Commitment);
|
|
32
32
|
subscribe(): Promise<boolean>;
|
|
33
33
|
subscribeToPerpMarketAccounts(): Promise<boolean>;
|
|
34
34
|
subscribeToPerpMarketAccount(marketIndex: number): Promise<boolean>;
|
|
@@ -10,7 +10,7 @@ const oracleClientCache_1 = require("../oracles/oracleClientCache");
|
|
|
10
10
|
const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
|
|
11
11
|
const config_1 = require("../config");
|
|
12
12
|
class WebSocketDriftClientAccountSubscriber {
|
|
13
|
-
constructor(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles,
|
|
13
|
+
constructor(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, resubOpts, commitment) {
|
|
14
14
|
this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
|
|
15
15
|
this.perpMarketAccountSubscribers = new Map();
|
|
16
16
|
this.perpOracleMap = new Map();
|
|
@@ -25,7 +25,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
25
25
|
this.spotMarketIndexes = spotMarketIndexes;
|
|
26
26
|
this.oracleInfos = oracleInfos;
|
|
27
27
|
this.shouldFindAllMarketsAndOracles = shouldFindAllMarketsAndOracles;
|
|
28
|
-
this.
|
|
28
|
+
this.resubOpts = resubOpts;
|
|
29
29
|
this.commitment = commitment;
|
|
30
30
|
}
|
|
31
31
|
async subscribe() {
|
|
@@ -74,7 +74,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
74
74
|
}
|
|
75
75
|
async subscribeToPerpMarketAccount(marketIndex) {
|
|
76
76
|
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
|
|
77
|
-
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('perpMarket', this.program, perpMarketPublicKey, undefined, this.
|
|
77
|
+
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('perpMarket', this.program, perpMarketPublicKey, undefined, this.resubOpts, this.commitment);
|
|
78
78
|
await accountSubscriber.subscribe((data) => {
|
|
79
79
|
this.eventEmitter.emit('perpMarketAccountUpdate', data);
|
|
80
80
|
this.eventEmitter.emit('update');
|
|
@@ -90,7 +90,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
90
90
|
}
|
|
91
91
|
async subscribeToSpotMarketAccount(marketIndex) {
|
|
92
92
|
const marketPublicKey = await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex);
|
|
93
|
-
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('spotMarket', this.program, marketPublicKey, undefined, this.
|
|
93
|
+
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('spotMarket', this.program, marketPublicKey, undefined, this.resubOpts, this.commitment);
|
|
94
94
|
await accountSubscriber.subscribe((data) => {
|
|
95
95
|
this.eventEmitter.emit('spotMarketAccountUpdate', data);
|
|
96
96
|
this.eventEmitter.emit('update');
|
|
@@ -110,7 +110,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
110
110
|
const client = this.oracleClientCache.get(oracleInfo.source, this.program.provider.connection, this.program);
|
|
111
111
|
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('oracle', this.program, oracleInfo.publicKey, (buffer) => {
|
|
112
112
|
return client.getOraclePriceDataFromBuffer(buffer);
|
|
113
|
-
}, this.
|
|
113
|
+
}, this.resubOpts, this.commitment);
|
|
114
114
|
await accountSubscriber.subscribe((data) => {
|
|
115
115
|
this.eventEmitter.emit('oraclePriceUpdate', oracleInfo.publicKey, data);
|
|
116
116
|
this.eventEmitter.emit('update');
|
|
@@ -7,13 +7,13 @@ import { Commitment, PublicKey } from '@solana/web3.js';
|
|
|
7
7
|
import { InsuranceFundStake } from '../types';
|
|
8
8
|
export declare class WebSocketInsuranceFundStakeAccountSubscriber implements InsuranceFundStakeAccountSubscriber {
|
|
9
9
|
isSubscribed: boolean;
|
|
10
|
-
|
|
10
|
+
resubTimeoutMs?: number;
|
|
11
11
|
commitment?: Commitment;
|
|
12
12
|
program: Program;
|
|
13
13
|
eventEmitter: StrictEventEmitter<EventEmitter, InsuranceFundStakeAccountEvents>;
|
|
14
14
|
insuranceFundStakeAccountPublicKey: PublicKey;
|
|
15
15
|
insuranceFundStakeDataAccountSubscriber: AccountSubscriber<InsuranceFundStake>;
|
|
16
|
-
constructor(program: Program, insuranceFundStakeAccountPublicKey: PublicKey,
|
|
16
|
+
constructor(program: Program, insuranceFundStakeAccountPublicKey: PublicKey, resubTimeoutMs?: number, commitment?: Commitment);
|
|
17
17
|
subscribe(insuranceFundStakeAccount?: InsuranceFundStake): Promise<boolean>;
|
|
18
18
|
fetch(): Promise<void>;
|
|
19
19
|
unsubscribe(): Promise<void>;
|
|
@@ -5,13 +5,13 @@ const types_1 = require("./types");
|
|
|
5
5
|
const events_1 = require("events");
|
|
6
6
|
const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
|
|
7
7
|
class WebSocketInsuranceFundStakeAccountSubscriber {
|
|
8
|
-
constructor(program, insuranceFundStakeAccountPublicKey,
|
|
8
|
+
constructor(program, insuranceFundStakeAccountPublicKey, resubTimeoutMs, commitment) {
|
|
9
9
|
this.isSubscribed = false;
|
|
10
10
|
this.program = program;
|
|
11
11
|
this.insuranceFundStakeAccountPublicKey =
|
|
12
12
|
insuranceFundStakeAccountPublicKey;
|
|
13
13
|
this.eventEmitter = new events_1.EventEmitter();
|
|
14
|
-
this.
|
|
14
|
+
this.resubTimeoutMs = resubTimeoutMs;
|
|
15
15
|
this.commitment = commitment;
|
|
16
16
|
}
|
|
17
17
|
async subscribe(insuranceFundStakeAccount) {
|
|
@@ -19,7 +19,9 @@ class WebSocketInsuranceFundStakeAccountSubscriber {
|
|
|
19
19
|
return true;
|
|
20
20
|
}
|
|
21
21
|
this.insuranceFundStakeDataAccountSubscriber =
|
|
22
|
-
new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('insuranceFundStake', this.program, this.insuranceFundStakeAccountPublicKey, undefined,
|
|
22
|
+
new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('insuranceFundStake', this.program, this.insuranceFundStakeAccountPublicKey, undefined, {
|
|
23
|
+
resubTimeoutMs: this.resubTimeoutMs,
|
|
24
|
+
}, this.commitment);
|
|
23
25
|
if (insuranceFundStakeAccount) {
|
|
24
26
|
this.insuranceFundStakeDataAccountSubscriber.setData(insuranceFundStakeAccount);
|
|
25
27
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
-
import { DataAndSlot, BufferAndSlot, ProgramAccountSubscriber } from './types';
|
|
3
|
+
import { DataAndSlot, BufferAndSlot, ProgramAccountSubscriber, ResubOpts } from './types';
|
|
4
4
|
import { Program } from '@coral-xyz/anchor';
|
|
5
5
|
import { Commitment, Context, KeyedAccountInfo, MemcmpFilter, PublicKey } from '@solana/web3.js';
|
|
6
6
|
export declare class WebSocketProgramAccountSubscriber<T> implements ProgramAccountSubscriber<T> {
|
|
@@ -14,7 +14,7 @@ export declare class WebSocketProgramAccountSubscriber<T> implements ProgramAcco
|
|
|
14
14
|
decodeBuffer: (accountName: string, ix: Buffer) => T;
|
|
15
15
|
onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void;
|
|
16
16
|
listenerId?: number;
|
|
17
|
-
|
|
17
|
+
resubOpts?: ResubOpts;
|
|
18
18
|
isUnsubscribing: boolean;
|
|
19
19
|
timeoutId?: NodeJS.Timeout;
|
|
20
20
|
options: {
|
|
@@ -25,7 +25,7 @@ export declare class WebSocketProgramAccountSubscriber<T> implements ProgramAcco
|
|
|
25
25
|
constructor(subscriptionName: string, accountDiscriminator: string, program: Program, decodeBufferFn: (accountName: string, ix: Buffer) => T, options?: {
|
|
26
26
|
filters: MemcmpFilter[];
|
|
27
27
|
commitment?: Commitment;
|
|
28
|
-
},
|
|
28
|
+
}, resubOpts?: ResubOpts);
|
|
29
29
|
subscribe(onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
|
|
30
30
|
private setTimeout;
|
|
31
31
|
handleRpcResponse(context: Context, keyedAccountInfo: KeyedAccountInfo): void;
|
|
@@ -4,28 +4,30 @@ exports.WebSocketProgramAccountSubscriber = void 0;
|
|
|
4
4
|
class WebSocketProgramAccountSubscriber {
|
|
5
5
|
constructor(subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
|
|
6
6
|
filters: [],
|
|
7
|
-
},
|
|
7
|
+
}, resubOpts) {
|
|
8
|
+
var _a;
|
|
8
9
|
this.isUnsubscribing = false;
|
|
9
10
|
this.receivingData = false;
|
|
10
11
|
this.subscriptionName = subscriptionName;
|
|
11
12
|
this.accountDiscriminator = accountDiscriminator;
|
|
12
13
|
this.program = program;
|
|
13
14
|
this.decodeBuffer = decodeBufferFn;
|
|
14
|
-
this.
|
|
15
|
-
if (this.resubTimeoutMs < 1000) {
|
|
15
|
+
this.resubOpts = resubOpts;
|
|
16
|
+
if (((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) < 1000) {
|
|
16
17
|
console.log('resubTimeoutMs should be at least 1000ms to avoid spamming resub');
|
|
17
18
|
}
|
|
18
19
|
this.options = options;
|
|
19
20
|
this.receivingData = false;
|
|
20
21
|
}
|
|
21
22
|
async subscribe(onChange) {
|
|
22
|
-
var _a;
|
|
23
|
+
var _a, _b;
|
|
23
24
|
if (this.listenerId != null || this.isUnsubscribing) {
|
|
24
25
|
return;
|
|
25
26
|
}
|
|
26
27
|
this.onChange = onChange;
|
|
27
28
|
this.listenerId = this.program.provider.connection.onProgramAccountChange(this.program.programId, (keyedAccountInfo, context) => {
|
|
28
|
-
|
|
29
|
+
var _a;
|
|
30
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
|
|
29
31
|
this.receivingData = true;
|
|
30
32
|
clearTimeout(this.timeoutId);
|
|
31
33
|
this.handleRpcResponse(context, keyedAccountInfo);
|
|
@@ -35,27 +37,31 @@ class WebSocketProgramAccountSubscriber {
|
|
|
35
37
|
this.handleRpcResponse(context, keyedAccountInfo);
|
|
36
38
|
}
|
|
37
39
|
}, (_a = this.options.commitment) !== null && _a !== void 0 ? _a : this.program.provider.opts.commitment, this.options.filters);
|
|
38
|
-
if (this.resubTimeoutMs) {
|
|
40
|
+
if ((_b = this.resubOpts) === null || _b === void 0 ? void 0 : _b.resubTimeoutMs) {
|
|
39
41
|
this.receivingData = true;
|
|
40
42
|
this.setTimeout();
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
setTimeout() {
|
|
46
|
+
var _a;
|
|
44
47
|
if (!this.onChange) {
|
|
45
48
|
throw new Error('onChange callback function must be set');
|
|
46
49
|
}
|
|
47
50
|
this.timeoutId = setTimeout(async () => {
|
|
51
|
+
var _a, _b;
|
|
48
52
|
if (this.isUnsubscribing) {
|
|
49
53
|
// If we are in the process of unsubscribing, do not attempt to resubscribe
|
|
50
54
|
return;
|
|
51
55
|
}
|
|
52
56
|
if (this.receivingData) {
|
|
53
|
-
|
|
57
|
+
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.logResubMessages) {
|
|
58
|
+
console.log(`No ws data from ${this.subscriptionName} in ${(_b = this.resubOpts) === null || _b === void 0 ? void 0 : _b.resubTimeoutMs}ms, resubscribing`);
|
|
59
|
+
}
|
|
54
60
|
await this.unsubscribe(true);
|
|
55
61
|
this.receivingData = false;
|
|
56
62
|
await this.subscribe(this.onChange);
|
|
57
63
|
}
|
|
58
|
-
}, this.resubTimeoutMs);
|
|
64
|
+
}, (_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs);
|
|
59
65
|
}
|
|
60
66
|
handleRpcResponse(context, keyedAccountInfo) {
|
|
61
67
|
const newSlot = context.slot;
|
|
@@ -99,7 +105,7 @@ class WebSocketProgramAccountSubscriber {
|
|
|
99
105
|
}
|
|
100
106
|
unsubscribe(onResub = false) {
|
|
101
107
|
if (!onResub) {
|
|
102
|
-
this.resubTimeoutMs = undefined;
|
|
108
|
+
this.resubOpts.resubTimeoutMs = undefined;
|
|
103
109
|
}
|
|
104
110
|
this.isUnsubscribing = true;
|
|
105
111
|
clearTimeout(this.timeoutId);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { DataAndSlot, AccountSubscriber, UserAccountEvents, UserAccountSubscriber } from './types';
|
|
2
|
+
import { DataAndSlot, AccountSubscriber, UserAccountEvents, UserAccountSubscriber, ResubOpts } from './types';
|
|
3
3
|
import { Program } from '@coral-xyz/anchor';
|
|
4
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
5
|
import { EventEmitter } from 'events';
|
|
@@ -7,13 +7,13 @@ import { Commitment, PublicKey } from '@solana/web3.js';
|
|
|
7
7
|
import { UserAccount } from '../types';
|
|
8
8
|
export declare class WebSocketUserAccountSubscriber implements UserAccountSubscriber {
|
|
9
9
|
isSubscribed: boolean;
|
|
10
|
-
|
|
10
|
+
resubOpts?: ResubOpts;
|
|
11
11
|
commitment?: Commitment;
|
|
12
12
|
program: Program;
|
|
13
13
|
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
14
14
|
userAccountPublicKey: PublicKey;
|
|
15
15
|
userDataAccountSubscriber: AccountSubscriber<UserAccount>;
|
|
16
|
-
constructor(program: Program, userAccountPublicKey: PublicKey,
|
|
16
|
+
constructor(program: Program, userAccountPublicKey: PublicKey, resubOpts?: ResubOpts, commitment?: Commitment);
|
|
17
17
|
subscribe(userAccount?: UserAccount): Promise<boolean>;
|
|
18
18
|
fetch(): Promise<void>;
|
|
19
19
|
unsubscribe(): Promise<void>;
|
|
@@ -5,19 +5,19 @@ const types_1 = require("./types");
|
|
|
5
5
|
const events_1 = require("events");
|
|
6
6
|
const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
|
|
7
7
|
class WebSocketUserAccountSubscriber {
|
|
8
|
-
constructor(program, userAccountPublicKey,
|
|
8
|
+
constructor(program, userAccountPublicKey, resubOpts, commitment) {
|
|
9
9
|
this.isSubscribed = false;
|
|
10
10
|
this.program = program;
|
|
11
|
+
this.resubOpts = resubOpts;
|
|
11
12
|
this.userAccountPublicKey = userAccountPublicKey;
|
|
12
13
|
this.eventEmitter = new events_1.EventEmitter();
|
|
13
|
-
this.reconnectTimeoutMs = reconnectTimeoutMs;
|
|
14
14
|
this.commitment = commitment;
|
|
15
15
|
}
|
|
16
16
|
async subscribe(userAccount) {
|
|
17
17
|
if (this.isSubscribed) {
|
|
18
18
|
return true;
|
|
19
19
|
}
|
|
20
|
-
this.userDataAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('user', this.program, this.userAccountPublicKey, undefined, this.
|
|
20
|
+
this.userDataAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('user', this.program, this.userAccountPublicKey, undefined, this.resubOpts, this.commitment);
|
|
21
21
|
if (userAccount) {
|
|
22
22
|
this.userDataAccountSubscriber.setData(userAccount);
|
|
23
23
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { DataAndSlot, AccountSubscriber, UserStatsAccountSubscriber, UserStatsAccountEvents } from './types';
|
|
2
|
+
import { DataAndSlot, AccountSubscriber, UserStatsAccountSubscriber, UserStatsAccountEvents, ResubOpts } from './types';
|
|
3
3
|
import { Program } from '@coral-xyz/anchor';
|
|
4
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
5
|
import { EventEmitter } from 'events';
|
|
@@ -7,13 +7,13 @@ import { Commitment, PublicKey } from '@solana/web3.js';
|
|
|
7
7
|
import { UserStatsAccount } from '../types';
|
|
8
8
|
export declare class WebSocketUserStatsAccountSubscriber implements UserStatsAccountSubscriber {
|
|
9
9
|
isSubscribed: boolean;
|
|
10
|
-
|
|
10
|
+
resubOpts?: ResubOpts;
|
|
11
11
|
commitment?: Commitment;
|
|
12
12
|
program: Program;
|
|
13
13
|
eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
|
|
14
14
|
userStatsAccountPublicKey: PublicKey;
|
|
15
15
|
userStatsAccountSubscriber: AccountSubscriber<UserStatsAccount>;
|
|
16
|
-
constructor(program: Program, userStatsAccountPublicKey: PublicKey,
|
|
16
|
+
constructor(program: Program, userStatsAccountPublicKey: PublicKey, resubOpts?: ResubOpts, commitment?: Commitment);
|
|
17
17
|
subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean>;
|
|
18
18
|
fetch(): Promise<void>;
|
|
19
19
|
unsubscribe(): Promise<void>;
|
|
@@ -5,19 +5,19 @@ const types_1 = require("./types");
|
|
|
5
5
|
const events_1 = require("events");
|
|
6
6
|
const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
|
|
7
7
|
class WebSocketUserStatsAccountSubscriber {
|
|
8
|
-
constructor(program, userStatsAccountPublicKey,
|
|
8
|
+
constructor(program, userStatsAccountPublicKey, resubOpts, commitment) {
|
|
9
9
|
this.isSubscribed = false;
|
|
10
10
|
this.program = program;
|
|
11
11
|
this.userStatsAccountPublicKey = userStatsAccountPublicKey;
|
|
12
12
|
this.eventEmitter = new events_1.EventEmitter();
|
|
13
|
-
this.
|
|
13
|
+
this.resubOpts = resubOpts;
|
|
14
14
|
this.commitment = commitment;
|
|
15
15
|
}
|
|
16
16
|
async subscribe(userStatsAccount) {
|
|
17
17
|
if (this.isSubscribed) {
|
|
18
18
|
return true;
|
|
19
19
|
}
|
|
20
|
-
this.userStatsAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('userStats', this.program, this.userStatsAccountPublicKey, undefined, this.
|
|
20
|
+
this.userStatsAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('userStats', this.program, this.userStatsAccountPublicKey, undefined, this.resubOpts, this.commitment);
|
|
21
21
|
if (userStatsAccount) {
|
|
22
22
|
this.userStatsAccountSubscriber.setData(userStatsAccount);
|
|
23
23
|
}
|
|
@@ -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;
|
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,
|
|
@@ -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();
|