Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dzejkop/fix-invalid-response-dto #753

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/tx-sitter-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
26 changes: 24 additions & 2 deletions crates/tx-sitter-client/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,37 @@ pub struct GetTxResponse {
// Sent tx data
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tx_hash: Option<H256>,
pub status: TxStatus,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<TxStatus>,
Dzejkop marked this conversation as resolved.
Show resolved Hide resolved
}

#[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::<GetTxResponse>(DATA).unwrap();
}
}
31 changes: 19 additions & 12 deletions crates/tx-sitter-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,25 @@ impl TxSitterClient {
}

#[instrument(skip(self))]
pub async fn get_txs(&self, tx_status: Option<TxStatus>) -> anyhow::Result<Vec<GetTxResponse>> {
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<Vec<GetTxResponse>> {
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<Vec<GetTxResponse>> {
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<Vec<GetTxResponse>> {
let url = format!("{}/txs?unsent=true", self.url);

self.json_get(&url).await
}
Expand Down
6 changes: 3 additions & 3 deletions src/ethereum/write_provider/tx_sitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -93,14 +93,14 @@ impl Inner for TxSitter {
async fn fetch_pending_transactions(&self) -> Result<Vec<TransactionId>, 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)?;
Expand Down
Loading