@drift-labs/sdk 2.96.0-beta.2 → 2.96.0-beta.3
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 +0 -8
- package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +3 -3
- package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -1
- package/lib/driftClient.js +14 -35
- package/lib/driftClientConfig.d.ts +0 -6
- package/lib/orderSubscriber/OrderSubscriber.d.ts +1 -2
- package/lib/orderSubscriber/OrderSubscriber.js +4 -19
- package/lib/orderSubscriber/types.d.ts +0 -9
- package/lib/user.js +4 -11
- package/lib/userConfig.d.ts +1 -6
- package/lib/userMap/userMap.js +0 -14
- package/lib/userMap/userMapConfig.d.ts +0 -7
- package/lib/userStatsConfig.d.ts +0 -6
- package/package.json +1 -3
- package/src/accounts/types.ts +0 -9
- package/src/accounts/webSocketAccountSubscriber.ts +1 -1
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +3 -3
- package/src/accounts/webSocketProgramAccountSubscriber.ts +1 -1
- package/src/driftClient.ts +0 -28
- package/src/driftClientConfig.ts +0 -7
- package/src/orderSubscriber/OrderSubscriber.ts +1 -15
- package/src/orderSubscriber/types.ts +0 -10
- package/src/user.ts +0 -11
- package/src/userConfig.ts +1 -7
- package/src/userMap/userMap.ts +1 -17
- package/src/userMap/userMapConfig.ts +0 -8
- package/src/userStatsConfig.ts +0 -7
- package/lib/accounts/grpcAccountSubscriber.d.ts +0 -16
- package/lib/accounts/grpcAccountSubscriber.js +0 -155
- package/lib/accounts/grpcDriftClientAccountSubscriber.d.ts +0 -13
- package/lib/accounts/grpcDriftClientAccountSubscriber.js +0 -96
- package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +0 -10
- package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.js +0 -30
- package/lib/accounts/grpcProgramAccountSubscriber.d.ts +0 -19
- package/lib/accounts/grpcProgramAccountSubscriber.js +0 -161
- package/lib/accounts/grpcUserAccountSubscriber.d.ts +0 -10
- package/lib/accounts/grpcUserAccountSubscriber.js +0 -28
- package/lib/accounts/grpcUserStatsAccountSubscriber.d.ts +0 -10
- package/lib/accounts/grpcUserStatsAccountSubscriber.js +0 -28
- package/lib/orderSubscriber/grpcSubscription.d.ts +0 -25
- package/lib/orderSubscriber/grpcSubscription.js +0 -68
- package/lib/userMap/grpcSubscription.d.ts +0 -26
- package/lib/userMap/grpcSubscription.js +0 -42
- package/src/accounts/grpcAccountSubscriber.ts +0 -158
- package/src/accounts/grpcDriftClientAccountSubscriber.ts +0 -196
- package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +0 -62
- package/src/accounts/grpcProgramAccountSubscriber.ts +0 -181
- package/src/accounts/grpcUserAccountSubscriber.ts +0 -48
- package/src/accounts/grpcUserStatsAccountSubscriber.ts +0 -51
- package/src/orderSubscriber/grpcSubscription.ts +0 -126
- package/src/userMap/grpcSubscription.ts +0 -83
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { ResubOpts, GrpcConfigs } from './types';
|
|
2
|
-
import { Program } from '@coral-xyz/anchor';
|
|
3
|
-
import { Commitment, Context, MemcmpFilter, PublicKey } from '@solana/web3.js';
|
|
4
|
-
import * as Buffer from 'buffer';
|
|
5
|
-
import { WebSocketProgramAccountSubscriber } from './webSocketProgramAccountSubscriber';
|
|
6
|
-
import { ClientDuplexStream } from '@grpc/grpc-js';
|
|
7
|
-
import Client, {
|
|
8
|
-
SubscribeRequest,
|
|
9
|
-
SubscribeUpdate,
|
|
10
|
-
CommitmentLevel,
|
|
11
|
-
} from '@triton-one/yellowstone-grpc';
|
|
12
|
-
|
|
13
|
-
export class grpcProgramAccountSubscriber<
|
|
14
|
-
T,
|
|
15
|
-
> extends WebSocketProgramAccountSubscriber<T> {
|
|
16
|
-
client: Client;
|
|
17
|
-
stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
|
|
18
|
-
commitmentLevel: CommitmentLevel;
|
|
19
|
-
listenerId?: number;
|
|
20
|
-
|
|
21
|
-
public constructor(
|
|
22
|
-
grpcConfigs: GrpcConfigs,
|
|
23
|
-
subscriptionName: string,
|
|
24
|
-
accountDiscriminator: string,
|
|
25
|
-
program: Program,
|
|
26
|
-
decodeBufferFn: (accountName: string, ix: Buffer) => T,
|
|
27
|
-
options: { filters: MemcmpFilter[]; commitment?: Commitment } = {
|
|
28
|
-
filters: [],
|
|
29
|
-
},
|
|
30
|
-
resubOpts?: ResubOpts
|
|
31
|
-
) {
|
|
32
|
-
super(
|
|
33
|
-
subscriptionName,
|
|
34
|
-
accountDiscriminator,
|
|
35
|
-
program,
|
|
36
|
-
decodeBufferFn,
|
|
37
|
-
options,
|
|
38
|
-
resubOpts
|
|
39
|
-
);
|
|
40
|
-
this.client = new Client(
|
|
41
|
-
grpcConfigs.endpoint,
|
|
42
|
-
grpcConfigs.token,
|
|
43
|
-
grpcConfigs.channelOptions ?? {}
|
|
44
|
-
);
|
|
45
|
-
this.commitmentLevel =
|
|
46
|
-
grpcConfigs.commitmentLevel ?? CommitmentLevel.CONFIRMED;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async subscribe(
|
|
50
|
-
onChange: (
|
|
51
|
-
accountId: PublicKey,
|
|
52
|
-
data: T,
|
|
53
|
-
context: Context,
|
|
54
|
-
buffer: Buffer
|
|
55
|
-
) => void
|
|
56
|
-
): Promise<void> {
|
|
57
|
-
if (this.listenerId != null || this.isUnsubscribing) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
this.onChange = onChange;
|
|
62
|
-
|
|
63
|
-
// Subscribe with grpc
|
|
64
|
-
this.stream = await this.client.subscribe();
|
|
65
|
-
const request: SubscribeRequest = {
|
|
66
|
-
slots: {
|
|
67
|
-
slots: {},
|
|
68
|
-
},
|
|
69
|
-
accounts: {
|
|
70
|
-
drift: {
|
|
71
|
-
account: [],
|
|
72
|
-
owner: [this.program.programId.toBase58()],
|
|
73
|
-
// @ts-ignore
|
|
74
|
-
filters: this.options.filters,
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
transactions: {},
|
|
78
|
-
blocks: {},
|
|
79
|
-
blocksMeta: {},
|
|
80
|
-
accountsDataSlice: [],
|
|
81
|
-
commitment: this.commitmentLevel,
|
|
82
|
-
entry: {},
|
|
83
|
-
transactionsStatus: {},
|
|
84
|
-
};
|
|
85
|
-
this.stream.on('data', (chunk: SubscribeUpdate) => {
|
|
86
|
-
if (!chunk.account) {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
const slot = Number(chunk.account.slot);
|
|
90
|
-
const accountInfo = {
|
|
91
|
-
owner: new PublicKey(chunk.account.account.owner),
|
|
92
|
-
lamports: Number(chunk.account.account.lamports),
|
|
93
|
-
data: Buffer.Buffer.from(chunk.account.account.data),
|
|
94
|
-
executable: chunk.account.account.executable,
|
|
95
|
-
rentEpoch: Number(chunk.account.account.rentEpoch),
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
if (this.resubOpts?.resubTimeoutMs) {
|
|
99
|
-
this.receivingData = true;
|
|
100
|
-
clearTimeout(this.timeoutId);
|
|
101
|
-
this.handleRpcResponse(
|
|
102
|
-
{
|
|
103
|
-
slot,
|
|
104
|
-
},
|
|
105
|
-
{
|
|
106
|
-
accountId: new PublicKey(chunk.account.account.pubkey),
|
|
107
|
-
accountInfo,
|
|
108
|
-
}
|
|
109
|
-
);
|
|
110
|
-
this.setTimeout();
|
|
111
|
-
} else {
|
|
112
|
-
this.handleRpcResponse(
|
|
113
|
-
{
|
|
114
|
-
slot,
|
|
115
|
-
},
|
|
116
|
-
{
|
|
117
|
-
accountId: new PublicKey(chunk.account.account.pubkey),
|
|
118
|
-
accountInfo,
|
|
119
|
-
}
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
return new Promise<void>((resolve, reject) => {
|
|
125
|
-
this.stream.write(request, (err) => {
|
|
126
|
-
if (err === null || err === undefined) {
|
|
127
|
-
this.listenerId = 1;
|
|
128
|
-
if (this.resubOpts?.resubTimeoutMs) {
|
|
129
|
-
this.receivingData = true;
|
|
130
|
-
this.setTimeout();
|
|
131
|
-
}
|
|
132
|
-
resolve();
|
|
133
|
-
} else {
|
|
134
|
-
reject(err);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
}).catch((reason) => {
|
|
138
|
-
console.error(reason);
|
|
139
|
-
throw reason;
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public async unsubscribe(onResub = false): Promise<void> {
|
|
144
|
-
if (!onResub && this.resubOpts) {
|
|
145
|
-
this.resubOpts.resubTimeoutMs = undefined;
|
|
146
|
-
}
|
|
147
|
-
this.isUnsubscribing = true;
|
|
148
|
-
clearTimeout(this.timeoutId);
|
|
149
|
-
this.timeoutId = undefined;
|
|
150
|
-
|
|
151
|
-
if (this.listenerId != null) {
|
|
152
|
-
const promise = new Promise<void>((resolve, reject) => {
|
|
153
|
-
const request: SubscribeRequest = {
|
|
154
|
-
slots: {},
|
|
155
|
-
accounts: {},
|
|
156
|
-
transactions: {},
|
|
157
|
-
blocks: {},
|
|
158
|
-
blocksMeta: {},
|
|
159
|
-
accountsDataSlice: [],
|
|
160
|
-
entry: {},
|
|
161
|
-
transactionsStatus: {},
|
|
162
|
-
};
|
|
163
|
-
this.stream.write(request, (err) => {
|
|
164
|
-
if (err === null || err === undefined) {
|
|
165
|
-
this.listenerId = undefined;
|
|
166
|
-
this.isUnsubscribing = false;
|
|
167
|
-
resolve();
|
|
168
|
-
} else {
|
|
169
|
-
reject(err);
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
}).catch((reason) => {
|
|
173
|
-
console.error(reason);
|
|
174
|
-
throw reason;
|
|
175
|
-
});
|
|
176
|
-
return promise;
|
|
177
|
-
} else {
|
|
178
|
-
this.isUnsubscribing = false;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { ResubOpts, GrpcConfigs } from './types';
|
|
2
|
-
import { Program } from '@coral-xyz/anchor';
|
|
3
|
-
import { PublicKey } from '@solana/web3.js';
|
|
4
|
-
import { UserAccount } from '../types';
|
|
5
|
-
import { WebSocketUserAccountSubscriber } from './webSocketUserAccountSubscriber';
|
|
6
|
-
import { grpcAccountSubscriber } from './grpcAccountSubscriber';
|
|
7
|
-
|
|
8
|
-
export class grpcUserAccountSubscriber extends WebSocketUserAccountSubscriber {
|
|
9
|
-
private grpcConfigs: GrpcConfigs;
|
|
10
|
-
|
|
11
|
-
public constructor(
|
|
12
|
-
grpcConfigs: GrpcConfigs,
|
|
13
|
-
program: Program,
|
|
14
|
-
userAccountPublicKey: PublicKey,
|
|
15
|
-
resubOpts?: ResubOpts
|
|
16
|
-
) {
|
|
17
|
-
super(program, userAccountPublicKey, resubOpts);
|
|
18
|
-
this.grpcConfigs = grpcConfigs;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async subscribe(userAccount?: UserAccount): Promise<boolean> {
|
|
22
|
-
if (this.isSubscribed) {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
this.userDataAccountSubscriber = new grpcAccountSubscriber(
|
|
27
|
-
this.grpcConfigs,
|
|
28
|
-
'user',
|
|
29
|
-
this.program,
|
|
30
|
-
this.userAccountPublicKey,
|
|
31
|
-
undefined,
|
|
32
|
-
this.resubOpts
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
if (userAccount) {
|
|
36
|
-
this.userDataAccountSubscriber.setData(userAccount);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
await this.userDataAccountSubscriber.subscribe((data: UserAccount) => {
|
|
40
|
-
this.eventEmitter.emit('userAccountUpdate', data);
|
|
41
|
-
this.eventEmitter.emit('update');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
this.eventEmitter.emit('update');
|
|
45
|
-
this.isSubscribed = true;
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { ResubOpts, GrpcConfigs } from './types';
|
|
2
|
-
import { Program } from '@coral-xyz/anchor';
|
|
3
|
-
import { Commitment, PublicKey } from '@solana/web3.js';
|
|
4
|
-
import { UserStatsAccount } from '../types';
|
|
5
|
-
import { WebSocketUserStatsAccountSubscriber } from './webSocketUserStatsAccountSubsriber';
|
|
6
|
-
import { grpcAccountSubscriber } from './grpcAccountSubscriber';
|
|
7
|
-
|
|
8
|
-
export class grpcUserStatsAccountSubscriber extends WebSocketUserStatsAccountSubscriber {
|
|
9
|
-
private grpcConfigs: GrpcConfigs;
|
|
10
|
-
|
|
11
|
-
public constructor(
|
|
12
|
-
grpcConfigs: GrpcConfigs,
|
|
13
|
-
program: Program,
|
|
14
|
-
userStatsAccountPublicKey: PublicKey,
|
|
15
|
-
resubOpts?: ResubOpts,
|
|
16
|
-
commitment?: Commitment
|
|
17
|
-
) {
|
|
18
|
-
super(program, userStatsAccountPublicKey, resubOpts, commitment);
|
|
19
|
-
this.grpcConfigs = grpcConfigs;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async subscribe(userStatsAccount?: UserStatsAccount): Promise<boolean> {
|
|
23
|
-
if (this.isSubscribed) {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
this.userStatsAccountSubscriber = new grpcAccountSubscriber(
|
|
28
|
-
this.grpcConfigs,
|
|
29
|
-
'userStats',
|
|
30
|
-
this.program,
|
|
31
|
-
this.userStatsAccountPublicKey,
|
|
32
|
-
undefined,
|
|
33
|
-
this.resubOpts
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
if (userStatsAccount) {
|
|
37
|
-
this.userStatsAccountSubscriber.setData(userStatsAccount);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
await this.userStatsAccountSubscriber.subscribe(
|
|
41
|
-
(data: UserStatsAccount) => {
|
|
42
|
-
this.eventEmitter.emit('userStatsAccountUpdate', data);
|
|
43
|
-
this.eventEmitter.emit('update');
|
|
44
|
-
}
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
this.eventEmitter.emit('update');
|
|
48
|
-
this.isSubscribed = true;
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { Commitment, Context, PublicKey } from '@solana/web3.js';
|
|
2
|
-
import { Buffer } from 'buffer';
|
|
3
|
-
import { grpcProgramAccountSubscriber } from '../accounts/grpcProgramAccountSubscriber';
|
|
4
|
-
import { OrderSubscriber } from './OrderSubscriber';
|
|
5
|
-
import { GrpcConfigs, ResubOpts } from '../accounts/types';
|
|
6
|
-
import { UserAccount } from '../types';
|
|
7
|
-
import { getNonIdleUserFilter, getUserFilter } from '../memcmp';
|
|
8
|
-
|
|
9
|
-
export class grpcSubscription {
|
|
10
|
-
private orderSubscriber: OrderSubscriber;
|
|
11
|
-
private commitment: Commitment;
|
|
12
|
-
private skipInitialLoad: boolean;
|
|
13
|
-
private resubOpts?: ResubOpts;
|
|
14
|
-
private resyncIntervalMs?: number;
|
|
15
|
-
|
|
16
|
-
private subscriber?: grpcProgramAccountSubscriber<UserAccount>;
|
|
17
|
-
private resyncTimeoutId?: NodeJS.Timeout;
|
|
18
|
-
|
|
19
|
-
private decoded?: boolean;
|
|
20
|
-
|
|
21
|
-
private grpcConfigs: GrpcConfigs;
|
|
22
|
-
|
|
23
|
-
constructor({
|
|
24
|
-
grpcConfigs,
|
|
25
|
-
orderSubscriber,
|
|
26
|
-
commitment,
|
|
27
|
-
skipInitialLoad = false,
|
|
28
|
-
resubOpts,
|
|
29
|
-
resyncIntervalMs,
|
|
30
|
-
decoded = true,
|
|
31
|
-
}: {
|
|
32
|
-
grpcConfigs: GrpcConfigs;
|
|
33
|
-
orderSubscriber: OrderSubscriber;
|
|
34
|
-
commitment: Commitment;
|
|
35
|
-
skipInitialLoad?: boolean;
|
|
36
|
-
resubOpts?: ResubOpts;
|
|
37
|
-
resyncIntervalMs?: number;
|
|
38
|
-
decoded?: boolean;
|
|
39
|
-
}) {
|
|
40
|
-
this.orderSubscriber = orderSubscriber;
|
|
41
|
-
this.commitment = commitment;
|
|
42
|
-
this.skipInitialLoad = skipInitialLoad;
|
|
43
|
-
this.resubOpts = resubOpts;
|
|
44
|
-
this.resyncIntervalMs = resyncIntervalMs;
|
|
45
|
-
this.decoded = decoded;
|
|
46
|
-
this.grpcConfigs = grpcConfigs;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
public async subscribe(): Promise<void> {
|
|
50
|
-
if (this.subscriber) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
this.subscriber = new grpcProgramAccountSubscriber<UserAccount>(
|
|
55
|
-
this.grpcConfigs,
|
|
56
|
-
'OrderSubscriber',
|
|
57
|
-
'User',
|
|
58
|
-
this.orderSubscriber.driftClient.program,
|
|
59
|
-
this.orderSubscriber.decodeFn,
|
|
60
|
-
{
|
|
61
|
-
filters: [getUserFilter(), getNonIdleUserFilter()],
|
|
62
|
-
commitment: this.commitment,
|
|
63
|
-
},
|
|
64
|
-
this.resubOpts
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
await this.subscriber.subscribe(
|
|
68
|
-
(
|
|
69
|
-
accountId: PublicKey,
|
|
70
|
-
account: UserAccount,
|
|
71
|
-
context: Context,
|
|
72
|
-
buffer: Buffer
|
|
73
|
-
) => {
|
|
74
|
-
const userKey = accountId.toBase58();
|
|
75
|
-
if (this.decoded ?? true) {
|
|
76
|
-
this.orderSubscriber.tryUpdateUserAccount(
|
|
77
|
-
userKey,
|
|
78
|
-
'decoded',
|
|
79
|
-
account,
|
|
80
|
-
context.slot
|
|
81
|
-
);
|
|
82
|
-
} else {
|
|
83
|
-
this.orderSubscriber.tryUpdateUserAccount(
|
|
84
|
-
userKey,
|
|
85
|
-
'buffer',
|
|
86
|
-
buffer,
|
|
87
|
-
context.slot
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
if (!this.skipInitialLoad) {
|
|
94
|
-
await this.orderSubscriber.fetch();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (this.resyncIntervalMs) {
|
|
98
|
-
const recursiveResync = () => {
|
|
99
|
-
this.resyncTimeoutId = setTimeout(() => {
|
|
100
|
-
this.orderSubscriber
|
|
101
|
-
.fetch()
|
|
102
|
-
.catch((e) => {
|
|
103
|
-
console.error('Failed to resync in OrderSubscriber');
|
|
104
|
-
console.log(e);
|
|
105
|
-
})
|
|
106
|
-
.finally(() => {
|
|
107
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
108
|
-
if (!this.resyncTimeoutId) return;
|
|
109
|
-
recursiveResync();
|
|
110
|
-
});
|
|
111
|
-
}, this.resyncIntervalMs);
|
|
112
|
-
};
|
|
113
|
-
recursiveResync();
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public async unsubscribe(): Promise<void> {
|
|
118
|
-
if (!this.subscriber) return;
|
|
119
|
-
await this.subscriber.unsubscribe();
|
|
120
|
-
this.subscriber = undefined;
|
|
121
|
-
if (this.resyncTimeoutId !== undefined) {
|
|
122
|
-
clearTimeout(this.resyncTimeoutId);
|
|
123
|
-
this.resyncTimeoutId = undefined;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import { UserMap } from './userMap';
|
|
2
|
-
import { getNonIdleUserFilter, getUserFilter } from '../memcmp';
|
|
3
|
-
import { WebSocketProgramAccountSubscriber } from '../accounts/webSocketProgramAccountSubscriber';
|
|
4
|
-
import { UserAccount } from '../types';
|
|
5
|
-
import { Commitment, Context, PublicKey } from '@solana/web3.js';
|
|
6
|
-
import { GrpcConfigs, ResubOpts } from '../accounts/types';
|
|
7
|
-
import { grpcProgramAccountSubscriber } from '../accounts/grpcProgramAccountSubscriber';
|
|
8
|
-
|
|
9
|
-
export class grpcSubscription {
|
|
10
|
-
private configs: GrpcConfigs;
|
|
11
|
-
private userMap: UserMap;
|
|
12
|
-
private commitment: Commitment;
|
|
13
|
-
private skipInitialLoad: boolean;
|
|
14
|
-
private resubOpts?: ResubOpts;
|
|
15
|
-
private includeIdle?: boolean;
|
|
16
|
-
private decodeFn: (name: string, data: Buffer) => UserAccount;
|
|
17
|
-
|
|
18
|
-
private subscriber: WebSocketProgramAccountSubscriber<UserAccount>;
|
|
19
|
-
|
|
20
|
-
constructor({
|
|
21
|
-
configs,
|
|
22
|
-
userMap,
|
|
23
|
-
commitment,
|
|
24
|
-
skipInitialLoad = false,
|
|
25
|
-
resubOpts,
|
|
26
|
-
includeIdle = false,
|
|
27
|
-
decodeFn,
|
|
28
|
-
}: {
|
|
29
|
-
configs: GrpcConfigs;
|
|
30
|
-
userMap: UserMap;
|
|
31
|
-
commitment: Commitment;
|
|
32
|
-
skipInitialLoad?: boolean;
|
|
33
|
-
resubOpts?: ResubOpts;
|
|
34
|
-
includeIdle?: boolean;
|
|
35
|
-
decodeFn: (name: string, data: Buffer) => UserAccount;
|
|
36
|
-
}) {
|
|
37
|
-
this.userMap = userMap;
|
|
38
|
-
this.commitment = commitment;
|
|
39
|
-
this.skipInitialLoad = skipInitialLoad;
|
|
40
|
-
this.resubOpts = resubOpts;
|
|
41
|
-
this.includeIdle = includeIdle || false;
|
|
42
|
-
this.decodeFn = decodeFn;
|
|
43
|
-
this.configs = configs;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public async subscribe(): Promise<void> {
|
|
47
|
-
if (!this.subscriber) {
|
|
48
|
-
const filters = [getUserFilter()];
|
|
49
|
-
if (!this.includeIdle) {
|
|
50
|
-
filters.push(getNonIdleUserFilter());
|
|
51
|
-
}
|
|
52
|
-
this.subscriber = new grpcProgramAccountSubscriber<UserAccount>(
|
|
53
|
-
this.configs,
|
|
54
|
-
'UserMap',
|
|
55
|
-
'User',
|
|
56
|
-
this.userMap.driftClient.program,
|
|
57
|
-
this.decodeFn,
|
|
58
|
-
{
|
|
59
|
-
filters,
|
|
60
|
-
commitment: this.commitment,
|
|
61
|
-
},
|
|
62
|
-
this.resubOpts
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
await this.subscriber.subscribe(
|
|
67
|
-
(accountId: PublicKey, account: UserAccount, context: Context) => {
|
|
68
|
-
const userKey = accountId.toBase58();
|
|
69
|
-
this.userMap.updateUserAccount(userKey, account, context.slot);
|
|
70
|
-
}
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
if (!this.skipInitialLoad) {
|
|
74
|
-
await this.userMap.sync();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
public async unsubscribe(): Promise<void> {
|
|
79
|
-
if (!this.subscriber) return;
|
|
80
|
-
await this.subscriber.unsubscribe();
|
|
81
|
-
this.subscriber = undefined;
|
|
82
|
-
}
|
|
83
|
-
}
|