Creates a new JSONRpcProvider instance with the specified configuration. The URL is automatically formatted to include the JSON-RPC endpoint path if not already present.
JSON RPC Provider Implementation
JSONRpcProvider Class
The JSONRpcProvider class extends AbstractRpcProvider to provide HTTP-based JSON-RPC communication with OP_NET nodes.
export class JSONRpcProvider extends AbstractRpcProvider {
public readonly url: string;
constructor(
url: string,
network: Network,
private readonly timeout: number = 20_000,
private readonly fetcherConfigurations: Agent.Options = {
keepAliveTimeout: 30_000,
keepAliveTimeoutThreshold: 30_000,
connections: 128,
pipelining: 2,
},
private useRESTAPI: boolean = true,
private readonly useThreadedParsing: boolean = false,
private readonly useThreadedHttp: boolean = false,
);
public async close(): Promise<void>;
public setFetchMode(useRESTAPI: boolean);
public async _send(payload: JsonRpcPayload | JsonRpcPayload[]): Promise<JsonRpcCallResult>;
protected providerUrl(url: string): string;
}Methods & Properties Reference
Constructor
- url: string
The RPC endpoint URL - network: Network
The network to connect to - timeout: number
Request timeout in milliseconds (default: 20000) - fetcherConfigurations: Agent.Options
Configuration options for the HTTP agent including keepAliveTimeout, connections, and pipelining - useRESTAPI: boolean
Whether to use REST API mode (default: true) - useThreadedParsing: boolean
Whether to parse JSON responses in a worker thread (default: false) - useThreadedHttp: boolean
Whether to perform HTTP requests in a worker thread (default: false)
- provider: JSONRpcProvider - A new JSONRpcProvider instance
Properties
The RPC endpoint URL used for all requests. This URL is automatically formatted during construction to include the JSON-RPC API path.
- url: string - The formatted RPC endpoint URL
Methods
Closes the provider and releases all associated resources including HTTP connections. This method should be called when the provider is no longer needed to prevent resource leaks.
- Promise<void>
Sets the fetch mode to use REST API or pure JSON-RPC mode. This allows switching between modes at runtime without creating a new provider instance.
- useRESTAPI: boolean
Whether to use REST API mode or pure JSON-RPC mode
- void
Sends a JSON-RPC payload to the provider. This method implements the abstract _send() method from AbstractRpcProvider. When threaded HTTP is enabled, the entire request runs in a worker thread. Otherwise, the request runs on the main thread with optional threaded JSON parsing.
Throws an error if no data is fetched or if the request times out.
- payload: JsonRpcPayload | JsonRpcPayload[]
The JSON-RPC payload or array of payloads to send
- result: Promise<JsonRpcCallResult> - The result of the JSON-RPC call
Formats the provider URL to include the JSON-RPC API endpoint path. Trailing slashes are removed and the path /api/v1/json-rpc is appended if not already present.
- url: string
The base URL to format
- url: string - The formatted provider URL with the JSON-RPC endpoint path