Skip to content
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
49 changes: 29 additions & 20 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,7 @@ impl TransactionQueue {
/// Gets a transaction receipt with retries for "transaction indexing is in progress" errors.
/// Waits until the transaction is indexed and the receipt is available, or returns an error.
async fn get_receipt_with_retry(&self, tx_hash: Hash) -> anyhow::Result<TransactionReceipt> {
loop {
match self.provider.get_transaction_receipt(tx_hash).await {
Ok(opt_receipt) => match opt_receipt {
Some(receipt) => return Ok(receipt),
_ => {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
continue;
}
},
Err(e) => {
if e.to_string()
.contains("transaction indexing is in progress")
{
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
continue;
}
return Err(anyhow!("Failed to get transaction receipt: {}", e));
}
}
}
get_receipt(&self.provider, tx_hash).await
}

/// Processes a single transaction:
Expand Down Expand Up @@ -347,3 +328,31 @@ impl Account {
Ok(self.provider.get_balance(self.address()).await?)
}
}

/// Gets a transaction receipt with retries for "transaction indexing is in progress" errors.
/// Waits until the transaction is indexed and the receipt is available, or returns an error.
pub async fn get_receipt(
provider: &DynProvider,
tx_hash: Hash,
) -> anyhow::Result<TransactionReceipt> {
loop {
match provider.get_transaction_receipt(tx_hash).await {
Ok(opt_receipt) => match opt_receipt {
Some(receipt) => return Ok(receipt),
_ => {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
continue;
}
},
Err(e) => {
if e.to_string()
.contains("transaction indexing is in progress")
{
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
continue;
}
return Err(anyhow!("Failed to get transaction receipt: {}", e));
}
}
}
}
8 changes: 7 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy::eips::BlockNumberOrTag;
use alloy::primitives::{Address, B256};
use alloy::providers::{DynProvider, Provider, ProviderBuilder};
use alloy::rpc::client::ClientRef;
use alloy::rpc::types::SyncStatus;
use alloy::rpc::types::{SyncStatus, TransactionReceipt};
use alloy::signers::local::PrivateKeySigner;
use alloy::transports::http::reqwest::Url;
use bigdecimal::BigDecimal;
Expand Down Expand Up @@ -519,6 +519,12 @@ impl GolemBaseClient {
Ok(latest_block.header.number)
}

/// Waits for a arbitrary (not created by this client) transaction to be mined and returns
/// its receipt. Handles retries for transaction indexing error.
pub async fn wait_for_transaction(&self, tx_hash: Hash) -> anyhow::Result<TransactionReceipt> {
crate::account::get_receipt(&self.provider, tx_hash).await
}

/// Creates a new WebSocket client for event subscriptions using the default RPC URL.
pub async fn events_client(&self) -> anyhow::Result<EventsClient> {
let mut ws_url = self.rpc_url.clone();
Expand Down
Loading