@drift-labs/sdk-browser
Version:
SDK for Drift Protocol
41 lines (40 loc) • 1.93 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwitchboardClient = void 0;
const numericConstants_1 = require("../constants/numericConstants");
const switchboard_json_1 = __importDefault(require("../idl/switchboard.json"));
const anchor_1 = require("@coral-xyz/anchor");
class SwitchboardClient {
constructor(connection) {
this.connection = connection;
this.coder = new anchor_1.BorshAccountsCoder(switchboard_json_1.default);
}
async getOraclePriceData(pricePublicKey) {
const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
return this.getOraclePriceDataFromBuffer(accountInfo.data);
}
getOraclePriceDataFromBuffer(buffer) {
const aggregatorAccountData = this.coder.decodeUnchecked('AggregatorAccountData', buffer);
const price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
const confidence = anchor_1.BN.max(convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.stdDeviation), price.divn(1000));
const hasSufficientNumberOfDataPoints = aggregatorAccountData.latestConfirmedRound.numSuccess >=
aggregatorAccountData.minOracleResults;
const slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
return {
price,
slot,
confidence,
hasSufficientNumberOfDataPoints,
};
}
}
exports.SwitchboardClient = SwitchboardClient;
function convertSwitchboardDecimal(switchboardDecimal) {
const switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
return switchboardDecimal.mantissa
.mul(numericConstants_1.PRICE_PRECISION)
.div(switchboardPrecision);
}
;