diff --git a/src/base_client.rs b/src/base_client.rs index 2a27729..40e198f 100644 --- a/src/base_client.rs +++ b/src/base_client.rs @@ -331,12 +331,7 @@ impl BaseClient { let resp = resp.into_inner(); log::debug!("broadcast_tx response: {:#?}", resp); let tx_response = resp.tx_response.ok_or("Tx response not found")?; - if tx_response.code != 0 { - return Err(Error::Unknown(format!( - "Transaction failed with code: {} ({})", - tx_response.code, tx_response.raw_log - ))); - } + Self::assert_tx_success(&tx_response)?; // Bump up the local account sequence after successful tx. self.account_sequence = Some(sequence + 1); @@ -363,6 +358,7 @@ impl BaseClient { self.wait_for_tx(&hash, Some(tokio::time::Duration::from_secs(10))) .await?; let tx_response: TxResponse = self.get_tx_response(&hash).await?; + Self::assert_tx_success(&tx_response)?; let tx_msg_data = cosmos_sdk_proto::cosmos::base::abci::v1beta1::TxMsgData::decode( &*hex::decode(tx_response.data)?, )?; @@ -374,6 +370,28 @@ impl BaseClient { } } + /// Checks if Tx did not failed with non-zero code. + /// + /// # Arguments + /// + /// * `tx_response` - TxResponse. + /// + /// # Returns + /// + /// An empty Result or a Tx error. + fn assert_tx_success(tx_response: &TxResponse) -> Result<()> { + let (tx_hash, tx_code, raw_log) = ( + tx_response.txhash.to_owned(), + tx_response.code, + tx_response.raw_log.to_owned(), + ); + if tx_code != 0 { + return Err(Error::Tx(tx_hash, tx_code, raw_log)); + } + + Ok(()) + } + /// Retrieves the latest block from the blockchain. /// /// # Returns diff --git a/src/error.rs b/src/error.rs index 78dfe3c..43113bf 100644 --- a/src/error.rs +++ b/src/error.rs @@ -22,6 +22,8 @@ pub enum Error { Parse(String), #[error("tendermint error: {0}")] Tendermint(#[from] tendermint::Error), + #[error("tx {0} failed with code {1}: {2}")] + Tx(String, u32, String), #[error("unknown error: {0}")] Unknown(String), }