"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Type2TxMgr = void 0;
const ethers_1 = require("ethers");
class Type2TxMgr {
    static priceBumpPercent = ethers_1.BigNumber.from(110); // 100 + priceBump
    static blobPriceBumpPercent = ethers_1.BigNumber.from(200); // 100 + blobPriceBump
    static oneHundred = ethers_1.BigNumber.from(100);
    static ninetyNine = ethers_1.BigNumber.from(99);
    static two = ethers_1.BigNumber.from(2);
    static calcThresholdValue(x, isBlobTx) {
        const threshold = isBlobTx ? Type2TxMgr.blobPriceBumpPercent : Type2TxMgr.priceBumpPercent;
        return threshold.mul(x).add(Type2TxMgr.ninetyNine).div(Type2TxMgr.oneHundred);
    }
    static calcGasFeeCap(baseFee, gasTipCap) {
        return gasTipCap.add(baseFee.mul(Type2TxMgr.two));
    }
    static updateFees(oldTip, oldFeeCap, newTip, newBaseFee, isBlobTx) {
        const newFeeCap = Type2TxMgr.calcGasFeeCap(newBaseFee, newTip);
        const thresholdTip = Type2TxMgr.calcThresholdValue(oldTip, isBlobTx);
        const thresholdFeeCap = Type2TxMgr.calcThresholdValue(oldFeeCap, isBlobTx);
        if (newTip.gte(thresholdTip) && newFeeCap.gte(thresholdFeeCap)) {
            return [newTip, newFeeCap];
        }
        else if (newTip.gte(thresholdTip)) {
            return [newTip, thresholdFeeCap];
        }
        else if (newFeeCap.gte(thresholdFeeCap)) {
            return [thresholdTip, Type2TxMgr.calcGasFeeCap(newBaseFee, thresholdTip)];
        }
        else {
            return [thresholdTip, thresholdFeeCap];
        }
    }
    /*
       Example usage:
          let [updatedTip, updatedFeeCap] = Type2TxMgr.updateFeesWithRetry(
              feeData.maxPriorityFeePerGas,
              feeData.maxFeePerGas,
              feeData.maxPriorityFeePerGas,
              feeData.lastBaseFeePerGas,
              false, // isBlobTx, adjust as necessary
              queueItem.retries,
          )
          txParams.maxFeePerGas = updatedFeeCap
          txParams.maxPriorityFeePerGas = updatedTip
       */
    static updateFeesWithRetry(oldTip, oldFeeCap, newTip, newBaseFee, isBlobTx, maxRetries) {
        let retries = 0;
        let updatedTip;
        let updatedFeeCap;
        if (newBaseFee === null) {
            newBaseFee = oldFeeCap;
        }
        if (maxRetries < 1) {
            return [newTip, newBaseFee.add(newBaseFee)];
        }
        do {
            ;
            [updatedTip, updatedFeeCap] = Type2TxMgr.updateFees(oldTip, oldFeeCap, newTip, newBaseFee, isBlobTx);
            retries++;
        } while (retries < maxRetries);
        return [updatedTip, updatedFeeCap.add(newBaseFee)];
    }
}
exports.Type2TxMgr = Type2TxMgr;
//# sourceMappingURL=type2.mngr.js.map