import { __awaiter, __generator } from "tslib";
import { estimateGas } from "viem/linea";
import { DEFAULT_SIMULATED_RELAYER_ADDRESS as account } from "../../constants";
import { fixedPointAdjustment } from "../../utils";
/**
 * @notice The Linea viem provider calls the linea_estimateGas RPC endpoint to estimate gas. Linea is unique
 * in that the recommended fee per gas is hardcoded to 7 wei while the priority fee is dynamic based on the
 * compressed transaction size, layer 1 verification costs and capacity, gas price ratio between layer 1 and layer 2,
 * the transaction's gas usage, the minimum gas price on layer 2,
 * and a minimum margin (for error) for gas price estimation.
 * Source: https://docs.linea.build/get-started/how-to/gas-fees#how-gas-works-on-linea
 * @dev Because the Linea priority fee is more volatile than the base fee, the base fee multiplier will be applied
 * to the priority fee.
 * @param provider Viem PublicClient
 * @param opts Relevant options for Linea are baseFeeMultiplier and unsignedTx.
 * @param baseFeeMultiplier Unused since Linea's base fee is hardcoded while its priority fee is dynamic.
 * @param priorityFeeMultiplier Amount to multiply priority fee.
 * @param unsignedTx Should contain any params passed to linea_estimateGas, which are listed
 * here: https://docs.linea.build/api/reference/linea-estimategas#parameters
 * @returns
 */
export function eip1559(provider, opts) {
    var _a, _b, _c, _d, _e;
    return __awaiter(this, void 0, void 0, function () {
        var _unsignedTx, priorityFeeMultiplier, unsignedTx, _f, baseFeePerGas, _priorityFeePerGas, priorityFeePerGas;
        return __generator(this, function (_g) {
            switch (_g.label) {
                case 0:
                    _unsignedTx = opts.unsignedTx, priorityFeeMultiplier = opts.priorityFeeMultiplier;
                    unsignedTx = _unsignedTx;
                    return [4 /*yield*/, estimateGas(provider, {
                            account: (_a = unsignedTx === null || unsignedTx === void 0 ? void 0 : unsignedTx.from) !== null && _a !== void 0 ? _a : account,
                            to: (_b = unsignedTx === null || unsignedTx === void 0 ? void 0 : unsignedTx.to) !== null && _b !== void 0 ? _b : account,
                            value: BigInt((_d = (_c = unsignedTx === null || unsignedTx === void 0 ? void 0 : unsignedTx.value) === null || _c === void 0 ? void 0 : _c.toString()) !== null && _d !== void 0 ? _d : "0"),
                            data: (_e = unsignedTx === null || unsignedTx === void 0 ? void 0 : unsignedTx.data) !== null && _e !== void 0 ? _e : "0x",
                        })];
                case 1:
                    _f = _g.sent(), baseFeePerGas = _f.baseFeePerGas, _priorityFeePerGas = _f.priorityFeePerGas;
                    priorityFeePerGas = (_priorityFeePerGas * BigInt(priorityFeeMultiplier.toString())) / BigInt(fixedPointAdjustment.toString());
                    return [2 /*return*/, {
                            maxFeePerGas: baseFeePerGas + priorityFeePerGas,
                            maxPriorityFeePerGas: priorityFeePerGas,
                        }];
            }
        });
    });
}
//# sourceMappingURL=linea-viem.js.map