@d8x/perpetuals-sdk
Version:
Node TypeScript SDK for D8X Perpetual Futures
29 lines • 964 B
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ethers_1 = require("ethers");
/**
* OnChainPxFeed: get a price from a chainlink-style oracle
*/
class OnChainPxFeed {
constructor(rpcs) {
this.lastRpc = 0;
this.lastResponseTs = 0;
this.rpcs = rpcs;
this.lastRpc = Math.floor(Math.random() * rpcs.length);
this.provider = new ethers_1.JsonRpcProvider(this.rpcs[this.lastRpc]);
}
setRpc() {
this.lastRpc = (this.lastRpc + 1) % this.rpcs.length;
this.provider = new ethers_1.JsonRpcProvider(this.rpcs[this.lastRpc]);
}
async getPrice() {
if (this.lastPx == undefined || Date.now() - this.lastResponseTs > 10 * 60 * 1000) {
await this.fetchPrice(false);
return this.lastPx;
}
this.fetchPrice(true);
return this.lastPx;
}
}
exports.default = OnChainPxFeed;
//# sourceMappingURL=onChainPxFeed.js.map