@d8x/perpetuals-sdk
Version:
Node TypeScript SDK for D8X Perpetual Futures
46 lines • 1.77 kB
JavaScript
import { decNToFloat } from "./d8XMath";
import { sleepForSec } from "./utils";
import OnChainPxFeed from "./onChainPxFeed";
import { Contract } from "ethers";
/**
* OnChainPxFeedRedStone: get a price from a chainlink-style oracle
*/
export default class OnChainPxFeedRedStone extends OnChainPxFeed {
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 Contract(this.ctrctAddr, OnChainPxFeedRedStone.abi, this.provider);
let data = await proxyContract.latestRoundData();
this.lastPx = 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 sleepForSec(this.delayMs / 1000);
}
this.fetchInProgress = false;
}
}
}
//static ctrctAddr = "0x119A190b510c9c0D5Ec301b60B2fE70A50356aE9";
OnChainPxFeedRedStone.abi = require("./abi/redStoneAbi.json");
//# sourceMappingURL=onChainPxFeedRedStone.js.map