#json-rpc #nimiq #json #rpc

macro nimiq-jsonrpc-derive

Derivation macros for generating JSON RPC methods

7 unstable releases (3 breaking)

new 0.3.0 Apr 29, 2025
0.2.2 Jan 29, 2025
0.1.2 Jan 8, 2025
0.1.1 Nov 23, 2024
0.0.0 Nov 12, 2024

#5 in #nimiq

Download history 334/week @ 2025-01-08 276/week @ 2025-01-15 1473/week @ 2025-01-22 1774/week @ 2025-01-29 2812/week @ 2025-02-05 2858/week @ 2025-02-12 1649/week @ 2025-02-19 1433/week @ 2025-02-26 1438/week @ 2025-03-05 1499/week @ 2025-03-12 814/week @ 2025-03-19 1104/week @ 2025-03-26 1498/week @ 2025-04-02 1410/week @ 2025-04-09 1424/week @ 2025-04-16 1191/week @ 2025-04-23

5,731 downloads per month

Apache-2.0

105KB
2K SLoC

Build + Test

Nimiq JSON-RPC

A Rust implementation of the JSON-RPC 2.0 specification.

  • nimiq-jsonrpc-core implements the data structures (using serde) for JSON-RPC.
  • nimiq-jsonrpc-client is a client implementation for a HTTP (using reqwest) and websocket client (using tokio-tungstenite).
  • nimiq-jsonrpc-server is a server implementation for HTTP and websocket (using warp).

Example

use async_trait::async_trait;
use serde::{Serialize, Deserialize};

use nimiq_jsonrpc_server::{Server, Config};
use nimiq_jsonrpc_client::http::HttpClient;


#[nimiq_jsonrpc_derive::proxy]
#[async_trait]
trait Foobar {
    async fn hello(&mut self, name: String) -> String;
}

struct FoobarService;

#[nimiq_jsonrpc_derive::service]
#[async_trait]
impl Foobar for FoobarService {
    async fn hello(&mut self, name: String) -> String {
        println!("Hello, {}", name);
        format!("Hello, {}", name)
    }
}


#[tokio::main]
async fn main() {
    dotenvy::dotenv().ok();
    pretty_env_logger::init();

    let config = Config::default();

    log::info!("Listening on: {}", config.bind_to);

    let server = Server::new(config, FoobarService);
    tokio::spawn(async move {
        server.run().await;
    });

    let client = HttpClient::new("http://localhost:8000/");
    let mut proxy = FoobarProxy::new(client);

    let retval = proxy.hello("World".to_owned()).await;
    log::info!("RPC call returned: {}", retval);
}

TODO

  • Share code between websocket clients.

Dependencies

~11–19MB
~253K SLoC