unc_workspaces/worker/
mod.rs

1mod impls;
2
3use std::fmt;
4use std::sync::Arc;
5
6use crate::network::builder::NetworkBuilder;
7use crate::network::{Custom, Mainnet, Sandbox, Testnet};
8use crate::types::gas_meter::GasHook;
9use crate::{Network, Result};
10
11/// The `Worker` type allows us to interact with any UNC related networks, such
12/// as mainnet and testnet. This controls where the environment the worker is
13/// running on top of it. Refer to this for all network related actions such as
14/// deploying a contract, or interacting with transactions.
15pub struct Worker<T: ?Sized> {
16    pub(crate) workspace: Arc<T>,
17    pub(crate) tx_callbacks: Vec<GasHook>,
18}
19
20impl<T> Worker<T>
21where
22    T: Network,
23{
24    pub(crate) fn new(network: T) -> Self {
25        Self {
26            workspace: Arc::new(network),
27            tx_callbacks: vec![],
28        }
29    }
30}
31
32impl<T: Network + 'static> Worker<T> {
33    pub(crate) fn coerce(self) -> Worker<dyn Network> {
34        Worker {
35            workspace: self.workspace,
36            tx_callbacks: self.tx_callbacks,
37        }
38    }
39}
40
41impl<T: fmt::Debug> fmt::Debug for Worker<T> {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        f.debug_struct("Worker")
44            .field("workspace", &self.workspace)
45            .finish()
46    }
47}
48
49/// Spin up a new sandbox instance, and grab a [`Worker`] that interacts with it.
50pub fn sandbox<'a>() -> NetworkBuilder<'a, Sandbox> {
51    NetworkBuilder::new("sandbox")
52}
53
54/// Spin up a new sandbox instance, and grab a [`Worker`] that interacts with it.
55pub async fn sandbox_with_version<'a>(version: &str) -> Result<Worker<Sandbox>> {
56    let network_builder = NetworkBuilder::new("sandbox");
57    let network = Sandbox::from_builder_with_version(network_builder, version).await?;
58    Ok(Worker::new(network))
59}
60
61/// Connect to the [testnet](https://explorer.testnet.unc.org/) network, and grab
62/// a [`Worker`] that can interact with it.
63pub fn testnet<'a>() -> NetworkBuilder<'a, Testnet> {
64    NetworkBuilder::new("testnet")
65}
66
67/// Connect to the [testnet archival](https://unc-nodes.io/intro/node-types#archival-node)
68/// network, and grab a [`Worker`] that can interact with it.
69pub fn testnet_archival<'a>() -> NetworkBuilder<'a, Testnet> {
70    NetworkBuilder::new("testnet-archival").rpc_addr(crate::network::testnet::ARCHIVAL_URL)
71}
72
73/// Connect to the [mainnet](https://explorer.unc.org/) network, and grab
74/// a [`Worker`] that can interact with it.
75pub fn mainnet<'a>() -> NetworkBuilder<'a, Mainnet> {
76    NetworkBuilder::new("mainnet")
77}
78
79/// Connect to the [mainnet archival](https://unc-nodes.io/intro/node-types#archival-node)
80/// network, and grab a [`Worker`] that can interact with it.
81pub fn mainnet_archival<'a>() -> NetworkBuilder<'a, Mainnet> {
82    NetworkBuilder::new("mainnet-archival").rpc_addr(crate::network::mainnet::ARCHIVAL_URL)
83}
84
85/// Connect to a custom network, and grab a [`Worker`] that can interact with it.
86///
87/// Note: the burden of ensuring the methods that are able to be called are left up to the user.
88pub fn custom<'a>(rpc_url: &str) -> NetworkBuilder<'a, Custom> {
89    NetworkBuilder::new("custom").rpc_addr(rpc_url)
90}
91
92/// Run a locally scoped task where a [`sandbox`] instanced [`Worker`] is supplied.
93pub async fn with_sandbox<F, T>(task: F) -> Result<T::Output>
94where
95    F: Fn(Worker<Sandbox>) -> T + Send + Sync,
96    T: core::future::Future + Send,
97{
98    Ok(task(sandbox().await?).await)
99}
100
101/// Run a locally scoped task where a [`testnet`] instanced [`Worker`] is supplied.
102pub async fn with_testnet<F, T>(task: F) -> Result<T::Output>
103where
104    F: Fn(Worker<Testnet>) -> T + Send + Sync,
105    T: core::future::Future + Send,
106{
107    Ok(task(testnet().await?).await)
108}
109
110/// Run a locally scoped task where a [`testnet_archival`] instanced [`Worker`] is supplied.
111pub async fn with_testnet_archival<F, T>(task: F) -> Result<T::Output>
112where
113    F: Fn(Worker<Testnet>) -> T + Send + Sync,
114    T: core::future::Future + Send,
115{
116    Ok(task(testnet_archival().await?).await)
117}
118
119/// Run a locally scoped task where a [`mainnet`] instanced [`Worker`] is supplied.
120pub async fn with_mainnet<F, T>(task: F) -> Result<T::Output>
121where
122    F: Fn(Worker<Mainnet>) -> T + Send + Sync,
123    T: core::future::Future + Send,
124{
125    Ok(task(mainnet().await?).await)
126}
127
128/// Run a locally scoped task where a [`mainnet_archival`] instanced [`Worker`] is supplied.
129pub async fn with_mainnet_archival<F, T>(task: F) -> Result<T::Output>
130where
131    F: Fn(Worker<Mainnet>) -> T + Send + Sync,
132    T: core::future::Future + Send,
133{
134    Ok(task(mainnet_archival().await?).await)
135}
136
137#[allow(dead_code)]
138pub async fn with_custom<F, T>(task: F, rpc_url: &str) -> Result<T::Output>
139where
140    F: Fn(Worker<Custom>) -> T + Send + Sync,
141    T: core::future::Future + Send,
142{
143    Ok(task(custom(rpc_url).await?).await)
144}