@drift-labs/sdk 2.96.0-beta.2 → 2.96.0-beta.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/VERSION +1 -1
- package/bun.lockb +0 -0
- package/lib/accounts/pollingDriftClientAccountSubscriber.d.ts +5 -3
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +24 -1
- package/lib/accounts/types.d.ts +5 -8
- package/lib/accounts/types.js +7 -1
- package/lib/accounts/utils.d.ts +7 -0
- package/lib/accounts/utils.js +33 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +8 -7
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +24 -1
- package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -1
- package/lib/config.d.ts +5 -1
- package/lib/config.js +9 -1
- package/lib/constants/perpMarkets.js +21 -0
- package/lib/constants/spotMarkets.js +12 -1
- package/lib/driftClient.d.ts +44 -9
- package/lib/driftClient.js +181 -61
- package/lib/driftClientConfig.d.ts +2 -6
- package/lib/events/eventSubscriber.js +9 -8
- package/lib/events/types.js +1 -5
- package/lib/idl/drift.json +169 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/margin.d.ts +16 -1
- package/lib/math/margin.js +67 -1
- package/lib/orderParams.js +8 -8
- 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/tokenFaucet.js +2 -1
- package/lib/tx/baseTxSender.js +2 -2
- package/lib/tx/fastSingleTxSender.js +2 -2
- package/lib/tx/forwardOnlyTxSender.js +2 -2
- package/lib/tx/retryTxSender.js +2 -2
- package/lib/tx/txHandler.js +10 -7
- package/lib/tx/whileValidTxSender.d.ts +2 -4
- package/lib/tx/whileValidTxSender.js +16 -17
- package/lib/types.d.ts +21 -1
- package/lib/types.js +6 -1
- package/lib/user.d.ts +4 -1
- package/lib/user.js +13 -13
- 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/lib/util/TransactionConfirmationManager.d.ts +14 -0
- package/lib/util/TransactionConfirmationManager.js +96 -0
- package/package.json +4 -5
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +41 -5
- package/src/accounts/types.ts +6 -9
- package/src/accounts/utils.ts +42 -0
- package/src/accounts/webSocketAccountSubscriber.ts +1 -1
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +43 -8
- package/src/accounts/webSocketProgramAccountSubscriber.ts +1 -1
- package/src/config.ts +15 -1
- package/src/constants/perpMarkets.ts +22 -0
- package/src/constants/spotMarkets.ts +14 -1
- package/src/driftClient.ts +423 -91
- package/src/driftClientConfig.ts +2 -7
- package/src/events/eventSubscriber.ts +18 -11
- package/src/events/types.ts +1 -5
- package/src/idl/drift.json +169 -1
- package/src/index.ts +1 -0
- package/src/math/margin.ts +137 -1
- package/src/orderParams.ts +20 -12
- package/src/orderSubscriber/OrderSubscriber.ts +1 -15
- package/src/orderSubscriber/types.ts +0 -10
- package/src/tokenFaucet.ts +2 -2
- package/src/tx/baseTxSender.ts +2 -2
- package/src/tx/fastSingleTxSender.ts +2 -2
- package/src/tx/forwardOnlyTxSender.ts +2 -2
- package/src/tx/retryTxSender.ts +2 -2
- package/src/tx/txHandler.ts +8 -2
- package/src/tx/whileValidTxSender.ts +23 -26
- package/src/types.ts +30 -1
- package/src/user.ts +35 -13
- 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/src/util/TransactionConfirmationManager.ts +155 -0
- package/tests/ci/idl.ts +12 -3
- package/tests/ci/verifyConstants.ts +13 -0
- package/tests/tx/TransactionConfirmationManager.test.ts +286 -0
- 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,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
|
-
}
|
|
@@ -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
|
-
}
|