mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-09 23:38:10 -05:00
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:
@@ -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<()>
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user