diff --git a/Cargo.lock b/Cargo.lock index adb17e80..14cf6fcf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6707,6 +6707,7 @@ version = "0.1.0" dependencies = [ "anyhow", "ethers", + "indoc", "reqwest", "serde", "serde_json", diff --git a/crates/tx-sitter-client/Cargo.toml b/crates/tx-sitter-client/Cargo.toml index d3e6a026..993e4047 100644 --- a/crates/tx-sitter-client/Cargo.toml +++ b/crates/tx-sitter-client/Cargo.toml @@ -12,3 +12,6 @@ serde = { version = "1.0.154", features = ["derive"] } serde_json = "1.0.94" strum = { version = "0.25", features = ["derive"] } tracing = "0.1" + +[dev-dependencies] +indoc = "2" diff --git a/crates/tx-sitter-client/src/data.rs b/crates/tx-sitter-client/src/data.rs index 0a00e3f3..fee4109c 100644 --- a/crates/tx-sitter-client/src/data.rs +++ b/crates/tx-sitter-client/src/data.rs @@ -58,15 +58,37 @@ pub struct GetTxResponse { // Sent tx data #[serde(default, skip_serializing_if = "Option::is_none")] pub tx_hash: Option, - pub status: TxStatus, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub status: Option, } #[derive(Debug, Clone, Copy, Serialize, Deserialize, Display, PartialEq, Eq)] #[serde(rename_all = "camelCase")] #[strum(serialize_all = "camelCase")] pub enum TxStatus { - Unsent, Pending, Mined, Finalized, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn decode_partial_tx_response() { + const DATA: &str = indoc::indoc! {r#"{ + "data": "0xff", + "gasLimit": "2000000", + "nonce": 54, + "status": null, + "to": "0x928a514350a403e2f5e3288c102f6b1ccabeb37c", + "txHash": null, + "txId": "99e83a12-d6df-4f9c-aa43-048b38561dfd", + "value": "0" + } + "#}; + + serde_json::from_str::(DATA).unwrap(); + } +} diff --git a/crates/tx-sitter-client/src/lib.rs b/crates/tx-sitter-client/src/lib.rs index 461102dc..82680cd5 100644 --- a/crates/tx-sitter-client/src/lib.rs +++ b/crates/tx-sitter-client/src/lib.rs @@ -65,18 +65,25 @@ impl TxSitterClient { } #[instrument(skip(self))] - pub async fn get_txs(&self, tx_status: Option) -> anyhow::Result> { - let mut url = format!("{}/txs", self.url); - - match tx_status { - Some(TxStatus::Unsent) => { - url.push_str("?unsent=true"); - } - Some(tx_status) => { - url.push_str(&format!("?status={}", tx_status)); - } - None => {} - } + pub async fn get_txs(&self) -> anyhow::Result> { + let url = format!("{}/txs", self.url); + + self.json_get(&url).await + } + + #[instrument(skip(self))] + pub async fn get_txs_by_status( + &self, + tx_status: TxStatus, + ) -> anyhow::Result> { + let url = format!("{}/txs?status={}", self.url, tx_status); + + self.json_get(&url).await + } + + #[instrument(skip(self))] + pub async fn get_unsent_txs(&self) -> anyhow::Result> { + let url = format!("{}/txs?unsent=true", self.url); self.json_get(&url).await } diff --git a/src/ethereum/write_provider/tx_sitter.rs b/src/ethereum/write_provider/tx_sitter.rs index 2f834ccf..140b4308 100644 --- a/src/ethereum/write_provider/tx_sitter.rs +++ b/src/ethereum/write_provider/tx_sitter.rs @@ -39,7 +39,7 @@ impl TxSitter { .context("Error fetching tx") .map_err(TxError::Send)?; - if tx.status == TxStatus::Mined || tx.status == TxStatus::Finalized { + if tx.status == Some(TxStatus::Mined) || tx.status == Some(TxStatus::Finalized) { return Ok(TransactionResult { transaction_id: tx.tx_id, hash: Some( @@ -93,14 +93,14 @@ impl Inner for TxSitter { async fn fetch_pending_transactions(&self) -> Result, TxError> { let unsent_txs = self .client - .get_txs(Some(TxStatus::Unsent)) + .get_unsent_txs() .await .context("Error fetching unsent transactions") .map_err(TxError::Send)?; let pending_txs = self .client - .get_txs(Some(TxStatus::Pending)) + .get_txs_by_status(TxStatus::Pending) .await .context("Error fetching pending transactions") .map_err(TxError::Send)?;