"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.gasStation = exports.MockPolygonGasStationPriorityFee = exports.MockPolygonGasStationBaseFee = exports.PolygonGasStation = void 0;
var tslib_1 = require("tslib");
var baseAdapter_1 = require("../../priceClient/adapters/baseAdapter");
var utils_1 = require("../../utils");
var constants_1 = require("../../constants");
var util_1 = require("../util");
var ethereum_1 = require("./ethereum");
var POLYGON = constants_1.CHAIN_IDs.POLYGON;
var PolygonGasStation = (function (_super) {
    tslib_1.__extends(PolygonGasStation, _super);
    function PolygonGasStation(_a) {
        var _this = this;
        var _b = _a === void 0 ? {} : _a, _c = _b.chainId, chainId = _c === void 0 ? POLYGON : _c, host = _b.host, _d = _b.timeout, timeout = _d === void 0 ? 1500 : _d, _e = _b.retries, retries = _e === void 0 ? 1 : _e;
        host = (host !== null && host !== void 0 ? host : chainId === POLYGON) ? "gasstation.polygon.technology" : "gasstation-testnet.polygon.technology";
        _this = _super.call(this, "Polygon Gas Station", host, { timeout: timeout, retries: retries }) || this;
        _this.chainId = chainId;
        return _this;
    }
    PolygonGasStation.prototype.getFeeData = function (strategy) {
        if (strategy === void 0) { strategy = "fast"; }
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var gas, gasPrice, maxPriorityFeePerGas, maxFeePerGas;
            var _this = this;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4, this.query("v2", {})];
                    case 1:
                        gas = _a.sent();
                        gasPrice = gas === null || gas === void 0 ? void 0 : gas[strategy];
                        if (!this.isPolygon1559GasPrice(gasPrice)) {
                            (0, util_1.gasPriceError)("getFeeData()", this.chainId, utils_1.bnZero);
                        }
                        [gasPrice.maxFee, gasPrice.maxPriorityFee].forEach(function (gasPrice) {
                            if (Number(gasPrice) < 0) {
                                (0, util_1.gasPriceError)("getFeeData()", _this.chainId, (0, utils_1.parseUnits)(gasPrice.toString(), 9));
                            }
                        });
                        maxPriorityFeePerGas = (0, utils_1.parseUnits)(gasPrice.maxPriorityFee.toString(), 9);
                        maxFeePerGas = (0, utils_1.parseUnits)(gasPrice.maxFee.toString(), 9);
                        return [2, { maxPriorityFeePerGas: maxPriorityFeePerGas, maxFeePerGas: maxFeePerGas }];
                }
            });
        });
    };
    PolygonGasStation.prototype.isPolygon1559GasPrice = function (gasPrice) {
        if (!(0, utils_1.isDefined)(gasPrice)) {
            return false;
        }
        var _gasPrice = gasPrice;
        return [_gasPrice.maxPriorityFee, _gasPrice.maxFee].every(function (field) { return ["number", "string"].includes(typeof field); });
    };
    return PolygonGasStation;
}(baseAdapter_1.BaseHTTPAdapter));
exports.PolygonGasStation = PolygonGasStation;
var MockRevertingPolygonGasStation = (function (_super) {
    tslib_1.__extends(MockRevertingPolygonGasStation, _super);
    function MockRevertingPolygonGasStation() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    MockRevertingPolygonGasStation.prototype.getFeeData = function () {
        throw new Error();
    };
    return MockRevertingPolygonGasStation;
}(PolygonGasStation));
var MockPolygonGasStationBaseFee = function () { return (0, utils_1.parseUnits)("12", 9); };
exports.MockPolygonGasStationBaseFee = MockPolygonGasStationBaseFee;
var MockPolygonGasStationPriorityFee = function () { return (0, utils_1.parseUnits)("1", 9); };
exports.MockPolygonGasStationPriorityFee = MockPolygonGasStationPriorityFee;
var MockPolygonGasStation = (function (_super) {
    tslib_1.__extends(MockPolygonGasStation, _super);
    function MockPolygonGasStation() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    MockPolygonGasStation.prototype.getFeeData = function () {
        return Promise.resolve({
            maxPriorityFeePerGas: (0, exports.MockPolygonGasStationPriorityFee)(),
            maxFeePerGas: (0, exports.MockPolygonGasStationBaseFee)().add((0, exports.MockPolygonGasStationPriorityFee)()),
        });
    };
    return MockPolygonGasStation;
}(PolygonGasStation));
function gasStation(provider, opts) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var chainId, baseFeeMultiplier, priorityFeeMultiplier, gasStation, maxPriorityFeePerGas, maxFeePerGas, baseFeeMinusPriorityFee, scaledBaseFee, scaledPriorityFee, err_1, minPriorityFee, priorityDelta;
        var _a, _b;
        return tslib_1.__generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    chainId = opts.chainId, baseFeeMultiplier = opts.baseFeeMultiplier, priorityFeeMultiplier = opts.priorityFeeMultiplier;
                    if (process.env.TEST_POLYGON_GAS_STATION === "true") {
                        gasStation = new MockPolygonGasStation();
                    }
                    else if (process.env.TEST_REVERTING_POLYGON_GAS_STATION === "true") {
                        gasStation = new MockRevertingPolygonGasStation();
                    }
                    else {
                        gasStation = new PolygonGasStation({ chainId: chainId, timeout: 2000, retries: 0 });
                    }
                    _c.label = 1;
                case 1:
                    _c.trys.push([1, 3, , 5]);
                    return [4, gasStation.getFeeData()];
                case 2:
                    (_a = _c.sent(), maxPriorityFeePerGas = _a.maxPriorityFeePerGas, maxFeePerGas = _a.maxFeePerGas);
                    baseFeeMinusPriorityFee = maxFeePerGas.sub(maxPriorityFeePerGas);
                    scaledBaseFee = baseFeeMinusPriorityFee.mul(baseFeeMultiplier).div(utils_1.fixedPointAdjustment);
                    scaledPriorityFee = maxPriorityFeePerGas.mul(priorityFeeMultiplier).div(utils_1.fixedPointAdjustment);
                    maxFeePerGas = scaledBaseFee.add(scaledPriorityFee);
                    maxPriorityFeePerGas = scaledPriorityFee;
                    return [3, 5];
                case 3:
                    err_1 = _c.sent();
                    return [4, (0, ethereum_1.eip1559)(provider, opts)];
                case 4:
                    (_b = _c.sent(), maxPriorityFeePerGas = _b.maxPriorityFeePerGas, maxFeePerGas = _b.maxFeePerGas);
                    minPriorityFee = (0, utils_1.parseUnits)("30", 9).mul(priorityFeeMultiplier).div(utils_1.fixedPointAdjustment);
                    if (maxPriorityFeePerGas.lt(minPriorityFee)) {
                        priorityDelta = minPriorityFee.sub(maxPriorityFeePerGas);
                        maxPriorityFeePerGas = minPriorityFee;
                        maxFeePerGas = maxFeePerGas.add(priorityDelta);
                    }
                    return [3, 5];
                case 5: return [2, { maxPriorityFeePerGas: maxPriorityFeePerGas, maxFeePerGas: maxFeePerGas }];
            }
        });
    });
}
exports.gasStation = gasStation;
//# sourceMappingURL=polygon.js.map