"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.legacy = exports.eip1559Raw = exports.eip1559 = void 0;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importDefault(require("assert"));
var utils_1 = require("../../utils");
var util_1 = require("../util");
function eip1559(provider, opts) {
    return eip1559Raw(provider, opts.chainId, opts.baseFeeMultiplier, opts.priorityFeeMultiplier);
}
exports.eip1559 = eip1559;
function eip1559Raw(provider, chainId, baseFeeMultiplier, priorityFeeMultiplier) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var _a, baseFeePerGas, _maxPriorityFeePerGas, maxPriorityFeePerGas, scaledPriorityFee, flooredPriorityFeePerGas, scaledBaseFee;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0: return [4, Promise.all([
                        provider.getBlock("latest"),
                        provider.send("eth_maxPriorityFeePerGas", []),
                    ])];
                case 1:
                    _a = _b.sent(), baseFeePerGas = _a[0].baseFeePerGas, _maxPriorityFeePerGas = _a[1];
                    maxPriorityFeePerGas = utils_1.BigNumber.from(_maxPriorityFeePerGas);
                    (0, assert_1.default)(utils_1.BigNumber.isBigNumber(baseFeePerGas), "No baseFeePerGas received on ".concat((0, utils_1.getNetworkName)(chainId)));
                    scaledPriorityFee = maxPriorityFeePerGas.mul(priorityFeeMultiplier).div(utils_1.fixedPointAdjustment);
                    flooredPriorityFeePerGas = (0, utils_1.parseUnits)(process.env["MIN_PRIORITY_FEE_PER_GAS_".concat(chainId)] || "0", 9);
                    if (scaledPriorityFee.lt(flooredPriorityFeePerGas)) {
                        scaledPriorityFee = utils_1.BigNumber.from(flooredPriorityFeePerGas);
                    }
                    scaledBaseFee = baseFeePerGas.mul(baseFeeMultiplier).div(utils_1.fixedPointAdjustment);
                    return [2, {
                            maxFeePerGas: scaledPriorityFee.add(scaledBaseFee),
                            maxPriorityFeePerGas: scaledPriorityFee,
                        }];
            }
        });
    });
}
exports.eip1559Raw = eip1559Raw;
function legacy(provider, opts) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var chainId, baseFeeMultiplier, gasPrice;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    chainId = opts.chainId, baseFeeMultiplier = opts.baseFeeMultiplier;
                    return [4, provider.getGasPrice()];
                case 1:
                    gasPrice = _a.sent();
                    if (!utils_1.BigNumber.isBigNumber(gasPrice) || gasPrice.lt(utils_1.bnZero))
                        (0, util_1.gasPriceError)("getGasPrice()", chainId, gasPrice);
                    return [2, {
                            maxFeePerGas: gasPrice.mul(baseFeeMultiplier).div(utils_1.fixedPointAdjustment),
                            maxPriorityFeePerGas: utils_1.bnZero,
                        }];
            }
        });
    });
}
exports.legacy = legacy;
//# sourceMappingURL=ethereum.js.map