-
Hi everyone! I'm at the beginning of working on a crypto miner and to start I'm working on making a crate that implements this protocol. Do I need to bring serde into my project to create a sturct from this json? {"id": 1, "method": "mining.subscribe", "params": ["MyMiner/1.0.0", null, "my.pool.com", 1234]}
{"id": null, "method": "mining.set_target", "params": ["FFFF000000000000000000000000000000000000000000000000000000000000"]}
{"id": 4, "method": "mining.submit", "params": ["WORKER_NAME", "d70fd222", "98b6ac44d2", ""]} Also how would you recommend having different params? It's a tuple of strings and ints in different orders depending on the method. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Hey @Redhawk18 Yes, the library handles serialization/de-serialization but it requires the individual types to implement serde. So for instance the easiest for you is to use the proc macro API that takes care of it for you: #[rpc(server)]
pub trait Api {
#[method(name = "mining.set_target")]
async fn set_target(
&self,
// you probably need another type here
target: String,
) -> Result<(), ErrorObjectOwned>;
#[method(name = "mining.subscribe")]
async fn subscribe(
&self,
version: String,
session_id: String,
host: String,
port: usize,
) -> Result<Vec<String>, ErrorObjectOwned>;
} Please have a look over the examples which shows how use it. |
Beta Was this translation helpful? Give feedback.
-
Hey again, I thought you wanted to write a server implementation but you are using the client alright.
jsonrpsee takes care of that for you and you don't have to define request and response types with jsonrpc specific stuff. So another example how to use a typed client for your use-case and to call #[rpc(client)]
pub trait Rpc {
#[method(name = "mining.subscribe")]
async fn subscribe(
&self,
version: String,
session_id: String,
host: String,
port: usize,
) -> Result<(String, String), ErrorObjectOwned>;
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// create a websocket connection.
let client = WsClientBuilder::default().build("ws://localhost:9944").await?;
// call `mining.subscribe` which returns (String, String) on success.
let rp = client.subscribe("MyMiner/1.0.0".to_string(), "1".to_string(), "my.pool.com".to_string(), 1234).await?;
Ok(())
} Hopefully clear enough for you to unblock you... and you just have to provide similar stuff for the rest of the API. |
Beta Was this translation helpful? Give feedback.
Hey again,
I thought you wanted to write a server implementation but you are using the client alright.
jsonrpsee takes care of that for you and you don't have to define request and response types with jsonrpc specific stuff.
So another example how to use a typed client for your use-case and to call
mining.subscribe
you can do it as follows: