@drift-labs/sdk 2.114.0-beta.0 → 2.114.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/orderSubscriber/OrderSubscriber.d.ts +4 -1
- package/lib/browser/orderSubscriber/OrderSubscriber.js +17 -1
- package/lib/browser/orderSubscriber/types.d.ts +1 -0
- package/lib/browser/swift/swiftOrderSubscriber.d.ts +6 -3
- package/lib/browser/swift/swiftOrderSubscriber.js +5 -2
- package/lib/browser/types.d.ts +3 -0
- package/lib/browser/types.js +3 -0
- package/lib/browser/userMap/userMap.d.ts +1 -0
- package/lib/browser/userMap/userMap.js +4 -0
- package/lib/node/orderSubscriber/OrderSubscriber.d.ts +4 -1
- package/lib/node/orderSubscriber/OrderSubscriber.js +17 -1
- package/lib/node/orderSubscriber/types.d.ts +1 -0
- package/lib/node/swift/swiftOrderSubscriber.d.ts +6 -3
- package/lib/node/swift/swiftOrderSubscriber.js +5 -2
- package/lib/node/types.d.ts +3 -0
- package/lib/node/types.js +3 -0
- package/lib/node/userMap/userMap.d.ts +1 -0
- package/lib/node/userMap/userMap.js +4 -0
- package/package.json +1 -1
- package/src/orderSubscriber/OrderSubscriber.ts +36 -2
- package/src/orderSubscriber/types.ts +1 -0
- package/src/swift/swiftOrderSubscriber.ts +16 -7
- package/src/types.ts +3 -0
- package/src/userMap/userMap.ts +5 -0
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.114.0-beta.
|
|
1
|
+
2.114.0-beta.2
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
import { DriftClient } from '../driftClient';
|
|
5
5
|
import { UserAccount } from '../types';
|
|
6
|
-
import { Commitment } from '@solana/web3.js';
|
|
6
|
+
import { Commitment, PublicKey } from '@solana/web3.js';
|
|
7
7
|
import { Buffer } from 'buffer';
|
|
8
8
|
import { DLOB } from '../dlob/DLOB';
|
|
9
9
|
import { OrderSubscriberConfig, OrderSubscriberEvents } from './types';
|
|
@@ -26,6 +26,7 @@ export declare class OrderSubscriber {
|
|
|
26
26
|
mostRecentSlot: number;
|
|
27
27
|
decodeFn: (name: string, data: Buffer) => UserAccount;
|
|
28
28
|
decodeData?: boolean;
|
|
29
|
+
fetchAllNonIdleUsers?: boolean;
|
|
29
30
|
constructor(config: OrderSubscriberConfig);
|
|
30
31
|
subscribe(): Promise<void>;
|
|
31
32
|
fetch(): Promise<void>;
|
|
@@ -38,5 +39,7 @@ export declare class OrderSubscriber {
|
|
|
38
39
|
protected createDLOB(protectedMakerView?: boolean): DLOB;
|
|
39
40
|
getDLOB(slot: number, protectedMakerView?: boolean): Promise<DLOB>;
|
|
40
41
|
getSlot(): number;
|
|
42
|
+
addPubkey(userAccountPublicKey: PublicKey): Promise<void>;
|
|
43
|
+
mustGetUserAccount(key: string): Promise<UserAccount>;
|
|
41
44
|
unsubscribe(): Promise<void>;
|
|
42
45
|
}
|
|
@@ -58,6 +58,7 @@ class OrderSubscriber {
|
|
|
58
58
|
this.driftClient.program.account.user.coder.accounts.decodeUnchecked.bind(this.driftClient.program.account.user.coder.accounts);
|
|
59
59
|
}
|
|
60
60
|
this.eventEmitter = new events_1.EventEmitter();
|
|
61
|
+
this.fetchAllNonIdleUsers = config.fetchAllNonIdleUsers;
|
|
61
62
|
}
|
|
62
63
|
async subscribe() {
|
|
63
64
|
await this.subscription.subscribe();
|
|
@@ -69,12 +70,15 @@ class OrderSubscriber {
|
|
|
69
70
|
this.fetchPromise = new Promise((resolver) => {
|
|
70
71
|
this.fetchPromiseResolver = resolver;
|
|
71
72
|
});
|
|
73
|
+
const filters = this.fetchAllNonIdleUsers
|
|
74
|
+
? [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getNonIdleUserFilter)()]
|
|
75
|
+
: [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getUserWithOrderFilter)()];
|
|
72
76
|
try {
|
|
73
77
|
const rpcRequestArgs = [
|
|
74
78
|
this.driftClient.program.programId.toBase58(),
|
|
75
79
|
{
|
|
76
80
|
commitment: this.commitment,
|
|
77
|
-
filters
|
|
81
|
+
filters,
|
|
78
82
|
encoding: 'base64',
|
|
79
83
|
withContext: true,
|
|
80
84
|
},
|
|
@@ -164,6 +168,18 @@ class OrderSubscriber {
|
|
|
164
168
|
var _a;
|
|
165
169
|
return (_a = this.mostRecentSlot) !== null && _a !== void 0 ? _a : 0;
|
|
166
170
|
}
|
|
171
|
+
async addPubkey(userAccountPublicKey) {
|
|
172
|
+
const accountInfo = await this.driftClient.connection.getAccountInfoAndContext(userAccountPublicKey, this.commitment);
|
|
173
|
+
if (accountInfo) {
|
|
174
|
+
this.tryUpdateUserAccount(userAccountPublicKey.toString(), 'buffer', accountInfo.value.data, accountInfo.context.slot);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async mustGetUserAccount(key) {
|
|
178
|
+
if (!this.usersAccounts.has(key)) {
|
|
179
|
+
await this.addPubkey(new web3_js_1.PublicKey(key));
|
|
180
|
+
}
|
|
181
|
+
return this.usersAccounts.get(key).userAccount;
|
|
182
|
+
}
|
|
167
183
|
async unsubscribe() {
|
|
168
184
|
this.usersAccounts.clear();
|
|
169
185
|
await this.subscription.unsubscribe();
|
|
@@ -26,6 +26,7 @@ export type OrderSubscriberConfig = {
|
|
|
26
26
|
};
|
|
27
27
|
fastDecode?: boolean;
|
|
28
28
|
decodeData?: boolean;
|
|
29
|
+
fetchAllNonIdleUsers?: boolean;
|
|
29
30
|
};
|
|
30
31
|
export interface OrderSubscriberEvents {
|
|
31
32
|
orderCreated: (account: UserAccount, updatedOrders: Order[], pubkey: PublicKey, slot: number, dataType: 'raw' | 'decoded' | 'buffer') => void;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { DriftClient, DriftEnv, OptionalOrderParams, SignedMsgOrderParamsMessage,
|
|
1
|
+
import { DriftClient, DriftEnv, OptionalOrderParams, SignedMsgOrderParamsMessage, UserAccount } from '..';
|
|
2
2
|
import { Keypair, TransactionInstruction } from '@solana/web3.js';
|
|
3
|
+
export interface AccountGetter {
|
|
4
|
+
mustGetUserAccount(publicKey: string): Promise<UserAccount>;
|
|
5
|
+
}
|
|
3
6
|
export type SwiftOrderSubscriberConfig = {
|
|
4
7
|
driftClient: DriftClient;
|
|
5
|
-
|
|
8
|
+
userAccountGetter?: AccountGetter;
|
|
6
9
|
driftEnv: DriftEnv;
|
|
7
10
|
endpoint?: string;
|
|
8
11
|
marketIndexes: number[];
|
|
@@ -19,7 +22,7 @@ export declare class SwiftOrderSubscriber {
|
|
|
19
22
|
private readonly heartbeatIntervalMs;
|
|
20
23
|
private ws;
|
|
21
24
|
private driftClient;
|
|
22
|
-
|
|
25
|
+
userAccountGetter?: AccountGetter;
|
|
23
26
|
onOrder: (orderMessageRaw: any, signedMsgOrderParamsMessage: SignedMsgOrderParamsMessage) => Promise<void>;
|
|
24
27
|
subscribed: boolean;
|
|
25
28
|
constructor(config: SwiftOrderSubscriberConfig);
|
|
@@ -17,7 +17,7 @@ class SwiftOrderSubscriber {
|
|
|
17
17
|
this.ws = null;
|
|
18
18
|
this.subscribed = false;
|
|
19
19
|
this.driftClient = config.driftClient;
|
|
20
|
-
this.
|
|
20
|
+
this.userAccountGetter = config.userAccountGetter;
|
|
21
21
|
}
|
|
22
22
|
getSymbolForMarketIndex(marketIndex) {
|
|
23
23
|
const markets = this.config.driftEnv === 'devnet'
|
|
@@ -91,11 +91,14 @@ class SwiftOrderSubscriber {
|
|
|
91
91
|
});
|
|
92
92
|
}
|
|
93
93
|
async getPlaceAndMakeSignedMsgOrderIxs(orderMessageRaw, signedMsgOrderParamsMessage, makerOrderParams) {
|
|
94
|
+
if (!this.userAccountGetter) {
|
|
95
|
+
throw new Error('userAccountGetter must be set to use this function');
|
|
96
|
+
}
|
|
94
97
|
const signedMsgOrderParamsBuf = Buffer.from(orderMessageRaw['order_message'], 'hex');
|
|
95
98
|
const takerAuthority = new web3_js_1.PublicKey(orderMessageRaw['taker_authority']);
|
|
96
99
|
const signingAuthority = new web3_js_1.PublicKey(orderMessageRaw['signing_authority']);
|
|
97
100
|
const takerUserPubkey = await (0, __1.getUserAccountPublicKey)(this.driftClient.program.programId, takerAuthority, signedMsgOrderParamsMessage.subAccountId);
|
|
98
|
-
const takerUserAccount =
|
|
101
|
+
const takerUserAccount = await this.userAccountGetter.mustGetUserAccount(takerUserPubkey.toString());
|
|
99
102
|
const ixs = await this.driftClient.getPlaceAndMakeSignedMsgPerpOrderIxs({
|
|
100
103
|
orderParams: signedMsgOrderParamsBuf,
|
|
101
104
|
signature: Buffer.from(orderMessageRaw['order_signature'], 'base64'),
|
package/lib/browser/types.d.ts
CHANGED
|
@@ -346,6 +346,9 @@ export declare class OrderActionExplanation {
|
|
|
346
346
|
static readonly DERISK_LP: {
|
|
347
347
|
deriskLp: {};
|
|
348
348
|
};
|
|
349
|
+
static readonly TRANSFER_PERP_POSITION: {
|
|
350
|
+
transferPerpPosition: {};
|
|
351
|
+
};
|
|
349
352
|
}
|
|
350
353
|
export declare class OrderTriggerCondition {
|
|
351
354
|
static readonly ABOVE: {
|
package/lib/browser/types.js
CHANGED
|
@@ -232,6 +232,9 @@ OrderActionExplanation.REDUCE_ONLY_ORDER_INCREASED_POSITION = {
|
|
|
232
232
|
OrderActionExplanation.DERISK_LP = {
|
|
233
233
|
deriskLp: {},
|
|
234
234
|
};
|
|
235
|
+
OrderActionExplanation.TRANSFER_PERP_POSITION = {
|
|
236
|
+
transferPerpPosition: {},
|
|
237
|
+
};
|
|
235
238
|
class OrderTriggerCondition {
|
|
236
239
|
}
|
|
237
240
|
exports.OrderTriggerCondition = OrderTriggerCondition;
|
|
@@ -55,6 +55,7 @@ export declare class UserMap implements UserMapInterface {
|
|
|
55
55
|
*/
|
|
56
56
|
mustGet(key: string, accountSubscription?: UserSubscriptionConfig): Promise<User>;
|
|
57
57
|
mustGetWithSlot(key: string, accountSubscription?: UserSubscriptionConfig): Promise<DataAndSlot<User>>;
|
|
58
|
+
mustGetUserAccount(key: string): Promise<UserAccount>;
|
|
58
59
|
/**
|
|
59
60
|
* gets the Authority for a particular userAccountPublicKey, if no User exists, undefined is returned
|
|
60
61
|
* @param key userAccountPublicKey to get User for
|
|
@@ -148,6 +148,10 @@ class UserMap {
|
|
|
148
148
|
}
|
|
149
149
|
return this.userMap.get(key);
|
|
150
150
|
}
|
|
151
|
+
async mustGetUserAccount(key) {
|
|
152
|
+
const user = await this.mustGet(key);
|
|
153
|
+
return user.getUserAccount();
|
|
154
|
+
}
|
|
151
155
|
/**
|
|
152
156
|
* gets the Authority for a particular userAccountPublicKey, if no User exists, undefined is returned
|
|
153
157
|
* @param key userAccountPublicKey to get User for
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
import { DriftClient } from '../driftClient';
|
|
5
5
|
import { UserAccount } from '../types';
|
|
6
|
-
import { Commitment } from '@solana/web3.js';
|
|
6
|
+
import { Commitment, PublicKey } from '@solana/web3.js';
|
|
7
7
|
import { Buffer } from 'buffer';
|
|
8
8
|
import { DLOB } from '../dlob/DLOB';
|
|
9
9
|
import { OrderSubscriberConfig, OrderSubscriberEvents } from './types';
|
|
@@ -26,6 +26,7 @@ export declare class OrderSubscriber {
|
|
|
26
26
|
mostRecentSlot: number;
|
|
27
27
|
decodeFn: (name: string, data: Buffer) => UserAccount;
|
|
28
28
|
decodeData?: boolean;
|
|
29
|
+
fetchAllNonIdleUsers?: boolean;
|
|
29
30
|
constructor(config: OrderSubscriberConfig);
|
|
30
31
|
subscribe(): Promise<void>;
|
|
31
32
|
fetch(): Promise<void>;
|
|
@@ -38,5 +39,7 @@ export declare class OrderSubscriber {
|
|
|
38
39
|
protected createDLOB(protectedMakerView?: boolean): DLOB;
|
|
39
40
|
getDLOB(slot: number, protectedMakerView?: boolean): Promise<DLOB>;
|
|
40
41
|
getSlot(): number;
|
|
42
|
+
addPubkey(userAccountPublicKey: PublicKey): Promise<void>;
|
|
43
|
+
mustGetUserAccount(key: string): Promise<UserAccount>;
|
|
41
44
|
unsubscribe(): Promise<void>;
|
|
42
45
|
}
|
|
@@ -58,6 +58,7 @@ class OrderSubscriber {
|
|
|
58
58
|
this.driftClient.program.account.user.coder.accounts.decodeUnchecked.bind(this.driftClient.program.account.user.coder.accounts);
|
|
59
59
|
}
|
|
60
60
|
this.eventEmitter = new events_1.EventEmitter();
|
|
61
|
+
this.fetchAllNonIdleUsers = config.fetchAllNonIdleUsers;
|
|
61
62
|
}
|
|
62
63
|
async subscribe() {
|
|
63
64
|
await this.subscription.subscribe();
|
|
@@ -69,12 +70,15 @@ class OrderSubscriber {
|
|
|
69
70
|
this.fetchPromise = new Promise((resolver) => {
|
|
70
71
|
this.fetchPromiseResolver = resolver;
|
|
71
72
|
});
|
|
73
|
+
const filters = this.fetchAllNonIdleUsers
|
|
74
|
+
? [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getNonIdleUserFilter)()]
|
|
75
|
+
: [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getUserWithOrderFilter)()];
|
|
72
76
|
try {
|
|
73
77
|
const rpcRequestArgs = [
|
|
74
78
|
this.driftClient.program.programId.toBase58(),
|
|
75
79
|
{
|
|
76
80
|
commitment: this.commitment,
|
|
77
|
-
filters
|
|
81
|
+
filters,
|
|
78
82
|
encoding: 'base64',
|
|
79
83
|
withContext: true,
|
|
80
84
|
},
|
|
@@ -164,6 +168,18 @@ class OrderSubscriber {
|
|
|
164
168
|
var _a;
|
|
165
169
|
return (_a = this.mostRecentSlot) !== null && _a !== void 0 ? _a : 0;
|
|
166
170
|
}
|
|
171
|
+
async addPubkey(userAccountPublicKey) {
|
|
172
|
+
const accountInfo = await this.driftClient.connection.getAccountInfoAndContext(userAccountPublicKey, this.commitment);
|
|
173
|
+
if (accountInfo) {
|
|
174
|
+
this.tryUpdateUserAccount(userAccountPublicKey.toString(), 'buffer', accountInfo.value.data, accountInfo.context.slot);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async mustGetUserAccount(key) {
|
|
178
|
+
if (!this.usersAccounts.has(key)) {
|
|
179
|
+
await this.addPubkey(new web3_js_1.PublicKey(key));
|
|
180
|
+
}
|
|
181
|
+
return this.usersAccounts.get(key).userAccount;
|
|
182
|
+
}
|
|
167
183
|
async unsubscribe() {
|
|
168
184
|
this.usersAccounts.clear();
|
|
169
185
|
await this.subscription.unsubscribe();
|
|
@@ -26,6 +26,7 @@ export type OrderSubscriberConfig = {
|
|
|
26
26
|
};
|
|
27
27
|
fastDecode?: boolean;
|
|
28
28
|
decodeData?: boolean;
|
|
29
|
+
fetchAllNonIdleUsers?: boolean;
|
|
29
30
|
};
|
|
30
31
|
export interface OrderSubscriberEvents {
|
|
31
32
|
orderCreated: (account: UserAccount, updatedOrders: Order[], pubkey: PublicKey, slot: number, dataType: 'raw' | 'decoded' | 'buffer') => void;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { DriftClient, DriftEnv, OptionalOrderParams, SignedMsgOrderParamsMessage,
|
|
1
|
+
import { DriftClient, DriftEnv, OptionalOrderParams, SignedMsgOrderParamsMessage, UserAccount } from '..';
|
|
2
2
|
import { Keypair, TransactionInstruction } from '@solana/web3.js';
|
|
3
|
+
export interface AccountGetter {
|
|
4
|
+
mustGetUserAccount(publicKey: string): Promise<UserAccount>;
|
|
5
|
+
}
|
|
3
6
|
export type SwiftOrderSubscriberConfig = {
|
|
4
7
|
driftClient: DriftClient;
|
|
5
|
-
|
|
8
|
+
userAccountGetter?: AccountGetter;
|
|
6
9
|
driftEnv: DriftEnv;
|
|
7
10
|
endpoint?: string;
|
|
8
11
|
marketIndexes: number[];
|
|
@@ -19,7 +22,7 @@ export declare class SwiftOrderSubscriber {
|
|
|
19
22
|
private readonly heartbeatIntervalMs;
|
|
20
23
|
private ws;
|
|
21
24
|
private driftClient;
|
|
22
|
-
|
|
25
|
+
userAccountGetter?: AccountGetter;
|
|
23
26
|
onOrder: (orderMessageRaw: any, signedMsgOrderParamsMessage: SignedMsgOrderParamsMessage) => Promise<void>;
|
|
24
27
|
subscribed: boolean;
|
|
25
28
|
constructor(config: SwiftOrderSubscriberConfig);
|
|
@@ -17,7 +17,7 @@ class SwiftOrderSubscriber {
|
|
|
17
17
|
this.ws = null;
|
|
18
18
|
this.subscribed = false;
|
|
19
19
|
this.driftClient = config.driftClient;
|
|
20
|
-
this.
|
|
20
|
+
this.userAccountGetter = config.userAccountGetter;
|
|
21
21
|
}
|
|
22
22
|
getSymbolForMarketIndex(marketIndex) {
|
|
23
23
|
const markets = this.config.driftEnv === 'devnet'
|
|
@@ -91,11 +91,14 @@ class SwiftOrderSubscriber {
|
|
|
91
91
|
});
|
|
92
92
|
}
|
|
93
93
|
async getPlaceAndMakeSignedMsgOrderIxs(orderMessageRaw, signedMsgOrderParamsMessage, makerOrderParams) {
|
|
94
|
+
if (!this.userAccountGetter) {
|
|
95
|
+
throw new Error('userAccountGetter must be set to use this function');
|
|
96
|
+
}
|
|
94
97
|
const signedMsgOrderParamsBuf = Buffer.from(orderMessageRaw['order_message'], 'hex');
|
|
95
98
|
const takerAuthority = new web3_js_1.PublicKey(orderMessageRaw['taker_authority']);
|
|
96
99
|
const signingAuthority = new web3_js_1.PublicKey(orderMessageRaw['signing_authority']);
|
|
97
100
|
const takerUserPubkey = await (0, __1.getUserAccountPublicKey)(this.driftClient.program.programId, takerAuthority, signedMsgOrderParamsMessage.subAccountId);
|
|
98
|
-
const takerUserAccount =
|
|
101
|
+
const takerUserAccount = await this.userAccountGetter.mustGetUserAccount(takerUserPubkey.toString());
|
|
99
102
|
const ixs = await this.driftClient.getPlaceAndMakeSignedMsgPerpOrderIxs({
|
|
100
103
|
orderParams: signedMsgOrderParamsBuf,
|
|
101
104
|
signature: Buffer.from(orderMessageRaw['order_signature'], 'base64'),
|
package/lib/node/types.d.ts
CHANGED
|
@@ -346,6 +346,9 @@ export declare class OrderActionExplanation {
|
|
|
346
346
|
static readonly DERISK_LP: {
|
|
347
347
|
deriskLp: {};
|
|
348
348
|
};
|
|
349
|
+
static readonly TRANSFER_PERP_POSITION: {
|
|
350
|
+
transferPerpPosition: {};
|
|
351
|
+
};
|
|
349
352
|
}
|
|
350
353
|
export declare class OrderTriggerCondition {
|
|
351
354
|
static readonly ABOVE: {
|
package/lib/node/types.js
CHANGED
|
@@ -232,6 +232,9 @@ OrderActionExplanation.REDUCE_ONLY_ORDER_INCREASED_POSITION = {
|
|
|
232
232
|
OrderActionExplanation.DERISK_LP = {
|
|
233
233
|
deriskLp: {},
|
|
234
234
|
};
|
|
235
|
+
OrderActionExplanation.TRANSFER_PERP_POSITION = {
|
|
236
|
+
transferPerpPosition: {},
|
|
237
|
+
};
|
|
235
238
|
class OrderTriggerCondition {
|
|
236
239
|
}
|
|
237
240
|
exports.OrderTriggerCondition = OrderTriggerCondition;
|
|
@@ -55,6 +55,7 @@ export declare class UserMap implements UserMapInterface {
|
|
|
55
55
|
*/
|
|
56
56
|
mustGet(key: string, accountSubscription?: UserSubscriptionConfig): Promise<User>;
|
|
57
57
|
mustGetWithSlot(key: string, accountSubscription?: UserSubscriptionConfig): Promise<DataAndSlot<User>>;
|
|
58
|
+
mustGetUserAccount(key: string): Promise<UserAccount>;
|
|
58
59
|
/**
|
|
59
60
|
* gets the Authority for a particular userAccountPublicKey, if no User exists, undefined is returned
|
|
60
61
|
* @param key userAccountPublicKey to get User for
|
|
@@ -148,6 +148,10 @@ class UserMap {
|
|
|
148
148
|
}
|
|
149
149
|
return this.userMap.get(key);
|
|
150
150
|
}
|
|
151
|
+
async mustGetUserAccount(key) {
|
|
152
|
+
const user = await this.mustGet(key);
|
|
153
|
+
return user.getUserAccount();
|
|
154
|
+
}
|
|
151
155
|
/**
|
|
152
156
|
* gets the Authority for a particular userAccountPublicKey, if no User exists, undefined is returned
|
|
153
157
|
* @param key userAccountPublicKey to get User for
|
package/package.json
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { DriftClient } from '../driftClient';
|
|
2
2
|
import { UserAccount } from '../types';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
getNonIdleUserFilter,
|
|
5
|
+
getUserFilter,
|
|
6
|
+
getUserWithOrderFilter,
|
|
7
|
+
} from '../memcmp';
|
|
4
8
|
import { Commitment, PublicKey, RpcResponseAndContext } from '@solana/web3.js';
|
|
5
9
|
import { Buffer } from 'buffer';
|
|
6
10
|
import { DLOB } from '../dlob/DLOB';
|
|
@@ -28,6 +32,8 @@ export class OrderSubscriber {
|
|
|
28
32
|
decodeFn: (name: string, data: Buffer) => UserAccount;
|
|
29
33
|
decodeData?: boolean;
|
|
30
34
|
|
|
35
|
+
fetchAllNonIdleUsers?: boolean;
|
|
36
|
+
|
|
31
37
|
constructor(config: OrderSubscriberConfig) {
|
|
32
38
|
this.driftClient = config.driftClient;
|
|
33
39
|
this.commitment = config.subscriptionConfig.commitment || 'processed';
|
|
@@ -70,6 +76,7 @@ export class OrderSubscriber {
|
|
|
70
76
|
);
|
|
71
77
|
}
|
|
72
78
|
this.eventEmitter = new EventEmitter();
|
|
79
|
+
this.fetchAllNonIdleUsers = config.fetchAllNonIdleUsers;
|
|
73
80
|
}
|
|
74
81
|
|
|
75
82
|
public async subscribe(): Promise<void> {
|
|
@@ -85,12 +92,16 @@ export class OrderSubscriber {
|
|
|
85
92
|
this.fetchPromiseResolver = resolver;
|
|
86
93
|
});
|
|
87
94
|
|
|
95
|
+
const filters = this.fetchAllNonIdleUsers
|
|
96
|
+
? [getUserFilter(), getNonIdleUserFilter()]
|
|
97
|
+
: [getUserFilter(), getUserWithOrderFilter()];
|
|
98
|
+
|
|
88
99
|
try {
|
|
89
100
|
const rpcRequestArgs = [
|
|
90
101
|
this.driftClient.program.programId.toBase58(),
|
|
91
102
|
{
|
|
92
103
|
commitment: this.commitment,
|
|
93
|
-
filters
|
|
104
|
+
filters,
|
|
94
105
|
encoding: 'base64',
|
|
95
106
|
withContext: true,
|
|
96
107
|
},
|
|
@@ -245,6 +256,29 @@ export class OrderSubscriber {
|
|
|
245
256
|
return this.mostRecentSlot ?? 0;
|
|
246
257
|
}
|
|
247
258
|
|
|
259
|
+
public async addPubkey(userAccountPublicKey: PublicKey): Promise<void> {
|
|
260
|
+
const accountInfo =
|
|
261
|
+
await this.driftClient.connection.getAccountInfoAndContext(
|
|
262
|
+
userAccountPublicKey,
|
|
263
|
+
this.commitment
|
|
264
|
+
);
|
|
265
|
+
if (accountInfo) {
|
|
266
|
+
this.tryUpdateUserAccount(
|
|
267
|
+
userAccountPublicKey.toString(),
|
|
268
|
+
'buffer',
|
|
269
|
+
accountInfo.value.data,
|
|
270
|
+
accountInfo.context.slot
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
public async mustGetUserAccount(key: string): Promise<UserAccount> {
|
|
276
|
+
if (!this.usersAccounts.has(key)) {
|
|
277
|
+
await this.addPubkey(new PublicKey(key));
|
|
278
|
+
}
|
|
279
|
+
return this.usersAccounts.get(key).userAccount;
|
|
280
|
+
}
|
|
281
|
+
|
|
248
282
|
public async unsubscribe(): Promise<void> {
|
|
249
283
|
this.usersAccounts.clear();
|
|
250
284
|
await this.subscription.unsubscribe();
|
|
@@ -9,16 +9,21 @@ import {
|
|
|
9
9
|
OptionalOrderParams,
|
|
10
10
|
PostOnlyParams,
|
|
11
11
|
SignedMsgOrderParamsMessage,
|
|
12
|
-
|
|
12
|
+
UserAccount,
|
|
13
13
|
} from '..';
|
|
14
14
|
import { Keypair, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
15
15
|
import nacl from 'tweetnacl';
|
|
16
16
|
import { decodeUTF8 } from 'tweetnacl-util';
|
|
17
17
|
import WebSocket from 'ws';
|
|
18
18
|
|
|
19
|
+
// In practice, this for now is just an OrderSubscriber or a UserMap
|
|
20
|
+
export interface AccountGetter {
|
|
21
|
+
mustGetUserAccount(publicKey: string): Promise<UserAccount>;
|
|
22
|
+
}
|
|
23
|
+
|
|
19
24
|
export type SwiftOrderSubscriberConfig = {
|
|
20
25
|
driftClient: DriftClient;
|
|
21
|
-
|
|
26
|
+
userAccountGetter?: AccountGetter;
|
|
22
27
|
driftEnv: DriftEnv;
|
|
23
28
|
endpoint?: string;
|
|
24
29
|
marketIndexes: number[];
|
|
@@ -35,7 +40,7 @@ export class SwiftOrderSubscriber {
|
|
|
35
40
|
private readonly heartbeatIntervalMs = 60000;
|
|
36
41
|
private ws: WebSocket | null = null;
|
|
37
42
|
private driftClient: DriftClient;
|
|
38
|
-
public
|
|
43
|
+
public userAccountGetter?: AccountGetter; // In practice, this for now is just an OrderSubscriber or a UserMap
|
|
39
44
|
public onOrder: (
|
|
40
45
|
orderMessageRaw: any,
|
|
41
46
|
signedMsgOrderParamsMessage: SignedMsgOrderParamsMessage
|
|
@@ -45,7 +50,7 @@ export class SwiftOrderSubscriber {
|
|
|
45
50
|
|
|
46
51
|
constructor(private config: SwiftOrderSubscriberConfig) {
|
|
47
52
|
this.driftClient = config.driftClient;
|
|
48
|
-
this.
|
|
53
|
+
this.userAccountGetter = config.userAccountGetter;
|
|
49
54
|
}
|
|
50
55
|
|
|
51
56
|
getSymbolForMarketIndex(marketIndex: number): string {
|
|
@@ -163,6 +168,10 @@ export class SwiftOrderSubscriber {
|
|
|
163
168
|
signedMsgOrderParamsMessage: SignedMsgOrderParamsMessage,
|
|
164
169
|
makerOrderParams: OptionalOrderParams
|
|
165
170
|
): Promise<TransactionInstruction[]> {
|
|
171
|
+
if (!this.userAccountGetter) {
|
|
172
|
+
throw new Error('userAccountGetter must be set to use this function');
|
|
173
|
+
}
|
|
174
|
+
|
|
166
175
|
const signedMsgOrderParamsBuf = Buffer.from(
|
|
167
176
|
orderMessageRaw['order_message'],
|
|
168
177
|
'hex'
|
|
@@ -176,9 +185,9 @@ export class SwiftOrderSubscriber {
|
|
|
176
185
|
takerAuthority,
|
|
177
186
|
signedMsgOrderParamsMessage.subAccountId
|
|
178
187
|
);
|
|
179
|
-
const takerUserAccount = (
|
|
180
|
-
|
|
181
|
-
)
|
|
188
|
+
const takerUserAccount = await this.userAccountGetter.mustGetUserAccount(
|
|
189
|
+
takerUserPubkey.toString()
|
|
190
|
+
);
|
|
182
191
|
const ixs = await this.driftClient.getPlaceAndMakeSignedMsgPerpOrderIxs(
|
|
183
192
|
{
|
|
184
193
|
orderParams: signedMsgOrderParamsBuf,
|
package/src/types.ts
CHANGED
package/src/userMap/userMap.ts
CHANGED
|
@@ -263,6 +263,11 @@ export class UserMap implements UserMapInterface {
|
|
|
263
263
|
return this.userMap.get(key);
|
|
264
264
|
}
|
|
265
265
|
|
|
266
|
+
public async mustGetUserAccount(key: string): Promise<UserAccount> {
|
|
267
|
+
const user = await this.mustGet(key);
|
|
268
|
+
return user.getUserAccount();
|
|
269
|
+
}
|
|
270
|
+
|
|
266
271
|
/**
|
|
267
272
|
* gets the Authority for a particular userAccountPublicKey, if no User exists, undefined is returned
|
|
268
273
|
* @param key userAccountPublicKey to get User for
|