@drift-labs/sdk 2.71.0-beta.3 → 2.71.0-beta.4

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 = anchor_1.BN.max(convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.stdDeviation), price.divn(1000));
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
  OracleSource.Prelaunch = { prelaunch: {} };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.71.0-beta.3",
3
+ "version": "2.71.0-beta.4",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -6,6 +6,7 @@ import { PythClient } from '../oracles/pythClient';
6
6
  import { QuoteAssetOracleClient } from '../oracles/quoteAssetOracleClient';
7
7
  import { BN, Program } from '@coral-xyz/anchor';
8
8
  import { PrelaunchOracleClient } from '../oracles/prelaunchOracleClient';
9
+ import { SwitchboardClient } from '../oracles/switchboardClient';
9
10
 
10
11
  export function getOracleClient(
11
12
  oracleSource: OracleSource,
@@ -28,9 +29,9 @@ export function getOracleClient(
28
29
  return new PythClient(connection, undefined, true);
29
30
  }
30
31
 
31
- // if (isVariant(oracleSource, 'switchboard')) {
32
- // return new SwitchboardClient(connection);
33
- // }
32
+ if (isVariant(oracleSource, 'switchboard')) {
33
+ return new SwitchboardClient(connection);
34
+ }
34
35
 
35
36
  if (isVariant(oracleSource, 'prelaunch')) {
36
37
  return new PrelaunchOracleClient(connection, program);