dop-stick
Version:
Source control tooling for versionable-upgradeable smart contracts
87 lines • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GasUtils = void 0;
const ethers_1 = require("ethers");
const logger_1 = require("./logsAndMetrics/core/logger");
const provider_1 = require("./provider");
class GasUtils {
/**
* Gets current network gas price with optional priority fee
*/
static async getCurrentGasPrice() {
var _a;
const provider = await (0, provider_1.getProvider)();
const feeData = await provider.getFeeData();
return {
gasPrice: feeData.gasPrice || ethers_1.BigNumber.from(0),
maxPriorityFeePerGas: (_a = feeData.maxPriorityFeePerGas) !== null && _a !== void 0 ? _a : undefined
};
}
/**
* Helper method to safely convert null to undefined for fee data
*/
static nullToUndefined(value) {
return value === null ? undefined : value;
}
/**
* Helper method to safely get fee data with proper types
*/
static async getSafeFeeData() {
const provider = await (0, provider_1.getProvider)();
const feeData = await provider.getFeeData();
return {
gasPrice: feeData.gasPrice || ethers_1.BigNumber.from(0),
baseFee: this.nullToUndefined(feeData.lastBaseFeePerGas),
priorityFee: this.nullToUndefined(feeData.maxPriorityFeePerGas)
};
}
/**
* Checks if the current network has zero gas price
*/
static async isGasPriceZero() {
try {
const fees = await this.getSafeFeeData();
// For EIP-1559 networks
if (fees.baseFee && fees.priorityFee) {
return fees.baseFee.isZero() && fees.priorityFee.isZero();
}
// For legacy networks
return fees.gasPrice.isZero();
}
catch (error) {
logger_1.Logger.warning('Failed to check gas price, assuming non-zero gas price');
logger_1.Logger.error(`Gas price check error: ${error instanceof Error ? error.message : String(error)}`);
return false;
}
}
/**
* Gets detailed gas price information
*/
static async getGasPriceInfo() {
try {
const fees = await this.getSafeFeeData();
return {
isZero: await this.isGasPriceZero(),
gasPrice: ethers_1.ethers.utils.formatUnits(fees.gasPrice, 'gwei'),
baseFee: fees.baseFee ?
ethers_1.ethers.utils.formatUnits(fees.baseFee, 'gwei') :
undefined,
priorityFee: fees.priorityFee ?
ethers_1.ethers.utils.formatUnits(fees.priorityFee, 'gwei') :
undefined
};
}
catch (error) {
logger_1.Logger.error(`Failed to get gas price info: ${error instanceof Error ? error.message : String(error)}`);
throw error;
}
}
/**
* Calculates safe gas limit with buffer
*/
static calculateSafeGasLimit(baseLimit, bufferPercentage = 20) {
return baseLimit.mul(100 + bufferPercentage).div(100);
}
}
exports.GasUtils = GasUtils;
//# sourceMappingURL=gasUtils.js.map