near_workspaces/worker/
mod.rs

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