UNPKG

@d8x/perpetuals-sdk

Version:

Node TypeScript SDK for D8X Perpetual Futures

52 lines 2.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const d8XMath_1 = require("./d8XMath"); const utils_1 = require("./utils"); const onChainPxFeed_1 = __importDefault(require("./onChainPxFeed")); const ethers_1 = require("ethers"); /** * OnChainPxFeedRedStone: get a price from a chainlink-style oracle */ class OnChainPxFeedRedStone extends onChainPxFeed_1.default { constructor(ctrctAddr, decimals, rpcs) { super(rpcs); this.delayMs = 5000; this.fetchInProgress = false; this.ctrctAddr = ctrctAddr; this.decimals = decimals; } async fetchPrice(delay) { if (!this.fetchInProgress) { this.fetchInProgress = true; this.lastResponseTs = Date.now(); let hasErr = false; for (let trial = 0; trial < this.rpcs.length; trial++) { try { let proxyContract = new ethers_1.Contract(this.ctrctAddr, OnChainPxFeedRedStone.abi, this.provider); let data = await proxyContract.latestRoundData(); this.lastPx = (0, d8XMath_1.decNToFloat)(data.answer, this.decimals); break; } catch (err) { console.log(`onChainPxSources error ${this.rpcs[this.lastRpc]}: retrying with other rpc`); hasErr = true; } this.setRpc(); } if (hasErr) { console.log("onChainPxSources not successful"); } if (delay) { await (0, utils_1.sleepForSec)(this.delayMs / 1000); } this.fetchInProgress = false; } } } exports.default = OnChainPxFeedRedStone; //static ctrctAddr = "0x119A190b510c9c0D5Ec301b60B2fE70A50356aE9"; OnChainPxFeedRedStone.abi = require("./abi/redStoneAbi.json"); //# sourceMappingURL=onChainPxFeedRedStone.js.map