docs: add code example to extend_rpc_modules method (#17446)

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Jennifer <jenpaff0@gmail.com>
This commit is contained in:
Matthias Seitz
2025-07-18 11:57:07 +02:00
committed by GitHub
parent 3c9ff6e157
commit ca116aa7b7
3 changed files with 38 additions and 0 deletions

View File

@@ -547,6 +547,39 @@ where
}
/// Sets the hook that is run to configure the rpc modules.
///
/// This hook can obtain the node's components (txpool, provider, etc.) and can modify the
/// modules that the RPC server installs.
///
/// # Examples
///
/// ```rust,ignore
/// use jsonrpsee::{core::RpcResult, proc_macros::rpc};
///
/// #[derive(Clone)]
/// struct CustomApi<Pool> { pool: Pool }
///
/// #[rpc(server, namespace = "custom")]
/// impl CustomApi {
/// #[method(name = "hello")]
/// async fn hello(&self) -> RpcResult<String> {
/// Ok("World".to_string())
/// }
/// }
///
/// let node = NodeBuilder::new(config)
/// .node(EthereumNode::default())
/// .extend_rpc_modules(|ctx| {
/// // Access node components, so they can used by the CustomApi
/// let pool = ctx.pool().clone();
///
/// // Add custom RPC namespace
/// ctx.modules.merge_configured(CustomApi { pool }.into_rpc())?;
///
/// Ok(())
/// })
/// .build()?;
/// ```
pub fn extend_rpc_modules<F>(self, hook: F) -> Self
where
F: FnOnce(RpcContext<'_, NodeAdapter<T, CB::Components>, AO::EthApi>) -> eyre::Result<()>

View File

@@ -283,6 +283,8 @@ where
}
/// Returns a reference to the configured node.
///
/// This gives access to the node's components.
pub const fn node(&self) -> &Node {
&self.node
}

View File

@@ -32,7 +32,9 @@ fn main() {
Cli::<EthereumChainSpecParser, RethCliTxpoolExt>::parse()
.run(|builder, args| async move {
let handle = builder
// configure default ethereum node
.node(EthereumNode::default())
// extend the rpc modules with our custom `TxpoolExt` endpoints
.extend_rpc_modules(move |ctx| {
if !args.enable_ext {
return Ok(())
@@ -50,6 +52,7 @@ fn main() {
Ok(())
})
// launch the node with custom rpc
.launch()
.await?;