UNPKG

@account-kit/infra

Version:

adapters for @aa-sdk/core for interacting with alchemy services

83 lines (82 loc) 4.14 kB
import { type ConnectionConfig, type NoUndefined } from "@aa-sdk/core"; import { type Chain, type EIP1193RequestFn, type HttpTransportConfig, type PublicRpcSchema, type Transport, type TransportConfig } from "viem"; import type { AlchemyRpcSchema } from "./client/types.js"; type Never<T> = T extends object ? { [K in keyof T]?: never; } : never; type AlchemyConnectionConfig = ConnectionConfig; type SplitTransportConfig = { alchemyConnection: AlchemyConnectionConfig; nodeRpcUrl: string; }; export type AlchemyTransportConfig = ((AlchemyConnectionConfig & Never<SplitTransportConfig>) | (SplitTransportConfig & Never<AlchemyConnectionConfig>)) & { /** The max number of times to retry. */ retryCount?: TransportConfig["retryCount"] | undefined; /** The base delay (in ms) between retries. */ retryDelay?: TransportConfig["retryDelay"] | undefined; fetchOptions?: NoUndefined<HttpTransportConfig["fetchOptions"]>; }; type AlchemyTransportBase = Transport<"alchemy", { alchemyRpcUrl: string; fetchOptions?: AlchemyTransportConfig["fetchOptions"]; }, EIP1193RequestFn<[...PublicRpcSchema, ...AlchemyRpcSchema]>>; export type AlchemyTransport = AlchemyTransportBase & { updateHeaders(newHeaders: HeadersInit): void; config: AlchemyTransportConfig; dynamicFetchOptions: AlchemyTransportConfig["fetchOptions"]; }; /** * A type guard for the transport to determine if it is an Alchemy transport. * Used in cases where we would like to do switching depending on the transport, where there used * to be two clients for an alchemy and a non-alchemy, and with this switch we don't need the two seperate clients. * * * @param {Transport} transport The transport to check * @param {Chain} chain Chain for the transport to run its function to return the transport config * @returns {boolean} `true` if the transport is an Alchemy transport, otherwise `false` */ export declare function isAlchemyTransport(transport: Transport, chain: Chain): transport is AlchemyTransport; /** * Creates an Alchemy transport with the specified configuration options. * When sending all traffic to Alchemy, you must pass in one of rpcUrl, apiKey, or jwt. * If you want to send Bundler and Paymaster traffic to Alchemy and Node traffic to a different RPC, you must pass in alchemyConnection and nodeRpcUrl. * * @example * ### Basic Example * If the chain you're using is supported for both Bundler and Node RPCs, then you can do the following: * ```ts * import { alchemy } from "@account-kit/infra"; * * const transport = alchemy({ * // NOTE: you can also pass in an rpcUrl or jwt here or rpcUrl and jwt * apiKey: "your-api-key", * }); * ``` * * ### AA Only Chains * For AA-only chains, you need to specify the alchemyConnection and nodeRpcUrl since Alchemy only * handles the Bundler and Paymaster RPCs for these chains. * ```ts * import { alchemy } from "@account-kit/infra"; * * const transport = alchemy({ * alchemyConnection: { * apiKey: "your-api-key", * }, * nodeRpcUrl: "https://zora.rpc.url", * }); * ``` * * @param {AlchemyTransportConfig} config The configuration object for the Alchemy transport. * @param {number} config.retryDelay Optional The delay between retries, in milliseconds. * @param {number} config.retryCount Optional The number of retry attempts. * @param {string} [config.alchemyConnection] Optional Alchemy connection configuration (if this is passed in, nodeRpcUrl is required). * @param {string} [config.fetchOptions] Optional fetch options for HTTP requests. * @param {string} [config.nodeRpcUrl] Optional RPC URL for node (if this is passed in, alchemyConnection is required). * @param {string} [config.rpcUrl] Optional RPC URL. * @param {string} [config.apiKey] Optional API key for Alchemy. * @param {string} [config.jwt] Optional JSON Web Token for authorization. * @returns {AlchemyTransport} The configured Alchemy transport object. */ export declare function alchemy(config: AlchemyTransportConfig): AlchemyTransport; export declare const convertHeadersToObject: (headers?: HeadersInit) => Record<string, string>; export {};