Skip to content

Commit 094ed88

Browse files
committed
scs#65 add: Await InBlock and Broadcast Status When Sending Extrinsic
1 parent 23b6255 commit 094ed88

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,18 @@ where
344344
info!("ready: {}", res);
345345
Ok(None)
346346
}
347+
XtStatus::Broadcast => {
348+
rpc::send_extrinsic(self.url.clone(), jsonreq, result_in);
349+
let res = result_out.recv().unwrap();
350+
info!("broadcast: {}", res);
351+
Ok(None)
352+
}
353+
XtStatus::InBlock => {
354+
rpc::send_extrinsic(self.url.clone(), jsonreq, result_in);
355+
let res = result_out.recv().unwrap();
356+
info!("inBlock: {}", res);
357+
Ok(None)
358+
}
347359
_ => panic!("can only wait for finalized or ready extrinsic status"),
348360
}
349361
}

src/rpc/client.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub enum XtStatus {
2626
Future,
2727
Error,
2828
Unknown,
29+
InBlock,
30+
Broadcast,
2931
}
3032

3133
pub type OnMessageFn = fn(msg: Message, out: Sender, result: ThreadOut<String>) -> Result<()>;
@@ -114,6 +116,8 @@ pub fn on_extrinsic_msg_until_ready(
114116
match parse_status(retstr) {
115117
(XtStatus::Finalized, val) => end_process(out, result, val),
116118
(XtStatus::Ready, _) => end_process(out, result, None),
119+
(XtStatus::InBlock, _) => end_process(out, result, None),
120+
(XtStatus::Broadcast, _) => end_process(out, result, None),
117121
(XtStatus::Future, _) => end_process(out, result, None),
118122
(XtStatus::Error, _) => end_process(out, result, None),
119123
_ => (),
@@ -144,7 +148,12 @@ fn parse_status(msg: &str) -> (XtStatus, Option<String>) {
144148
if let Some(hash) = obj.get("finalized") {
145149
info!("finalized: {:?}", hash);
146150
(XtStatus::Finalized, Some(hash.to_string()))
147-
} else {
151+
}
152+
else if let Some(hash) = obj.get("inBlock") {
153+
info!("inBlock: {:?}", hash);
154+
(XtStatus::InBlock, Some(hash.to_string()))
155+
}
156+
else {
148157
(XtStatus::Unknown, None)
149158
}
150159
}
@@ -185,6 +194,20 @@ mod tests {
185194
let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":\"future\",\"subscription\":2}}";
186195
assert_eq!(parse_status(msg), (XtStatus::Future, None));
187196

197+
let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":{\"broadcast\":[\"QmfSF4VYWNqNf5KYHpDEdY8Rt1nPUgSkMweDkYzhSWirGY\",\"Qmchhx9SRFeNvqjUK4ZVQ9jH4zhARFkutf9KhbbAmZWBLx\",\"QmQJAqr98EF1X3YfjVKNwQUG9RryqX4Hv33RqGChbz3Ncg\"]},\"subscription\":232}}";
198+
assert_eq!(parse_status(msg), (XtStatus::Broadcast, None));
199+
200+
let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":{\"inBlock\":\"0x3104d362365ff5ddb61845e1de441b56c6722e94c1aee362f8aa8ba75bd7a3aa\"},\"subscription\":232}}";
201+
assert_eq!(
202+
parse_status(msg), (
203+
XtStatus::InBlock,
204+
Some(
205+
"\"0x3104d362365ff5ddb61845e1de441b56c6722e94c1aee362f8aa8ba75bd7a3aa\""
206+
.to_string()
207+
)
208+
)
209+
);
210+
188211
let msg = "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32700,\"message\":\"Parse error\"},\"id\":null}";
189212
assert_eq!(parse_status(msg), (XtStatus::Error, None));
190213

0 commit comments

Comments
 (0)