@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.
- package/VERSION +1 -1
- 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/factory/oracleClient.ts +4 -3
- package/src/idl/switchboard.json +8354 -0
- package/src/oracles/switchboardClient.ts +77 -0
- package/src/types.ts +1 -1
|
@@ -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
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
|
-
|
|
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
|
@@ -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
|
-
|
|
32
|
-
|
|
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);
|