diff --git a/src/examples/example_transfer.rs b/src/examples/example_transfer.rs index 094cbf63e..6a97118aa 100755 --- a/src/examples/example_transfer.rs +++ b/src/examples/example_transfer.rs @@ -45,11 +45,11 @@ fn main() { println!("[+] Composed extrinsic: {:?}\n", xt); - // send and watch extrinsic until finalized + // send and watch extrinsic until InBlock let tx_hash = api - .send_extrinsic(xt.hex_encode(), XtStatus::Finalized) + .send_extrinsic(xt.hex_encode(), XtStatus::InBlock) .unwrap(); - println!("[+] Transaction got finalized. Hash: {:?}\n", tx_hash); + println!("[+] Transaction got inBlock. Hash: {:?}\n", tx_hash); // verify that Bob's free Balance increased let bob = api.get_account_data(&to).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index d8e097037..6076df6e5 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -344,7 +344,19 @@ where info!("ready: {}", res); Ok(None) } - _ => panic!("can only wait for finalized or ready extrinsic status"), + XtStatus::Broadcast => { + rpc::send_extrinsic(self.url.clone(), jsonreq, result_in); + let res = result_out.recv().unwrap(); + info!("broadcast: {}", res); + Ok(None) + } + XtStatus::InBlock => { + rpc::send_extrinsic(self.url.clone(), jsonreq, result_in); + let res = result_out.recv().unwrap(); + info!("inBlock: {}", res); + Ok(Some(hexstr_to_hash(res).unwrap())) + } + _ => panic!("can only wait for finalized, ready, inBlock or broadcast extrinsic status"), } } diff --git a/src/rpc/client.rs b/src/rpc/client.rs index b35191dbf..5a5d233cb 100644 --- a/src/rpc/client.rs +++ b/src/rpc/client.rs @@ -22,6 +22,8 @@ use ws::{CloseCode, Handler, Handshake, Message, Result, Sender}; #[derive(Debug, PartialEq)] pub enum XtStatus { Finalized, + InBlock, + Broadcast, Ready, Future, Error, @@ -85,6 +87,41 @@ pub fn on_subscription_msg(msg: Message, _out: Sender, result: ThreadOut Ok(()) } +pub fn on_extrinsic_msg_until_in_block( + msg: Message, + out: Sender, + result: ThreadOut, +) -> Result<()> { + let retstr = msg.as_text().unwrap(); + debug!("got msg {}", retstr); + match parse_status(retstr) { + (XtStatus::Finalized, val) => end_process(out, result, val), + (XtStatus::InBlock, val) => end_process(out, result, val), + (XtStatus::Future, _) => end_process(out, result, None), + (XtStatus::Error, e) => end_process(out, result, e), + _ => (), + }; + Ok(()) +} + + +pub fn on_extrinsic_msg_until_broadcast( + msg: Message, + out: Sender, + result: ThreadOut, +) -> Result<()> { + let retstr = msg.as_text().unwrap(); + debug!("got msg {}", retstr); + match parse_status(retstr) { + (XtStatus::Finalized, val) => end_process(out, result, val), + (XtStatus::Broadcast, _) => end_process(out, result, None), + (XtStatus::Future, _) => end_process(out, result, None), + (XtStatus::Error, e) => end_process(out, result, e), + _ => (), + }; + Ok(()) +} + pub fn on_extrinsic_msg_until_finalized( msg: Message, out: Sender, @@ -114,6 +151,8 @@ pub fn on_extrinsic_msg_until_ready( match parse_status(retstr) { (XtStatus::Finalized, val) => end_process(out, result, val), (XtStatus::Ready, _) => end_process(out, result, None), + (XtStatus::InBlock, _) => end_process(out, result, None), + (XtStatus::Broadcast, _) => end_process(out, result, None), (XtStatus::Future, _) => end_process(out, result, None), (XtStatus::Error, e) => end_process(out, result, e), _ => (), @@ -150,7 +189,16 @@ fn parse_status(msg: &str) -> (XtStatus, Option) { if let Some(hash) = obj.get("finalized") { info!("finalized: {:?}", hash); (XtStatus::Finalized, Some(hash.to_string())) - } else { + } + else if let Some(hash) = obj.get("inBlock") { + info!("inBlock: {:?}", hash); + (XtStatus::InBlock, Some(hash.to_string())) + } + else if let Some(hash) = obj.get("broadcast") { + info!("Broadcast: {:?}", array); + (XtStatus::Broadcast, Some(array.to_string())) + } + else { (XtStatus::Unknown, None) } } @@ -188,6 +236,29 @@ mod tests { ) ); + let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":{\"broadcast\":[\"QmfSF4VYWNqNf5KYHpDEdY8Rt1nPUgSkMweDkYzhSWirGY\",\"Qmchhx9SRFeNvqjUK4ZVQ9jH4zhARFkutf9KhbbAmZWBLx\",\"QmQJAqr98EF1X3YfjVKNwQUG9RryqX4Hv33RqGChbz3Ncg\"]},\"subscription\":232}}"; + assert_eq!(parse_status(msg), (XtStatus::Broadcast, None)); assert_eq!( + parse_status(msg), ( + XtStatus::Broadcast, + Some( + "[\"QmfSF4VYWNqNf5KYHpDEdY8Rt1nPUgSkMweDkYzhSWirGY\",\"Qmchhx9SRFeNvqjUK4ZVQ9jH4zhARFkutf9KhbbAmZWBLx\",\"QmQJAqr98EF1X3YfjVKNwQUG9RryqX4Hv33RqGChbz3Ncg\"]" + .to_string() + ) + ) + ); + + let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":{\"inBlock\":\"0x3104d362365ff5ddb61845e1de441b56c6722e94c1aee362f8aa8ba75bd7a3aa\"},\"subscription\":232}}"; + assert_eq!( + parse_status(msg), ( + XtStatus::InBlock, + Some( + "\"0x3104d362365ff5ddb61845e1de441b56c6722e94c1aee362f8aa8ba75bd7a3aa\"" + .to_string() + ) + ) + ); + + let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":\"future\",\"subscription\":2}}"; assert_eq!(parse_status(msg), (XtStatus::Future, None)); diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index bd9a72e54..1c054cb28 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -34,6 +34,14 @@ pub fn send_extrinsic(url: String, json_req: String, result_in: ThreadOut) { + start_rpc_client_thread(url, json_req, result_in, on_extrinsic_msg_until_broadcast) +} + +pub fn send_extrinsic_and_wait_until_in_block(url: String, json_req: String, result_in: ThreadOut) { + start_rpc_client_thread(url, json_req, result_in, on_extrinsic_msg_until_in_block) +} + pub fn send_extrinsic_and_wait_until_finalized( url: String, json_req: String,