Skip to content

Commit

Permalink
Merge pull request #39 from gevulotnetwork/feature/tx-error-handling
Browse files Browse the repository at this point in the history
Tx error handling
  • Loading branch information
trusch authored Jan 8, 2025
2 parents 4aa00fe + dd659f8 commit 64755bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/base_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)?,
)?;
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down

0 comments on commit 64755bb

Please sign in to comment.