@drift-labs/sdk 2.43.0-beta.11 → 2.43.0-beta.13
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/webSocketAccountSubscriber.d.ts +1 -0
- package/lib/accounts/webSocketAccountSubscriber.js +18 -3
- package/lib/accounts/webSocketProgramAccountSubscriber.d.ts +1 -0
- package/lib/accounts/webSocketProgramAccountSubscriber.js +18 -3
- package/lib/user.js +7 -8
- package/package.json +1 -1
- package/src/accounts/webSocketAccountSubscriber.ts +20 -6
- package/src/accounts/webSocketProgramAccountSubscriber.ts +19 -6
- package/src/user.ts +12 -8
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.43.0-beta.
|
|
1
|
+
2.43.0-beta.13
|
|
@@ -13,6 +13,7 @@ export declare class WebSocketAccountSubscriber<T> implements AccountSubscriber<
|
|
|
13
13
|
onChange: (data: T) => void;
|
|
14
14
|
listenerId?: number;
|
|
15
15
|
resubTimeoutMs?: number;
|
|
16
|
+
isUnsubscribing: boolean;
|
|
16
17
|
timeoutId?: NodeJS.Timeout;
|
|
17
18
|
receivingData: boolean;
|
|
18
19
|
constructor(accountName: string, program: Program, accountPublicKey: PublicKey, decodeBuffer?: (buffer: Buffer) => T, resubTimeoutMs?: number);
|
|
@@ -4,6 +4,7 @@ exports.WebSocketAccountSubscriber = void 0;
|
|
|
4
4
|
const utils_1 = require("./utils");
|
|
5
5
|
class WebSocketAccountSubscriber {
|
|
6
6
|
constructor(accountName, program, accountPublicKey, decodeBuffer, resubTimeoutMs) {
|
|
7
|
+
this.isUnsubscribing = false;
|
|
7
8
|
this.accountName = accountName;
|
|
8
9
|
this.program = program;
|
|
9
10
|
this.accountPublicKey = accountPublicKey;
|
|
@@ -12,7 +13,7 @@ class WebSocketAccountSubscriber {
|
|
|
12
13
|
this.receivingData = false;
|
|
13
14
|
}
|
|
14
15
|
async subscribe(onChange) {
|
|
15
|
-
if (this.listenerId) {
|
|
16
|
+
if (this.listenerId || this.isUnsubscribing) {
|
|
16
17
|
return;
|
|
17
18
|
}
|
|
18
19
|
this.onChange = onChange;
|
|
@@ -49,6 +50,10 @@ class WebSocketAccountSubscriber {
|
|
|
49
50
|
throw new Error('onChange callback function must be set');
|
|
50
51
|
}
|
|
51
52
|
this.timeoutId = setTimeout(async () => {
|
|
53
|
+
if (this.isUnsubscribing) {
|
|
54
|
+
// If we are in the process of unsubscribing, do not attempt to resubscribe
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
52
57
|
if (this.receivingData) {
|
|
53
58
|
console.log(`No ws data from ${this.accountName} in ${this.resubTimeoutMs}ms, resubscribing`);
|
|
54
59
|
await this.unsubscribe();
|
|
@@ -108,11 +113,21 @@ class WebSocketAccountSubscriber {
|
|
|
108
113
|
}
|
|
109
114
|
}
|
|
110
115
|
unsubscribe() {
|
|
116
|
+
this.isUnsubscribing = true;
|
|
117
|
+
clearTimeout(this.timeoutId);
|
|
118
|
+
this.timeoutId = undefined;
|
|
111
119
|
if (this.listenerId) {
|
|
112
|
-
const promise = this.program.provider.connection
|
|
113
|
-
|
|
120
|
+
const promise = this.program.provider.connection
|
|
121
|
+
.removeAccountChangeListener(this.listenerId)
|
|
122
|
+
.then(() => {
|
|
123
|
+
this.listenerId = undefined;
|
|
124
|
+
this.isUnsubscribing = false;
|
|
125
|
+
});
|
|
114
126
|
return promise;
|
|
115
127
|
}
|
|
128
|
+
else {
|
|
129
|
+
this.isUnsubscribing = false;
|
|
130
|
+
}
|
|
116
131
|
}
|
|
117
132
|
}
|
|
118
133
|
exports.WebSocketAccountSubscriber = WebSocketAccountSubscriber;
|
|
@@ -15,6 +15,7 @@ export declare class WebSocketProgramAccountSubscriber<T> implements ProgramAcco
|
|
|
15
15
|
onChange: (accountId: PublicKey, data: T, context: Context) => void;
|
|
16
16
|
listenerId?: number;
|
|
17
17
|
resubTimeoutMs?: number;
|
|
18
|
+
isUnsubscribing: boolean;
|
|
18
19
|
timeoutId?: NodeJS.Timeout;
|
|
19
20
|
options: {
|
|
20
21
|
filters: MemcmpFilter[];
|
|
@@ -5,6 +5,7 @@ class WebSocketProgramAccountSubscriber {
|
|
|
5
5
|
constructor(subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
|
|
6
6
|
filters: [],
|
|
7
7
|
}, resubTimeoutMs) {
|
|
8
|
+
this.isUnsubscribing = false;
|
|
8
9
|
this.receivingData = false;
|
|
9
10
|
this.subscriptionName = subscriptionName;
|
|
10
11
|
this.accountDiscriminator = accountDiscriminator;
|
|
@@ -16,7 +17,7 @@ class WebSocketProgramAccountSubscriber {
|
|
|
16
17
|
}
|
|
17
18
|
async subscribe(onChange) {
|
|
18
19
|
var _a;
|
|
19
|
-
if (this.listenerId) {
|
|
20
|
+
if (this.listenerId || this.isUnsubscribing) {
|
|
20
21
|
return;
|
|
21
22
|
}
|
|
22
23
|
this.onChange = onChange;
|
|
@@ -40,6 +41,10 @@ class WebSocketProgramAccountSubscriber {
|
|
|
40
41
|
throw new Error('onChange callback function must be set');
|
|
41
42
|
}
|
|
42
43
|
this.timeoutId = setTimeout(async () => {
|
|
44
|
+
if (this.isUnsubscribing) {
|
|
45
|
+
// If we are in the process of unsubscribing, do not attempt to resubscribe
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
43
48
|
if (this.receivingData) {
|
|
44
49
|
console.log(`No ws data from ${this.subscriptionName} in ${this.resubTimeoutMs}ms, resubscribing`);
|
|
45
50
|
await this.unsubscribe();
|
|
@@ -89,11 +94,21 @@ class WebSocketProgramAccountSubscriber {
|
|
|
89
94
|
}
|
|
90
95
|
}
|
|
91
96
|
unsubscribe() {
|
|
97
|
+
this.isUnsubscribing = true;
|
|
98
|
+
clearTimeout(this.timeoutId);
|
|
99
|
+
this.timeoutId = undefined;
|
|
92
100
|
if (this.listenerId) {
|
|
93
|
-
const promise = this.program.provider.connection
|
|
94
|
-
|
|
101
|
+
const promise = this.program.provider.connection
|
|
102
|
+
.removeAccountChangeListener(this.listenerId)
|
|
103
|
+
.then(() => {
|
|
104
|
+
this.listenerId = undefined;
|
|
105
|
+
this.isUnsubscribing = false;
|
|
106
|
+
});
|
|
95
107
|
return promise;
|
|
96
108
|
}
|
|
109
|
+
else {
|
|
110
|
+
this.isUnsubscribing = false;
|
|
111
|
+
}
|
|
97
112
|
}
|
|
98
113
|
}
|
|
99
114
|
exports.WebSocketProgramAccountSubscriber = WebSocketProgramAccountSubscriber;
|
package/lib/user.js
CHANGED
|
@@ -1677,14 +1677,13 @@ class User {
|
|
|
1677
1677
|
withdrawLimit = _1.BN.max(withdrawLimit, userDepositAmount);
|
|
1678
1678
|
}
|
|
1679
1679
|
const assetWeight = (0, spotBalance_1.calculateAssetWeight)(userDepositAmount, oracleData.price, spotMarket, 'Initial');
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
.mul(precisionIncrease);
|
|
1680
|
+
let amountWithdrawable;
|
|
1681
|
+
if (assetWeight.eq(numericConstants_1.ZERO)) {
|
|
1682
|
+
amountWithdrawable = userDepositAmount;
|
|
1683
|
+
}
|
|
1684
|
+
else {
|
|
1685
|
+
amountWithdrawable = (0, _1.divCeil)((0, _1.divCeil)(freeCollateral.mul(numericConstants_1.MARGIN_PRECISION), assetWeight).mul(numericConstants_1.PRICE_PRECISION), oracleData.price).mul(precisionIncrease);
|
|
1686
|
+
}
|
|
1688
1687
|
const maxWithdrawValue = _1.BN.min(_1.BN.min(amountWithdrawable, userDepositAmount), withdrawLimit.abs());
|
|
1689
1688
|
if (reduceOnly) {
|
|
1690
1689
|
return _1.BN.max(maxWithdrawValue, numericConstants_1.ZERO);
|
package/package.json
CHANGED
|
@@ -14,6 +14,8 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
14
14
|
onChange: (data: T) => void;
|
|
15
15
|
listenerId?: number;
|
|
16
16
|
resubTimeoutMs?: number;
|
|
17
|
+
isUnsubscribing = false;
|
|
18
|
+
|
|
17
19
|
timeoutId?: NodeJS.Timeout;
|
|
18
20
|
|
|
19
21
|
receivingData: boolean;
|
|
@@ -34,7 +36,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
async subscribe(onChange: (data: T) => void): Promise<void> {
|
|
37
|
-
if (this.listenerId) {
|
|
39
|
+
if (this.listenerId || this.isUnsubscribing) {
|
|
38
40
|
return;
|
|
39
41
|
}
|
|
40
42
|
|
|
@@ -80,6 +82,11 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
80
82
|
throw new Error('onChange callback function must be set');
|
|
81
83
|
}
|
|
82
84
|
this.timeoutId = setTimeout(async () => {
|
|
85
|
+
if (this.isUnsubscribing) {
|
|
86
|
+
// If we are in the process of unsubscribing, do not attempt to resubscribe
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
83
90
|
if (this.receivingData) {
|
|
84
91
|
console.log(
|
|
85
92
|
`No ws data from ${this.accountName} in ${this.resubTimeoutMs}ms, resubscribing`
|
|
@@ -154,13 +161,20 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
|
|
|
154
161
|
}
|
|
155
162
|
|
|
156
163
|
unsubscribe(): Promise<void> {
|
|
164
|
+
this.isUnsubscribing = true;
|
|
165
|
+
clearTimeout(this.timeoutId);
|
|
166
|
+
this.timeoutId = undefined;
|
|
167
|
+
|
|
157
168
|
if (this.listenerId) {
|
|
158
|
-
const promise =
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
169
|
+
const promise = this.program.provider.connection
|
|
170
|
+
.removeAccountChangeListener(this.listenerId)
|
|
171
|
+
.then(() => {
|
|
172
|
+
this.listenerId = undefined;
|
|
173
|
+
this.isUnsubscribing = false;
|
|
174
|
+
});
|
|
163
175
|
return promise;
|
|
176
|
+
} else {
|
|
177
|
+
this.isUnsubscribing = false;
|
|
164
178
|
}
|
|
165
179
|
}
|
|
166
180
|
}
|
|
@@ -21,6 +21,7 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
21
21
|
onChange: (accountId: PublicKey, data: T, context: Context) => void;
|
|
22
22
|
listenerId?: number;
|
|
23
23
|
resubTimeoutMs?: number;
|
|
24
|
+
isUnsubscribing = false;
|
|
24
25
|
timeoutId?: NodeJS.Timeout;
|
|
25
26
|
options: { filters: MemcmpFilter[]; commitment?: Commitment };
|
|
26
27
|
|
|
@@ -48,7 +49,7 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
48
49
|
async subscribe(
|
|
49
50
|
onChange: (accountId: PublicKey, data: T, context: Context) => void
|
|
50
51
|
): Promise<void> {
|
|
51
|
-
if (this.listenerId) {
|
|
52
|
+
if (this.listenerId || this.isUnsubscribing) {
|
|
52
53
|
return;
|
|
53
54
|
}
|
|
54
55
|
|
|
@@ -81,6 +82,11 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
81
82
|
throw new Error('onChange callback function must be set');
|
|
82
83
|
}
|
|
83
84
|
this.timeoutId = setTimeout(async () => {
|
|
85
|
+
if (this.isUnsubscribing) {
|
|
86
|
+
// If we are in the process of unsubscribing, do not attempt to resubscribe
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
84
90
|
if (this.receivingData) {
|
|
85
91
|
console.log(
|
|
86
92
|
`No ws data from ${this.subscriptionName} in ${this.resubTimeoutMs}ms, resubscribing`
|
|
@@ -140,13 +146,20 @@ export class WebSocketProgramAccountSubscriber<T>
|
|
|
140
146
|
}
|
|
141
147
|
|
|
142
148
|
unsubscribe(): Promise<void> {
|
|
149
|
+
this.isUnsubscribing = true;
|
|
150
|
+
clearTimeout(this.timeoutId);
|
|
151
|
+
this.timeoutId = undefined;
|
|
152
|
+
|
|
143
153
|
if (this.listenerId) {
|
|
144
|
-
const promise =
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
154
|
+
const promise = this.program.provider.connection
|
|
155
|
+
.removeAccountChangeListener(this.listenerId)
|
|
156
|
+
.then(() => {
|
|
157
|
+
this.listenerId = undefined;
|
|
158
|
+
this.isUnsubscribing = false;
|
|
159
|
+
});
|
|
149
160
|
return promise;
|
|
161
|
+
} else {
|
|
162
|
+
this.isUnsubscribing = false;
|
|
150
163
|
}
|
|
151
164
|
}
|
|
152
165
|
}
|
package/src/user.ts
CHANGED
|
@@ -48,6 +48,7 @@ import {
|
|
|
48
48
|
calculateReservePrice,
|
|
49
49
|
calculateSpotMarketMarginRatio,
|
|
50
50
|
calculateUnrealizedAssetWeight,
|
|
51
|
+
divCeil,
|
|
51
52
|
getBalance,
|
|
52
53
|
getSignedTokenAmount,
|
|
53
54
|
getStrictTokenValue,
|
|
@@ -3054,14 +3055,17 @@ export class User {
|
|
|
3054
3055
|
'Initial'
|
|
3055
3056
|
);
|
|
3056
3057
|
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3058
|
+
let amountWithdrawable;
|
|
3059
|
+
if (assetWeight.eq(ZERO)) {
|
|
3060
|
+
amountWithdrawable = userDepositAmount;
|
|
3061
|
+
} else {
|
|
3062
|
+
amountWithdrawable = divCeil(
|
|
3063
|
+
divCeil(freeCollateral.mul(MARGIN_PRECISION), assetWeight).mul(
|
|
3064
|
+
PRICE_PRECISION
|
|
3065
|
+
),
|
|
3066
|
+
oracleData.price
|
|
3067
|
+
).mul(precisionIncrease);
|
|
3068
|
+
}
|
|
3065
3069
|
|
|
3066
3070
|
const maxWithdrawValue = BN.min(
|
|
3067
3071
|
BN.min(amountWithdrawable, userDepositAmount),
|