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
41
42
43
44
45
46
47
48
pub mod types;

use std::sync::Arc;

use ethers::{providers::{Middleware, PendingTransaction, JsonRpcClient}, prelude::{signer::SignerMiddlewareError, k256::ecdsa::SigningKey}, signers::Wallet, types::TransactionReceipt};

use crate::types::{TxErrors, TxStatus};


pub struct ClientWrapper<M> {
    pub client: Arc<M>,
}

impl <M: Middleware + 'static + JsonRpcClient> ClientWrapper<M> {
    async fn handle_tx(&self, tx: Result<PendingTransaction<'_, M>, SignerMiddlewareError<M, Wallet<SigningKey>>,>) -> TxStatus {
        let tx = tx.map_err(|e| format!("Failed to send transaction: {:?}", e));

        let tx = match tx {
            Ok(tx) => tx,
            Err(e) => return TxStatus::Failed(TxErrors::Failed(e)),
        };
    
        let hash = tx.tx_hash();
    
        println!("Sent tx hash: {:?}", hash);
        println!("Mining...");
        let receipt = tx
            .await
            .map_err(|e| format!("Failed to mine transaction: {:?}", e));
        let receipt = match receipt {
            Ok(receipt) => receipt,
            Err(e) => return TxStatus::Failed(TxErrors::Failed(e)),
        };
        match receipt {
            Some(receipt) => {
                println!("Tx mined!");
                if receipt.status == Some(0.into()) {
                    return types::TxStatus::Failed(TxErrors::Reverted(receipt));
                }
                TxStatus::Successful(receipt)
            }
            None => {
                // I don't even think this is possible
                TxStatus::Failed(TxErrors::NoReceipt(hash))
            }
        }
    }
}