alloy_provider/ext/
txpool.rs1use 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#[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 async fn txpool_content(&self) -> TransportResult<TxpoolContent<N::TransactionResponse>>;
20
21 async fn txpool_content_from(
25 &self,
26 from: Address,
27 ) -> TransportResult<TxpoolContentFrom<N::TransactionResponse>>;
28
29 async fn txpool_inspect(&self) -> TransportResult<TxpoolInspect>;
38
39 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}