Skip to content

RpcRequest.createStore

Creates a JSON-RPC request store to build requests with an incrementing id.

Returns a type-safe prepare function to build a JSON-RPC request object as per the JSON-RPC 2.0 specification.

Imports

Named
import { RpcRequest } from 'ox'

Examples

import { RpcRequest } from 'ox'
 
const store = RpcRequest.createStore()
 
const request_1 = store.prepare({
  method: 'eth_blockNumber',
})
{ id: 0, jsonrpc: '2.0', method: 'eth_blockNumber' }
const request_2 = store.prepare({ method: 'eth_call', params: [ { to: '0x0000000000000000000000000000000000000000', data: '0xdeadbeef', }, ], })
{ id: 1, jsonrpc: '2.0', method: 'eth_call', params: [{ to: '0x0000000000000000000000000000000000000000', data: '0xdeadbeef' }] }

Type-safe Custom Schemas

It is possible to define your own type-safe schema by using the RpcSchema.From type.

import { RpcSchema, RpcRequest } from 'ox'
 
type Schema = RpcSchema.From<{
  Request: { 
    method: 'eth_foobar'
    params: [number] 
  } 
  ReturnType: string
} | {
  Request: { 
    method: 'eth_foobaz'
    params: [string] 
  } 
  ReturnType: string
}>
 
const store = RpcRequest.createStore<Schema>()
 
const request = store.prepare({
  
method: "eth_foobar" | RpcSchema.MethodName
method
: 'eth_foobar',
params: [42], })

Definition

function createStore<schema>(
  options?: createStore.Options,
): createStore.ReturnType<schema>

Source: src/RpcRequest.ts

Parameters

options

  • Type: createStore.Options
  • Optional

Request store options.

options.id

  • Type: number
  • Optional

The initial request ID.

Return Type

The request store

createStore.ReturnType<schema>