Skip to content

Commit 8ef7ace

Browse files
authored
add waiting timeout for contract tx in mempool (#683)
1 parent 8a6c49f commit 8ef7ace

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/taker/api2.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,13 +1256,32 @@ impl Taker {
12561256
);
12571257

12581258
// Fetch and store the incoming contract transaction
1259-
let incoming_contract_tx = self
1260-
.wallet
1261-
.rpc
1262-
.get_raw_transaction(incoming_contract_txid, None)
1263-
.map_err(|e| {
1264-
TakerError::General(format!("Failed to get incoming contract tx: {:?}", e))
1265-
})?;
1259+
// Wait for transaction to appear in mempool/blockchain with retry logic
1260+
let mempool_wait_timeout = 60;
1261+
let start_time = std::time::Instant::now();
1262+
let incoming_contract_tx = loop {
1263+
match self
1264+
.wallet
1265+
.rpc
1266+
.get_raw_transaction(incoming_contract_txid, None)
1267+
{
1268+
Ok(tx) => break tx,
1269+
Err(_e) => {
1270+
let elapsed = start_time.elapsed().as_secs();
1271+
if elapsed > mempool_wait_timeout {
1272+
return Err(TakerError::General(format!(
1273+
"Timed out waiting for incoming contract tx {} to appear in mempool after {} secs",
1274+
incoming_contract_txid, elapsed
1275+
)));
1276+
}
1277+
log::info!(
1278+
"Waiting for incoming contract tx to appear in mempool | {} secs",
1279+
elapsed
1280+
);
1281+
std::thread::sleep(std::time::Duration::from_secs(2));
1282+
}
1283+
}
1284+
};
12661285
self.ongoing_swap_state.incoming_contract.contract_tx = incoming_contract_tx.clone();
12671286
// Set funding amount from the transaction output
12681287
self.ongoing_swap_state.incoming_contract.funding_amount =

0 commit comments

Comments
 (0)