@holographxyz/cli
Version:
Holograph operator CLI
88 lines (87 loc) • 3.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateGasPricing = exports.initializeGasPricing = exports.adjustBaseBlockFee = exports.calculateNextBlockFee = void 0;
const bignumber_1 = require("@ethersproject/bignumber");
const networks_1 = require("@holographxyz/networks");
// Implemented from https://eips.ethereum.org/EIPS/eip-1559
function calculateNextBlockFee(parent) {
const zero = bignumber_1.BigNumber.from('0');
if (parent.baseFeePerGas === undefined) {
return zero;
}
const one = bignumber_1.BigNumber.from('1');
const elasticityMultiplier = bignumber_1.BigNumber.from('2');
const baseFeeMaxChangeDenominator = bignumber_1.BigNumber.from('8');
const baseFeePerGas = parent.baseFeePerGas;
const parentGasTarget = parent.gasLimit.div(elasticityMultiplier);
if (parent.gasUsed.eq(parentGasTarget)) {
return baseFeePerGas;
}
let gasUsedDelta;
let baseFeeDelta;
// If the parent block used more gas than its target, the baseFee should increase.
if (parent.gasUsed.gt(parentGasTarget)) {
gasUsedDelta = parent.gasUsed.sub(parentGasTarget);
baseFeeDelta = baseFeePerGas.mul(gasUsedDelta).div(parentGasTarget).div(baseFeeMaxChangeDenominator);
if (one.gt(baseFeeDelta)) {
baseFeeDelta = one;
}
return baseFeePerGas.add(baseFeeDelta);
}
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
gasUsedDelta = parentGasTarget.sub(parent.gasUsed);
baseFeeDelta = baseFeePerGas.mul(gasUsedDelta).div(parentGasTarget).div(baseFeeMaxChangeDenominator);
return baseFeePerGas.sub(baseFeeDelta);
}
exports.calculateNextBlockFee = calculateNextBlockFee;
// This function is here to accomodate instances where a network has a minimum BaseBlockFee
function adjustBaseBlockFee(network, baseBlockFee) {
// Avalanche has a minimum BaseBlockFee of 25 GWEI
// https://docs.avax.network/quickstart/transaction-fees#base-fee
if ((network === networks_1.networks['avalanche'].key ||
network === networks_1.networks['avalancheTestnet'].key) &&
baseBlockFee.lt(bignumber_1.BigNumber.from('25000000000'))) {
return bignumber_1.BigNumber.from('25000000000');
}
return baseBlockFee;
}
exports.adjustBaseBlockFee = adjustBaseBlockFee;
async function initializeGasPricing(network, provider) {
const block = await provider.getBlock('latest');
const gasPrices = updateGasPricing(network, block, {
isEip1559: false,
gasPrice: null,
nextBlockFee: null,
nextPriorityFee: null,
maxFeePerGas: null,
lowestBlockFee: null,
averageBlockFee: null,
highestBlockFee: null,
lowestPriorityFee: null,
averagePriorityFee: null,
highestPriorityFee: null,
});
if (!gasPrices.isEip1559) {
// need to replace this with internal calculations
gasPrices.gasPrice = await provider.getGasPrice();
}
return gasPrices;
}
exports.initializeGasPricing = initializeGasPricing;
function updateGasPricing(network, block, gasPricing) {
if (block.baseFeePerGas) {
gasPricing.isEip1559 = true;
gasPricing.nextBlockFee = adjustBaseBlockFee(network, calculateNextBlockFee(block));
gasPricing.maxFeePerGas = gasPricing.nextBlockFee;
if (gasPricing.nextPriorityFee === null) {
gasPricing.nextPriorityFee = bignumber_1.BigNumber.from('0');
gasPricing.gasPrice = gasPricing.nextBlockFee;
}
else {
gasPricing.maxFeePerGas = gasPricing.nextBlockFee.add(gasPricing.nextPriorityFee);
gasPricing.gasPrice = gasPricing.maxFeePerGas;
}
}
return gasPricing;
}
exports.updateGasPricing = updateGasPricing;