Skip to content

RpcRequest.from

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

Imports

Named
import { RpcRequest } from 'ox'

Examples

import { RpcRequest, RpcResponse } from 'ox'
 
// 1. Build a request object.
const request = RpcRequest.from({ 
  id: 0, 
  method: 'eth_estimateGas', 
  params: [ 
    { 
      from: '0xd2135CfB216b74109775236E36d4b433F1DF507B', 
      to: '0x0D44f617435088c947F00B31160f64b074e412B4', 
      value: '0x69420', 
    }, 
  ], 
})
 
// 2. Send the JSON-RPC request via HTTP.
const gas = await fetch('https://1.rpc.thirdweb.com', {
  body: JSON.stringify(request),
  headers: {
    'Content-Type': 'application/json',
  },
  method: 'POST',
})
 .then((response) => response.json())
 // 3. Parse the JSON-RPC response into a type-safe result.
 .then((response) => RpcResponse.parse(response, { request }))

Definition

function from<methodName>(
  options: from.Options<methodName>,
): from.ReturnType<methodName>

Source: src/RpcRequest.ts

Parameters

options

  • Type: from.Options<methodName>

JSON-RPC request options.

options.Request

  • Type: { method: methodName; }

options.id

  • Type: number

options.method

  • Type: methodName

options.params

  • Type: unknown
  • Optional

Return Type

The fully-formed JSON-RPC request object.

from.ReturnType<methodName>