@pythnetwork/price-pusher
Version:
Pyth Price Pusher
41 lines (40 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomGasStation = void 0;
exports.getCustomGasStation = getCustomGasStation;
const utils_1 = require("../utils");
const viem_1 = require("viem");
class CustomGasStation {
chain;
speed;
chainMethods = {
137: this.fetchMaticMainnetGasPrice.bind(this),
};
logger;
constructor(logger, chain, speed) {
this.logger = logger;
this.speed = (0, utils_1.verifyValidOption)(speed, utils_1.txSpeeds);
this.chain = (0, utils_1.verifyValidOption)(chain, utils_1.customGasChainIds);
}
async getCustomGasPrice() {
return this.chainMethods[this.chain]();
}
async fetchMaticMainnetGasPrice() {
try {
const res = await fetch("https://gasstation.polygon.technology/v2");
const jsonRes = await res.json();
const gasPrice = jsonRes[this.speed].maxFee;
return (0, viem_1.parseGwei)(gasPrice.toFixed(2));
}
catch (err) {
this.logger.error(err, "Failed to fetch gas price from Matic mainnet. Returning undefined");
return undefined;
}
}
}
exports.CustomGasStation = CustomGasStation;
function getCustomGasStation(logger, customGasStation, txSpeed) {
if (customGasStation && txSpeed) {
return new CustomGasStation(logger, customGasStation, txSpeed);
}
}