@drift-labs/sdk 2.67.0-beta.1 → 2.67.0-beta.3
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/pollingDriftClientAccountSubscriber.d.ts +1 -0
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +33 -18
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +20 -18
- package/lib/driftClient.js +5 -2
- package/lib/factory/oracleClient.js +4 -3
- package/lib/idl/switchboard.json +8354 -0
- package/lib/oracles/switchboardClient.d.ts +11 -0
- package/lib/oracles/switchboardClient.js +40 -0
- package/lib/types.d.ts +3 -0
- package/lib/types.js +1 -1
- package/package.json +1 -1
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +43 -20
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +20 -18
- package/src/driftClient.ts +6 -2
- package/src/factory/oracleClient.ts +4 -3
- package/src/idl/switchboard.json +8354 -0
- package/src/oracles/switchboardClient.ts +74 -0
- package/src/types.ts +1 -1
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.67.0-beta.
|
|
1
|
+
2.67.0-beta.3
|
|
@@ -49,6 +49,7 @@ export declare class PollingDriftClientAccountSubscriber implements DriftClientA
|
|
|
49
49
|
addSpotMarket(marketIndex: number): Promise<boolean>;
|
|
50
50
|
addPerpMarket(marketIndex: number): Promise<boolean>;
|
|
51
51
|
addOracle(oracleInfo: OracleInfo): Promise<boolean>;
|
|
52
|
+
private pauseForOracleToBeAdded;
|
|
52
53
|
private setPerpOracleMap;
|
|
53
54
|
private setSpotOracleMap;
|
|
54
55
|
assertIsSubscribed(): void;
|
|
@@ -59,8 +59,8 @@ class PollingDriftClientAccountSubscriber {
|
|
|
59
59
|
if (subscriptionSucceeded) {
|
|
60
60
|
this.eventEmitter.emit('update');
|
|
61
61
|
}
|
|
62
|
-
this.setPerpOracleMap();
|
|
63
|
-
this.setSpotOracleMap();
|
|
62
|
+
await this.setPerpOracleMap();
|
|
63
|
+
await this.setSpotOracleMap();
|
|
64
64
|
this.isSubscribing = false;
|
|
65
65
|
this.isSubscribed = subscriptionSucceeded;
|
|
66
66
|
this.subscriptionPromiseResolver(subscriptionSucceeded);
|
|
@@ -244,7 +244,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
244
244
|
await this.addPerpMarketAccountToPoll(marketIndex);
|
|
245
245
|
const accountToPoll = this.accountsToPoll.get(marketPublicKey.toString());
|
|
246
246
|
await this.addAccountToAccountLoader(accountToPoll);
|
|
247
|
-
this.setPerpOracleMap();
|
|
247
|
+
await this.setPerpOracleMap();
|
|
248
248
|
return true;
|
|
249
249
|
}
|
|
250
250
|
async addOracle(oracleInfo) {
|
|
@@ -253,25 +253,50 @@ class PollingDriftClientAccountSubscriber {
|
|
|
253
253
|
return true;
|
|
254
254
|
}
|
|
255
255
|
this.addOracleToPoll(oracleInfo);
|
|
256
|
-
const
|
|
256
|
+
const oracleString = oracleInfo.publicKey.toBase58();
|
|
257
|
+
const oracleToPoll = this.oraclesToPoll.get(oracleString);
|
|
257
258
|
await this.addOracleToAccountLoader(oracleToPoll);
|
|
259
|
+
await this.pauseForOracleToBeAdded(3, oracleString);
|
|
258
260
|
return true;
|
|
259
261
|
}
|
|
260
|
-
|
|
262
|
+
async pauseForOracleToBeAdded(tries, oracle) {
|
|
263
|
+
let i = 0;
|
|
264
|
+
while (i < tries) {
|
|
265
|
+
await new Promise((r) => setTimeout(r, this.accountLoader.pollingFrequency));
|
|
266
|
+
if (this.accountLoader.bufferAndSlotMap.has(oracle)) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
i++;
|
|
270
|
+
}
|
|
271
|
+
console.log(`Pausing to find oracle ${oracle} failed`);
|
|
272
|
+
}
|
|
273
|
+
async setPerpOracleMap() {
|
|
261
274
|
const perpMarkets = this.getMarketAccountsAndSlots();
|
|
262
275
|
for (const perpMarket of perpMarkets) {
|
|
263
276
|
const perpMarketAccount = perpMarket.data;
|
|
264
277
|
const perpMarketIndex = perpMarketAccount.marketIndex;
|
|
265
278
|
const oracle = perpMarketAccount.amm.oracle;
|
|
279
|
+
if (!this.oracles.has(oracle.toBase58())) {
|
|
280
|
+
await this.addOracle({
|
|
281
|
+
publicKey: oracle,
|
|
282
|
+
source: perpMarketAccount.amm.oracleSource,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
266
285
|
this.perpOracleMap.set(perpMarketIndex, oracle);
|
|
267
286
|
}
|
|
268
287
|
}
|
|
269
|
-
setSpotOracleMap() {
|
|
288
|
+
async setSpotOracleMap() {
|
|
270
289
|
const spotMarkets = this.getSpotMarketAccountsAndSlots();
|
|
271
290
|
for (const spotMarket of spotMarkets) {
|
|
272
291
|
const spotMarketAccount = spotMarket.data;
|
|
273
292
|
const spotMarketIndex = spotMarketAccount.marketIndex;
|
|
274
293
|
const oracle = spotMarketAccount.oracle;
|
|
294
|
+
if (!this.oracles.has(oracle.toBase58())) {
|
|
295
|
+
await this.addOracle({
|
|
296
|
+
publicKey: oracle,
|
|
297
|
+
source: spotMarketAccount.oracleSource,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
275
300
|
this.spotOracleMap.set(spotMarketIndex, oracle);
|
|
276
301
|
}
|
|
277
302
|
}
|
|
@@ -314,12 +339,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
314
339
|
}
|
|
315
340
|
if (!perpMarketAccount.data.amm.oracle.equals(oracle)) {
|
|
316
341
|
// If the oracle has changed, we need to update the oracle map in background
|
|
317
|
-
this.
|
|
318
|
-
source: perpMarketAccount.data.amm.oracleSource,
|
|
319
|
-
publicKey: perpMarketAccount.data.amm.oracle,
|
|
320
|
-
}).then(() => {
|
|
321
|
-
this.setPerpOracleMap();
|
|
322
|
-
});
|
|
342
|
+
this.setPerpOracleMap();
|
|
323
343
|
}
|
|
324
344
|
return this.getOraclePriceDataAndSlot(oracle);
|
|
325
345
|
}
|
|
@@ -331,12 +351,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
331
351
|
}
|
|
332
352
|
if (!spotMarketAccount.data.oracle.equals(oracle)) {
|
|
333
353
|
// If the oracle has changed, we need to update the oracle map in background
|
|
334
|
-
this.
|
|
335
|
-
source: spotMarketAccount.data.oracleSource,
|
|
336
|
-
publicKey: spotMarketAccount.data.oracle,
|
|
337
|
-
}).then(() => {
|
|
338
|
-
this.setSpotOracleMap();
|
|
339
|
-
});
|
|
354
|
+
this.setSpotOracleMap();
|
|
340
355
|
}
|
|
341
356
|
return this.getOraclePriceDataAndSlot(oracle);
|
|
342
357
|
}
|
|
@@ -59,8 +59,8 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
59
59
|
// subscribe to oracles
|
|
60
60
|
await this.subscribeToOracles();
|
|
61
61
|
this.eventEmitter.emit('update');
|
|
62
|
-
this.setPerpOracleMap();
|
|
63
|
-
this.setSpotOracleMap();
|
|
62
|
+
await this.setPerpOracleMap();
|
|
63
|
+
await this.setSpotOracleMap();
|
|
64
64
|
this.isSubscribing = false;
|
|
65
65
|
this.isSubscribed = true;
|
|
66
66
|
this.subscriptionPromiseResolver(true);
|
|
@@ -157,7 +157,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
157
157
|
return true;
|
|
158
158
|
}
|
|
159
159
|
const subscriptionSuccess = this.subscribeToSpotMarketAccount(marketIndex);
|
|
160
|
-
this.setSpotOracleMap();
|
|
160
|
+
await this.setSpotOracleMap();
|
|
161
161
|
return subscriptionSuccess;
|
|
162
162
|
}
|
|
163
163
|
async addPerpMarket(marketIndex) {
|
|
@@ -165,7 +165,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
165
165
|
return true;
|
|
166
166
|
}
|
|
167
167
|
const subscriptionSuccess = this.subscribeToPerpMarketAccount(marketIndex);
|
|
168
|
-
this.setPerpOracleMap();
|
|
168
|
+
await this.setPerpOracleMap();
|
|
169
169
|
return subscriptionSuccess;
|
|
170
170
|
}
|
|
171
171
|
async addOracle(oracleInfo) {
|
|
@@ -177,7 +177,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
177
177
|
}
|
|
178
178
|
return this.subscribeToOracle(oracleInfo);
|
|
179
179
|
}
|
|
180
|
-
setPerpOracleMap() {
|
|
180
|
+
async setPerpOracleMap() {
|
|
181
181
|
const perpMarkets = this.getMarketAccountsAndSlots();
|
|
182
182
|
for (const perpMarket of perpMarkets) {
|
|
183
183
|
if (!perpMarket) {
|
|
@@ -186,10 +186,16 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
186
186
|
const perpMarketAccount = perpMarket.data;
|
|
187
187
|
const perpMarketIndex = perpMarketAccount.marketIndex;
|
|
188
188
|
const oracle = perpMarketAccount.amm.oracle;
|
|
189
|
+
if (!this.oracleSubscribers.has(oracle.toBase58())) {
|
|
190
|
+
await this.addOracle({
|
|
191
|
+
publicKey: oracle,
|
|
192
|
+
source: perpMarket.data.amm.oracleSource,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
189
195
|
this.perpOracleMap.set(perpMarketIndex, oracle);
|
|
190
196
|
}
|
|
191
197
|
}
|
|
192
|
-
setSpotOracleMap() {
|
|
198
|
+
async setSpotOracleMap() {
|
|
193
199
|
const spotMarkets = this.getSpotMarketAccountsAndSlots();
|
|
194
200
|
for (const spotMarket of spotMarkets) {
|
|
195
201
|
if (!spotMarket) {
|
|
@@ -198,6 +204,12 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
198
204
|
const spotMarketAccount = spotMarket.data;
|
|
199
205
|
const spotMarketIndex = spotMarketAccount.marketIndex;
|
|
200
206
|
const oracle = spotMarketAccount.oracle;
|
|
207
|
+
if (!this.oracleSubscribers.has(oracle.toBase58())) {
|
|
208
|
+
await this.addOracle({
|
|
209
|
+
publicKey: oracle,
|
|
210
|
+
source: spotMarketAccount.oracleSource,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
201
213
|
this.spotOracleMap.set(spotMarketIndex, oracle);
|
|
202
214
|
}
|
|
203
215
|
}
|
|
@@ -242,12 +254,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
242
254
|
}
|
|
243
255
|
if (!perpMarketAccount.data.amm.oracle.equals(oracle)) {
|
|
244
256
|
// If the oracle has changed, we need to update the oracle map in background
|
|
245
|
-
this.
|
|
246
|
-
source: perpMarketAccount.data.amm.oracleSource,
|
|
247
|
-
publicKey: perpMarketAccount.data.amm.oracle,
|
|
248
|
-
}).then(() => {
|
|
249
|
-
this.setPerpOracleMap();
|
|
250
|
-
});
|
|
257
|
+
this.setPerpOracleMap();
|
|
251
258
|
}
|
|
252
259
|
return this.getOraclePriceDataAndSlot(oracle);
|
|
253
260
|
}
|
|
@@ -259,12 +266,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
259
266
|
}
|
|
260
267
|
if (!spotMarketAccount.data.oracle.equals(oracle)) {
|
|
261
268
|
// If the oracle has changed, we need to update the oracle map in background
|
|
262
|
-
this.
|
|
263
|
-
source: spotMarketAccount.data.oracleSource,
|
|
264
|
-
publicKey: spotMarketAccount.data.oracle,
|
|
265
|
-
}).then(() => {
|
|
266
|
-
this.setSpotOracleMap();
|
|
267
|
-
});
|
|
269
|
+
this.setSpotOracleMap();
|
|
268
270
|
}
|
|
269
271
|
return this.getOraclePriceDataAndSlot(oracle);
|
|
270
272
|
}
|
package/lib/driftClient.js
CHANGED
|
@@ -3302,6 +3302,7 @@ class DriftClient {
|
|
|
3302
3302
|
return txSig;
|
|
3303
3303
|
}
|
|
3304
3304
|
async removeInsuranceFundStake(marketIndex, collateralAccountPublicKey) {
|
|
3305
|
+
var _a, _b;
|
|
3305
3306
|
const removeIfStakeIxs = [];
|
|
3306
3307
|
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
3307
3308
|
const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
|
|
@@ -3324,9 +3325,11 @@ class DriftClient {
|
|
|
3324
3325
|
removeIfStakeIxs.push(createTokenAccountIx);
|
|
3325
3326
|
}
|
|
3326
3327
|
}
|
|
3328
|
+
const userAccountExists = !!((_b = (_a = this.getUser()) === null || _a === void 0 ? void 0 : _a.accountSubscriber) === null || _b === void 0 ? void 0 : _b.isSubscribed) &&
|
|
3329
|
+
(await this.checkIfAccountExists(this.getUser().userAccountPublicKey));
|
|
3327
3330
|
const remainingAccounts = this.getRemainingAccounts({
|
|
3328
|
-
userAccounts: [this.getUserAccount()],
|
|
3329
|
-
useMarketLastSlotCache:
|
|
3331
|
+
userAccounts: userAccountExists ? [this.getUserAccount()] : [],
|
|
3332
|
+
useMarketLastSlotCache: false,
|
|
3330
3333
|
writableSpotMarketIndexes: [marketIndex],
|
|
3331
3334
|
});
|
|
3332
3335
|
const removeStakeIx = await this.program.instruction.removeInsuranceFundStake(marketIndex, {
|
|
@@ -6,6 +6,7 @@ const pythClient_1 = require("../oracles/pythClient");
|
|
|
6
6
|
// import { SwitchboardClient } from '../oracles/switchboardClient';
|
|
7
7
|
const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
|
|
8
8
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
9
|
+
const switchboardClient_1 = require("../oracles/switchboardClient");
|
|
9
10
|
function getOracleClient(oracleSource, connection) {
|
|
10
11
|
if ((0, types_1.isVariant)(oracleSource, 'pyth')) {
|
|
11
12
|
return new pythClient_1.PythClient(connection);
|
|
@@ -19,9 +20,9 @@ function getOracleClient(oracleSource, connection) {
|
|
|
19
20
|
if ((0, types_1.isVariant)(oracleSource, 'pythStableCoin')) {
|
|
20
21
|
return new pythClient_1.PythClient(connection, undefined, true);
|
|
21
22
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
if ((0, types_1.isVariant)(oracleSource, 'switchboard')) {
|
|
24
|
+
return new switchboardClient_1.SwitchboardClient(connection);
|
|
25
|
+
}
|
|
25
26
|
if ((0, types_1.isVariant)(oracleSource, 'quoteAsset')) {
|
|
26
27
|
return new quoteAssetOracleClient_1.QuoteAssetOracleClient();
|
|
27
28
|
}
|