UNPKG

viem

Version:

TypeScript Interface for Ethereum

89 lines 3.28 kB
import { RpcRequestError } from '../../errors/request.js'; import { UrlRequiredError, } from '../../errors/transport.js'; import { createBatchScheduler } from '../../utils/promise/createBatchScheduler.js'; import { getHttpRpcClient, } from '../../utils/rpc/http.js'; import { createTransport, } from './createTransport.js'; let signalId = 0; const signalIds = new WeakMap(); function getSignalId(signal) { if (!signal) return 'default'; const id = signalIds.get(signal); if (id !== undefined) return id; const nextId = signalId++; signalIds.set(signal, nextId); return nextId; } /** * @description Creates a HTTP transport that connects to a JSON-RPC API. */ export function http( /** URL of the JSON-RPC API. Defaults to the chain's public RPC URL. */ url, config = {}) { const { batch, fetchFn, fetchOptions, key = 'http', methods, name = 'HTTP JSON-RPC', onFetchRequest, onFetchResponse, retryDelay, raw, } = config; return ({ chain, retryCount: retryCount_, timeout: timeout_ }) => { const { batchSize = 1000, wait = 0 } = typeof batch === 'object' ? batch : {}; const retryCount = config.retryCount ?? retryCount_; const timeout = timeout_ ?? config.timeout ?? 10_000; const url_ = url || chain?.rpcUrls.default.http[0]; if (!url_) throw new UrlRequiredError(); const rpcClient = getHttpRpcClient(url_, { fetchFn, fetchOptions, onRequest: onFetchRequest, onResponse: onFetchResponse, timeout, }); return createTransport({ key, methods, name, async request({ method, params }, options) { const body = { method, params }; const fetchOptions = options?.signal ? { signal: options.signal } : undefined; const { schedule } = createBatchScheduler({ id: `${url_}.${getSignalId(options?.signal)}`, wait, shouldSplitBatch(requests) { return requests.length > batchSize; }, fn: (body) => rpcClient.request({ body, fetchOptions, }), sort: (a, b) => a.id - b.id, }); const fn = async (body) => batch ? schedule(body) : [ await rpcClient.request({ body, fetchOptions, }), ]; const [{ error, result }] = await fn(body); if (raw) return { error, result }; if (error) throw new RpcRequestError({ body, error, url: url_, }); return result; }, retryCount, retryDelay, timeout, type: 'http', }, { fetchOptions, url: url_, }); }; } //# sourceMappingURL=http.js.map