"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.waitFor = exports.WAITFOR_FAIL = void 0;
var tslib_1 = require("tslib");
var assert_1 = tslib_1.__importDefault(require("assert"));
var viem_1 = require("viem");
var utils_1 = require("../../../utils");
var WAITFOR_FAIL;
(function (WAITFOR_FAIL) {
    WAITFOR_FAIL[WAITFOR_FAIL["TIMEOUT"] = 0] = "TIMEOUT";
})(WAITFOR_FAIL || (exports.WAITFOR_FAIL = WAITFOR_FAIL = {}));
function waitFor(providerUrl, eventFilter) {
    var provider = (0, viem_1.createPublicClient)({
        transport: (0, viem_1.http)(providerUrl),
    });
    var address = eventFilter.address;
    var timeout = eventFilter.timeout * 1000;
    (0, assert_1.default)(!(0, utils_1.isDefined)(address) || (0, viem_1.isAddress)(address));
    return new Promise(function (resolve) {
        var abortController = new AbortController();
        var onLogs = function (logs) {
            var txnRef = logs[0].transactionHash;
            abortController.abort();
            resolve({ error: false, txnRef: txnRef });
        };
        var timeoutId = setTimeout(function () {
            abortController.abort();
            resolve({ error: true, reason: WAITFOR_FAIL.TIMEOUT });
        }, timeout);
        var unsub = provider.watchEvent({ address: address, onLogs: onLogs });
        abortController.signal.addEventListener("abort", function () {
            clearTimeout(timeoutId);
            unsub();
        });
    });
}
exports.waitFor = waitFor;
function run() {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var url, event, filter, result;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    url = process.env.RPC_PROVIDER;
                    (0, assert_1.default)((0, utils_1.isDefined)(url), "No mainnet RPC_PROVIDER defined");
                    event = "RootBundleExecuted(uint256,uint256,uint256,address[],uint256[],int256[],int256[],address)";
                    filter = {
                        address: "0xc186fA914353c44b2E33eBE05f21846F1048bEda",
                        event: event,
                        timeout: 5400,
                    };
                    return [4, waitFor(url, filter)];
                case 1:
                    result = _a.sent();
                    if (result.error) {
                        switch (result.reason) {
                            case WAITFOR_FAIL.TIMEOUT:
                                console.log("Error: timed out waiting for event");
                                break;
                            default:
                                console.log("Unknown error (".concat(result.reason, ")"));
                        }
                        throw Error;
                    }
                    console.log("Event transaction hash: ".concat(result.txnRef));
                    return [2];
            }
        });
    });
}
if (require.main === module) {
    run()
        .then(function () {
        process.exitCode = 0;
    })
        .catch(function () {
        console.error("Process errored");
        process.exitCode = 9;
    });
}
//# sourceMappingURL=wait.js.map