@drift-labs/sdk 2.96.0-beta.1 → 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/events/eventSubscriber.d.ts +7 -0
- package/lib/events/eventSubscriber.js +69 -32
- package/lib/events/eventsServerLogProvider.d.ts +21 -0
- package/lib/events/eventsServerLogProvider.js +121 -0
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/events/types.d.ts +12 -5
- package/lib/events/types.js +5 -1
- package/lib/events/webSocketLogProvider.js +2 -2
- 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/events/eventSubscriber.ts +125 -54
- package/src/events/eventsServerLogProvider.ts +152 -0
- package/src/events/pollingLogProvider.ts +1 -1
- package/src/events/types.ts +29 -6
- package/src/events/webSocketLogProvider.ts +4 -4
- 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,158 +0,0 @@
|
|
|
1
|
-
import { ResubOpts, GrpcConfigs } from './types';
|
|
2
|
-
import { Program } from '@coral-xyz/anchor';
|
|
3
|
-
import { PublicKey } from '@solana/web3.js';
|
|
4
|
-
import * as Buffer from 'buffer';
|
|
5
|
-
import { ClientDuplexStream } from '@grpc/grpc-js';
|
|
6
|
-
import Client, {
|
|
7
|
-
CommitmentLevel,
|
|
8
|
-
SubscribeRequest,
|
|
9
|
-
SubscribeUpdate,
|
|
10
|
-
} from '@triton-one/yellowstone-grpc';
|
|
11
|
-
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
|
|
12
|
-
|
|
13
|
-
export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
|
|
14
|
-
client: Client;
|
|
15
|
-
stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
|
|
16
|
-
commitmentLevel: CommitmentLevel;
|
|
17
|
-
listenerId?: number;
|
|
18
|
-
|
|
19
|
-
public constructor(
|
|
20
|
-
grpcConfigs: GrpcConfigs,
|
|
21
|
-
accountName: string,
|
|
22
|
-
program: Program,
|
|
23
|
-
accountPublicKey: PublicKey,
|
|
24
|
-
decodeBuffer?: (buffer: Buffer) => T,
|
|
25
|
-
resubOpts?: ResubOpts
|
|
26
|
-
) {
|
|
27
|
-
super(accountName, program, accountPublicKey, decodeBuffer, resubOpts);
|
|
28
|
-
this.client = new Client(
|
|
29
|
-
grpcConfigs.endpoint,
|
|
30
|
-
grpcConfigs.token,
|
|
31
|
-
grpcConfigs.channelOptions ?? {}
|
|
32
|
-
);
|
|
33
|
-
this.commitmentLevel =
|
|
34
|
-
grpcConfigs.commitmentLevel ?? CommitmentLevel.CONFIRMED;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
override async subscribe(onChange: (data: T) => void): Promise<void> {
|
|
38
|
-
if (this.listenerId != null || this.isUnsubscribing) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
this.onChange = onChange;
|
|
43
|
-
if (!this.dataAndSlot) {
|
|
44
|
-
await this.fetch();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Subscribe with grpc
|
|
48
|
-
this.stream = await this.client.subscribe();
|
|
49
|
-
const request: SubscribeRequest = {
|
|
50
|
-
slots: {
|
|
51
|
-
slots: {},
|
|
52
|
-
},
|
|
53
|
-
accounts: {
|
|
54
|
-
account: {
|
|
55
|
-
account: [this.accountPublicKey.toString()],
|
|
56
|
-
owner: [],
|
|
57
|
-
filters: [],
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
transactions: {},
|
|
61
|
-
blocks: {},
|
|
62
|
-
blocksMeta: {},
|
|
63
|
-
accountsDataSlice: [],
|
|
64
|
-
commitment: this.commitmentLevel,
|
|
65
|
-
entry: {},
|
|
66
|
-
transactionsStatus: {},
|
|
67
|
-
};
|
|
68
|
-
this.stream.on('data', (chunk: SubscribeUpdate) => {
|
|
69
|
-
if (!chunk.account) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
const slot = Number(chunk.account.slot);
|
|
73
|
-
const accountInfo = {
|
|
74
|
-
owner: new PublicKey(chunk.account.account.owner),
|
|
75
|
-
lamports: Number(chunk.account.account.lamports),
|
|
76
|
-
data: Buffer.Buffer.from(chunk.account.account.data),
|
|
77
|
-
executable: chunk.account.account.executable,
|
|
78
|
-
rentEpoch: Number(chunk.account.account.rentEpoch),
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
if (this.resubOpts?.resubTimeoutMs) {
|
|
82
|
-
this.receivingData = true;
|
|
83
|
-
clearTimeout(this.timeoutId);
|
|
84
|
-
this.handleRpcResponse(
|
|
85
|
-
{
|
|
86
|
-
slot,
|
|
87
|
-
},
|
|
88
|
-
accountInfo
|
|
89
|
-
);
|
|
90
|
-
this.setTimeout();
|
|
91
|
-
} else {
|
|
92
|
-
this.handleRpcResponse(
|
|
93
|
-
{
|
|
94
|
-
slot,
|
|
95
|
-
},
|
|
96
|
-
accountInfo
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
return new Promise<void>((resolve, reject) => {
|
|
102
|
-
this.stream.write(request, (err) => {
|
|
103
|
-
if (err === null || err === undefined) {
|
|
104
|
-
this.listenerId = 1;
|
|
105
|
-
if (this.resubOpts?.resubTimeoutMs) {
|
|
106
|
-
this.receivingData = true;
|
|
107
|
-
this.setTimeout();
|
|
108
|
-
}
|
|
109
|
-
resolve();
|
|
110
|
-
} else {
|
|
111
|
-
reject(err);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}).catch((reason) => {
|
|
115
|
-
console.error(reason);
|
|
116
|
-
throw reason;
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
override async unsubscribe(onResub = false): Promise<void> {
|
|
121
|
-
if (!onResub && this.resubOpts) {
|
|
122
|
-
this.resubOpts.resubTimeoutMs = undefined;
|
|
123
|
-
}
|
|
124
|
-
this.isUnsubscribing = true;
|
|
125
|
-
clearTimeout(this.timeoutId);
|
|
126
|
-
this.timeoutId = undefined;
|
|
127
|
-
|
|
128
|
-
if (this.listenerId != null) {
|
|
129
|
-
const promise = new Promise<void>((resolve, reject) => {
|
|
130
|
-
const request: SubscribeRequest = {
|
|
131
|
-
slots: {},
|
|
132
|
-
accounts: {},
|
|
133
|
-
transactions: {},
|
|
134
|
-
blocks: {},
|
|
135
|
-
blocksMeta: {},
|
|
136
|
-
accountsDataSlice: [],
|
|
137
|
-
entry: {},
|
|
138
|
-
transactionsStatus: {},
|
|
139
|
-
};
|
|
140
|
-
this.stream.write(request, (err) => {
|
|
141
|
-
if (err === null || err === undefined) {
|
|
142
|
-
this.listenerId = undefined;
|
|
143
|
-
this.isUnsubscribing = false;
|
|
144
|
-
resolve();
|
|
145
|
-
} else {
|
|
146
|
-
reject(err);
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
}).catch((reason) => {
|
|
150
|
-
console.error(reason);
|
|
151
|
-
throw reason;
|
|
152
|
-
});
|
|
153
|
-
return promise;
|
|
154
|
-
} else {
|
|
155
|
-
this.isUnsubscribing = false;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import { WebSocketDriftClientAccountSubscriber } from './webSocketDriftClientAccountSubscriber';
|
|
2
|
-
import { OracleInfo, OraclePriceData } from '../oracles/types';
|
|
3
|
-
import { Program } from '@coral-xyz/anchor';
|
|
4
|
-
import { findAllMarketAndOracles } from '../config';
|
|
5
|
-
import {
|
|
6
|
-
getDriftStateAccountPublicKey,
|
|
7
|
-
getPerpMarketPublicKey,
|
|
8
|
-
getSpotMarketPublicKey,
|
|
9
|
-
} from '../addresses/pda';
|
|
10
|
-
import { GrpcConfigs, ResubOpts } from './types';
|
|
11
|
-
import { grpcAccountSubscriber } from './grpcAccountSubscriber';
|
|
12
|
-
import { PerpMarketAccount, SpotMarketAccount, StateAccount } from '../types';
|
|
13
|
-
import { Commitment } from '@solana/web3.js';
|
|
14
|
-
|
|
15
|
-
export class gprcDriftClientAccountSubscriber extends WebSocketDriftClientAccountSubscriber {
|
|
16
|
-
private grpcConfigs: GrpcConfigs;
|
|
17
|
-
|
|
18
|
-
constructor(
|
|
19
|
-
grpcConfigs: GrpcConfigs,
|
|
20
|
-
program: Program,
|
|
21
|
-
perpMarketIndexes: number[],
|
|
22
|
-
spotMarketIndexes: number[],
|
|
23
|
-
oracleInfos: OracleInfo[],
|
|
24
|
-
shouldFindAllMarketsAndOracles: boolean,
|
|
25
|
-
resubOpts?: ResubOpts,
|
|
26
|
-
commitment?: Commitment
|
|
27
|
-
) {
|
|
28
|
-
super(
|
|
29
|
-
program,
|
|
30
|
-
perpMarketIndexes,
|
|
31
|
-
spotMarketIndexes,
|
|
32
|
-
oracleInfos,
|
|
33
|
-
shouldFindAllMarketsAndOracles,
|
|
34
|
-
resubOpts,
|
|
35
|
-
commitment
|
|
36
|
-
);
|
|
37
|
-
this.grpcConfigs = grpcConfigs;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public async subscribe(): Promise<boolean> {
|
|
41
|
-
if (this.isSubscribed) {
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (this.isSubscribing) {
|
|
46
|
-
return await this.subscriptionPromise;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
this.isSubscribing = true;
|
|
50
|
-
|
|
51
|
-
this.subscriptionPromise = new Promise((res) => {
|
|
52
|
-
this.subscriptionPromiseResolver = res;
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
if (this.shouldFindAllMarketsAndOracles) {
|
|
56
|
-
const {
|
|
57
|
-
perpMarketIndexes,
|
|
58
|
-
perpMarketAccounts,
|
|
59
|
-
spotMarketIndexes,
|
|
60
|
-
spotMarketAccounts,
|
|
61
|
-
oracleInfos,
|
|
62
|
-
} = await findAllMarketAndOracles(this.program);
|
|
63
|
-
this.perpMarketIndexes = perpMarketIndexes;
|
|
64
|
-
this.spotMarketIndexes = spotMarketIndexes;
|
|
65
|
-
this.oracleInfos = oracleInfos;
|
|
66
|
-
// front run and set the initial data here to save extra gma call in set initial data
|
|
67
|
-
this.initialPerpMarketAccountData = new Map(
|
|
68
|
-
perpMarketAccounts.map((market) => [market.marketIndex, market])
|
|
69
|
-
);
|
|
70
|
-
this.initialSpotMarketAccountData = new Map(
|
|
71
|
-
spotMarketAccounts.map((market) => [market.marketIndex, market])
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const statePublicKey = await getDriftStateAccountPublicKey(
|
|
76
|
-
this.program.programId
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
// create and activate main state account subscription
|
|
80
|
-
this.stateAccountSubscriber = new grpcAccountSubscriber(
|
|
81
|
-
this.grpcConfigs,
|
|
82
|
-
'state',
|
|
83
|
-
this.program,
|
|
84
|
-
statePublicKey,
|
|
85
|
-
undefined,
|
|
86
|
-
undefined
|
|
87
|
-
);
|
|
88
|
-
await this.stateAccountSubscriber.subscribe((data: StateAccount) => {
|
|
89
|
-
this.eventEmitter.emit('stateAccountUpdate', data);
|
|
90
|
-
this.eventEmitter.emit('update');
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// set initial data to avoid spamming getAccountInfo calls in webSocketAccountSubscriber
|
|
94
|
-
await this.setInitialData();
|
|
95
|
-
|
|
96
|
-
await Promise.all([
|
|
97
|
-
// subscribe to market accounts
|
|
98
|
-
this.subscribeToPerpMarketAccounts(),
|
|
99
|
-
// subscribe to spot market accounts
|
|
100
|
-
this.subscribeToSpotMarketAccounts(),
|
|
101
|
-
// subscribe to oracles
|
|
102
|
-
this.subscribeToOracles(),
|
|
103
|
-
]);
|
|
104
|
-
|
|
105
|
-
this.eventEmitter.emit('update');
|
|
106
|
-
|
|
107
|
-
await Promise.all([this.setPerpOracleMap(), this.setSpotOracleMap()]);
|
|
108
|
-
|
|
109
|
-
this.subscriptionPromiseResolver(true);
|
|
110
|
-
|
|
111
|
-
this.isSubscribing = false;
|
|
112
|
-
this.isSubscribed = true;
|
|
113
|
-
|
|
114
|
-
// delete initial data
|
|
115
|
-
this.removeInitialData();
|
|
116
|
-
|
|
117
|
-
return true;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
override async subscribeToSpotMarketAccount(
|
|
121
|
-
marketIndex: number
|
|
122
|
-
): Promise<boolean> {
|
|
123
|
-
const marketPublicKey = await getSpotMarketPublicKey(
|
|
124
|
-
this.program.programId,
|
|
125
|
-
marketIndex
|
|
126
|
-
);
|
|
127
|
-
const accountSubscriber = new grpcAccountSubscriber<SpotMarketAccount>(
|
|
128
|
-
this.grpcConfigs,
|
|
129
|
-
'spotMarket',
|
|
130
|
-
this.program,
|
|
131
|
-
marketPublicKey,
|
|
132
|
-
undefined,
|
|
133
|
-
this.resubOpts
|
|
134
|
-
);
|
|
135
|
-
accountSubscriber.setData(
|
|
136
|
-
this.initialSpotMarketAccountData.get(marketIndex)
|
|
137
|
-
);
|
|
138
|
-
await accountSubscriber.subscribe((data: SpotMarketAccount) => {
|
|
139
|
-
this.eventEmitter.emit('spotMarketAccountUpdate', data);
|
|
140
|
-
this.eventEmitter.emit('update');
|
|
141
|
-
});
|
|
142
|
-
this.spotMarketAccountSubscribers.set(marketIndex, accountSubscriber);
|
|
143
|
-
return true;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
async subscribeToPerpMarketAccount(marketIndex: number): Promise<boolean> {
|
|
147
|
-
const perpMarketPublicKey = await getPerpMarketPublicKey(
|
|
148
|
-
this.program.programId,
|
|
149
|
-
marketIndex
|
|
150
|
-
);
|
|
151
|
-
const accountSubscriber = new grpcAccountSubscriber<PerpMarketAccount>(
|
|
152
|
-
this.grpcConfigs,
|
|
153
|
-
'perpMarket',
|
|
154
|
-
this.program,
|
|
155
|
-
perpMarketPublicKey,
|
|
156
|
-
undefined,
|
|
157
|
-
this.resubOpts
|
|
158
|
-
);
|
|
159
|
-
accountSubscriber.setData(
|
|
160
|
-
this.initialPerpMarketAccountData.get(marketIndex)
|
|
161
|
-
);
|
|
162
|
-
await accountSubscriber.subscribe((data: PerpMarketAccount) => {
|
|
163
|
-
this.eventEmitter.emit('perpMarketAccountUpdate', data);
|
|
164
|
-
this.eventEmitter.emit('update');
|
|
165
|
-
});
|
|
166
|
-
this.perpMarketAccountSubscribers.set(marketIndex, accountSubscriber);
|
|
167
|
-
return true;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
async subscribeToOracle(oracleInfo: OracleInfo): Promise<boolean> {
|
|
171
|
-
const oracleString = oracleInfo.publicKey.toString();
|
|
172
|
-
const client = this.oracleClientCache.get(
|
|
173
|
-
oracleInfo.source,
|
|
174
|
-
this.program.provider.connection,
|
|
175
|
-
this.program
|
|
176
|
-
);
|
|
177
|
-
const accountSubscriber = new grpcAccountSubscriber<OraclePriceData>(
|
|
178
|
-
this.grpcConfigs,
|
|
179
|
-
'oracle',
|
|
180
|
-
this.program,
|
|
181
|
-
oracleInfo.publicKey,
|
|
182
|
-
(buffer: Buffer) => {
|
|
183
|
-
return client.getOraclePriceDataFromBuffer(buffer);
|
|
184
|
-
},
|
|
185
|
-
this.resubOpts
|
|
186
|
-
);
|
|
187
|
-
accountSubscriber.setData(this.initialOraclePriceData.get(oracleString));
|
|
188
|
-
await accountSubscriber.subscribe((data: OraclePriceData) => {
|
|
189
|
-
this.eventEmitter.emit('oraclePriceUpdate', oracleInfo.publicKey, data);
|
|
190
|
-
this.eventEmitter.emit('update');
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
this.oracleSubscribers.set(oracleString, accountSubscriber);
|
|
194
|
-
return true;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { GrpcConfigs } from './types';
|
|
2
|
-
import { Program } from '@coral-xyz/anchor';
|
|
3
|
-
import { Commitment, PublicKey } from '@solana/web3.js';
|
|
4
|
-
import { InsuranceFundStake } from '../types';
|
|
5
|
-
import { WebSocketInsuranceFundStakeAccountSubscriber } from './webSocketInsuranceFundStakeAccountSubscriber';
|
|
6
|
-
import { grpcAccountSubscriber } from './grpcAccountSubscriber';
|
|
7
|
-
|
|
8
|
-
export class grpcInsuranceFundStakeAccountSubscriber extends WebSocketInsuranceFundStakeAccountSubscriber {
|
|
9
|
-
private grpcConfigs: GrpcConfigs;
|
|
10
|
-
|
|
11
|
-
public constructor(
|
|
12
|
-
grpcConfigs: GrpcConfigs,
|
|
13
|
-
program: Program,
|
|
14
|
-
insuranceFundStakeAccountPublicKey: PublicKey,
|
|
15
|
-
resubTimeoutMs?: number,
|
|
16
|
-
commitment?: Commitment
|
|
17
|
-
) {
|
|
18
|
-
super(
|
|
19
|
-
program,
|
|
20
|
-
insuranceFundStakeAccountPublicKey,
|
|
21
|
-
resubTimeoutMs,
|
|
22
|
-
commitment
|
|
23
|
-
);
|
|
24
|
-
this.grpcConfigs = grpcConfigs;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async subscribe(
|
|
28
|
-
insuranceFundStakeAccount?: InsuranceFundStake
|
|
29
|
-
): Promise<boolean> {
|
|
30
|
-
if (this.isSubscribed) {
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
this.insuranceFundStakeDataAccountSubscriber = new grpcAccountSubscriber(
|
|
35
|
-
this.grpcConfigs,
|
|
36
|
-
'insuranceFundStake',
|
|
37
|
-
this.program,
|
|
38
|
-
this.insuranceFundStakeAccountPublicKey,
|
|
39
|
-
undefined,
|
|
40
|
-
{
|
|
41
|
-
resubTimeoutMs: this.resubTimeoutMs,
|
|
42
|
-
}
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
if (insuranceFundStakeAccount) {
|
|
46
|
-
this.insuranceFundStakeDataAccountSubscriber.setData(
|
|
47
|
-
insuranceFundStakeAccount
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
await this.insuranceFundStakeDataAccountSubscriber.subscribe(
|
|
52
|
-
(data: InsuranceFundStake) => {
|
|
53
|
-
this.eventEmitter.emit('insuranceFundStakeAccountUpdate', data);
|
|
54
|
-
this.eventEmitter.emit('update');
|
|
55
|
-
}
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
this.eventEmitter.emit('update');
|
|
59
|
-
this.isSubscribed = true;
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
@@ -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
|
-
}
|