loom_node_debug_provider/
dynprovider.rs

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::ops::Deref;
use std::sync::Arc;

use alloy::{
    network::Ethereum,
    providers::{Provider, ProviderBuilder, RootProvider},
    transports::{http::Http, BoxTransport, Transport},
};
use reqwest::Client;

#[derive(Clone)]
struct DynPrv<T> {
    pub provider: Arc<Box<dyn Provider<T>>>,
}

impl<T> Deref for DynPrv<T>
where
    T: Transport + Clone,
{
    type Target = Arc<Box<dyn Provider<T> + 'static>>;

    fn deref(&self) -> &Self::Target {
        &self.provider
    }
}

struct DynProvider {
    inner: DynPrv<BoxTransport>,
}

impl From<DynPrv<Http<Client>>> for DynProvider {
    fn from(value: DynPrv<Http<Client>>) -> Self {
        let provider = DynPrv { provider: Arc::new(Box::new(ProviderBuilder::new().on_provider(value.root().clone().boxed()))) };
        Self { inner: provider }
    }
}

impl Provider for DynProvider {
    fn root(&self) -> &RootProvider<BoxTransport, Ethereum> {
        self.inner.root()
    }
}

#[cfg(test)]
mod test {
    use std::sync::Arc;

    use alloy_provider::ext::AnvilApi;
    use alloy_provider::{Provider, ProviderBuilder};
    use eyre::Result;

    use super::*;

    #[tokio::test]
    async fn test_dyn_provider() -> Result<()> {
        let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil();

        let dyn_prv = Arc::new(Box::new(provider) as Box<dyn Provider<_>>);

        let dyn_prv = DynPrv { provider: dyn_prv.clone() };

        let dyn_prv = DynProvider::from(dyn_prv);

        let _ = dyn_prv.anvil_drop_all_transactions().await;

        Ok(())
    }
}