@drift-labs/sdk 2.67.0-beta.1 → 2.67.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.
@@ -0,0 +1,11 @@
1
+ /// <reference types="node" />
2
+ import { Connection, PublicKey } from '@solana/web3.js';
3
+ import { OracleClient, OraclePriceData } from './types';
4
+ import { BorshAccountsCoder } from '@coral-xyz/anchor';
5
+ export declare class SwitchboardClient implements OracleClient {
6
+ connection: Connection;
7
+ coder: BorshAccountsCoder;
8
+ constructor(connection: Connection);
9
+ getOraclePriceData(pricePublicKey: PublicKey): Promise<OraclePriceData>;
10
+ getOraclePriceDataFromBuffer(buffer: Buffer): OraclePriceData;
11
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SwitchboardClient = void 0;
7
+ const numericConstants_1 = require("../constants/numericConstants");
8
+ const switchboard_json_1 = __importDefault(require("../idl/switchboard.json"));
9
+ const anchor_1 = require("@coral-xyz/anchor");
10
+ class SwitchboardClient {
11
+ constructor(connection) {
12
+ this.connection = connection;
13
+ this.coder = new anchor_1.BorshAccountsCoder(switchboard_json_1.default);
14
+ }
15
+ async getOraclePriceData(pricePublicKey) {
16
+ const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
17
+ return this.getOraclePriceDataFromBuffer(accountInfo.data);
18
+ }
19
+ getOraclePriceDataFromBuffer(buffer) {
20
+ const aggregatorAccountData = this.coder.decodeUnchecked('AggregatorAccountData', buffer);
21
+ const price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
22
+ const confidence = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.stdDeviation);
23
+ const hasSufficientNumberOfDataPoints = aggregatorAccountData.latestConfirmedRound.numSuccess >=
24
+ aggregatorAccountData.minOracleResults;
25
+ const slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
26
+ return {
27
+ price,
28
+ slot,
29
+ confidence,
30
+ hasSufficientNumberOfDataPoints,
31
+ };
32
+ }
33
+ }
34
+ exports.SwitchboardClient = SwitchboardClient;
35
+ function convertSwitchboardDecimal(switchboardDecimal) {
36
+ const switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
37
+ return switchboardDecimal.mantissa
38
+ .mul(numericConstants_1.PRICE_PRECISION)
39
+ .div(switchboardPrecision);
40
+ }
package/lib/types.d.ts CHANGED
@@ -143,6 +143,9 @@ export declare class OracleSource {
143
143
  static readonly PYTH_1M: {
144
144
  pyth1M: {};
145
145
  };
146
+ static readonly SWITCHBOARD: {
147
+ switchboard: {};
148
+ };
146
149
  static readonly QUOTE_ASSET: {
147
150
  quoteAsset: {};
148
151
  };
package/lib/types.js CHANGED
@@ -95,7 +95,7 @@ exports.OracleSource = OracleSource;
95
95
  OracleSource.PYTH = { pyth: {} };
96
96
  OracleSource.PYTH_1K = { pyth1K: {} };
97
97
  OracleSource.PYTH_1M = { pyth1M: {} };
98
- // static readonly SWITCHBOARD = { switchboard: {} };
98
+ OracleSource.SWITCHBOARD = { switchboard: {} };
99
99
  OracleSource.QUOTE_ASSET = { quoteAsset: {} };
100
100
  OracleSource.PYTH_STABLE_COIN = { pythStableCoin: {} };
101
101
  class OrderType {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.67.0-beta.1",
3
+ "version": "2.67.0-beta.2",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -6183,9 +6183,13 @@ export class DriftClient {
6183
6183
  }
6184
6184
  }
6185
6185
 
6186
+ const userAccountExists =
6187
+ !!this.getUser()?.accountSubscriber?.isSubscribed &&
6188
+ (await this.checkIfAccountExists(this.getUser().userAccountPublicKey));
6189
+
6186
6190
  const remainingAccounts = this.getRemainingAccounts({
6187
- userAccounts: [this.getUserAccount()],
6188
- useMarketLastSlotCache: true,
6191
+ userAccounts: userAccountExists ? [this.getUserAccount()] : [],
6192
+ useMarketLastSlotCache: false,
6189
6193
  writableSpotMarketIndexes: [marketIndex],
6190
6194
  });
6191
6195
 
@@ -5,6 +5,7 @@ import { PythClient } from '../oracles/pythClient';
5
5
  // import { SwitchboardClient } from '../oracles/switchboardClient';
6
6
  import { QuoteAssetOracleClient } from '../oracles/quoteAssetOracleClient';
7
7
  import { BN } from '@coral-xyz/anchor';
8
+ import { SwitchboardClient } from '../oracles/switchboardClient';
8
9
 
9
10
  export function getOracleClient(
10
11
  oracleSource: OracleSource,
@@ -26,9 +27,9 @@ export function getOracleClient(
26
27
  return new PythClient(connection, undefined, true);
27
28
  }
28
29
 
29
- // if (isVariant(oracleSource, 'switchboard')) {
30
- // return new SwitchboardClient(connection);
31
- // }
30
+ if (isVariant(oracleSource, 'switchboard')) {
31
+ return new SwitchboardClient(connection);
32
+ }
32
33
 
33
34
  if (isVariant(oracleSource, 'quoteAsset')) {
34
35
  return new QuoteAssetOracleClient();