|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use clap::Parser; |
| 4 | +use futures_util::StreamExt; |
| 5 | +use hedera::{ |
| 6 | + AccountId, Client, PrivateKey, TopicCreateTransaction, TopicMessageQuery, TopicMessageSubmitTransaction |
| 7 | +}; |
| 8 | + |
| 9 | +#[derive(Parser, Debug)] |
| 10 | +struct Args { |
| 11 | + #[clap(long, env)] |
| 12 | + operator_account_id: AccountId, |
| 13 | + |
| 14 | + #[clap(long, env)] |
| 15 | + operator_key: PrivateKey, |
| 16 | + |
| 17 | + #[clap(long, env, default_value = "testnet")] |
| 18 | + hedera_network: String, |
| 19 | +} |
| 20 | + |
| 21 | +#[tokio::main] |
| 22 | +async fn main() -> anyhow::Result<()> { |
| 23 | + let _ = dotenvy::dotenv(); |
| 24 | + |
| 25 | + let args = Args::parse(); |
| 26 | + |
| 27 | + let client = Client::for_name(&args.hedera_network)?; |
| 28 | + |
| 29 | + client.set_operator(args.operator_account_id, args.operator_key); |
| 30 | + |
| 31 | + // generate a submit key to use with the topic. |
| 32 | + let submit_key = PrivateKey::generate_ed25519(); |
| 33 | + |
| 34 | + let topic_id = TopicCreateTransaction::new() |
| 35 | + .topic_memo("sdk::rust::consensus_pub_sub_with_submit_key") |
| 36 | + .submit_key(submit_key.public_key()) |
| 37 | + .execute(&client) |
| 38 | + .await? |
| 39 | + .get_receipt(&client) |
| 40 | + .await? |
| 41 | + .topic_id |
| 42 | + .unwrap(); |
| 43 | + |
| 44 | + println!("Created Topic `{topic_id}` with submit key `{submit_key}`"); |
| 45 | + |
| 46 | + println!("Waiting 10s for the mirror node to catch up"); |
| 47 | + |
| 48 | + tokio::time::sleep(Duration::from_secs(10)).await; |
| 49 | + |
| 50 | + let _handle: tokio::task::JoinHandle<hedera::Result<()>> = tokio::spawn({ |
| 51 | + let client = client.clone(); |
| 52 | + async move { |
| 53 | + println!("sending 5 messages"); |
| 54 | + |
| 55 | + for i in 0..5 { |
| 56 | + let v: i64 = rand::random(); |
| 57 | + let message = format!("random message: {v}"); |
| 58 | + |
| 59 | + println!("publishing message {i}: `{message}`"); |
| 60 | + |
| 61 | + TopicMessageSubmitTransaction::new() |
| 62 | + .topic_id(topic_id) |
| 63 | + .message(message) |
| 64 | + .sign(submit_key.clone()) |
| 65 | + .execute(&client) |
| 66 | + .await? |
| 67 | + .get_receipt(&client) |
| 68 | + .await?; |
| 69 | + |
| 70 | + tokio::time::sleep(Duration::from_secs(2)).await |
| 71 | + } |
| 72 | + |
| 73 | + println!( |
| 74 | + "Finished sending the messages, press ctrl+c to exit once they're all recieved" |
| 75 | + ); |
| 76 | + |
| 77 | + Ok(()) |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + let client = client.clone(); |
| 82 | + let mut stream = TopicMessageQuery::new() |
| 83 | + .topic_id(topic_id) |
| 84 | + .subscribe(&client); |
| 85 | + |
| 86 | + while let Some(elem) = stream.next().await { |
| 87 | + let elem = match elem { |
| 88 | + Ok(it) => it, |
| 89 | + Err(e) => { |
| 90 | + eprintln!("Error while handling message stream: {e:?}"); |
| 91 | + break; |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + println!( |
| 96 | + "(seq: `{}`, contents: `{}`) reached consensus at {}", |
| 97 | + elem.sequence_number, |
| 98 | + String::from_utf8_lossy(&elem.contents), |
| 99 | + elem.consensus_timestamp |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + Ok(()) |
| 104 | +} |
0 commit comments