alloy_provider/ext/
txpool.rs

1//! This modules extends the Ethereum JSON-RPC provider with the `txpool` namespace.
2use crate::Provider;
3use alloy_network::{Ethereum, Network};
4use alloy_primitives::Address;
5use alloy_rpc_types_txpool::{TxpoolContent, TxpoolContentFrom, TxpoolInspect, TxpoolStatus};
6use alloy_transport::TransportResult;
7
8/// Txpool namespace rpc interface.
9#[allow(unused, unreachable_pub)]
10#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
11#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
12pub trait TxPoolApi<N: Network = Ethereum>: Send + Sync {
13    /// Returns the content of the transaction pool.
14    ///
15    /// Lists the exact details of all the transactions currently pending for inclusion in the next
16    /// block(s), as well as the ones that are being scheduled for future execution only.
17    ///
18    /// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_content) for more details
19    async fn txpool_content(&self) -> TransportResult<TxpoolContent<N::TransactionResponse>>;
20
21    /// Returns the content of the transaction pool filtered by a specific address.
22    ///
23    /// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_contentFrom) for more details
24    async fn txpool_content_from(
25        &self,
26        from: Address,
27    ) -> TransportResult<TxpoolContentFrom<N::TransactionResponse>>;
28
29    /// Returns a textual summary of each transaction in the pool.
30    ///
31    /// Lists a textual summary of all the transactions currently pending for inclusion in the next
32    /// block(s), as well as the ones that are being scheduled for future execution only.
33    /// This is a method specifically tailored to developers to quickly see the
34    /// transactions in the pool and find any potential issues.
35    ///
36    /// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_inspect) for more details
37    async fn txpool_inspect(&self) -> TransportResult<TxpoolInspect>;
38
39    /// Returns the current status of the transaction pool.
40    ///
41    /// i.e the number of transactions currently pending for inclusion in the next block(s), as well
42    /// as the ones that are being scheduled for future execution only.
43    ///
44    /// See [here](https://geth.ethereum.org/docs/rpc/ns-txpool#txpool_status) for more details
45    async fn txpool_status(&self) -> TransportResult<TxpoolStatus>;
46}
47
48#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
49#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
50impl<P, N> TxPoolApi<N> for P
51where
52    P: Provider<N>,
53    N: Network,
54{
55    async fn txpool_content(&self) -> TransportResult<TxpoolContent<N::TransactionResponse>> {
56        self.client().request_noparams("txpool_content").await
57    }
58
59    async fn txpool_content_from(
60        &self,
61        from: Address,
62    ) -> TransportResult<TxpoolContentFrom<N::TransactionResponse>> {
63        self.client().request("txpool_contentFrom", (from,)).await
64    }
65
66    async fn txpool_inspect(&self) -> TransportResult<TxpoolInspect> {
67        self.client().request_noparams("txpool_inspect").await
68    }
69
70    async fn txpool_status(&self) -> TransportResult<TxpoolStatus> {
71        self.client().request_noparams("txpool_status").await
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78    use crate::{ext::test::async_ci_only, ProviderBuilder};
79    use alloy_node_bindings::{utils::run_with_tempdir, Geth};
80
81    #[tokio::test]
82    async fn test_txpool_content() {
83        async_ci_only(|| async move {
84            run_with_tempdir("geth-test-", |temp_dir| async move {
85                let geth = Geth::new().disable_discovery().data_dir(temp_dir).spawn();
86                let provider = ProviderBuilder::new().on_http(geth.endpoint_url());
87                let content = provider.txpool_content().await.unwrap();
88                assert_eq!(content, TxpoolContent::default());
89            })
90            .await;
91        })
92        .await;
93    }
94
95    #[tokio::test]
96    async fn test_txpool_content_from() {
97        async_ci_only(|| async move {
98            run_with_tempdir("geth-test-", |temp_dir| async move {
99                let geth = Geth::new().disable_discovery().data_dir(temp_dir).spawn();
100                let provider = ProviderBuilder::new().on_http(geth.endpoint_url());
101                let content = provider.txpool_content_from(Address::default()).await.unwrap();
102                assert_eq!(content, TxpoolContentFrom::default());
103            })
104            .await;
105        })
106        .await;
107    }
108
109    #[tokio::test]
110    async fn test_txpool_inspect() {
111        async_ci_only(|| async move {
112            run_with_tempdir("geth-test-", |temp_dir| async move {
113                let geth = Geth::new().disable_discovery().data_dir(temp_dir).spawn();
114                let provider = ProviderBuilder::new().on_http(geth.endpoint_url());
115                let content = provider.txpool_inspect().await.unwrap();
116                assert_eq!(content, TxpoolInspect::default());
117            })
118            .await;
119        })
120        .await;
121    }
122
123    #[tokio::test]
124    async fn test_txpool_status() {
125        async_ci_only(|| async move {
126            run_with_tempdir("geth-test-", |temp_dir| async move {
127                let geth = Geth::new().disable_discovery().data_dir(temp_dir).spawn();
128                let provider = ProviderBuilder::new().on_http(geth.endpoint_url());
129                let content = provider.txpool_status().await.unwrap();
130                assert_eq!(content, TxpoolStatus::default());
131            })
132            .await;
133        })
134        .await;
135    }
136}