|
| 1 | +--- |
| 2 | +title: Protocol and RPC Types |
| 3 | +description: Choose Alloy primitives, consensus envelopes, EIP helpers, RPC responses, genesis, trie, and serialization types |
| 4 | +--- |
| 5 | + |
| 6 | +# Protocol and RPC types |
| 7 | + |
| 8 | +Alloy models the same chain data at several boundaries. Choosing the type that belongs to the |
| 9 | +boundary avoids manual conversion and preserves network-specific fields. |
| 10 | + |
| 11 | +| Boundary | Primary crates or modules | Examples | |
| 12 | +| --- | --- | --- | |
| 13 | +| Values and ABI | `alloy-primitives`, `alloy-sol-types`, `alloy-dyn-abi`, `alloy-json-abi` | address, `U256`, bytes, Solidity bindings | |
| 14 | +| Execution consensus | `alloy-consensus` | transaction envelopes, blocks, receipts, headers | |
| 15 | +| Protocol extensions | `alloy-eips` | block identifiers and implemented EIP data structures | |
| 16 | +| Network behavior | `alloy-network`, `alloy-network-primitives` | associated request, response, envelope, and receipt types | |
| 17 | +| JSON-RPC wire data | `alloy-rpc-types-*` | `TransactionRequest`, RPC block and receipt responses | |
| 18 | +| Chain configuration | `alloy-genesis` | execution genesis files and allocations | |
| 19 | +| State proofs | `alloy-trie` | trie roots, proofs, and prefix-sorted updates | |
| 20 | +| Representation helpers | `alloy-serde` | quantity, bytes, and compatibility serializers | |
| 21 | + |
| 22 | +The `alloy` meta-crate re-exports enabled families as `alloy::primitives`, `alloy::consensus`, |
| 23 | +`alloy::eips`, `alloy::network`, and `alloy::rpc::types`. |
| 24 | + |
| 25 | +## Primitives and ABI types |
| 26 | + |
| 27 | +Use primitives for chain-independent values: `Address`, `B256`, `Bytes`, fixed bytes, signed and |
| 28 | +unsigned integers, hashes, and maps. The `sol!` macro and Solidity type traits provide static ABI |
| 29 | +encoding, event and error decoding, and EIP-712 definitions. Dynamic ABI and JSON ABI types are for |
| 30 | +interfaces discovered at runtime. |
| 31 | + |
| 32 | +See [primitive types](/using-primitive-types/introduction), [`sol!`](/contract-interactions/using-sol!), |
| 33 | +and [static vs. dynamic ABI](/guides/static-dynamic-abi-in-alloy). |
| 34 | + |
| 35 | +## Consensus types |
| 36 | + |
| 37 | +Consensus types represent execution-layer objects independently of their JSON-RPC metadata: |
| 38 | + |
| 39 | +- signed and unsigned transaction variants and EIP-2718 envelopes; |
| 40 | +- headers and blocks; |
| 41 | +- receipts, logs, withdrawals, and blob sidecars; |
| 42 | +- recovered transactions that pair an envelope with its signer. |
| 43 | + |
| 44 | +Enable the `consensus` feature when using these through the meta-crate. Add `rlp`, `k256`, `kzg`, or |
| 45 | +`consensus-secp256k1` only when the operation needs that codec or cryptographic implementation. |
| 46 | + |
| 47 | +Use consensus types for database storage, networking, hashing, roots, signing payloads, and |
| 48 | +protocol-level transformations. Use the network's RPC types at the provider boundary. |
| 49 | + |
| 50 | +## RPC types wrap consensus data |
| 51 | + |
| 52 | +Ethereum RPC responses embed consensus values and add fields supplied by the node. For example, an |
| 53 | +RPC transaction includes block placement metadata while containing a recovered consensus envelope; |
| 54 | +an RPC receipt includes transaction and block metadata around the consensus receipt. |
| 55 | + |
| 56 | +Common conversion and access patterns include: |
| 57 | + |
| 58 | +- `block.into_consensus()` to discard RPC-only metadata and obtain a consensus block; |
| 59 | +- `transaction.into_recovered()` for the recovered consensus transaction; |
| 60 | +- `.inner` or `.into_inner()` on wrapper types; |
| 61 | +- `map_*` and `try_map_*` methods when replacing an embedded header or transaction type. |
| 62 | + |
| 63 | +See the runnable [consensus and RPC embedding example](/examples/providers/embed_consensus_rpc). |
| 64 | +Avoid re-serializing through JSON just to convert between these representations. |
| 65 | + |
| 66 | +## Network-associated types |
| 67 | + |
| 68 | +`Provider<N>` is generic over a `Network`. The network chooses the transaction type, envelope, |
| 69 | +receipt, header, request, and RPC response types used by provider methods. Ethereum is the default. |
| 70 | + |
| 71 | +Use: |
| 72 | + |
| 73 | +- `Ethereum` for Ethereum execution-layer responses; |
| 74 | +- a chain-specific implementation such as `op-alloy` for a supported ecosystem with extra |
| 75 | + transaction or receipt variants; |
| 76 | +- `AnyNetwork` when consuming heterogeneous responses and the catch-all representation is |
| 77 | + acceptable; |
| 78 | +- a custom `Network` implementation when a library owns a distinct typed protocol surface. |
| 79 | + |
| 80 | +Choosing `Ethereum` for a chain with extra transaction variants can fail deserialization. See |
| 81 | +[interacting with multiple networks](/guides/interacting-with-multiple-networks) and the |
| 82 | +[`AnyNetwork` example](/examples/advanced/any_network). |
| 83 | + |
| 84 | +## EIP modules |
| 85 | + |
| 86 | +`alloy-eips` collects protocol structures and behavior that are shared across consensus, provider, |
| 87 | +and RPC crates. Support for a type does not imply that the connected chain has activated that EIP. |
| 88 | +Fork activation and RPC availability remain network and node concerns. |
| 89 | + |
| 90 | +Examples include block identifiers and tags, typed envelope encodings, access lists, |
| 91 | +authorizations, blob sidecars, requests, and hardfork-related structures. Consult the |
| 92 | +[`alloy-eips` module list](https://docs.rs/alloy-eips/latest/alloy_eips/) for the exact release. |
| 93 | + |
| 94 | +## Genesis and trie data |
| 95 | + |
| 96 | +Use `alloy-genesis` for execution genesis configuration and account allocations. Genesis formats |
| 97 | +can contain client extensions, so preserve unknown or chain-specific fields when round-tripping a |
| 98 | +configuration. |
| 99 | + |
| 100 | +Use `alloy-trie` for Merkle-Patricia Trie roots and proofs. Trie APIs generally require keys and |
| 101 | +updates in the documented order; incorrect ordering can produce a different root rather than a |
| 102 | +helpful RPC error. Treat proof verification input as untrusted data and validate the expected root. |
| 103 | + |
| 104 | +These families are optional `genesis` and `trie` features on the meta-crate and are also available |
| 105 | +as direct dependencies for infrastructure libraries. |
| 106 | + |
| 107 | +## Serialization |
| 108 | + |
| 109 | +Ethereum JSON-RPC quantities, fixed bytes, byte strings, and nullability do not follow ordinary |
| 110 | +human-readable JSON conventions. Prefer the provided RPC and serde helper types over custom |
| 111 | +hex-string logic. |
| 112 | + |
| 113 | +Enable `serde` for serde implementations and helpers. Compatibility formats such as |
| 114 | +`serde-bincode-compat` are opt-in; use the documented compatibility wrapper rather than assuming a |
| 115 | +type's normal serde representation is stable for non-self-describing storage. |
| 116 | + |
| 117 | +For persistent data: |
| 118 | + |
| 119 | +1. Choose whether the stored form is consensus, RPC-enriched, or application-specific. |
| 120 | +2. Version the schema independently of the Rust type name. |
| 121 | +3. Test round trips across dependency upgrades. |
| 122 | +4. Do not use debug output as a data format. |
| 123 | + |
| 124 | +## Constrained and `no_std` users |
| 125 | + |
| 126 | +The network-facing meta-crate primarily targets `std`. Some core and protocol crates support |
| 127 | +`no_std` with default features disabled. Depend on the narrow crate directly, inspect its own |
| 128 | +feature list, and compile the exact target. See [feature flags](/reference/feature-flags). |
0 commit comments