import { __awaiter, __generator } from "tslib";
import assert from "assert";
import { BigNumber, bnZero, fixedPointAdjustment, getNetworkName, parseUnits } from "../../utils";
import { gasPriceError } from "../util";
/**
 * @dev Constructs total fee by adding eth_getBlock("pending").baseFee to eth_maxPriorityFeePerGas
 * @param provider ethers RPC provider instance.
 * @param {GasPriceEstimateOptions} opts See notes below on specific parameters.
 * @param baseFeeMultiplier Amount to multiply base fee or total fee for legacy gas pricing.
 * @param priorityFeeMultiplier Amount to multiply priority fee or unused for legacy gas pricing.
 * @returns Promise of gas price estimate object.
 */
export function eip1559(provider, opts) {
    return eip1559Raw(provider, opts.chainId, opts.baseFeeMultiplier, opts.priorityFeeMultiplier);
}
/**
 * @note Performs direct RPC calls to retrieve the RPC-suggested priority fee for the next block.
 * @dev Constructs total fee by adding eth_getBlock("pending").baseFee to eth_maxPriorityFeePerGas
 * @param provider ethers RPC provider instance.
 * @param chainId Chain ID of the provider instance.
 * @returns Promise of gas price estimate object.
 */
export function eip1559Raw(provider, chainId, baseFeeMultiplier, priorityFeeMultiplier) {
    return __awaiter(this, void 0, void 0, function () {
        var _a, baseFeePerGas, _maxPriorityFeePerGas, maxPriorityFeePerGas, scaledPriorityFee, flooredPriorityFeePerGas, scaledBaseFee;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0: return [4 /*yield*/, Promise.all([
                        provider.getBlock("latest"),
                        provider.send("eth_maxPriorityFeePerGas", []),
                    ])];
                case 1:
                    _a = _b.sent(), baseFeePerGas = _a[0].baseFeePerGas, _maxPriorityFeePerGas = _a[1];
                    maxPriorityFeePerGas = BigNumber.from(_maxPriorityFeePerGas);
                    assert(BigNumber.isBigNumber(baseFeePerGas), "No baseFeePerGas received on ".concat(getNetworkName(chainId)));
                    scaledPriorityFee = maxPriorityFeePerGas.mul(priorityFeeMultiplier).div(fixedPointAdjustment);
                    flooredPriorityFeePerGas = parseUnits(process.env["MIN_PRIORITY_FEE_PER_GAS_".concat(chainId)] || "0", 9);
                    if (scaledPriorityFee.lt(flooredPriorityFeePerGas)) {
                        scaledPriorityFee = BigNumber.from(flooredPriorityFeePerGas);
                    }
                    scaledBaseFee = baseFeePerGas.mul(baseFeeMultiplier).div(fixedPointAdjustment);
                    return [2 /*return*/, {
                            maxFeePerGas: scaledPriorityFee.add(scaledBaseFee),
                            maxPriorityFeePerGas: scaledPriorityFee,
                        }];
            }
        });
    });
}
/**
 * @notice Returns result of eth_gasPrice RPC call
 * @dev Its recommended to use the eip1559Raw method over this one where possible as it will be more accurate.
 * @returns GasPriceEstimate
 */
export function legacy(provider, opts) {
    return __awaiter(this, void 0, void 0, function () {
        var chainId, baseFeeMultiplier, gasPrice;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    chainId = opts.chainId, baseFeeMultiplier = opts.baseFeeMultiplier;
                    return [4 /*yield*/, provider.getGasPrice()];
                case 1:
                    gasPrice = _a.sent();
                    if (!BigNumber.isBigNumber(gasPrice) || gasPrice.lt(bnZero))
                        gasPriceError("getGasPrice()", chainId, gasPrice);
                    return [2 /*return*/, {
                            maxFeePerGas: gasPrice.mul(baseFeeMultiplier).div(fixedPointAdjustment),
                            maxPriorityFeePerGas: bnZero,
                        }];
            }
        });
    });
}
//# sourceMappingURL=ethereum.js.map