Skip to content

Commit

Permalink
add rpc_methods endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Feb 22, 2024
1 parent 2f93b91 commit c2177a9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib-xps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ log.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
serde.workspace = true
serde_json.workspace = true
tokio.workspace = true
async-trait.workspace = true
jsonrpsee.workspace = true
Expand All @@ -30,4 +31,3 @@ messaging = { path = "../messaging" }
jsonrpsee = { workspace = true, features = ["macros", "server", "client"] }
tokio = { workspace = true, features = ["macros", "rt", "time"] }
futures = "0.3"
serde_json.workspace = true
29 changes: 26 additions & 3 deletions lib-xps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ethers::{
abi::Address,
providers::{Provider, Ws},
};
use jsonrpsee::server::Server;
use jsonrpsee::{server::Server, RpcModule};
use std::str::FromStr;
use xps_types::{CONVERSATION, DID_ETH_REGISTRY};

Expand All @@ -29,10 +29,33 @@ pub async fn run<P: AsRef<str>>(host: String, port: u16, provider: P) -> Result<
let provider = Provider::<Ws>::connect(provider.as_ref()).await.unwrap();

let context = GatewayContext::new(registry_contract, conversation_contract, provider).await?;
let xps_methods = rpc::XpsMethods::new(&context);
let handle = server.start(xps_methods.into_rpc());
let mut methods = RpcModule::new(());
methods.merge(rpc::XpsMethods::new(&context).into_rpc())?;
let methods = build_rpc_api(methods);

let handle = server.start(methods);

Check warning on line 36 in lib-xps/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib-xps/src/lib.rs#L32-L36

Added lines #L32 - L36 were not covered by tests

log::info!("Server Started at {addr}");
handle.stopped().await;
Ok(())
}

// create an endpoint that lists all the methods available on the server, at the
// endpoint `/rpc_methods`
fn build_rpc_api<M: Send + Sync + 'static>(mut rpc_api: RpcModule<M>) -> RpcModule<M> {
let mut available_methods = rpc_api.method_names().collect::<Vec<_>>();
// The "rpc_methods" is defined below and we want it to be part of the reported methods.
available_methods.push("rpc_methods");
available_methods.sort();

rpc_api
.register_method("rpc_methods", move |_, _| {
serde_json::json!({
"methods": available_methods,
})
})
.expect("infallible all other methods have their own address space; qed");

rpc_api
}

Check warning on line 60 in lib-xps/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib-xps/src/lib.rs#L45-L60

Added lines #L45 - L60 were not covered by tests

0 comments on commit c2177a9

Please sign in to comment.