"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheType = void 0;
exports.isSupportedProvider = isSupportedProvider;
exports.getURL = getURL;
exports.compareResultsAndFilterIgnoredKeys = compareResultsAndFilterIgnoredKeys;
exports.compareArrayResultsWithIgnoredKeys = compareArrayResultsWithIgnoredKeys;
exports.formatProviderError = formatProviderError;
exports.createSendErrorWithMessage = createSendErrorWithMessage;
exports.parseJsonRpcError = parseJsonRpcError;
exports.compareRpcResults = compareRpcResults;
exports.compareSvmRpcResults = compareSvmRpcResults;
const tslib_1 = require("tslib");
const assert_1 = tslib_1.__importDefault(require("assert"));
const lodash_1 = require("lodash");
const utils_1 = require("../utils");
const types_1 = require("./types");
const alchemy = tslib_1.__importStar(require("./alchemy"));
const infura = tslib_1.__importStar(require("./infura"));
const drpc = tslib_1.__importStar(require("./drpc"));
const quicknode = tslib_1.__importStar(require("./quicknode"));
const PROVIDERS = {
    ALCHEMY: alchemy.getURL,
    INFURA: infura.getURL,
    INFURA_DIN: infura.getURL,
    DRPC: drpc.getURL,
    QUICKNODE: quicknode.getURL,
};
function isSupportedProvider(provider) {
    return Object.keys(PROVIDERS).includes(provider);
}
function getURL(provider, chainId, apiKey, transport = "https") {
    const getURL = PROVIDERS[provider];
    (0, assert_1.default)(getURL, `Unsupported RPC provider (${provider})`);
    return getURL(chainId, apiKey, transport);
}
function deleteIgnoredKeys(ignoredKeys, obj) {
    if (!(0, utils_1.isDefined)(obj)) {
        return;
    }
    const newObj = { ...obj };
    for (const key of ignoredKeys) {
        delete newObj[key];
    }
    return newObj;
}
function compareResultsAndFilterIgnoredKeys(ignoredKeys, _objA, _objB) {
    const filteredA = deleteIgnoredKeys(ignoredKeys, _objA);
    const filteredB = deleteIgnoredKeys(ignoredKeys, _objB);
    return (0, lodash_1.isEqual)(filteredA, filteredB);
}
function compareArrayResultsWithIgnoredKeys(ignoredKeys, objA, objB) {
    const filteredA = objA?.map((obj) => deleteIgnoredKeys(ignoredKeys, obj));
    const filteredB = objB?.map((obj) => deleteIgnoredKeys(ignoredKeys, obj));
    const sortKeys = ["transactionIndex", "logIndex"];
    return ((0, utils_1.isDefined)(filteredA) && (0, utils_1.isDefined)(filteredB) && (0, lodash_1.isEqual)((0, lodash_1.sortBy)(filteredA, sortKeys), (0, lodash_1.sortBy)(filteredB, sortKeys)));
}
const IGNORED_FIELDS = {
    eth_getBlockByNumber: [
        "miner",
        "l1BatchNumber",
        "l1BatchTimestamp",
        "requestsHash",
        "size",
        "totalDifficulty",
        "logsBloom",
        "transactions",
        "withdrawals",
        "sendCount",
        "sendRoot",
    ],
    eth_getLogs: ["blockTimestamp", "transactionLogIndex", "l1BatchNumber", "logType"],
};
function formatProviderError(provider, rawErrorText) {
    return `Provider ${(0, utils_1.getOriginFromURL)(provider.connection.url)} failed with error: ${rawErrorText}`;
}
function createSendErrorWithMessage(message, sendError) {
    const error = new Error(message);
    return { ...sendError, ...error };
}
function parseJsonRpcError(response) {
    if (!types_1.RpcError.is(response)) {
        return;
    }
    try {
        const error = JSON.parse(response.body);
        if (types_1.JsonRpcError.is(error)) {
            return error.error;
        }
    }
    catch {
    }
    return;
}
function compareRpcResults(method, rpcResultA, rpcResultB) {
    if (method === "eth_getBlockByNumber") {
        return compareResultsAndFilterIgnoredKeys(IGNORED_FIELDS.eth_getBlockByNumber, rpcResultA, rpcResultB);
    }
    else if (method === "eth_getLogs") {
        return compareArrayResultsWithIgnoredKeys(IGNORED_FIELDS.eth_getLogs, rpcResultA, rpcResultB);
    }
    else {
        return (0, lodash_1.isEqual)(rpcResultA, rpcResultB);
    }
}
function compareSvmRpcResults(_method, rpcResultA, rpcResultB) {
    return (0, lodash_1.isEqual)(rpcResultA, rpcResultB);
}
var CacheType;
(function (CacheType) {
    CacheType[CacheType["NONE"] = 0] = "NONE";
    CacheType[CacheType["WITH_TTL"] = 1] = "WITH_TTL";
    CacheType[CacheType["NO_TTL"] = 2] = "NO_TTL";
    CacheType[CacheType["DECIDE_TTL_POST_SEND"] = 3] = "DECIDE_TTL_POST_SEND";
})(CacheType || (exports.CacheType = CacheType = {}));
//# sourceMappingURL=utils.js.map