tornado-oracles/lib/providers.js

94 lines
4.3 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProvider = exports.getGasOraclePlugin = void 0;
const ethers_1 = require("ethers");
const contracts_1 = require("./contracts");
const constants_1 = require("./constants");
const multicall_1 = require("./multicall");
const utils_1 = require("./utils");
// caching to improve performance
const oracleMapper = new Map();
const multicallMapper = new Map();
function getGasOraclePlugin(networkKey, gasOracleOptions) {
const gasStationApi = (gasOracleOptions === null || gasOracleOptions === void 0 ? void 0 : gasOracleOptions.gasStationApi) || constants_1.POLYGON_GAS_STATION;
return new ethers_1.FetchUrlFeeDataNetworkPlugin(gasStationApi, (fetchFeeData, provider, request) => __awaiter(this, void 0, void 0, function* () {
if (!oracleMapper.has(networkKey)) {
oracleMapper.set(networkKey, contracts_1.GasPriceOracle__factory.connect(gasOracleOptions === null || gasOracleOptions === void 0 ? void 0 : gasOracleOptions.gasPriceOracle, provider));
}
if (!multicallMapper.has(networkKey)) {
multicallMapper.set(networkKey, contracts_1.Multicall__factory.connect(constants_1.multiCallAddress, provider));
}
const Oracle = oracleMapper.get(networkKey);
const Multicall = multicallMapper.get(networkKey);
const [timestamp, heartbeat, feePerGas, priorityFeePerGas] = yield (0, multicall_1.multicall)(Multicall, [
{
contract: Oracle,
name: 'timestamp',
},
{
contract: Oracle,
name: 'heartbeat',
},
{
contract: Oracle,
name: 'maxFeePerGas',
},
{
contract: Oracle,
name: 'maxPriorityFeePerGas',
},
]);
const isOutdated = Number(timestamp) <= Date.now() / 1000 - Number(heartbeat);
if (!isOutdated) {
const maxPriorityFeePerGas = (priorityFeePerGas * BigInt(13)) / BigInt(10);
const maxFeePerGas = feePerGas * BigInt(2) + maxPriorityFeePerGas;
return {
gasPrice: maxFeePerGas,
maxFeePerGas,
maxPriorityFeePerGas,
};
}
if (utils_1.isNode) {
// Prevent Cloudflare from blocking our request in node.js
request.setHeader('User-Agent', 'ethers');
}
const [{ bodyJson: { fast }, }, { gasPrice },] = yield Promise.all([request.send(), fetchFeeData()]);
return {
gasPrice,
maxFeePerGas: (0, ethers_1.parseUnits)(`${fast.maxFee}`, 9),
maxPriorityFeePerGas: (0, ethers_1.parseUnits)(`${fast.maxPriorityFee}`, 9),
};
}));
}
exports.getGasOraclePlugin = getGasOraclePlugin;
function getProvider(netId, rpcUrl, config) {
const { networkName, gasPriceOracleContract, gasStationApi, pollInterval } = config;
const hasEns = constants_1.ENS_CHAINS.includes(netId);
const staticNetwork = new ethers_1.Network(networkName, netId);
if (hasEns) {
staticNetwork.attachPlugin(new ethers_1.EnsPlugin(null, Number(netId)));
}
staticNetwork.attachPlugin(new ethers_1.GasCostPlugin());
if (gasPriceOracleContract) {
staticNetwork.attachPlugin(getGasOraclePlugin(`${netId}_${rpcUrl}`, {
gasPriceOracle: gasPriceOracleContract,
gasStationApi,
}));
}
const provider = new ethers_1.JsonRpcProvider(rpcUrl, staticNetwork, {
staticNetwork,
});
provider.pollingInterval = pollInterval * 1000;
return provider;
}
exports.getProvider = getProvider;
//# sourceMappingURL=providers.js.map