@drift-labs/sdk 2.95.0-beta.9 → 2.96.0-beta.1
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/grpcAccountSubscriber.d.ts +16 -0
- package/lib/accounts/grpcAccountSubscriber.js +155 -0
- package/lib/accounts/grpcDriftClientAccountSubscriber.d.ts +13 -0
- package/lib/accounts/grpcDriftClientAccountSubscriber.js +96 -0
- package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.d.ts +10 -0
- package/lib/accounts/grpcInsuranceFundStakeAccountSubscriber.js +30 -0
- package/lib/accounts/grpcProgramAccountSubscriber.d.ts +19 -0
- package/lib/accounts/grpcProgramAccountSubscriber.js +161 -0
- package/lib/accounts/grpcUserAccountSubscriber.d.ts +10 -0
- package/lib/accounts/grpcUserAccountSubscriber.js +28 -0
- package/lib/accounts/grpcUserStatsAccountSubscriber.d.ts +10 -0
- package/lib/accounts/grpcUserStatsAccountSubscriber.js +28 -0
- package/lib/accounts/pollingDriftClientAccountSubscriber.d.ts +3 -1
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +14 -4
- package/lib/accounts/types.d.ts +9 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +11 -4
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +70 -8
- package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -1
- package/lib/addresses/pda.d.ts +2 -0
- package/lib/addresses/pda.js +15 -1
- package/lib/config.d.ts +3 -0
- package/lib/config.js +2 -0
- package/lib/constants/spotMarkets.js +10 -0
- package/lib/driftClient.js +41 -16
- package/lib/driftClientConfig.d.ts +6 -0
- package/lib/events/parse.js +1 -17
- package/lib/idl/drift.json +9 -4
- package/lib/math/insurance.d.ts +3 -1
- package/lib/math/insurance.js +31 -1
- package/lib/math/spotBalance.d.ts +3 -3
- package/lib/math/spotBalance.js +7 -7
- package/lib/orderSubscriber/OrderSubscriber.d.ts +2 -1
- package/lib/orderSubscriber/OrderSubscriber.js +19 -4
- package/lib/orderSubscriber/grpcSubscription.d.ts +25 -0
- package/lib/orderSubscriber/grpcSubscription.js +68 -0
- package/lib/orderSubscriber/types.d.ts +9 -0
- package/lib/user.js +11 -4
- package/lib/userConfig.d.ts +6 -1
- package/lib/userMap/grpcSubscription.d.ts +26 -0
- package/lib/userMap/grpcSubscription.js +42 -0
- package/lib/userMap/userMap.js +14 -0
- package/lib/userMap/userMapConfig.d.ts +7 -0
- package/lib/userStatsConfig.d.ts +6 -0
- package/package.json +3 -1
- package/src/accounts/grpcAccountSubscriber.ts +158 -0
- package/src/accounts/grpcDriftClientAccountSubscriber.ts +196 -0
- package/src/accounts/grpcInsuranceFundStakeAccountSubscriber.ts +62 -0
- package/src/accounts/grpcProgramAccountSubscriber.ts +181 -0
- package/src/accounts/grpcUserAccountSubscriber.ts +48 -0
- package/src/accounts/grpcUserStatsAccountSubscriber.ts +51 -0
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +17 -5
- package/src/accounts/types.ts +10 -1
- package/src/accounts/webSocketAccountSubscriber.ts +1 -1
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +135 -17
- package/src/accounts/webSocketProgramAccountSubscriber.ts +1 -1
- package/src/addresses/pda.ts +26 -0
- package/src/config.ts +8 -0
- package/src/constants/spotMarkets.ts +11 -0
- package/src/driftClient.ts +39 -2
- package/src/driftClientConfig.ts +7 -0
- package/src/events/parse.ts +1 -23
- package/src/idl/drift.json +9 -4
- package/src/math/insurance.ts +48 -2
- package/src/math/spotBalance.ts +13 -7
- package/src/orderSubscriber/OrderSubscriber.ts +15 -1
- package/src/orderSubscriber/grpcSubscription.ts +126 -0
- package/src/orderSubscriber/types.ts +10 -0
- package/src/user.ts +11 -0
- package/src/userConfig.ts +7 -1
- package/src/userMap/grpcSubscription.ts +83 -0
- package/src/userMap/userMap.ts +17 -1
- package/src/userMap/userMapConfig.ts +8 -0
- package/src/userStatsConfig.ts +7 -0
|
@@ -9,6 +9,7 @@ const web3_js_1 = require("@solana/web3.js");
|
|
|
9
9
|
const oracleClientCache_1 = require("../oracles/oracleClientCache");
|
|
10
10
|
const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
|
|
11
11
|
const config_1 = require("../config");
|
|
12
|
+
const ORACLE_DEFAULT_KEY = web3_js_1.PublicKey.default.toBase58();
|
|
12
13
|
class PollingDriftClientAccountSubscriber {
|
|
13
14
|
constructor(program, accountLoader, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles) {
|
|
14
15
|
this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
|
|
@@ -16,8 +17,10 @@ class PollingDriftClientAccountSubscriber {
|
|
|
16
17
|
this.oraclesToPoll = new Map();
|
|
17
18
|
this.perpMarket = new Map();
|
|
18
19
|
this.perpOracleMap = new Map();
|
|
20
|
+
this.perpOracleStringMap = new Map();
|
|
19
21
|
this.spotMarket = new Map();
|
|
20
22
|
this.spotOracleMap = new Map();
|
|
23
|
+
this.spotOracleStringMap = new Map();
|
|
21
24
|
this.oracles = new Map();
|
|
22
25
|
this.isSubscribing = false;
|
|
23
26
|
this.isSubscribed = false;
|
|
@@ -300,6 +303,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
300
303
|
}));
|
|
301
304
|
}
|
|
302
305
|
this.perpOracleMap.set(perpMarketIndex, oracle);
|
|
306
|
+
this.perpOracleStringMap.set(perpMarketIndex, oracle.toBase58());
|
|
303
307
|
}
|
|
304
308
|
await Promise.all(oraclePromises);
|
|
305
309
|
}
|
|
@@ -317,6 +321,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
317
321
|
}));
|
|
318
322
|
}
|
|
319
323
|
this.spotOracleMap.set(spotMarketIndex, oracle);
|
|
324
|
+
this.spotOracleStringMap.set(spotMarketIndex, oracle.toBase58());
|
|
320
325
|
}
|
|
321
326
|
await Promise.all(oraclePromises);
|
|
322
327
|
}
|
|
@@ -343,17 +348,21 @@ class PollingDriftClientAccountSubscriber {
|
|
|
343
348
|
}
|
|
344
349
|
getOraclePriceDataAndSlot(oraclePublicKey) {
|
|
345
350
|
this.assertIsSubscribed();
|
|
346
|
-
|
|
351
|
+
const oracleString = typeof oraclePublicKey === 'string'
|
|
352
|
+
? oraclePublicKey
|
|
353
|
+
: oraclePublicKey.toBase58();
|
|
354
|
+
if (oracleString === ORACLE_DEFAULT_KEY) {
|
|
347
355
|
return {
|
|
348
356
|
data: quoteAssetOracleClient_1.QUOTE_ORACLE_PRICE_DATA,
|
|
349
357
|
slot: 0,
|
|
350
358
|
};
|
|
351
359
|
}
|
|
352
|
-
return this.oracles.get(
|
|
360
|
+
return this.oracles.get(oracleString);
|
|
353
361
|
}
|
|
354
362
|
getOraclePriceDataAndSlotForPerpMarket(marketIndex) {
|
|
355
363
|
const perpMarketAccount = this.getMarketAccountAndSlot(marketIndex);
|
|
356
364
|
const oracle = this.perpOracleMap.get(marketIndex);
|
|
365
|
+
const oracleString = this.perpOracleStringMap.get(marketIndex);
|
|
357
366
|
if (!perpMarketAccount || !oracle) {
|
|
358
367
|
return undefined;
|
|
359
368
|
}
|
|
@@ -361,11 +370,12 @@ class PollingDriftClientAccountSubscriber {
|
|
|
361
370
|
// If the oracle has changed, we need to update the oracle map in background
|
|
362
371
|
this.setPerpOracleMap();
|
|
363
372
|
}
|
|
364
|
-
return this.getOraclePriceDataAndSlot(
|
|
373
|
+
return this.getOraclePriceDataAndSlot(oracleString);
|
|
365
374
|
}
|
|
366
375
|
getOraclePriceDataAndSlotForSpotMarket(marketIndex) {
|
|
367
376
|
const spotMarketAccount = this.getSpotMarketAccountAndSlot(marketIndex);
|
|
368
377
|
const oracle = this.spotOracleMap.get(marketIndex);
|
|
378
|
+
const oracleString = this.spotOracleStringMap.get(marketIndex);
|
|
369
379
|
if (!spotMarketAccount || !oracle) {
|
|
370
380
|
return undefined;
|
|
371
381
|
}
|
|
@@ -373,7 +383,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
373
383
|
// If the oracle has changed, we need to update the oracle map in background
|
|
374
384
|
this.setSpotOracleMap();
|
|
375
385
|
}
|
|
376
|
-
return this.getOraclePriceDataAndSlot(
|
|
386
|
+
return this.getOraclePriceDataAndSlot(oracleString);
|
|
377
387
|
}
|
|
378
388
|
updateAccountLoaderPollingFrequency(pollingFrequency) {
|
|
379
389
|
this.accountLoader.updatePollingFrequency(pollingFrequency);
|
package/lib/accounts/types.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { EventEmitter } from 'events';
|
|
|
6
6
|
import { Context, PublicKey } from '@solana/web3.js';
|
|
7
7
|
import { Account } from '@solana/spl-token';
|
|
8
8
|
import { OracleInfo, OraclePriceData } from '..';
|
|
9
|
+
import { CommitmentLevel } from '@triton-one/yellowstone-grpc';
|
|
10
|
+
import { ChannelOptions } from '@grpc/grpc-js';
|
|
9
11
|
export interface AccountSubscriber<T> {
|
|
10
12
|
dataAndSlot?: DataAndSlot<T>;
|
|
11
13
|
subscribe(onChange: (data: T) => void): Promise<void>;
|
|
@@ -45,7 +47,7 @@ export interface DriftClientAccountSubscriber {
|
|
|
45
47
|
getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[];
|
|
46
48
|
getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
|
|
47
49
|
getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
|
|
48
|
-
getOraclePriceDataAndSlot(oraclePublicKey: PublicKey): DataAndSlot<OraclePriceData> | undefined;
|
|
50
|
+
getOraclePriceDataAndSlot(oraclePublicKey: PublicKey | string): DataAndSlot<OraclePriceData> | undefined;
|
|
49
51
|
getOraclePriceDataAndSlotForPerpMarket(marketIndex: number): DataAndSlot<OraclePriceData> | undefined;
|
|
50
52
|
getOraclePriceDataAndSlotForSpotMarket(marketIndex: number): DataAndSlot<OraclePriceData> | undefined;
|
|
51
53
|
updateAccountLoaderPollingFrequency?: (pollingFrequency: number) => void;
|
|
@@ -140,3 +142,9 @@ export interface UserStatsAccountSubscriber {
|
|
|
140
142
|
unsubscribe(): Promise<void>;
|
|
141
143
|
getUserStatsAccountAndSlot(): DataAndSlot<UserStatsAccount>;
|
|
142
144
|
}
|
|
145
|
+
export type GrpcConfigs = {
|
|
146
|
+
endpoint: string;
|
|
147
|
+
token: string;
|
|
148
|
+
commitmentLevel?: CommitmentLevel;
|
|
149
|
+
channelOptions?: ChannelOptions;
|
|
150
|
+
};
|
|
@@ -20,7 +20,7 @@ export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<
|
|
|
20
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
|
+
protected setTimeout(): void;
|
|
24
24
|
fetch(): Promise<void>;
|
|
25
25
|
handleRpcResponse(context: Context, accountInfo?: AccountInfo<Buffer>): void;
|
|
26
26
|
decodeBuffer(buffer: Buffer): T;
|
|
@@ -22,14 +22,21 @@ export declare class WebSocketDriftClientAccountSubscriber implements DriftClien
|
|
|
22
22
|
stateAccountSubscriber?: AccountSubscriber<StateAccount>;
|
|
23
23
|
perpMarketAccountSubscribers: Map<number, AccountSubscriber<PerpMarketAccount>>;
|
|
24
24
|
perpOracleMap: Map<number, PublicKey>;
|
|
25
|
+
perpOracleStringMap: Map<number, string>;
|
|
25
26
|
spotMarketAccountSubscribers: Map<number, AccountSubscriber<SpotMarketAccount>>;
|
|
26
27
|
spotOracleMap: Map<number, PublicKey>;
|
|
28
|
+
spotOracleStringMap: Map<number, string>;
|
|
27
29
|
oracleSubscribers: Map<string, AccountSubscriber<OraclePriceData>>;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
initialPerpMarketAccountData: Map<number, PerpMarketAccount>;
|
|
31
|
+
initialSpotMarketAccountData: Map<number, SpotMarketAccount>;
|
|
32
|
+
initialOraclePriceData: Map<string, OraclePriceData>;
|
|
33
|
+
protected isSubscribing: boolean;
|
|
34
|
+
protected subscriptionPromise: Promise<boolean>;
|
|
35
|
+
protected subscriptionPromiseResolver: (val: boolean) => void;
|
|
31
36
|
constructor(program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[], shouldFindAllMarketsAndOracles: boolean, resubOpts?: ResubOpts, commitment?: Commitment);
|
|
32
37
|
subscribe(): Promise<boolean>;
|
|
38
|
+
setInitialData(): Promise<void>;
|
|
39
|
+
removeInitialData(): void;
|
|
33
40
|
subscribeToPerpMarketAccounts(): Promise<boolean>;
|
|
34
41
|
subscribeToPerpMarketAccount(marketIndex: number): Promise<boolean>;
|
|
35
42
|
subscribeToSpotMarketAccounts(): Promise<boolean>;
|
|
@@ -52,7 +59,7 @@ export declare class WebSocketDriftClientAccountSubscriber implements DriftClien
|
|
|
52
59
|
getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[];
|
|
53
60
|
getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
|
|
54
61
|
getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
|
|
55
|
-
getOraclePriceDataAndSlot(oraclePublicKey: PublicKey): DataAndSlot<OraclePriceData> | undefined;
|
|
62
|
+
getOraclePriceDataAndSlot(oraclePublicKey: PublicKey | string): DataAndSlot<OraclePriceData> | undefined;
|
|
56
63
|
getOraclePriceDataAndSlotForPerpMarket(marketIndex: number): DataAndSlot<OraclePriceData> | undefined;
|
|
57
64
|
getOraclePriceDataAndSlotForSpotMarket(marketIndex: number): DataAndSlot<OraclePriceData> | undefined;
|
|
58
65
|
}
|
|
@@ -9,13 +9,16 @@ const web3_js_1 = require("@solana/web3.js");
|
|
|
9
9
|
const oracleClientCache_1 = require("../oracles/oracleClientCache");
|
|
10
10
|
const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
|
|
11
11
|
const config_1 = require("../config");
|
|
12
|
+
const ORACLE_DEFAULT_KEY = web3_js_1.PublicKey.default.toBase58();
|
|
12
13
|
class WebSocketDriftClientAccountSubscriber {
|
|
13
14
|
constructor(program, perpMarketIndexes, spotMarketIndexes, oracleInfos, shouldFindAllMarketsAndOracles, resubOpts, commitment) {
|
|
14
15
|
this.oracleClientCache = new oracleClientCache_1.OracleClientCache();
|
|
15
16
|
this.perpMarketAccountSubscribers = new Map();
|
|
16
17
|
this.perpOracleMap = new Map();
|
|
18
|
+
this.perpOracleStringMap = new Map();
|
|
17
19
|
this.spotMarketAccountSubscribers = new Map();
|
|
18
20
|
this.spotOracleMap = new Map();
|
|
21
|
+
this.spotOracleStringMap = new Map();
|
|
19
22
|
this.oracleSubscribers = new Map();
|
|
20
23
|
this.isSubscribing = false;
|
|
21
24
|
this.isSubscribed = false;
|
|
@@ -40,10 +43,13 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
40
43
|
this.subscriptionPromiseResolver = res;
|
|
41
44
|
});
|
|
42
45
|
if (this.shouldFindAllMarketsAndOracles) {
|
|
43
|
-
const { perpMarketIndexes, spotMarketIndexes, oracleInfos } = await (0, config_1.findAllMarketAndOracles)(this.program);
|
|
46
|
+
const { perpMarketIndexes, perpMarketAccounts, spotMarketIndexes, spotMarketAccounts, oracleInfos, } = await (0, config_1.findAllMarketAndOracles)(this.program);
|
|
44
47
|
this.perpMarketIndexes = perpMarketIndexes;
|
|
45
48
|
this.spotMarketIndexes = spotMarketIndexes;
|
|
46
49
|
this.oracleInfos = oracleInfos;
|
|
50
|
+
// front run and set the initial data here to save extra gma call in set initial data
|
|
51
|
+
this.initialPerpMarketAccountData = new Map(perpMarketAccounts.map((market) => [market.marketIndex, market]));
|
|
52
|
+
this.initialSpotMarketAccountData = new Map(spotMarketAccounts.map((market) => [market.marketIndex, market]));
|
|
47
53
|
}
|
|
48
54
|
const statePublicKey = await (0, pda_1.getDriftStateAccountPublicKey)(this.program.programId);
|
|
49
55
|
// create and activate main state account subscription
|
|
@@ -52,6 +58,8 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
52
58
|
this.eventEmitter.emit('stateAccountUpdate', data);
|
|
53
59
|
this.eventEmitter.emit('update');
|
|
54
60
|
});
|
|
61
|
+
// set initial data to avoid spamming getAccountInfo calls in webSocketAccountSubscriber
|
|
62
|
+
await this.setInitialData();
|
|
55
63
|
await Promise.all([
|
|
56
64
|
// subscribe to market accounts
|
|
57
65
|
this.subscribeToPerpMarketAccounts(),
|
|
@@ -65,8 +73,48 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
65
73
|
this.isSubscribing = false;
|
|
66
74
|
this.isSubscribed = true;
|
|
67
75
|
this.subscriptionPromiseResolver(true);
|
|
76
|
+
// delete initial data
|
|
77
|
+
this.removeInitialData();
|
|
68
78
|
return true;
|
|
69
79
|
}
|
|
80
|
+
async setInitialData() {
|
|
81
|
+
const connection = this.program.provider.connection;
|
|
82
|
+
if (!this.initialPerpMarketAccountData) {
|
|
83
|
+
const perpMarketPublicKeys = this.perpMarketIndexes.map((marketIndex) => (0, pda_1.getPerpMarketPublicKeySync)(this.program.programId, marketIndex));
|
|
84
|
+
const perpMarketAccountInfos = await connection.getMultipleAccountsInfo(perpMarketPublicKeys);
|
|
85
|
+
this.initialPerpMarketAccountData = new Map(perpMarketAccountInfos
|
|
86
|
+
.filter((accountInfo) => !!accountInfo)
|
|
87
|
+
.map((accountInfo) => {
|
|
88
|
+
const perpMarket = this.program.coder.accounts.decode('PerpMarket', accountInfo.data);
|
|
89
|
+
return [perpMarket.marketIndex, perpMarket];
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
if (!this.initialSpotMarketAccountData) {
|
|
93
|
+
const spotMarketPublicKeys = this.spotMarketIndexes.map((marketIndex) => (0, pda_1.getSpotMarketPublicKeySync)(this.program.programId, marketIndex));
|
|
94
|
+
const spotMarketAccountInfos = await connection.getMultipleAccountsInfo(spotMarketPublicKeys);
|
|
95
|
+
this.initialSpotMarketAccountData = new Map(spotMarketAccountInfos
|
|
96
|
+
.filter((accountInfo) => !!accountInfo)
|
|
97
|
+
.map((accountInfo) => {
|
|
98
|
+
const spotMarket = this.program.coder.accounts.decode('SpotMarket', accountInfo.data);
|
|
99
|
+
return [spotMarket.marketIndex, spotMarket];
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
const oracleAccountInfos = await connection.getMultipleAccountsInfo(this.oracleInfos.map((oracleInfo) => oracleInfo.publicKey));
|
|
103
|
+
this.initialOraclePriceData = new Map(this.oracleInfos.reduce((result, oracleInfo, i) => {
|
|
104
|
+
if (!oracleAccountInfos[i]) {
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
const oracleClient = this.oracleClientCache.get(oracleInfo.source, connection, this.program);
|
|
108
|
+
const oraclePriceData = oracleClient.getOraclePriceDataFromBuffer(oracleAccountInfos[i].data);
|
|
109
|
+
result.push([oracleInfo.publicKey.toString(), oraclePriceData]);
|
|
110
|
+
return result;
|
|
111
|
+
}, []));
|
|
112
|
+
}
|
|
113
|
+
removeInitialData() {
|
|
114
|
+
this.initialPerpMarketAccountData = new Map();
|
|
115
|
+
this.initialSpotMarketAccountData = new Map();
|
|
116
|
+
this.initialOraclePriceData = new Map();
|
|
117
|
+
}
|
|
70
118
|
async subscribeToPerpMarketAccounts() {
|
|
71
119
|
await Promise.all(this.perpMarketIndexes.map((marketIndex) => this.subscribeToPerpMarketAccount(marketIndex)));
|
|
72
120
|
return true;
|
|
@@ -74,6 +122,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
74
122
|
async subscribeToPerpMarketAccount(marketIndex) {
|
|
75
123
|
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
|
|
76
124
|
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('perpMarket', this.program, perpMarketPublicKey, undefined, this.resubOpts, this.commitment);
|
|
125
|
+
accountSubscriber.setData(this.initialPerpMarketAccountData.get(marketIndex));
|
|
77
126
|
await accountSubscriber.subscribe((data) => {
|
|
78
127
|
this.eventEmitter.emit('perpMarketAccountUpdate', data);
|
|
79
128
|
this.eventEmitter.emit('update');
|
|
@@ -88,6 +137,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
88
137
|
async subscribeToSpotMarketAccount(marketIndex) {
|
|
89
138
|
const marketPublicKey = await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, marketIndex);
|
|
90
139
|
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('spotMarket', this.program, marketPublicKey, undefined, this.resubOpts, this.commitment);
|
|
140
|
+
accountSubscriber.setData(this.initialSpotMarketAccountData.get(marketIndex));
|
|
91
141
|
await accountSubscriber.subscribe((data) => {
|
|
92
142
|
this.eventEmitter.emit('spotMarketAccountUpdate', data);
|
|
93
143
|
this.eventEmitter.emit('update');
|
|
@@ -102,15 +152,20 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
102
152
|
return true;
|
|
103
153
|
}
|
|
104
154
|
async subscribeToOracle(oracleInfo) {
|
|
155
|
+
const oracleString = oracleInfo.publicKey.toString();
|
|
105
156
|
const client = this.oracleClientCache.get(oracleInfo.source, this.program.provider.connection, this.program);
|
|
106
157
|
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('oracle', this.program, oracleInfo.publicKey, (buffer) => {
|
|
107
158
|
return client.getOraclePriceDataFromBuffer(buffer);
|
|
108
159
|
}, this.resubOpts, this.commitment);
|
|
160
|
+
const initialOraclePriceData = this.initialOraclePriceData.get(oracleString);
|
|
161
|
+
if (initialOraclePriceData) {
|
|
162
|
+
accountSubscriber.setData(initialOraclePriceData);
|
|
163
|
+
}
|
|
109
164
|
await accountSubscriber.subscribe((data) => {
|
|
110
165
|
this.eventEmitter.emit('oraclePriceUpdate', oracleInfo.publicKey, data);
|
|
111
166
|
this.eventEmitter.emit('update');
|
|
112
167
|
});
|
|
113
|
-
this.oracleSubscribers.set(
|
|
168
|
+
this.oracleSubscribers.set(oracleString, accountSubscriber);
|
|
114
169
|
return true;
|
|
115
170
|
}
|
|
116
171
|
async unsubscribeFromMarketAccounts() {
|
|
@@ -170,7 +225,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
170
225
|
const perpMarkets = this.getMarketAccountsAndSlots();
|
|
171
226
|
const addOraclePromises = [];
|
|
172
227
|
for (const perpMarket of perpMarkets) {
|
|
173
|
-
if (!perpMarket) {
|
|
228
|
+
if (!perpMarket || !perpMarket.data) {
|
|
174
229
|
continue;
|
|
175
230
|
}
|
|
176
231
|
const perpMarketAccount = perpMarket.data;
|
|
@@ -183,6 +238,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
183
238
|
}));
|
|
184
239
|
}
|
|
185
240
|
this.perpOracleMap.set(perpMarketIndex, oracle);
|
|
241
|
+
this.perpOracleStringMap.set(perpMarketIndex, oracle.toBase58());
|
|
186
242
|
}
|
|
187
243
|
await Promise.all(addOraclePromises);
|
|
188
244
|
}
|
|
@@ -190,7 +246,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
190
246
|
const spotMarkets = this.getSpotMarketAccountsAndSlots();
|
|
191
247
|
const addOraclePromises = [];
|
|
192
248
|
for (const spotMarket of spotMarkets) {
|
|
193
|
-
if (!spotMarket) {
|
|
249
|
+
if (!spotMarket || !spotMarket.data) {
|
|
194
250
|
continue;
|
|
195
251
|
}
|
|
196
252
|
const spotMarketAccount = spotMarket.data;
|
|
@@ -203,6 +259,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
203
259
|
}));
|
|
204
260
|
}
|
|
205
261
|
this.spotOracleMap.set(spotMarketIndex, oracle);
|
|
262
|
+
this.spotOracleStringMap.set(spotMarketIndex, oracle.toBase58());
|
|
206
263
|
}
|
|
207
264
|
await Promise.all(addOraclePromises);
|
|
208
265
|
}
|
|
@@ -231,17 +288,21 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
231
288
|
}
|
|
232
289
|
getOraclePriceDataAndSlot(oraclePublicKey) {
|
|
233
290
|
this.assertIsSubscribed();
|
|
234
|
-
|
|
291
|
+
const oracleString = typeof oraclePublicKey === 'string'
|
|
292
|
+
? oraclePublicKey
|
|
293
|
+
: oraclePublicKey.toBase58();
|
|
294
|
+
if (oracleString === ORACLE_DEFAULT_KEY) {
|
|
235
295
|
return {
|
|
236
296
|
data: quoteAssetOracleClient_1.QUOTE_ORACLE_PRICE_DATA,
|
|
237
297
|
slot: 0,
|
|
238
298
|
};
|
|
239
299
|
}
|
|
240
|
-
return this.oracleSubscribers.get(
|
|
300
|
+
return this.oracleSubscribers.get(oracleString).dataAndSlot;
|
|
241
301
|
}
|
|
242
302
|
getOraclePriceDataAndSlotForPerpMarket(marketIndex) {
|
|
243
303
|
const perpMarketAccount = this.getMarketAccountAndSlot(marketIndex);
|
|
244
304
|
const oracle = this.perpOracleMap.get(marketIndex);
|
|
305
|
+
const oracleString = this.perpOracleStringMap.get(marketIndex);
|
|
245
306
|
if (!perpMarketAccount || !oracle) {
|
|
246
307
|
return undefined;
|
|
247
308
|
}
|
|
@@ -249,11 +310,12 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
249
310
|
// If the oracle has changed, we need to update the oracle map in background
|
|
250
311
|
this.setPerpOracleMap();
|
|
251
312
|
}
|
|
252
|
-
return this.getOraclePriceDataAndSlot(
|
|
313
|
+
return this.getOraclePriceDataAndSlot(oracleString);
|
|
253
314
|
}
|
|
254
315
|
getOraclePriceDataAndSlotForSpotMarket(marketIndex) {
|
|
255
316
|
const spotMarketAccount = this.getSpotMarketAccountAndSlot(marketIndex);
|
|
256
317
|
const oracle = this.spotOracleMap.get(marketIndex);
|
|
318
|
+
const oracleString = this.spotOracleStringMap.get(marketIndex);
|
|
257
319
|
if (!spotMarketAccount || !oracle) {
|
|
258
320
|
return undefined;
|
|
259
321
|
}
|
|
@@ -261,7 +323,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
261
323
|
// If the oracle has changed, we need to update the oracle map in background
|
|
262
324
|
this.setSpotOracleMap();
|
|
263
325
|
}
|
|
264
|
-
return this.getOraclePriceDataAndSlot(
|
|
326
|
+
return this.getOraclePriceDataAndSlot(oracleString);
|
|
265
327
|
}
|
|
266
328
|
}
|
|
267
329
|
exports.WebSocketDriftClientAccountSubscriber = WebSocketDriftClientAccountSubscriber;
|
|
@@ -27,7 +27,7 @@ export declare class WebSocketProgramAccountSubscriber<T> implements ProgramAcco
|
|
|
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
|
+
protected setTimeout(): void;
|
|
31
31
|
handleRpcResponse(context: Context, keyedAccountInfo: KeyedAccountInfo): void;
|
|
32
32
|
unsubscribe(onResub?: boolean): Promise<void>;
|
|
33
33
|
}
|
package/lib/addresses/pda.d.ts
CHANGED
|
@@ -9,7 +9,9 @@ export declare function getUserAccountPublicKey(programId: PublicKey, authority:
|
|
|
9
9
|
export declare function getUserAccountPublicKeySync(programId: PublicKey, authority: PublicKey, subAccountId?: number): PublicKey;
|
|
10
10
|
export declare function getUserStatsAccountPublicKey(programId: PublicKey, authority: PublicKey): PublicKey;
|
|
11
11
|
export declare function getPerpMarketPublicKey(programId: PublicKey, marketIndex: number): Promise<PublicKey>;
|
|
12
|
+
export declare function getPerpMarketPublicKeySync(programId: PublicKey, marketIndex: number): PublicKey;
|
|
12
13
|
export declare function getSpotMarketPublicKey(programId: PublicKey, marketIndex: number): Promise<PublicKey>;
|
|
14
|
+
export declare function getSpotMarketPublicKeySync(programId: PublicKey, marketIndex: number): PublicKey;
|
|
13
15
|
export declare function getSpotMarketVaultPublicKey(programId: PublicKey, marketIndex: number): Promise<PublicKey>;
|
|
14
16
|
export declare function getInsuranceFundVaultPublicKey(programId: PublicKey, marketIndex: number): Promise<PublicKey>;
|
|
15
17
|
export declare function getInsuranceFundStakeAccountPublicKey(programId: PublicKey, authority: PublicKey, marketIndex: number): PublicKey;
|
package/lib/addresses/pda.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.getTokenProgramForSpotMarket = exports.getPythPullOraclePublicKey = exports.getPrelaunchOraclePublicKey = exports.getProtocolIfSharesTransferConfigPublicKey = exports.getReferrerNamePublicKeySync = exports.getOpenbookV2FulfillmentConfigPublicKey = exports.getPhoenixFulfillmentConfigPublicKey = exports.getSerumFulfillmentConfigPublicKey = exports.getSerumSignerPublicKey = exports.getSerumOpenOrdersPublicKey = exports.getDriftSignerPublicKey = exports.getInsuranceFundStakeAccountPublicKey = exports.getInsuranceFundVaultPublicKey = exports.getSpotMarketVaultPublicKey = exports.getSpotMarketPublicKey = exports.getPerpMarketPublicKey = exports.getUserStatsAccountPublicKey = exports.getUserAccountPublicKeySync = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getDriftStateAccountPublicKey = exports.getDriftStateAccountPublicKeyAndNonce = void 0;
|
|
26
|
+
exports.getTokenProgramForSpotMarket = exports.getPythPullOraclePublicKey = exports.getPrelaunchOraclePublicKey = exports.getProtocolIfSharesTransferConfigPublicKey = exports.getReferrerNamePublicKeySync = exports.getOpenbookV2FulfillmentConfigPublicKey = exports.getPhoenixFulfillmentConfigPublicKey = exports.getSerumFulfillmentConfigPublicKey = exports.getSerumSignerPublicKey = exports.getSerumOpenOrdersPublicKey = exports.getDriftSignerPublicKey = exports.getInsuranceFundStakeAccountPublicKey = exports.getInsuranceFundVaultPublicKey = exports.getSpotMarketVaultPublicKey = exports.getSpotMarketPublicKeySync = exports.getSpotMarketPublicKey = exports.getPerpMarketPublicKeySync = exports.getPerpMarketPublicKey = exports.getUserStatsAccountPublicKey = exports.getUserAccountPublicKeySync = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getDriftStateAccountPublicKey = exports.getDriftStateAccountPublicKeyAndNonce = void 0;
|
|
27
27
|
const web3_js_1 = require("@solana/web3.js");
|
|
28
28
|
const anchor = __importStar(require("@coral-xyz/anchor"));
|
|
29
29
|
const spl_token_1 = require("@solana/spl-token");
|
|
@@ -69,6 +69,13 @@ async function getPerpMarketPublicKey(programId, marketIndex) {
|
|
|
69
69
|
], programId))[0];
|
|
70
70
|
}
|
|
71
71
|
exports.getPerpMarketPublicKey = getPerpMarketPublicKey;
|
|
72
|
+
function getPerpMarketPublicKeySync(programId, marketIndex) {
|
|
73
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
74
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('perp_market')),
|
|
75
|
+
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
76
|
+
], programId)[0];
|
|
77
|
+
}
|
|
78
|
+
exports.getPerpMarketPublicKeySync = getPerpMarketPublicKeySync;
|
|
72
79
|
async function getSpotMarketPublicKey(programId, marketIndex) {
|
|
73
80
|
return (await web3_js_1.PublicKey.findProgramAddress([
|
|
74
81
|
Buffer.from(anchor.utils.bytes.utf8.encode('spot_market')),
|
|
@@ -76,6 +83,13 @@ async function getSpotMarketPublicKey(programId, marketIndex) {
|
|
|
76
83
|
], programId))[0];
|
|
77
84
|
}
|
|
78
85
|
exports.getSpotMarketPublicKey = getSpotMarketPublicKey;
|
|
86
|
+
function getSpotMarketPublicKeySync(programId, marketIndex) {
|
|
87
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
88
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('spot_market')),
|
|
89
|
+
new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
|
|
90
|
+
], programId)[0];
|
|
91
|
+
}
|
|
92
|
+
exports.getSpotMarketPublicKeySync = getSpotMarketPublicKeySync;
|
|
79
93
|
async function getSpotMarketVaultPublicKey(programId, marketIndex) {
|
|
80
94
|
return (await web3_js_1.PublicKey.findProgramAddress([
|
|
81
95
|
Buffer.from(anchor.utils.bytes.utf8.encode('spot_market_vault')),
|
package/lib/config.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PerpMarketAccount, SpotMarketAccount } from '.';
|
|
1
2
|
import { PerpMarketConfig } from './constants/perpMarkets';
|
|
2
3
|
import { SpotMarketConfig } from './constants/spotMarkets';
|
|
3
4
|
import { OracleInfo } from './oracles/types';
|
|
@@ -44,7 +45,9 @@ export declare function getMarketsAndOraclesForSubscription(env: DriftEnv): {
|
|
|
44
45
|
};
|
|
45
46
|
export declare function findAllMarketAndOracles(program: Program): Promise<{
|
|
46
47
|
perpMarketIndexes: number[];
|
|
48
|
+
perpMarketAccounts: PerpMarketAccount[];
|
|
47
49
|
spotMarketIndexes: number[];
|
|
48
50
|
oracleInfos: OracleInfo[];
|
|
51
|
+
spotMarketAccounts: SpotMarketAccount[];
|
|
49
52
|
}>;
|
|
50
53
|
export {};
|
package/lib/config.js
CHANGED
|
@@ -106,7 +106,9 @@ async function findAllMarketAndOracles(program) {
|
|
|
106
106
|
}
|
|
107
107
|
return {
|
|
108
108
|
perpMarketIndexes,
|
|
109
|
+
perpMarketAccounts: perpMarketProgramAccounts.map((account) => account.account),
|
|
109
110
|
spotMarketIndexes,
|
|
111
|
+
spotMarketAccounts: spotMarketProgramAccounts.map((account) => account.account),
|
|
110
112
|
oracleInfos: Array.from(oracleInfos.values()),
|
|
111
113
|
};
|
|
112
114
|
}
|
|
@@ -333,6 +333,16 @@ exports.MainnetSpotMarkets = [
|
|
|
333
333
|
precisionExp: numericConstants_1.NINE,
|
|
334
334
|
pythFeedId: '0xca3ba9a619a4b3755c10ac7d5e760275aa95e9823d38a84fedd416856cdba37c',
|
|
335
335
|
},
|
|
336
|
+
{
|
|
337
|
+
symbol: 'BNSOL',
|
|
338
|
+
marketIndex: 25,
|
|
339
|
+
oracle: new web3_js_1.PublicKey('BAtFj4kQttZRVep3UZS2aZRDixkGYgWsbqTBVDbnSsPF'),
|
|
340
|
+
oracleSource: __1.OracleSource.PYTH_PULL,
|
|
341
|
+
mint: new web3_js_1.PublicKey('BNso1VUJnh4zcfpZa6986Ea66P6TCp59hvtNJ8b1X85'),
|
|
342
|
+
precision: numericConstants_1.LAMPORTS_PRECISION,
|
|
343
|
+
precisionExp: numericConstants_1.LAMPORTS_EXP,
|
|
344
|
+
pythFeedId: '0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d',
|
|
345
|
+
},
|
|
336
346
|
];
|
|
337
347
|
exports.SpotMarkets = {
|
|
338
348
|
devnet: exports.DevnetSpotMarkets,
|
package/lib/driftClient.js
CHANGED
|
@@ -68,6 +68,7 @@ const utils_2 = require("./tx/utils");
|
|
|
68
68
|
const pyth_solana_receiver_json_1 = __importDefault(require("./idl/pyth_solana_receiver.json"));
|
|
69
69
|
const on_demand_1 = require("@switchboard-xyz/on-demand");
|
|
70
70
|
const switchboard_on_demand_30_json_1 = __importDefault(require("./idl/switchboard_on_demand_30.json"));
|
|
71
|
+
const grpcDriftClientAccountSubscriber_1 = require("./accounts/grpcDriftClientAccountSubscriber");
|
|
71
72
|
/**
|
|
72
73
|
* # DriftClient
|
|
73
74
|
* This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
|
|
@@ -80,7 +81,7 @@ class DriftClient {
|
|
|
80
81
|
this._isSubscribed = val;
|
|
81
82
|
}
|
|
82
83
|
constructor(config) {
|
|
83
|
-
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, _6;
|
|
84
|
+
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, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19;
|
|
84
85
|
this.users = new Map();
|
|
85
86
|
this._isSubscribed = false;
|
|
86
87
|
this.perpMarketLastSlotCache = new Map();
|
|
@@ -143,18 +144,32 @@ class DriftClient {
|
|
|
143
144
|
accountLoader: config.accountSubscription.accountLoader,
|
|
144
145
|
};
|
|
145
146
|
}
|
|
147
|
+
else if (((_q = config.accountSubscription) === null || _q === void 0 ? void 0 : _q.type) === 'grpc') {
|
|
148
|
+
this.userAccountSubscriptionConfig = {
|
|
149
|
+
type: 'grpc',
|
|
150
|
+
resubTimeoutMs: (_r = config.accountSubscription) === null || _r === void 0 ? void 0 : _r.resubTimeoutMs,
|
|
151
|
+
logResubMessages: (_s = config.accountSubscription) === null || _s === void 0 ? void 0 : _s.logResubMessages,
|
|
152
|
+
configs: (_t = config.accountSubscription) === null || _t === void 0 ? void 0 : _t.configs,
|
|
153
|
+
};
|
|
154
|
+
this.userStatsAccountSubscriptionConfig = {
|
|
155
|
+
type: 'grpc',
|
|
156
|
+
resubTimeoutMs: (_u = config.accountSubscription) === null || _u === void 0 ? void 0 : _u.resubTimeoutMs,
|
|
157
|
+
logResubMessages: (_v = config.accountSubscription) === null || _v === void 0 ? void 0 : _v.logResubMessages,
|
|
158
|
+
configs: (_w = config.accountSubscription) === null || _w === void 0 ? void 0 : _w.configs,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
146
161
|
else {
|
|
147
162
|
this.userAccountSubscriptionConfig = {
|
|
148
163
|
type: 'websocket',
|
|
149
|
-
resubTimeoutMs: (
|
|
150
|
-
logResubMessages: (
|
|
151
|
-
commitment: (
|
|
164
|
+
resubTimeoutMs: (_x = config.accountSubscription) === null || _x === void 0 ? void 0 : _x.resubTimeoutMs,
|
|
165
|
+
logResubMessages: (_y = config.accountSubscription) === null || _y === void 0 ? void 0 : _y.logResubMessages,
|
|
166
|
+
commitment: (_z = config.accountSubscription) === null || _z === void 0 ? void 0 : _z.commitment,
|
|
152
167
|
};
|
|
153
168
|
this.userStatsAccountSubscriptionConfig = {
|
|
154
169
|
type: 'websocket',
|
|
155
|
-
resubTimeoutMs: (
|
|
156
|
-
logResubMessages: (
|
|
157
|
-
commitment: (
|
|
170
|
+
resubTimeoutMs: (_0 = config.accountSubscription) === null || _0 === void 0 ? void 0 : _0.resubTimeoutMs,
|
|
171
|
+
logResubMessages: (_1 = config.accountSubscription) === null || _1 === void 0 ? void 0 : _1.logResubMessages,
|
|
172
|
+
commitment: (_2 = config.accountSubscription) === null || _2 === void 0 ? void 0 : _2.commitment,
|
|
158
173
|
};
|
|
159
174
|
}
|
|
160
175
|
if (config.userStats) {
|
|
@@ -171,14 +186,20 @@ class DriftClient {
|
|
|
171
186
|
const noMarketsAndOraclesSpecified = config.perpMarketIndexes === undefined &&
|
|
172
187
|
config.spotMarketIndexes === undefined &&
|
|
173
188
|
config.oracleInfos === undefined;
|
|
174
|
-
if (((
|
|
175
|
-
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (
|
|
189
|
+
if (((_3 = config.accountSubscription) === null || _3 === void 0 ? void 0 : _3.type) === 'polling') {
|
|
190
|
+
this.accountSubscriber = new pollingDriftClientAccountSubscriber_1.PollingDriftClientAccountSubscriber(this.program, config.accountSubscription.accountLoader, (_4 = config.perpMarketIndexes) !== null && _4 !== void 0 ? _4 : [], (_5 = config.spotMarketIndexes) !== null && _5 !== void 0 ? _5 : [], (_6 = config.oracleInfos) !== null && _6 !== void 0 ? _6 : [], noMarketsAndOraclesSpecified);
|
|
191
|
+
}
|
|
192
|
+
else if (((_7 = config.accountSubscription) === null || _7 === void 0 ? void 0 : _7.type) === 'grpc') {
|
|
193
|
+
this.accountSubscriber = new grpcDriftClientAccountSubscriber_1.gprcDriftClientAccountSubscriber(config.accountSubscription.configs, this.program, (_8 = config.perpMarketIndexes) !== null && _8 !== void 0 ? _8 : [], (_9 = config.spotMarketIndexes) !== null && _9 !== void 0 ? _9 : [], (_10 = config.oracleInfos) !== null && _10 !== void 0 ? _10 : [], noMarketsAndOraclesSpecified, {
|
|
194
|
+
resubTimeoutMs: (_11 = config.accountSubscription) === null || _11 === void 0 ? void 0 : _11.resubTimeoutMs,
|
|
195
|
+
logResubMessages: (_12 = config.accountSubscription) === null || _12 === void 0 ? void 0 : _12.logResubMessages,
|
|
196
|
+
});
|
|
176
197
|
}
|
|
177
198
|
else {
|
|
178
|
-
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (
|
|
179
|
-
resubTimeoutMs: (
|
|
180
|
-
logResubMessages: (
|
|
181
|
-
}, (
|
|
199
|
+
this.accountSubscriber = new webSocketDriftClientAccountSubscriber_1.WebSocketDriftClientAccountSubscriber(this.program, (_13 = config.perpMarketIndexes) !== null && _13 !== void 0 ? _13 : [], (_14 = config.spotMarketIndexes) !== null && _14 !== void 0 ? _14 : [], (_15 = config.oracleInfos) !== null && _15 !== void 0 ? _15 : [], noMarketsAndOraclesSpecified, {
|
|
200
|
+
resubTimeoutMs: (_16 = config.accountSubscription) === null || _16 === void 0 ? void 0 : _16.resubTimeoutMs,
|
|
201
|
+
logResubMessages: (_17 = config.accountSubscription) === null || _17 === void 0 ? void 0 : _17.logResubMessages,
|
|
202
|
+
}, (_18 = config.accountSubscription) === null || _18 === void 0 ? void 0 : _18.commitment);
|
|
182
203
|
}
|
|
183
204
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
184
205
|
this.metricsEventEmitter = new events_1.EventEmitter();
|
|
@@ -186,7 +207,7 @@ class DriftClient {
|
|
|
186
207
|
this.enableMetricsEvents = true;
|
|
187
208
|
}
|
|
188
209
|
this.txSender =
|
|
189
|
-
(
|
|
210
|
+
(_19 = config.txSender) !== null && _19 !== void 0 ? _19 : new retryTxSender_1.RetryTxSender({
|
|
190
211
|
connection: this.connection,
|
|
191
212
|
wallet: this.wallet,
|
|
192
213
|
opts: this.opts,
|
|
@@ -309,7 +330,7 @@ class DriftClient {
|
|
|
309
330
|
return this.accountSubscriber.getSpotMarketAccountAndSlot(numericConstants_1.QUOTE_SPOT_MARKET_INDEX).data;
|
|
310
331
|
}
|
|
311
332
|
getOraclePriceDataAndSlot(oraclePublicKey) {
|
|
312
|
-
return this.accountSubscriber.getOraclePriceDataAndSlot(oraclePublicKey);
|
|
333
|
+
return this.accountSubscriber.getOraclePriceDataAndSlot(oraclePublicKey.toBase58());
|
|
313
334
|
}
|
|
314
335
|
async getSerumV3FulfillmentConfig(serumMarket) {
|
|
315
336
|
const address = await (0, pda_1.getSerumFulfillmentConfigPublicKey)(this.program.programId, serumMarket);
|
|
@@ -1725,10 +1746,14 @@ class DriftClient {
|
|
|
1725
1746
|
writablePerpMarketIndexes: [marketIndex],
|
|
1726
1747
|
writableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
|
|
1727
1748
|
});
|
|
1749
|
+
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
|
|
1728
1750
|
return await this.program.instruction.settleExpiredMarket(marketIndex, {
|
|
1729
1751
|
accounts: {
|
|
1730
1752
|
state: await this.getStatePublicKey(),
|
|
1731
|
-
|
|
1753
|
+
admin: this.isSubscribed
|
|
1754
|
+
? this.getStateAccount().admin
|
|
1755
|
+
: this.wallet.publicKey,
|
|
1756
|
+
perpMarket: perpMarketPublicKey,
|
|
1732
1757
|
},
|
|
1733
1758
|
remainingAccounts,
|
|
1734
1759
|
});
|
|
@@ -5,6 +5,7 @@ import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
|
5
5
|
import { DriftEnv } from './config';
|
|
6
6
|
import { TxSender } from './tx/types';
|
|
7
7
|
import { TxHandler, TxHandlerConfig } from './tx/txHandler';
|
|
8
|
+
import { GrpcConfigs } from './accounts/types';
|
|
8
9
|
export type DriftClientConfig = {
|
|
9
10
|
connection: Connection;
|
|
10
11
|
wallet: IWallet;
|
|
@@ -38,4 +39,9 @@ export type DriftClientSubscriptionConfig = {
|
|
|
38
39
|
} | {
|
|
39
40
|
type: 'polling';
|
|
40
41
|
accountLoader: BulkAccountLoader;
|
|
42
|
+
} | {
|
|
43
|
+
type: 'grpc';
|
|
44
|
+
configs: GrpcConfigs;
|
|
45
|
+
resubTimeoutMs?: number;
|
|
46
|
+
logResubMessages?: boolean;
|
|
41
47
|
};
|
package/lib/events/parse.js
CHANGED
|
@@ -7,23 +7,7 @@ const PROGRAM_DATA = 'Program data: ';
|
|
|
7
7
|
const PROGRAM_LOG_START_INDEX = PROGRAM_LOG.length;
|
|
8
8
|
const PROGRAM_DATA_START_INDEX = PROGRAM_DATA.length;
|
|
9
9
|
function parseLogs(program, logs, programId = driftProgramId) {
|
|
10
|
-
const events =
|
|
11
|
-
const execution = new ExecutionContext();
|
|
12
|
-
for (const log of logs) {
|
|
13
|
-
if (log.startsWith('Log truncated')) {
|
|
14
|
-
break;
|
|
15
|
-
}
|
|
16
|
-
const [event, newProgram, didPop] = handleLog(execution, log, program, programId);
|
|
17
|
-
if (event) {
|
|
18
|
-
events.push(event);
|
|
19
|
-
}
|
|
20
|
-
if (newProgram) {
|
|
21
|
-
execution.push(newProgram);
|
|
22
|
-
}
|
|
23
|
-
if (didPop) {
|
|
24
|
-
execution.pop();
|
|
25
|
-
}
|
|
26
|
-
}
|
|
10
|
+
const { events } = parseLogsWithRaw(program, logs, programId);
|
|
27
11
|
return events;
|
|
28
12
|
}
|
|
29
13
|
exports.parseLogs = parseLogs;
|