Created
November 3, 2021 23:14
-
-
Save unyieldinggrace/61294b5bb10e4def1c186a7553f4b3d8 to your computer and use it in GitHub Desktop.
Small demo of how to listen for ZMQ notifications from monerod about txns being added to the mempool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add these to Cargo.toml | |
// zmq = "0.9.2" | |
// json = "0.12.4" | |
use substring::Substring; | |
async fn start_listening(&self) { | |
let context = zmq::Context::new(); | |
let subscriber = context.socket(zmq::SUB).unwrap(); | |
subscriber | |
.connect("tcp://myxmrnode.com:18085") | |
.expect("failed connecting subscriber"); | |
subscriber.set_subscribe(b"json-minimal-txpool_add").expect("failed subscribing"); | |
loop { | |
subscriber | |
.recv_string(0) | |
.expect("failed receiving envelope") | |
.unwrap(); | |
let message = subscriber | |
.recv_string(0) | |
.expect("failed receiving message") | |
.unwrap(); | |
self.process_message(message).await; | |
} | |
} | |
async fn process_message(&self, msg: String) { | |
let prefixlength = "json-minimal-txpool_add:".len(); | |
let jsonmessage = msg.substring(prefixlength, msg.len()); | |
let parsed = json::parse(jsonmessage).unwrap(); | |
println!("Message: {}\tTX Hash: {}", jsonmessage, parsed[0]["id"]); | |
// use reqwest to get tx data from monerod using the parsed tx hash | |
self.get_tx_data(parsed[0]["id"].to_string()).await; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment