@drift-labs/sdk 2.136.0-beta.0 → 2.136.0-beta.2
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/browser/accounts/types.d.ts +2 -0
- package/lib/browser/accounts/webSocketAccountSubscriberV2.d.ts +76 -3
- package/lib/browser/accounts/webSocketAccountSubscriberV2.js +211 -39
- package/lib/browser/accounts/webSocketDriftClientAccountSubscriberV2.d.ts +87 -0
- package/lib/browser/accounts/webSocketDriftClientAccountSubscriberV2.js +444 -0
- package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.d.ts +145 -0
- package/lib/browser/accounts/webSocketProgramAccountsSubscriberV2.js +744 -0
- package/lib/browser/accounts/websocketProgramUserAccountSubscriber.d.ts +22 -0
- package/lib/browser/accounts/websocketProgramUserAccountSubscriber.js +54 -0
- package/lib/browser/driftClient.js +22 -18
- package/lib/browser/driftClientConfig.d.ts +7 -2
- package/lib/browser/factory/bigNum.d.ts +2 -2
- package/lib/browser/factory/bigNum.js +20 -5
- package/lib/browser/index.d.ts +4 -0
- package/lib/browser/index.js +9 -1
- package/lib/browser/memcmp.d.ts +2 -0
- package/lib/browser/memcmp.js +19 -1
- package/lib/browser/oracles/oracleId.d.ts +5 -0
- package/lib/browser/oracles/oracleId.js +46 -1
- package/lib/browser/user.js +12 -5
- package/lib/browser/userConfig.d.ts +3 -0
- package/lib/node/accounts/types.d.ts +2 -0
- package/lib/node/accounts/types.d.ts.map +1 -1
- package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts +76 -3
- package/lib/node/accounts/webSocketAccountSubscriberV2.d.ts.map +1 -1
- package/lib/node/accounts/webSocketAccountSubscriberV2.js +211 -39
- package/lib/node/accounts/webSocketDriftClientAccountSubscriberV2.d.ts +88 -0
- package/lib/node/accounts/webSocketDriftClientAccountSubscriberV2.d.ts.map +1 -0
- package/lib/node/accounts/webSocketDriftClientAccountSubscriberV2.js +444 -0
- package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts +146 -0
- package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.d.ts.map +1 -0
- package/lib/node/accounts/webSocketProgramAccountsSubscriberV2.js +744 -0
- package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts +23 -0
- package/lib/node/accounts/websocketProgramUserAccountSubscriber.d.ts.map +1 -0
- package/lib/node/accounts/websocketProgramUserAccountSubscriber.js +54 -0
- package/lib/node/driftClient.d.ts.map +1 -1
- package/lib/node/driftClient.js +22 -18
- package/lib/node/driftClientConfig.d.ts +7 -2
- package/lib/node/driftClientConfig.d.ts.map +1 -1
- package/lib/node/factory/bigNum.d.ts +2 -2
- package/lib/node/factory/bigNum.d.ts.map +1 -1
- package/lib/node/factory/bigNum.js +20 -5
- package/lib/node/index.d.ts +4 -0
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +9 -1
- package/lib/node/memcmp.d.ts +2 -0
- package/lib/node/memcmp.d.ts.map +1 -1
- package/lib/node/memcmp.js +19 -1
- package/lib/node/oracles/oracleId.d.ts +5 -0
- package/lib/node/oracles/oracleId.d.ts.map +1 -1
- package/lib/node/oracles/oracleId.js +46 -1
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/user.js +12 -5
- package/lib/node/userConfig.d.ts +3 -0
- package/lib/node/userConfig.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/accounts/README_WebSocketAccountSubscriberV2.md +41 -0
- package/src/accounts/types.ts +3 -0
- package/src/accounts/webSocketAccountSubscriberV2.ts +243 -42
- package/src/accounts/webSocketDriftClientAccountSubscriberV2.ts +745 -0
- package/src/accounts/webSocketProgramAccountsSubscriberV2.ts +995 -0
- package/src/accounts/websocketProgramUserAccountSubscriber.ts +94 -0
- package/src/driftClient.ts +13 -7
- package/src/driftClientConfig.ts +15 -8
- package/src/factory/bigNum.ts +22 -5
- package/src/index.ts +4 -0
- package/src/memcmp.ts +17 -0
- package/src/oracles/oracleId.ts +34 -0
- package/src/user.ts +21 -9
- package/src/userConfig.ts +3 -0
- package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.d.ts +0 -53
- package/lib/browser/accounts/webSocketProgramAccountSubscriberV2.js +0 -453
- package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts +0 -54
- package/lib/node/accounts/webSocketProgramAccountSubscriberV2.d.ts.map +0 -1
- package/lib/node/accounts/webSocketProgramAccountSubscriberV2.js +0 -453
- package/src/accounts/webSocketProgramAccountSubscriberV2.ts +0 -596
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataAndSlot,
|
|
3
|
+
NotSubscribedError,
|
|
4
|
+
UserAccountEvents,
|
|
5
|
+
UserAccountSubscriber,
|
|
6
|
+
} from './types';
|
|
7
|
+
import { Program } from '@coral-xyz/anchor';
|
|
8
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
9
|
+
import { EventEmitter } from 'events';
|
|
10
|
+
import { Context, PublicKey } from '@solana/web3.js';
|
|
11
|
+
import { WebSocketProgramAccountSubscriber } from './webSocketProgramAccountSubscriber';
|
|
12
|
+
import { UserAccount } from '../types';
|
|
13
|
+
|
|
14
|
+
export class WebSocketProgramUserAccountSubscriber
|
|
15
|
+
implements UserAccountSubscriber
|
|
16
|
+
{
|
|
17
|
+
isSubscribed: boolean;
|
|
18
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
|
|
19
|
+
|
|
20
|
+
private userAccountPublicKey: PublicKey;
|
|
21
|
+
private program: Program;
|
|
22
|
+
private programSubscriber: WebSocketProgramAccountSubscriber<UserAccount>;
|
|
23
|
+
private userAccountAndSlot?: DataAndSlot<UserAccount>;
|
|
24
|
+
|
|
25
|
+
public constructor(
|
|
26
|
+
program: Program,
|
|
27
|
+
userAccountPublicKey: PublicKey,
|
|
28
|
+
programSubscriber: WebSocketProgramAccountSubscriber<UserAccount>
|
|
29
|
+
) {
|
|
30
|
+
this.isSubscribed = false;
|
|
31
|
+
this.program = program;
|
|
32
|
+
this.userAccountPublicKey = userAccountPublicKey;
|
|
33
|
+
this.eventEmitter = new EventEmitter();
|
|
34
|
+
this.programSubscriber = programSubscriber;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async subscribe(userAccount?: UserAccount): Promise<boolean> {
|
|
38
|
+
if (this.isSubscribed) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (userAccount) {
|
|
43
|
+
this.updateData(userAccount, 0);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.programSubscriber.onChange = (
|
|
47
|
+
accountId: PublicKey,
|
|
48
|
+
data: UserAccount,
|
|
49
|
+
context: Context
|
|
50
|
+
) => {
|
|
51
|
+
if (accountId.equals(this.userAccountPublicKey)) {
|
|
52
|
+
this.updateData(data, context.slot);
|
|
53
|
+
this.eventEmitter.emit('userAccountUpdate', data);
|
|
54
|
+
this.eventEmitter.emit('update');
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
this.isSubscribed = true;
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async fetch(): Promise<void> {
|
|
63
|
+
if (!this.isSubscribed) {
|
|
64
|
+
throw new NotSubscribedError(
|
|
65
|
+
'Must subscribe before fetching account updates'
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const account = await this.program.account.user.fetch(
|
|
70
|
+
this.userAccountPublicKey
|
|
71
|
+
);
|
|
72
|
+
this.updateData(account as UserAccount, 0);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
updateData(userAccount: UserAccount, slot: number): void {
|
|
76
|
+
this.userAccountAndSlot = {
|
|
77
|
+
data: userAccount,
|
|
78
|
+
slot,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async unsubscribe(): Promise<void> {
|
|
83
|
+
this.isSubscribed = false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
getUserAccountAndSlot(): DataAndSlot<UserAccount> {
|
|
87
|
+
if (!this.userAccountAndSlot) {
|
|
88
|
+
throw new NotSubscribedError(
|
|
89
|
+
'Must subscribe before getting user account data'
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return this.userAccountAndSlot;
|
|
93
|
+
}
|
|
94
|
+
}
|
package/src/driftClient.ts
CHANGED
|
@@ -137,7 +137,6 @@ import { decodeName, DEFAULT_USER_NAME, encodeName } from './userName';
|
|
|
137
137
|
import { MMOraclePriceData, OraclePriceData } from './oracles/types';
|
|
138
138
|
import { DriftClientConfig } from './driftClientConfig';
|
|
139
139
|
import { PollingDriftClientAccountSubscriber } from './accounts/pollingDriftClientAccountSubscriber';
|
|
140
|
-
import { WebSocketDriftClientAccountSubscriber } from './accounts/webSocketDriftClientAccountSubscriber';
|
|
141
140
|
import { RetryTxSender } from './tx/retryTxSender';
|
|
142
141
|
import { User } from './user';
|
|
143
142
|
import { UserSubscriptionConfig } from './userConfig';
|
|
@@ -194,6 +193,8 @@ import { getOracleId } from './oracles/oracleId';
|
|
|
194
193
|
import { SignedMsgOrderParams } from './types';
|
|
195
194
|
import { sha256 } from '@noble/hashes/sha256';
|
|
196
195
|
import { getOracleConfidenceFromMMOracleData } from './oracles/utils';
|
|
196
|
+
import { Commitment } from 'gill';
|
|
197
|
+
import { WebSocketDriftClientAccountSubscriber } from './accounts/webSocketDriftClientAccountSubscriber';
|
|
197
198
|
|
|
198
199
|
type RemainingAccountParams = {
|
|
199
200
|
userAccounts: UserAccount[];
|
|
@@ -371,6 +372,8 @@ export class DriftClient {
|
|
|
371
372
|
resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
|
|
372
373
|
logResubMessages: config.accountSubscription?.logResubMessages,
|
|
373
374
|
commitment: config.accountSubscription?.commitment,
|
|
375
|
+
programUserAccountSubscriber:
|
|
376
|
+
config.accountSubscription?.programUserAccountSubscriber,
|
|
374
377
|
};
|
|
375
378
|
this.userStatsAccountSubscriptionConfig = {
|
|
376
379
|
type: 'websocket',
|
|
@@ -436,7 +439,10 @@ export class DriftClient {
|
|
|
436
439
|
}
|
|
437
440
|
);
|
|
438
441
|
} else {
|
|
439
|
-
|
|
442
|
+
const accountSubscriberClass =
|
|
443
|
+
config.accountSubscription?.driftClientAccountSubscriber ??
|
|
444
|
+
WebSocketDriftClientAccountSubscriber;
|
|
445
|
+
this.accountSubscriber = new accountSubscriberClass(
|
|
440
446
|
this.program,
|
|
441
447
|
config.perpMarketIndexes ?? [],
|
|
442
448
|
config.spotMarketIndexes ?? [],
|
|
@@ -447,9 +453,7 @@ export class DriftClient {
|
|
|
447
453
|
resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
|
|
448
454
|
logResubMessages: config.accountSubscription?.logResubMessages,
|
|
449
455
|
},
|
|
450
|
-
config.accountSubscription?.commitment
|
|
451
|
-
config.accountSubscription?.perpMarketAccountSubscriber,
|
|
452
|
-
config.accountSubscription?.oracleAccountSubscriber
|
|
456
|
+
config.accountSubscription?.commitment as Commitment
|
|
453
457
|
);
|
|
454
458
|
}
|
|
455
459
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
@@ -610,7 +614,8 @@ export class DriftClient {
|
|
|
610
614
|
public getSpotMarketAccount(
|
|
611
615
|
marketIndex: number
|
|
612
616
|
): SpotMarketAccount | undefined {
|
|
613
|
-
return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)
|
|
617
|
+
return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)
|
|
618
|
+
?.data;
|
|
614
619
|
}
|
|
615
620
|
|
|
616
621
|
/**
|
|
@@ -621,7 +626,8 @@ export class DriftClient {
|
|
|
621
626
|
marketIndex: number
|
|
622
627
|
): Promise<SpotMarketAccount | undefined> {
|
|
623
628
|
await this.accountSubscriber.fetch();
|
|
624
|
-
return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)
|
|
629
|
+
return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex)
|
|
630
|
+
?.data;
|
|
625
631
|
}
|
|
626
632
|
|
|
627
633
|
public getSpotMarketAccounts(): SpotMarketAccount[] {
|
package/src/driftClientConfig.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
PublicKey,
|
|
6
6
|
TransactionVersion,
|
|
7
7
|
} from '@solana/web3.js';
|
|
8
|
-
import { IWallet, TxParams } from './types';
|
|
8
|
+
import { IWallet, TxParams, UserAccount } from './types';
|
|
9
9
|
import { OracleInfo } from './oracles/types';
|
|
10
10
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
11
11
|
import { DriftEnv } from './config';
|
|
@@ -19,6 +19,9 @@ import {
|
|
|
19
19
|
import { Coder, Program } from '@coral-xyz/anchor';
|
|
20
20
|
import { WebSocketAccountSubscriber } from './accounts/webSocketAccountSubscriber';
|
|
21
21
|
import { WebSocketAccountSubscriberV2 } from './accounts/webSocketAccountSubscriberV2';
|
|
22
|
+
import { WebSocketProgramAccountSubscriber } from './accounts/webSocketProgramAccountSubscriber';
|
|
23
|
+
import { WebSocketDriftClientAccountSubscriberV2 } from './accounts/webSocketDriftClientAccountSubscriberV2';
|
|
24
|
+
import { WebSocketDriftClientAccountSubscriber } from './accounts/webSocketDriftClientAccountSubscriber';
|
|
22
25
|
|
|
23
26
|
export type DriftClientConfig = {
|
|
24
27
|
connection: Connection;
|
|
@@ -63,6 +66,7 @@ export type DriftClientSubscriptionConfig =
|
|
|
63
66
|
resubTimeoutMs?: number;
|
|
64
67
|
logResubMessages?: boolean;
|
|
65
68
|
commitment?: Commitment;
|
|
69
|
+
programUserAccountSubscriber?: WebSocketProgramAccountSubscriber<UserAccount>;
|
|
66
70
|
perpMarketAccountSubscriber?: new (
|
|
67
71
|
accountName: string,
|
|
68
72
|
program: Program,
|
|
@@ -71,14 +75,17 @@ export type DriftClientSubscriptionConfig =
|
|
|
71
75
|
resubOpts?: ResubOpts,
|
|
72
76
|
commitment?: Commitment
|
|
73
77
|
) => WebSocketAccountSubscriberV2<any> | WebSocketAccountSubscriber<any>;
|
|
74
|
-
oracleAccountSubscriber
|
|
75
|
-
|
|
78
|
+
/** If you use V2 here, whatever you pass for perpMarketAccountSubscriber and oracleAccountSubscriber will be ignored and it will use v2 under the hood regardless */
|
|
79
|
+
driftClientAccountSubscriber?: new (
|
|
76
80
|
program: Program,
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
perpMarketIndexes: number[],
|
|
82
|
+
spotMarketIndexes: number[],
|
|
83
|
+
oracleInfos: OracleInfo[],
|
|
84
|
+
shouldFindAllMarketsAndOracles: boolean,
|
|
85
|
+
delistedMarketSetting: DelistedMarketSetting
|
|
86
|
+
) =>
|
|
87
|
+
| WebSocketDriftClientAccountSubscriber
|
|
88
|
+
| WebSocketDriftClientAccountSubscriberV2;
|
|
82
89
|
}
|
|
83
90
|
| {
|
|
84
91
|
type: 'polling';
|
package/src/factory/bigNum.ts
CHANGED
|
@@ -250,7 +250,8 @@ export class BigNum {
|
|
|
250
250
|
|
|
251
251
|
public prettyPrint(
|
|
252
252
|
useTradePrecision?: boolean,
|
|
253
|
-
precisionOverride?: number
|
|
253
|
+
precisionOverride?: number,
|
|
254
|
+
decimalOverride?: number
|
|
254
255
|
): string {
|
|
255
256
|
const [leftSide, rightSide] = this.printShort(
|
|
256
257
|
useTradePrecision,
|
|
@@ -258,6 +259,21 @@ export class BigNum {
|
|
|
258
259
|
).split(BigNum.delim);
|
|
259
260
|
|
|
260
261
|
let formattedLeftSide = leftSide;
|
|
262
|
+
let formattedRightSide = rightSide;
|
|
263
|
+
|
|
264
|
+
// Apply decimal override if specified
|
|
265
|
+
if (decimalOverride !== undefined) {
|
|
266
|
+
if (decimalOverride === 0) {
|
|
267
|
+
formattedRightSide = undefined;
|
|
268
|
+
} else {
|
|
269
|
+
// If no decimal part exists, create one with zeros
|
|
270
|
+
const currentRightSide = rightSide || '';
|
|
271
|
+
// Pad with zeros if needed or truncate if too long
|
|
272
|
+
formattedRightSide = currentRightSide
|
|
273
|
+
.padEnd(decimalOverride, '0')
|
|
274
|
+
.substring(0, decimalOverride);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
261
277
|
|
|
262
278
|
const isNeg = formattedLeftSide.includes('-');
|
|
263
279
|
if (isNeg) {
|
|
@@ -277,7 +293,7 @@ export class BigNum {
|
|
|
277
293
|
}
|
|
278
294
|
|
|
279
295
|
return `${isNeg ? '-' : ''}${formattedLeftSide}${
|
|
280
|
-
|
|
296
|
+
formattedRightSide ? `${BigNum.delim}${formattedRightSide}` : ''
|
|
281
297
|
}`;
|
|
282
298
|
}
|
|
283
299
|
|
|
@@ -503,15 +519,16 @@ export class BigNum {
|
|
|
503
519
|
*/
|
|
504
520
|
public toNotional(
|
|
505
521
|
useTradePrecision?: boolean,
|
|
506
|
-
precisionOverride?: number
|
|
522
|
+
precisionOverride?: number,
|
|
523
|
+
decimalOverride?: number
|
|
507
524
|
): string {
|
|
508
525
|
const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
|
|
509
526
|
|
|
510
527
|
const usingCustomPrecision =
|
|
511
|
-
true && (useTradePrecision || precisionOverride);
|
|
528
|
+
true && (useTradePrecision || precisionOverride || decimalOverride);
|
|
512
529
|
|
|
513
530
|
let val = usingCustomPrecision
|
|
514
|
-
? this.prettyPrint(useTradePrecision, precisionOverride)
|
|
531
|
+
? this.prettyPrint(useTradePrecision, precisionOverride, decimalOverride)
|
|
515
532
|
: BigNum.fromPrint(this.toFixed(2), new BN(2)).prettyPrint();
|
|
516
533
|
|
|
517
534
|
// Append trailing zeroes out to 2 decimal places if not using custom precision
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,10 @@ export * from './accounts/webSocketDriftClientAccountSubscriber';
|
|
|
12
12
|
export * from './accounts/webSocketInsuranceFundStakeAccountSubscriber';
|
|
13
13
|
export * from './accounts/webSocketHighLeverageModeConfigAccountSubscriber';
|
|
14
14
|
export { WebSocketAccountSubscriberV2 } from './accounts/webSocketAccountSubscriberV2';
|
|
15
|
+
export { WebSocketProgramAccountSubscriber } from './accounts/webSocketProgramAccountSubscriber';
|
|
16
|
+
export { WebSocketProgramUserAccountSubscriber } from './accounts/websocketProgramUserAccountSubscriber';
|
|
17
|
+
export { WebSocketProgramAccountsSubscriberV2 } from './accounts/webSocketProgramAccountsSubscriberV2';
|
|
18
|
+
export { WebSocketDriftClientAccountSubscriberV2 } from './accounts/webSocketDriftClientAccountSubscriberV2';
|
|
15
19
|
export * from './accounts/bulkAccountLoader';
|
|
16
20
|
export * from './accounts/bulkUserSubscription';
|
|
17
21
|
export * from './accounts/bulkUserStatsSubscription';
|
package/src/memcmp.ts
CHANGED
|
@@ -112,3 +112,20 @@ export function getSignedMsgUserOrdersFilter(): MemcmpFilter {
|
|
|
112
112
|
},
|
|
113
113
|
};
|
|
114
114
|
}
|
|
115
|
+
|
|
116
|
+
export function getPerpMarketAccountsFilter(): MemcmpFilter {
|
|
117
|
+
return {
|
|
118
|
+
memcmp: {
|
|
119
|
+
offset: 0,
|
|
120
|
+
bytes: bs58.encode(BorshAccountsCoder.accountDiscriminator('PerpMarket')),
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
export function getSpotMarketAccountsFilter(): MemcmpFilter {
|
|
125
|
+
return {
|
|
126
|
+
memcmp: {
|
|
127
|
+
offset: 0,
|
|
128
|
+
bytes: bs58.encode(BorshAccountsCoder.accountDiscriminator('SpotMarket')),
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
package/src/oracles/oracleId.ts
CHANGED
|
@@ -24,9 +24,43 @@ export function getOracleSourceNum(source: OracleSource): number {
|
|
|
24
24
|
throw new Error('Invalid oracle source');
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
export function getOracleSourceFromNum(sourceNum: number): OracleSource {
|
|
28
|
+
if (sourceNum === OracleSourceNum.PYTH) return 'pyth';
|
|
29
|
+
if (sourceNum === OracleSourceNum.PYTH_1K) return 'pyth1K';
|
|
30
|
+
if (sourceNum === OracleSourceNum.PYTH_1M) return 'pyth1M';
|
|
31
|
+
if (sourceNum === OracleSourceNum.PYTH_PULL) return 'pythPull';
|
|
32
|
+
if (sourceNum === OracleSourceNum.PYTH_1K_PULL) return 'pyth1KPull';
|
|
33
|
+
if (sourceNum === OracleSourceNum.PYTH_1M_PULL) return 'pyth1MPull';
|
|
34
|
+
if (sourceNum === OracleSourceNum.SWITCHBOARD) return 'switchboard';
|
|
35
|
+
if (sourceNum === OracleSourceNum.QUOTE_ASSET) return 'quoteAsset';
|
|
36
|
+
if (sourceNum === OracleSourceNum.PYTH_STABLE_COIN) return 'pythStableCoin';
|
|
37
|
+
if (sourceNum === OracleSourceNum.PYTH_STABLE_COIN_PULL)
|
|
38
|
+
return 'pythStableCoinPull';
|
|
39
|
+
if (sourceNum === OracleSourceNum.PRELAUNCH) return 'prelaunch';
|
|
40
|
+
if (sourceNum === OracleSourceNum.SWITCHBOARD_ON_DEMAND)
|
|
41
|
+
return 'switchboardOnDemand';
|
|
42
|
+
if (sourceNum === OracleSourceNum.PYTH_LAZER) return 'pythLazer';
|
|
43
|
+
if (sourceNum === OracleSourceNum.PYTH_LAZER_1K) return 'pythLazer1K';
|
|
44
|
+
if (sourceNum === OracleSourceNum.PYTH_LAZER_1M) return 'pythLazer1M';
|
|
45
|
+
if (sourceNum === OracleSourceNum.PYTH_LAZER_STABLE_COIN)
|
|
46
|
+
return 'pythLazerStableCoin';
|
|
47
|
+
throw new Error('Invalid oracle source');
|
|
48
|
+
}
|
|
49
|
+
|
|
27
50
|
export function getOracleId(
|
|
28
51
|
publicKey: PublicKey,
|
|
29
52
|
source: OracleSource
|
|
30
53
|
): string {
|
|
31
54
|
return `${publicKey.toBase58()}-${getOracleSourceNum(source)}`;
|
|
32
55
|
}
|
|
56
|
+
|
|
57
|
+
export function getPublicKeyAndSourceFromOracleId(oracleId: string): {
|
|
58
|
+
publicKey: PublicKey;
|
|
59
|
+
source: OracleSource;
|
|
60
|
+
} {
|
|
61
|
+
const [publicKey, source] = oracleId.split('-');
|
|
62
|
+
return {
|
|
63
|
+
publicKey: new PublicKey(publicKey),
|
|
64
|
+
source: getOracleSourceFromNum(parseInt(source)),
|
|
65
|
+
};
|
|
66
|
+
}
|
package/src/user.ts
CHANGED
|
@@ -75,6 +75,7 @@ import {
|
|
|
75
75
|
} from './types';
|
|
76
76
|
import { standardizeBaseAssetAmount } from './math/orders';
|
|
77
77
|
import { UserStats } from './userStats';
|
|
78
|
+
import { WebSocketProgramUserAccountSubscriber } from './accounts/websocketProgramUserAccountSubscriber';
|
|
78
79
|
import {
|
|
79
80
|
calculateAssetWeight,
|
|
80
81
|
calculateLiabilityWeight,
|
|
@@ -149,15 +150,26 @@ export class User {
|
|
|
149
150
|
}
|
|
150
151
|
);
|
|
151
152
|
} else {
|
|
152
|
-
|
|
153
|
-
config.
|
|
154
|
-
config.
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
153
|
+
if (
|
|
154
|
+
config.accountSubscription?.type === 'websocket' &&
|
|
155
|
+
config.accountSubscription?.programUserAccountSubscriber
|
|
156
|
+
) {
|
|
157
|
+
this.accountSubscriber = new WebSocketProgramUserAccountSubscriber(
|
|
158
|
+
config.driftClient.program,
|
|
159
|
+
config.userAccountPublicKey,
|
|
160
|
+
config.accountSubscription.programUserAccountSubscriber
|
|
161
|
+
);
|
|
162
|
+
} else {
|
|
163
|
+
this.accountSubscriber = new WebSocketUserAccountSubscriber(
|
|
164
|
+
config.driftClient.program,
|
|
165
|
+
config.userAccountPublicKey,
|
|
166
|
+
{
|
|
167
|
+
resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
|
|
168
|
+
logResubMessages: config.accountSubscription?.logResubMessages,
|
|
169
|
+
},
|
|
170
|
+
config.accountSubscription?.commitment
|
|
171
|
+
);
|
|
172
|
+
}
|
|
161
173
|
}
|
|
162
174
|
this.eventEmitter = this.accountSubscriber.eventEmitter;
|
|
163
175
|
}
|
package/src/userConfig.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { DriftClient } from './driftClient';
|
|
|
2
2
|
import { Commitment, PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
4
|
import { GrpcConfigs, UserAccountSubscriber } from './accounts/types';
|
|
5
|
+
import { WebSocketProgramAccountSubscriber } from './accounts/webSocketProgramAccountSubscriber';
|
|
6
|
+
import { UserAccount } from './types';
|
|
5
7
|
|
|
6
8
|
export type UserConfig = {
|
|
7
9
|
accountSubscription?: UserSubscriptionConfig;
|
|
@@ -21,6 +23,7 @@ export type UserSubscriptionConfig =
|
|
|
21
23
|
resubTimeoutMs?: number;
|
|
22
24
|
logResubMessages?: boolean;
|
|
23
25
|
commitment?: Commitment;
|
|
26
|
+
programUserAccountSubscriber?: WebSocketProgramAccountSubscriber<UserAccount>;
|
|
24
27
|
}
|
|
25
28
|
| {
|
|
26
29
|
type: 'polling';
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import { BufferAndSlot, ProgramAccountSubscriber, ResubOpts } from './types';
|
|
4
|
-
import { Program } from '@coral-xyz/anchor';
|
|
5
|
-
import { Commitment, Context, MemcmpFilter, PublicKey } from '@solana/web3.js';
|
|
6
|
-
import { AccountInfoBase, AccountInfoWithBase58EncodedData, AccountInfoWithBase64EncodedData } from 'gill';
|
|
7
|
-
export declare class WebSocketProgramAccountSubscriberV2<T> implements ProgramAccountSubscriber<T> {
|
|
8
|
-
subscriptionName: string;
|
|
9
|
-
accountDiscriminator: string;
|
|
10
|
-
bufferAndSlot?: BufferAndSlot;
|
|
11
|
-
bufferAndSlotMap: Map<string, BufferAndSlot>;
|
|
12
|
-
program: Program;
|
|
13
|
-
decodeBuffer: (accountName: string, ix: Buffer) => T;
|
|
14
|
-
onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void;
|
|
15
|
-
listenerId?: number;
|
|
16
|
-
resubOpts?: ResubOpts;
|
|
17
|
-
isUnsubscribing: boolean;
|
|
18
|
-
timeoutId?: ReturnType<typeof setTimeout>;
|
|
19
|
-
options: {
|
|
20
|
-
filters: MemcmpFilter[];
|
|
21
|
-
commitment?: Commitment;
|
|
22
|
-
};
|
|
23
|
-
receivingData: boolean;
|
|
24
|
-
private rpc;
|
|
25
|
-
private rpcSubscriptions;
|
|
26
|
-
private abortController?;
|
|
27
|
-
private accountsToMonitor;
|
|
28
|
-
private pollingIntervalMs;
|
|
29
|
-
private pollingTimeouts;
|
|
30
|
-
private lastWsNotificationTime;
|
|
31
|
-
private accountsCurrentlyPolling;
|
|
32
|
-
private batchPollingTimeout?;
|
|
33
|
-
constructor(subscriptionName: string, accountDiscriminator: string, program: Program, decodeBufferFn: (accountName: string, ix: Buffer) => T, options?: {
|
|
34
|
-
filters: MemcmpFilter[];
|
|
35
|
-
commitment?: Commitment;
|
|
36
|
-
}, resubOpts?: ResubOpts, accountsToMonitor?: PublicKey[]);
|
|
37
|
-
subscribe(onChange: (accountId: PublicKey, data: T, context: Context, buffer: Buffer) => void): Promise<void>;
|
|
38
|
-
protected setTimeout(): void;
|
|
39
|
-
handleRpcResponse(context: {
|
|
40
|
-
slot: bigint;
|
|
41
|
-
}, accountInfo?: AccountInfoBase & (AccountInfoWithBase58EncodedData | AccountInfoWithBase64EncodedData)): void;
|
|
42
|
-
private startMonitoringForAccounts;
|
|
43
|
-
private startMonitoringForAccount;
|
|
44
|
-
private startPollingForAccount;
|
|
45
|
-
private startBatchPolling;
|
|
46
|
-
private pollAllAccounts;
|
|
47
|
-
private pollAccount;
|
|
48
|
-
private clearPollingTimeouts;
|
|
49
|
-
unsubscribe(onResub?: boolean): Promise<void>;
|
|
50
|
-
addAccountToMonitor(accountId: PublicKey): void;
|
|
51
|
-
removeAccountFromMonitor(accountId: PublicKey): void;
|
|
52
|
-
setPollingInterval(intervalMs: number): void;
|
|
53
|
-
}
|