@drift-labs/sdk 2.71.0-beta.3 → 2.71.0-beta.5
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/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +27 -3
- 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/math/oracles.ts +30 -3
- package/src/oracles/switchboardClient.ts +77 -0
- package/src/types.ts +1 -1
- package/tests/amm/test.ts +8 -8
package/lib/math/oracles.d.ts
CHANGED
|
@@ -3,7 +3,8 @@ import { AMM, OracleGuardRails } from '../types';
|
|
|
3
3
|
import { OraclePriceData } from '../oracles/types';
|
|
4
4
|
import { BN, HistoricalOracleData, PerpMarketAccount } from '../index';
|
|
5
5
|
export declare function oraclePriceBands(market: PerpMarketAccount, oraclePriceData: OraclePriceData): [BN, BN];
|
|
6
|
-
export declare function
|
|
6
|
+
export declare function getMaxConfidenceIntervalMultiplier(market: PerpMarketAccount): BN;
|
|
7
|
+
export declare function isOracleValid(market: PerpMarketAccount, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, slot: number): boolean;
|
|
7
8
|
export declare function isOracleTooDivergent(amm: AMM, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, now: BN): boolean;
|
|
8
9
|
export declare function calculateLiveOracleTwap(histOracleData: HistoricalOracleData, oraclePriceData: OraclePriceData, now: BN, period: BN): BN;
|
|
9
10
|
export declare function calculateLiveOracleStd(amm: AMM, oraclePriceData: OraclePriceData, now: BN): BN;
|
package/lib/math/oracles.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.oraclePriceBands = void 0;
|
|
3
|
+
exports.getNewOracleConfPct = exports.calculateLiveOracleStd = exports.calculateLiveOracleTwap = exports.isOracleTooDivergent = exports.isOracleValid = exports.getMaxConfidenceIntervalMultiplier = exports.oraclePriceBands = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
4
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
6
|
const index_1 = require("../index");
|
|
6
7
|
const assert_1 = require("../assert/assert");
|
|
@@ -13,7 +14,29 @@ function oraclePriceBands(market, oraclePriceData) {
|
|
|
13
14
|
return [oraclePriceData.price.sub(offset), oraclePriceData.price.add(offset)];
|
|
14
15
|
}
|
|
15
16
|
exports.oraclePriceBands = oraclePriceBands;
|
|
16
|
-
function
|
|
17
|
+
function getMaxConfidenceIntervalMultiplier(market) {
|
|
18
|
+
let maxConfidenceIntervalMultiplier;
|
|
19
|
+
if ((0, types_1.isVariant)(market.contractTier, 'a')) {
|
|
20
|
+
maxConfidenceIntervalMultiplier = new index_1.BN(1);
|
|
21
|
+
}
|
|
22
|
+
else if ((0, types_1.isVariant)(market.contractTier, 'b')) {
|
|
23
|
+
maxConfidenceIntervalMultiplier = new index_1.BN(1);
|
|
24
|
+
}
|
|
25
|
+
else if ((0, types_1.isVariant)(market.contractTier, 'c')) {
|
|
26
|
+
maxConfidenceIntervalMultiplier = new index_1.BN(2);
|
|
27
|
+
}
|
|
28
|
+
else if ((0, types_1.isVariant)(market.contractTier, 'speculative')) {
|
|
29
|
+
maxConfidenceIntervalMultiplier = new index_1.BN(10);
|
|
30
|
+
}
|
|
31
|
+
else if ((0, types_1.isVariant)(market.contractTier, 'isolated')) {
|
|
32
|
+
maxConfidenceIntervalMultiplier = new index_1.BN(50);
|
|
33
|
+
}
|
|
34
|
+
return maxConfidenceIntervalMultiplier;
|
|
35
|
+
}
|
|
36
|
+
exports.getMaxConfidenceIntervalMultiplier = getMaxConfidenceIntervalMultiplier;
|
|
37
|
+
function isOracleValid(market, oraclePriceData, oracleGuardRails, slot) {
|
|
38
|
+
// checks if oracle is valid for an AMM only fill
|
|
39
|
+
const amm = market.amm;
|
|
17
40
|
const isOraclePriceNonPositive = oraclePriceData.price.lte(numericConstants_1.ZERO);
|
|
18
41
|
const isOraclePriceTooVolatile = oraclePriceData.price
|
|
19
42
|
.div(index_1.BN.max(numericConstants_1.ONE, amm.historicalOracleData.lastOraclePriceTwap))
|
|
@@ -21,10 +44,11 @@ function isOracleValid(amm, oraclePriceData, oracleGuardRails, slot) {
|
|
|
21
44
|
amm.historicalOracleData.lastOraclePriceTwap
|
|
22
45
|
.div(index_1.BN.max(numericConstants_1.ONE, oraclePriceData.price))
|
|
23
46
|
.gt(oracleGuardRails.validity.tooVolatileRatio);
|
|
47
|
+
const maxConfidenceIntervalMultiplier = getMaxConfidenceIntervalMultiplier(market);
|
|
24
48
|
const isConfidenceTooLarge = index_1.BN.max(numericConstants_1.ONE, oraclePriceData.confidence)
|
|
25
49
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
26
50
|
.div(oraclePriceData.price)
|
|
27
|
-
.gt(oracleGuardRails.validity.confidenceIntervalMaxSize);
|
|
51
|
+
.gt(oracleGuardRails.validity.confidenceIntervalMaxSize.mul(maxConfidenceIntervalMultiplier));
|
|
28
52
|
const oracleIsStale = new index_1.BN(slot)
|
|
29
53
|
.sub(oraclePriceData.slot)
|
|
30
54
|
.gt(oracleGuardRails.validity.slotsBeforeStaleForAmm);
|
|
@@ -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);
|