alloy_provider/ext/
testing.rs

1//! Testing namespace for building a block in a single call.
2//!
3//! This follows the `testing_buildBlockV1` specification.
4
5use crate::Provider;
6use alloy_json_rpc::RpcRecv;
7use alloy_network::Network;
8use alloy_rpc_types_engine::{ExecutionPayloadEnvelopeV5, TestingBuildBlockRequestV1};
9use alloy_transport::TransportResult;
10
11/// Extension trait that gives access to Testing API RPC methods.
12#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
13#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
14pub trait TestingApi<N>: Send + Sync {
15    /// Builds a block using the provided method and request, returning a generic response type.
16    async fn build_block<R: RpcRecv>(
17        &self,
18        method: &'static str,
19        request: TestingBuildBlockRequestV1,
20    ) -> TransportResult<R>;
21
22    /// Builds a block using the provided parent, payload attributes, and transactions.
23    async fn build_block_v1(
24        &self,
25        request: TestingBuildBlockRequestV1,
26    ) -> TransportResult<ExecutionPayloadEnvelopeV5>;
27}
28
29#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
30#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
31impl<N, P> TestingApi<N> for P
32where
33    N: Network,
34    P: Provider<N>,
35{
36    async fn build_block<R: RpcRecv>(
37        &self,
38        method: &'static str,
39        request: TestingBuildBlockRequestV1,
40    ) -> TransportResult<R> {
41        self.client().request(method, (request,)).await
42    }
43
44    async fn build_block_v1(
45        &self,
46        request: TestingBuildBlockRequestV1,
47    ) -> TransportResult<ExecutionPayloadEnvelopeV5> {
48        self.build_block("testing_buildBlockV1", request).await
49    }
50}