1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::time::Duration;

use web3::{
    api::{Eth, EthFilter, Namespace},
    confirm::wait_for_confirmations,
    types::{H256, U64},
    Transport,
};

async fn transaction_receipt_block_number_check<T: Transport>(
    eth: &Eth<T>,
    hash: H256,
) -> web3::error::Result<Option<U64>> {
    let receipt = eth.transaction_receipt(hash).await?;
    Ok(receipt.and_then(|receipt| receipt.block_number))
}

// Given a transaction hash, wait for confirmations.
pub async fn wait_for_transaction_confirmation<T: Transport>(
    hash: H256,
    transport: T,
    poll_interval: Duration,
    confirmations: usize,
) -> web3::error::Result<()> {
    let eth = Eth::new(transport.clone());
    if confirmations > 0 {
        let confirmation_check = || transaction_receipt_block_number_check(&eth, hash);
        let eth_filter = EthFilter::new(transport.clone());
        let eth = eth.clone();
        wait_for_confirmations(
            eth,
            eth_filter,
            poll_interval,
            confirmations,
            confirmation_check,
        )
        .await?;
    }
    Ok(())
}