Exth.Rpc.Request (Exth v0.4.2)

View Source

Represents a JSON-RPC request with validation.

A request consists of:

  • method - The RPC method name (string or atom)
  • params - List of parameters for the method
  • id - Optional positive integer for request identification
  • jsonrpc - JSON-RPC version (defaults to "2.0")

Example

iex> Request.new("eth_getBalance", ["0x742d35Cc6634C0532925a3b844Bc454e4438f44e", "latest"])
%Request{
  method: "eth_getBalance",
  params: ["0x742d35Cc6634C0532925a3b844Bc454e4438f44e", "latest"],
  id: nil,
  jsonrpc: "2.0"
}

Summary

Functions

Creates a new request struct with the given method, parameters, and ID.

Serializes a request to JSON.

Types

t()

@type t() :: %Exth.Rpc.Request{
  id: Exth.Rpc.Types.id(),
  jsonrpc: Exth.Rpc.Types.jsonrpc(),
  method: Exth.Rpc.Types.method(),
  params: Exth.Rpc.Types.params()
}

Functions

new(method, params, id \\ nil)

Creates a new request struct with the given method, parameters, and ID.

Parameters

  • method - The RPC method name (string or atom)
  • params - List of parameters for the method
  • id - Optional positive integer for request identification

Returns

  • A new request struct with the given method, parameters, and ID

Examples

# Simple request with no ID
request = Request.new("eth_blockNumber", [])
# => %Request{id: nil, method: "eth_blockNumber", params: []}

# Request with ID
request = Request.new("eth_getBalance", ["0x742d...", "latest"], 1)
# => %Request{id: 1, method: "eth_getBalance", params: ["0x742d...", "latest"]}

serialize(request)

@spec serialize(t() | [t()]) :: {:ok, String.t()} | {:error, Exception.t()}

Serializes a request to JSON.

Parameters

  • request - The request to serialize

Returns

  • {:ok, json} - Successful serialization with JSON string
  • {:error, reason} - Serialization failed with error reason

Examples

# Single request
{:ok, json} = Request.serialize(%Request{
  method: "eth_blockNumber",
  params: [],
  id: 1
})

# Batch request
{:ok, json} = Request.serialize([
  %Request{method: "eth_blockNumber", params: [], id: 1},
  %Request{method: "eth_getBalance", params: ["0x123...", "latest"], id: 2}
])