rpc.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. use serde_json::{json, Value};
  2. use url::Url;
  3. use darkfi::{
  4. error::Result,
  5. rpc::{client::RpcClient, jsonrpc::JsonRequest},
  6. };
  7. use crate::error::{DnetViewError, DnetViewResult};
  8. pub struct RpcConnect {
  9. pub name: String,
  10. pub rpc_client: RpcClient,
  11. }
  12. impl RpcConnect {
  13. pub async fn new(url: Url, name: String) -> Result<Self> {
  14. let rpc_client = RpcClient::new(url).await?;
  15. Ok(Self { name, rpc_client })
  16. }
  17. // --> {"jsonrpc": "2.0", "method": "ping", "params": [], "id": 42}
  18. // <-- {"jsonrpc": "2.0", "result": "pong", "id": 42}
  19. pub async fn ping(&self) -> Result<Value> {
  20. let req = JsonRequest::new("ping", json!([]));
  21. self.rpc_client.request(req).await
  22. }
  23. // --> {"jsonrpc": "2.0", "method": "poll", "params": [], "id": 42}
  24. // <-- {"jsonrpc": "2.0", "result": {"nodeID": [], "nodeinfo" [], "id": 42}
  25. pub async fn get_info(&self) -> DnetViewResult<Value> {
  26. let req = JsonRequest::new("get_info", json!([]));
  27. match self.rpc_client.request(req).await {
  28. Ok(req) => Ok(req),
  29. Err(e) => Err(DnetViewError::Darkfi(e)),
  30. }
  31. }
  32. // Returns all lilith node spawned networks names with their node addresses.
  33. // --> {"jsonrpc": "2.0", "method": "spawns", "params": [], "id": 42}
  34. // <-- {"jsonrpc": "2.0", "result": "{spawns}", "id": 42}
  35. pub async fn lilith_spawns(&self) -> DnetViewResult<Value> {
  36. let req = JsonRequest::new("spawns", json!([]));
  37. match self.rpc_client.request(req).await {
  38. Ok(req) => Ok(req),
  39. Err(e) => Err(DnetViewError::Darkfi(e)),
  40. }
  41. }
  42. }