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 RpcSchema.from.

import { RpcSchema, RpcRequest } from 'ox'
 
const 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"
method
: 'eth_foobar',
params: [42], })

Definition

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

Source: src/core/RpcRequest.ts

Parameters

options

  • Type: createStore.Options<schema>
  • Optional

Request store options.

options.id

  • Type: number
  • Optional

The initial request ID.

options.schema

  • Type: schema | Generic
  • Optional

RPC Schema to use for the request store.

Return Type

The request store

createStore.ReturnType<schema>