@drift-labs/sdk 2.48.0-beta.10 → 2.48.0-beta.12
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/pollingUserAccountSubscriber.js +11 -6
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +11 -6
- package/lib/orderSubscriber/OrderSubscriber.d.ts +1 -1
- package/lib/orderSubscriber/OrderSubscriber.js +13 -5
- package/lib/orderSubscriber/WebsocketSubscription.js +1 -1
- package/package.json +1 -1
- package/src/accounts/pollingUserAccountSubscriber.ts +15 -9
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +16 -9
- package/src/orderSubscriber/OrderSubscriber.ts +23 -12
- package/src/orderSubscriber/WebsocketSubscription.ts +1 -0
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.48.0-beta.
|
|
1
|
+
2.48.0-beta.12
|
|
@@ -53,12 +53,17 @@ class PollingUserAccountSubscriber {
|
|
|
53
53
|
}
|
|
54
54
|
async fetch() {
|
|
55
55
|
var _a, _b;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.user
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
56
|
+
try {
|
|
57
|
+
const dataAndContext = await this.program.account.user.fetchAndContext(this.userAccountPublicKey, this.accountLoader.commitment);
|
|
58
|
+
if (dataAndContext.context.slot > ((_b = (_a = this.user) === null || _a === void 0 ? void 0 : _a.slot) !== null && _b !== void 0 ? _b : 0)) {
|
|
59
|
+
this.user = {
|
|
60
|
+
data: dataAndContext.data,
|
|
61
|
+
slot: dataAndContext.context.slot,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
console.log(`PollingUserAccountSubscriber.fetch() UserAccount does not exist: ${e.message}`);
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
69
|
doesAccountExist() {
|
|
@@ -53,12 +53,17 @@ class PollingUserStatsAccountSubscriber {
|
|
|
53
53
|
}
|
|
54
54
|
async fetch() {
|
|
55
55
|
var _a, _b;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.userStats
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
56
|
+
try {
|
|
57
|
+
const dataAndContext = await this.program.account.userStats.fetchAndContext(this.userStatsAccountPublicKey, this.accountLoader.commitment);
|
|
58
|
+
if (dataAndContext.context.slot > ((_b = (_a = this.userStats) === null || _a === void 0 ? void 0 : _a.slot) !== null && _b !== void 0 ? _b : 0)) {
|
|
59
|
+
this.userStats = {
|
|
60
|
+
data: dataAndContext.data,
|
|
61
|
+
slot: dataAndContext.context.slot,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
console.log(`PollingUserStatsAccountSubscriber.fetch() UserStatsAccount does not exist: ${e.message}`);
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
69
|
doesAccountExist() {
|
|
@@ -21,7 +21,7 @@ export declare class OrderSubscriber {
|
|
|
21
21
|
constructor(config: OrderSubscriberConfig);
|
|
22
22
|
subscribe(): Promise<void>;
|
|
23
23
|
fetch(): Promise<void>;
|
|
24
|
-
tryUpdateUserAccount(key: string,
|
|
24
|
+
tryUpdateUserAccount(key: string, dataType: 'raw' | 'decoded', data: string[] | UserAccount, slot: number): void;
|
|
25
25
|
getDLOB(slot: number): Promise<DLOB>;
|
|
26
26
|
getSlot(): number;
|
|
27
27
|
unsubscribe(): Promise<void>;
|
|
@@ -55,11 +55,8 @@ class OrderSubscriber {
|
|
|
55
55
|
const programAccountSet = new Set();
|
|
56
56
|
for (const programAccount of rpcResponseAndContext.value) {
|
|
57
57
|
const key = programAccount.pubkey.toString();
|
|
58
|
-
// @ts-ignore
|
|
59
|
-
const buffer = buffer_1.Buffer.from(programAccount.account.data[0], programAccount.account.data[1]);
|
|
60
58
|
programAccountSet.add(key);
|
|
61
|
-
|
|
62
|
-
this.tryUpdateUserAccount(key, userAccount, slot);
|
|
59
|
+
this.tryUpdateUserAccount(key, 'raw', programAccount.account.data, slot);
|
|
63
60
|
}
|
|
64
61
|
for (const key of this.usersAccounts.keys()) {
|
|
65
62
|
if (!programAccountSet.has(key)) {
|
|
@@ -75,12 +72,23 @@ class OrderSubscriber {
|
|
|
75
72
|
this.fetchPromise = undefined;
|
|
76
73
|
}
|
|
77
74
|
}
|
|
78
|
-
tryUpdateUserAccount(key,
|
|
75
|
+
tryUpdateUserAccount(key, dataType, data, slot) {
|
|
79
76
|
if (!this.mostRecentSlot || slot > this.mostRecentSlot) {
|
|
80
77
|
this.mostRecentSlot = slot;
|
|
81
78
|
}
|
|
82
79
|
const slotAndUserAccount = this.usersAccounts.get(key);
|
|
83
80
|
if (!slotAndUserAccount || slotAndUserAccount.slot < slot) {
|
|
81
|
+
let userAccount;
|
|
82
|
+
// Polling leads to a lot of redundant decoding, so we only decode if data is from a fresh slot
|
|
83
|
+
if (dataType === 'raw') {
|
|
84
|
+
// @ts-ignore
|
|
85
|
+
const buffer = buffer_1.Buffer.from(data[0], data[1]);
|
|
86
|
+
userAccount =
|
|
87
|
+
this.driftClient.program.account.user.coder.accounts.decode('User', buffer);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
userAccount = data;
|
|
91
|
+
}
|
|
84
92
|
const newOrders = userAccount.orders.filter((order) => {
|
|
85
93
|
var _a;
|
|
86
94
|
return order.slot.toNumber() > ((_a = slotAndUserAccount === null || slotAndUserAccount === void 0 ? void 0 : slotAndUserAccount.slot) !== null && _a !== void 0 ? _a : 0) &&
|
|
@@ -18,7 +18,7 @@ class WebsocketSubscription {
|
|
|
18
18
|
}
|
|
19
19
|
await this.subscriber.subscribe((accountId, account, context) => {
|
|
20
20
|
const userKey = accountId.toBase58();
|
|
21
|
-
this.orderSubscriber.tryUpdateUserAccount(userKey, account, context.slot);
|
|
21
|
+
this.orderSubscriber.tryUpdateUserAccount(userKey, 'decoded', account, context.slot);
|
|
22
22
|
});
|
|
23
23
|
if (!this.skipInitialLoad) {
|
|
24
24
|
await this.orderSubscriber.fetch();
|
package/package.json
CHANGED
|
@@ -93,15 +93,21 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
async fetch(): Promise<void> {
|
|
96
|
-
|
|
97
|
-
this.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
this.user
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
96
|
+
try {
|
|
97
|
+
const dataAndContext = await this.program.account.user.fetchAndContext(
|
|
98
|
+
this.userAccountPublicKey,
|
|
99
|
+
this.accountLoader.commitment
|
|
100
|
+
);
|
|
101
|
+
if (dataAndContext.context.slot > (this.user?.slot ?? 0)) {
|
|
102
|
+
this.user = {
|
|
103
|
+
data: dataAndContext.data as UserAccount,
|
|
104
|
+
slot: dataAndContext.context.slot,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
} catch (e) {
|
|
108
|
+
console.log(
|
|
109
|
+
`PollingUserAccountSubscriber.fetch() UserAccount does not exist: ${e.message}`
|
|
110
|
+
);
|
|
105
111
|
}
|
|
106
112
|
}
|
|
107
113
|
|
|
@@ -97,15 +97,22 @@ export class PollingUserStatsAccountSubscriber
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
async fetch(): Promise<void> {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
100
|
+
try {
|
|
101
|
+
const dataAndContext =
|
|
102
|
+
await this.program.account.userStats.fetchAndContext(
|
|
103
|
+
this.userStatsAccountPublicKey,
|
|
104
|
+
this.accountLoader.commitment
|
|
105
|
+
);
|
|
106
|
+
if (dataAndContext.context.slot > (this.userStats?.slot ?? 0)) {
|
|
107
|
+
this.userStats = {
|
|
108
|
+
data: dataAndContext.data as UserStatsAccount,
|
|
109
|
+
slot: dataAndContext.context.slot,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
} catch (e) {
|
|
113
|
+
console.log(
|
|
114
|
+
`PollingUserStatsAccountSubscriber.fetch() UserStatsAccount does not exist: ${e.message}`
|
|
115
|
+
);
|
|
109
116
|
}
|
|
110
117
|
}
|
|
111
118
|
|
|
@@ -83,18 +83,13 @@ export class OrderSubscriber {
|
|
|
83
83
|
const programAccountSet = new Set<string>();
|
|
84
84
|
for (const programAccount of rpcResponseAndContext.value) {
|
|
85
85
|
const key = programAccount.pubkey.toString();
|
|
86
|
-
// @ts-ignore
|
|
87
|
-
const buffer = Buffer.from(
|
|
88
|
-
programAccount.account.data[0],
|
|
89
|
-
programAccount.account.data[1]
|
|
90
|
-
);
|
|
91
86
|
programAccountSet.add(key);
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
87
|
+
this.tryUpdateUserAccount(
|
|
88
|
+
key,
|
|
89
|
+
'raw',
|
|
90
|
+
programAccount.account.data,
|
|
91
|
+
slot
|
|
92
|
+
);
|
|
98
93
|
}
|
|
99
94
|
|
|
100
95
|
for (const key of this.usersAccounts.keys()) {
|
|
@@ -112,7 +107,8 @@ export class OrderSubscriber {
|
|
|
112
107
|
|
|
113
108
|
tryUpdateUserAccount(
|
|
114
109
|
key: string,
|
|
115
|
-
|
|
110
|
+
dataType: 'raw' | 'decoded',
|
|
111
|
+
data: string[] | UserAccount,
|
|
116
112
|
slot: number
|
|
117
113
|
): void {
|
|
118
114
|
if (!this.mostRecentSlot || slot > this.mostRecentSlot) {
|
|
@@ -121,6 +117,21 @@ export class OrderSubscriber {
|
|
|
121
117
|
|
|
122
118
|
const slotAndUserAccount = this.usersAccounts.get(key);
|
|
123
119
|
if (!slotAndUserAccount || slotAndUserAccount.slot < slot) {
|
|
120
|
+
let userAccount: UserAccount;
|
|
121
|
+
// Polling leads to a lot of redundant decoding, so we only decode if data is from a fresh slot
|
|
122
|
+
if (dataType === 'raw') {
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
const buffer = Buffer.from(data[0], data[1]);
|
|
125
|
+
|
|
126
|
+
userAccount =
|
|
127
|
+
this.driftClient.program.account.user.coder.accounts.decode(
|
|
128
|
+
'User',
|
|
129
|
+
buffer
|
|
130
|
+
) as UserAccount;
|
|
131
|
+
} else {
|
|
132
|
+
userAccount = data as UserAccount;
|
|
133
|
+
}
|
|
134
|
+
|
|
124
135
|
const newOrders = userAccount.orders.filter(
|
|
125
136
|
(order) =>
|
|
126
137
|
order.slot.toNumber() > (slotAndUserAccount?.slot ?? 0) &&
|