Exth.Rpc.Response (Exth v0.4.2)

View Source

Represents JSON-RPC 2.0 response structures.

A response can be either a Success or an Error:

  • Success - Contains the result of a successful RPC call

    • id - Request identifier (matches the request)
    • result - The actual response data
    • jsonrpc - JSON-RPC version (defaults to "2.0")
  • Error - Contains error information when the RPC call fails

    • id - Request identifier (matches the request)
    • error.code - Integer error code
    • error.message - Error description
    • error.data - Optional additional error details
    • jsonrpc - JSON-RPC version (defaults to "2.0")

Examples

# Creating a success response
Response.success(1, "0x1234")
#=> %Response.Success{id: 1, result: "0x1234", jsonrpc: "2.0"}

# Creating an error response
Response.error(1, -32600, "Invalid Request")
#=> %Response.Error{
  id: 1,
  error: %{code: -32600, message: "Invalid Request", data: nil},
  jsonrpc: "2.0"
}

Summary

Types

Functions

deserialize(json)

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

Deserializes a JSON-RPC response.

Examples

iex> Exth.Rpc.Response.deserialize(~s({"jsonrpc": "2.0", "result": "0x1234", "id": 1}))
{:ok, %Exth.Rpc.Response.Success{id: 1, result: "0x1234"}}

iex> Exth.Rpc.Response.deserialize(~s({"jsonrpc": "2.0", "error": {"code": -32_601, "message": "Method not found"}, "id": 1}))
{:ok, %Exth.Rpc.Response.Error{id: 1, error: %{code: -32_601, message: "Method not found"}}}

iex> Exth.Rpc.Response.deserialize(~s({"jsonrpc": "2.0", "method": "eth_subscription", "params": {"subscription": "0x1234", "result": {"block": "0x123"}}}))
{:ok, %Exth.Rpc.Response.SubscriptionEvent{
  method: "eth_subscription",
  params: %{subscription: "0x1234", result: %{"block" => "0x123"}}
}}

error(id, code, message, data \\ nil)

@spec error(Exth.Rpc.Types.id(), integer(), String.t(), any() | nil) ::
  Exth.Rpc.Response.Error.t()

success(id, result)