UNPKG

@d8x/perpetuals-sdk

Version:

Node TypeScript SDK for D8X Perpetual Futures

26 lines 857 B
import { JsonRpcProvider } from "ethers"; /** * OnChainPxFeed: get a price from a chainlink-style oracle */ export default class OnChainPxFeed { constructor(rpcs) { this.lastRpc = 0; this.lastResponseTs = 0; this.rpcs = rpcs; this.lastRpc = Math.floor(Math.random() * rpcs.length); this.provider = new JsonRpcProvider(this.rpcs[this.lastRpc]); } setRpc() { this.lastRpc = (this.lastRpc + 1) % this.rpcs.length; this.provider = new 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; } } //# sourceMappingURL=onChainPxFeed.js.map