Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: move rpc client from transport readme #782

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions crates/rpc-client/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
# alloy-rpc-client

Low-level Ethereum JSON-RPC client implementation.

## Usage

Usage of this crate typically means instantiating an `RpcClient<T>` over some
`Transport`. The RPC client can then be used to make requests to the RPC
server. Requests are captured as `RpcCall` futures, which can then be polled to
completion.

For example, to make a simple request:

```rust,ignore
// Instantiate a new client over a transport.
let client: ReqwestClient = ClientBulider::default().http(url);

// Prepare a request to the server.
let request = client.request("eth_blockNumber", ());

// Poll the request to completion.
let block_number = request.await.unwrap();
```

Batch requests are also supported:

```rust,ignore
// Instantiate a new client over a transport.
let client: ReqwestClient = ClientBulider::default().http(url);

// Prepare a batch request to the server.
let batch = client.new_batch();

// Batches serialize params immediately. So we need to handle the result when
// adding calls.
let block_number_fut = batch.add_call("eth_blockNumber", ()).unwrap();
let balance_fut = batch.add_call("eth_getBalance", address).unwrap();

// Make sure to send the batch!
batch.send().await.unwrap();

// After the batch is complete, we can get the results.
// Note that requests may error separately!
let block_number = block_number_fut.await.unwrap();
let balance = balance_fut.await.unwrap();
```
4 changes: 4 additions & 0 deletions crates/rpc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ pub use alloy_transport_ws::WsConnect;

#[cfg(all(feature = "ipc", not(target_arch = "wasm32")))]
pub use alloy_transport_ipc::IpcConnect;

/// A client using a [`reqwest`] HTTP transport.
#[cfg(feature = "reqwest")]
pub type ReqwestClient = RpcClient<alloy_transport_http::ReqwestTransport>;
62 changes: 17 additions & 45 deletions crates/transport/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,25 @@ This crate handles RPC connection and request management. It builds an
futures for simple and batch RPC requests as well as a unified `TransportError`
type.

[alloy-provider]: ../provider/
[tower `Service`]: https://docs.rs/tower/latest/tower/trait.Service.html

## Usage

Usage of this crate typically means instantiating an `RpcClient<T>` over some
`Transport`. The RPC client can then be used to make requests to the RPC
server. Requests are captured as `RpcCall` futures, which can be polled to
completion.

For example, to make a simple request:

```rust,ignore
// Instantiate a new client over a transport.
let client: RpcClient<reqwest::Http> = "https://mainnet.infura.io/v3/...".parse().unwrap();

// Prepare a request to the server.
let request = client.request("eth_blockNumber", ());
Typically, this crate should not be used directly. Most EVM users will want to
use the [alloy-provider] crate, which provides a high-level API for interacting
with JSON-RPC servers that provide the standard Ethereum RPC endpoints, or the
[alloy-rpc-client] crate, which provides a low-level JSON-RPC API without the
specific Ethereum endpoints.

// Poll the request to completion.
let block_number = request.await.unwrap();
```

Batch requests are also supported:

```rust,ignore
// Instantiate a new client over a transport.
let client: RpcClient<reqwest::Http> = "https://mainnet.infura.io/v3/...".parse().unwrap();

// Prepare a batch request to the server.
let batch = client.new_batch();

// Batches serialize params immediately. So we need to handle the result when
// adding calls.
let block_number_fut = batch.add_call("eth_blockNumber", ()).unwrap();
let balance_fut = batch.add_call("eth_getBalance", address).unwrap();
[alloy-provider]: https://alloy-rs.github.io/alloy/alloy_provider/index.html
[tower `Service`]: https://docs.rs/tower/latest/tower/trait.Service.html

// Make sure to send the batch!
batch.send().await.unwrap();
### Transports

// After the batch is complete, we can get the results.
// Note that requests may error separately!
let block_number = block_number_fut.await.unwrap();
let balance = balance_fut.await.unwrap();
```
Alloy maintains the following transports:

### Features
- [alloy-transport-http]: JSON-RPC via HTTP.
- [alloy-transport-ws]: JSON-RPC via Websocket, supports pubsub via
[alloy-pubsub].
- [alloy-transport-ipc]: JSON-RPC via IPC, supports pubsub via [alloy-pubsub].

- `reqwest`: Enables the `reqwest` transport implementation.
- `hyper`: Enables the `hyper` transport implementation (not available in WASM).
[alloy-transport-http]: https://alloy-rs.github.io/alloy/alloy_transport_http/index.html
[alloy-transport-ws]: https://alloy-rs.github.io/alloy/alloy_transport_ws/index.html
[alloy-transport-ipc]: https://alloy-rs.github.io/alloy/alloy_transport_ipc/index.html
[alloy-pubsub]: https://alloy-rs.github.io/alloy/alloy_pubsub/index.html
Loading