near_workspaces/worker/
mod.rs1mod 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
11pub 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
51pub fn sandbox<'a>() -> NetworkBuilder<'a, Sandbox> {
53 NetworkBuilder::new("sandbox")
54}
55
56pub 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
63pub fn testnet<'a>() -> NetworkBuilder<'a, Testnet> {
66 NetworkBuilder::new("testnet")
67}
68
69pub fn testnet_archival<'a>() -> NetworkBuilder<'a, Testnet> {
72 NetworkBuilder::new("testnet-archival").rpc_addr(crate::network::testnet::ARCHIVAL_URL)
73}
74
75pub fn mainnet<'a>() -> NetworkBuilder<'a, Mainnet> {
78 NetworkBuilder::new("mainnet")
79}
80
81pub fn mainnet_archival<'a>() -> NetworkBuilder<'a, Mainnet> {
84 NetworkBuilder::new("mainnet-archival").rpc_addr(crate::network::mainnet::ARCHIVAL_URL)
85}
86
87pub fn betanet<'a>() -> NetworkBuilder<'a, Betanet> {
89 NetworkBuilder::new("betanet")
90}
91
92pub fn custom<'a>(rpc_url: &str) -> NetworkBuilder<'a, Custom> {
96 NetworkBuilder::new("custom").rpc_addr(rpc_url)
97}
98
99pub 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
108pub 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
117pub 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
126pub 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
135pub 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
144pub 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}