Skip to content

Commit 2949d29

Browse files
committed
fix: parsing of responses
1 parent 9f1e8c9 commit 2949d29

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

client/src/client.rs

+50-9
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,44 @@ pub struct PackageSubmissionFees {
4949
}
5050

5151
#[derive(Debug, Serialize, Deserialize)]
52-
pub struct PackageTransactionResult {
53-
pub txid: String,
54-
#[serde(rename = "other-wtxid", skip_serializing_if = "Option::is_none")]
55-
pub other_wtxid: Option<String>,
56-
pub vsize: u32,
57-
pub fees: PackageSubmissionFees,
52+
#[serde(untagged)]
53+
pub enum PackageTransactionResult {
54+
Success {
55+
txid: String,
56+
vsize: u32,
57+
fees: PackageSubmissionFees,
58+
},
59+
SuccessAlreadyInMempool {
60+
txid: String,
61+
#[serde(rename = "other-wtxid")]
62+
other_wtxid: Option<String>,
63+
},
64+
Failure {
65+
txid: String,
66+
error: String,
67+
},
68+
}
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
#[test]
73+
fn test_package_transaction_result() {
74+
let result = json!(
75+
{
76+
"txid": "123",
77+
"error": "error"
78+
}
79+
);
80+
let parsed: PackageTransactionResult = serde_json::from_value(result).unwrap();
81+
assert_eq!(
82+
parsed,
83+
PackageTransactionResult::Failure {
84+
txid: "123".to_string(),
85+
error: "error".to_string()
86+
}
87+
);
88+
}
5889
}
59-
6090
#[derive(Debug, Serialize, Deserialize)]
6191
pub struct PackageSubmissionResult {
6292
#[serde(rename = "tx-results")]
@@ -942,7 +972,10 @@ pub trait RpcApi: Sized {
942972
self.call("testmempoolaccept", &[hexes.into()]).await
943973
}
944974

945-
async fn submit_package<R: RawTx + Send + Sync>(&self, rawtxs: &[R]) -> Result<PackageSubmissionResult> {
975+
async fn submit_package<R: RawTx + Send + Sync>(
976+
&self,
977+
rawtxs: &[R],
978+
) -> Result<PackageSubmissionResult> {
946979
let hexes: Vec<serde_json::Value> =
947980
rawtxs.to_vec().into_iter().map(|r| r.raw_hex().into()).collect();
948981
self.call("submitpackage", &[hexes.into()]).await
@@ -1422,7 +1455,15 @@ impl RpcApi for Client {
14221455

14231456
let resp = self.client.send_request(req).await.map_err(Error::from);
14241457
log_response(cmd, &resp);
1425-
Ok(resp?.result()?)
1458+
1459+
let failedmsg = format!(
1460+
"Failed to parse response {:?}",
1461+
resp.as_ref().ok().and_then(|a| a.result.as_ref())
1462+
);
1463+
1464+
Ok(resp?.result().inspect_err(|_| {
1465+
println!("{}", failedmsg);
1466+
})?)
14261467
}
14271468
}
14281469

0 commit comments

Comments
 (0)