UNPKG

@drift-labs/sdk

Version:
34 lines (33 loc) 1.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwitchboardOnDemandClient = void 0; const anchor_1 = require("@coral-xyz/anchor"); const switchboard_on_demand_30_json_1 = __importDefault(require("../idl/switchboard_on_demand_30.json")); const numericConstants_1 = require("../constants/numericConstants"); const anchor_30_1 = require("@coral-xyz/anchor-30"); const SB_PRECISION_EXP = new anchor_1.BN(18); const SB_PRECISION = new anchor_1.BN(10).pow(SB_PRECISION_EXP.sub(numericConstants_1.PRICE_PRECISION_EXP)); class SwitchboardOnDemandClient { constructor(connection) { this.connection = connection; this.coder = new anchor_30_1.BorshAccountsCoder(switchboard_on_demand_30_json_1.default); } async getOraclePriceData(pricePublicKey) { const accountInfo = await this.connection.getAccountInfo(pricePublicKey); return this.getOraclePriceDataFromBuffer(accountInfo.data); } getOraclePriceDataFromBuffer(buffer) { const pullFeedAccountData = this.coder.decodeUnchecked('PullFeedAccountData', buffer); const landedAt = pullFeedAccountData.submissions.reduce((max, s) => anchor_1.BN.max(max, s.landed_at), new anchor_1.BN(0)); return { price: pullFeedAccountData.result.value.div(SB_PRECISION), slot: landedAt, confidence: pullFeedAccountData.result.range.div(SB_PRECISION), hasSufficientNumberOfDataPoints: true, }; } } exports.SwitchboardOnDemandClient = SwitchboardOnDemandClient;