// The async/queue library has a task-based interface for building a concurrent queue. import assert from "assert"; import { providers } from "ethers"; import { isEqual } from "lodash"; import { isDefined } from "../utils"; import { RPCProvider, RPCTransport } from "./types"; import * as alchemy from "./alchemy"; import * as infura from "./infura"; import * as drpc from "./drpc"; /** * Infura DIN is identified separately to allow it to be configured explicitly. */ const PROVIDERS = { ALCHEMY: alchemy.getURL, INFURA: infura.getURL, INFURA_DIN: infura.getURL, DRPC: drpc.getURL, }; /** * Type predicate for RPCProvider type. * @param provider Provider string (ALCHEMY, INFURA, ...). * @returns True if the provider string is a supported provider. */ export function isSupportedProvider(provider: string): provider is RPCProvider { return ["ALCHEMY", "INFURA", "INFURA_DIN", "DRPC"].includes(provider); } /** * Produce an RPC for a given RPC provider, chainId API key and transport. * @param provider RPC provider identifier (ALCHEMY, INFURA, ...) * @param chainId Chain ID to obtain a URL for. * @param apiKey API key for provider. * @param transport Optional transport specifier (HTTPS or WSS). * @returns An RPC URL confirming to the specified inputs. */ export function getURL( provider: RPCProvider, chainId: number, apiKey: string, transport: RPCTransport = "https" ): string { const getURL = PROVIDERS[provider]; assert(getURL, `Unsupported RPC provider (${provider})`); return getURL(chainId, apiKey, transport); } /** * Deletes keys from an object and returns new copy of object without ignored keys * @param ignoredKeys * @param obj * @returns Objects with ignored keys removed */ function deleteIgnoredKeys(ignoredKeys: string[], obj: Record) { if (!isDefined(obj)) { return; } const newObj = { ...obj }; for (const key of ignoredKeys) { delete newObj[key]; } return newObj; } export function compareResultsAndFilterIgnoredKeys( ignoredKeys: string[], _objA: Record, _objB: Record ): boolean { // Remove ignored keys from copied objects. const filteredA = deleteIgnoredKeys(ignoredKeys, _objA); const filteredB = deleteIgnoredKeys(ignoredKeys, _objB); // Compare objects without the ignored keys. return isEqual(filteredA, filteredB); } export function compareArrayResultsWithIgnoredKeys(ignoredKeys: string[], objA: unknown[], objB: unknown[]): boolean { // Remove ignored keys from each element of copied arrays. const filteredA = objA?.map((obj) => deleteIgnoredKeys(ignoredKeys, obj as Record)); const filteredB = objB?.map((obj) => deleteIgnoredKeys(ignoredKeys, obj as Record)); // Compare objects without the ignored keys. return isDefined(filteredA) && isDefined(filteredB) && isEqual(filteredA, filteredB); } /** * A record of error codes that correspond to fields that should be ignored when comparing RPC results. * This is used to compare results from different providers that may have different, but still valid, results. */ const IGNORED_FIELDS = { // We've seen some RPC's like QuickNode add in transactionLogIndex which isn't in the // JSON RPC spec: https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getfilterchanges // Additional reference: https://github.com/ethers-io/ethers.js/issues/1721 // 2023-08-31 Added blockHash because of upstream zkSync provider disagreements. Consider removing later. // 2024-05-07 Added l1BatchNumber and logType due to Alchemy. Consider removing later. // 2024-07-11 Added blockTimestamp after zkSync rolled out a new node release. eth_getBlockByNumber: [ "miner", // polygon (sometimes) "l1BatchNumber", // zkSync "l1BatchTimestamp", // zkSync "size", // Alchemy/Arbitrum (temporary) "totalDifficulty", // Quicknode/Alchemy (sometimes) "logsBloom", // zkSync (third-party providers return 0x0..0) "transactions", // Polygon yParity field in transactions[] ], eth_getLogs: ["blockTimestamp", "transactionLogIndex", "l1BatchNumber", "logType"], }; /** * This is the type we pass to define a request "task". */ export interface RateLimitTask { // These are the arguments to be passed to super.send(). sendArgs: [string, Array]; // These are the promise callbacks that will cause the initial send call made by the user to either return a result // or fail. resolve: (result: unknown) => void; reject: (err: unknown) => void; } /** * A helper function to format an error message for a provider. * @param provider The provider that failed. * @param rawErrorText The raw error text. * @returns The formatted error message. */ export function formatProviderError(provider: providers.StaticJsonRpcProvider, rawErrorText: string) { return `Provider ${provider.connection.url} failed with error: ${rawErrorText}`; } export function createSendErrorWithMessage(message: string, sendError: Record) { const error = new Error(message); return { ...sendError, ...error }; } /** * Compares two RPC results, filtering out fields that are known to differ between providers. * Note: this function references `IGNORED_ERROR_CODES` which is a record of error codes that correspond to fields * that should be ignored when comparing RPC results. * @param method The method that was called - conditionally filters out fields based on the method. * @param rpcResultA The first RPC result. * @param rpcResultB The second RPC result. * @returns True if the results are equal, false otherwise. */ export function compareRpcResults(method: string, rpcResultA: unknown, rpcResultB: unknown): boolean { if (method === "eth_getBlockByNumber") { // We've seen RPC's disagree on the miner field, for example when Polygon nodes updated software that // led alchemy and quicknode to disagree on the miner field's value. return compareResultsAndFilterIgnoredKeys( IGNORED_FIELDS.eth_getBlockByNumber, rpcResultA as Record, rpcResultB as Record ); } else if (method === "eth_getLogs") { return compareArrayResultsWithIgnoredKeys( IGNORED_FIELDS.eth_getLogs, rpcResultA as unknown[], rpcResultB as unknown[] ); } else { return isEqual(rpcResultA, rpcResultB); } } export enum CacheType { NONE, // Do not cache WITH_TTL, // Cache with TTL NO_TTL, // Cache with infinite TTL }