Expand description
Alloy JSON-RPC data types.
This crate provides data types for use with the JSON-RPC 2.0 protocol. It does not provide any functionality for actually sending or receiving JSON-RPC data.
If you find yourself importing this crate, and you are not implementing a
JSON-RPC client or transport, you are likely at the wrong layer of
abstraction. If you want to use a JSON-RPC client, consider using the
alloy-transport crate.
§Usage
This crate models the JSON-RPC 2.0 protocol data-types. It is intended to be used to build JSON-RPC clients or servers. Most users will not need to import this crate.
This crate provides the following low-level data types:
Request- A JSON-RPC request.Response- A JSON-RPC response.ErrorPayload- A JSON-RPC error response payload, including code and message.ResponsePayload- The payload of a JSON-RPC response, either a success payload, or anErrorPayload.
For client-side Rust ergonomics, we want to map responses to Results.
To that end, we provide the following types:
RpcError- An error that can occur during JSON-RPC communication. This type aggregates errors that are common to all transports, such as (de)serialization, error responses, and includes a generic transport error.RpcResult- A result modeling an Rpc outcome asResult<T, RpcError<E>>.
We recommend that transport implementers use RpcResult as the return
type for their transport methods, parameterized by their transport error
type. This will allow them to return either a successful response or an
error.
§Note On (De)Serialization
Request, Response, and similar types are generic over the
actual data being passed to and from the RPC. We can achieve partial
(de)serialization by making them generic over a serde_json::RawValue.
- For
Request-PartiallySerializedRequestis aRequest<Box<RawValue>. It represents aRequestwhose parameters have been serialized.SerializedRequest, on the other hand is a request that has been totally serialized. For client-development purposes, itsIdand method have been preserved. - For
Response-BorrowedResponseis aResponse<&RawValue>. It represents a Response whoseIdand return status (success or failure) have been deserialized, but whose payload has not.
Allowing partial serialization lets us include many unlike Request
objects in collections (e.g. in a batch request). This is useful for
implementing a client.
Allowing partial deserialization lets learn request status, and associate the raw response data with the corresponding client request before doing full deserialization work. This is useful for implementing a client.
In general, partially deserialized responses can be further deserialized.
E.g. an BorrowedRpcResult may have success responses deserialized
with crate::try_deserialize_ok::<U>, which will transform it to an
RpcResult<U>.
Structs§
- Error
Payload - A JSON-RPC 2.0 error object.
- EthNotification
- An ethereum-style notification, not to be confused with a JSON-RPC notification.
- Request
- A JSON-RPC 2.0 request object.
- Request
Meta RequestMetacontains theIdand method name of a request.- Response
- A JSON-RPC 2.0 response object containing a
ResponsePayload. - Serialized
Request - A JSON-RPC 2.0 request object that has been serialized, with its
Idand method preserved.
Enums§
- Id
- A JSON-RPC 2.0 ID object. This may be a number, a string, or null.
- PubSub
Item - An item received over an Ethereum pubsub transport.
- Request
Packet - A
RequestPacketis aSerializedRequestor a batch of serialized request. - Response
Packet - A
ResponsePacketis aResponseor a batch of responses. - Response
Payload - A JSON-RPC 2.0 response payload.
- RpcError
- An RPC error.
- SubId
- A subscription ID.
Traits§
- Borrowed
RpcObject - An object that can be both sent and received over RPC, borrowing from the the deserialization context.
- RpcBorrow
- An object that can be received over RPC, borrowing from the deserialization context.
- RpcObject
- An object that can be both sent and received over RPC.
- RpcRecv
- An object that can be received over RPC.
- RpcSend
- An object that can be sent over RPC.
Functions§
- transform_
response - Transform a transport response into an
RpcResult, discarding theId. - transform_
result - Transform a transport outcome into an
RpcResult, discarding theId. - try_
deserialize_ ok - Attempt to deserialize the
Ok(_)variant of anRpcResult.
Type Aliases§
- Borrowed
Error Payload - A
ErrorPayloadthat has been partially deserialized, borrowing its contents from the deserializer. This is used primarily for intermediate deserialization. Most users will not require it. - Borrowed
Response - A
Responsethat has been partially deserialized, borrowing its contents from the deserializer. This is used primarily for intermediate deserialization. Most users will not require it. - Borrowed
Response Packet - A
BorrowedResponsePacketis aResponsePacketthat has been partially deserialized, borrowing its contents from the deserializer. - Borrowed
Response Payload - A
ResponsePayloadthat has been partially deserialized, borrowing its contents from the deserializer. This is used primarily for intermediate deserialization. Most users will not require it. - Borrowed
RpcResult - A partially deserialized
RpcResult, borrowing from the deserializer. - Partially
Serialized Request - A
Requestthat has been partially serialized. - RpcResult
- The result of a JSON-RPC request.