import { __awaiter, __generator } from "tslib";
import assert from "assert";
import { createPublicClient, http, isAddress } from "viem";
import { isDefined } from "../../../utils";
export var WAITFOR_FAIL;
(function (WAITFOR_FAIL) {
    WAITFOR_FAIL[WAITFOR_FAIL["TIMEOUT"] = 0] = "TIMEOUT";
})(WAITFOR_FAIL || (WAITFOR_FAIL = {}));
export function waitFor(providerUrl, eventFilter) {
    var provider = createPublicClient({
        transport: http(providerUrl),
    });
    var address = eventFilter.address;
    var timeout = eventFilter.timeout * 1000;
    assert(!isDefined(address) || 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();
        });
    });
}
function run() {
    return __awaiter(this, void 0, void 0, function () {
        var url, event, filter, result;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    url = process.env.RPC_PROVIDER;
                    assert(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 /*yield*/, 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 /*return*/];
            }
        });
    });
}
if (require.main === module) {
    run()
        .then(function () {
        process.exitCode = 0;
    })
        .catch(function () {
        console.error("Process errored");
        process.exitCode = 9;
    });
}
//# sourceMappingURL=wait.js.map