Skip to content

RpcResponse.from

A type-safe interface to instantiate a JSON-RPC response object as per the JSON-RPC 2.0 specification.

Imports

Named
import { RpcResponse } from 'ox'

Examples

Instantiating a Response Object

import { RpcResponse } from 'ox'
 
const response = RpcResponse.from({
  id: 0,
  jsonrpc: '2.0',
  result: '0x69420',
})

Type-safe Instantiation

If you have a JSON-RPC request object, you can use it to strongly-type the response. If a request is provided, then the id and jsonrpc properties will be overridden with the values from the request.

import { RpcRequest, RpcResponse } from 'ox'
 
const request = RpcRequest.from({ id: 0, method: 'eth_blockNumber' })
 
const response = RpcResponse.from(
  { result: '0x69420' },
  { request },
)

Definition

function from<request, response>(
  response: from.Response<request, response>,
  options?: from.Options<request>,
): Compute<from.ReturnType<response>>

Source: src/core/RpcResponse.ts

Parameters

response

  • Type: from.Response<request, response>

Opaque JSON-RPC response object.

options

  • Type: from.Options<request>
  • Optional

Parsing options.

options.request

  • Type: request | { method: string; params?: unknown; id: number; jsonrpc: "2.0"; _returnType: unknown; }
  • Optional

Return Type

Typed JSON-RPC result, or response object (if raw is true).

Compute<from.ReturnType<response>>