mod impls;
use std::fmt;
use std::sync::Arc;
use crate::network::builder::NetworkBuilder;
use crate::network::{Betanet, Custom, Mainnet, Sandbox, Testnet};
use crate::types::GasHook;
use crate::{Network, Result};
pub struct Worker<T: ?Sized> {
pub(crate) workspace: Arc<T>,
pub(crate) tx_callbacks: Vec<GasHook>,
}
impl<T> Worker<T>
where
T: Network,
{
pub(crate) fn new(network: T) -> Self {
Self {
workspace: Arc::new(network),
tx_callbacks: vec![],
}
}
}
impl<T: Network + 'static> Worker<T> {
pub(crate) fn coerce(self) -> Worker<dyn Network> {
Worker {
workspace: self.workspace,
tx_callbacks: self.tx_callbacks,
}
}
}
impl<T: fmt::Debug> fmt::Debug for Worker<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Worker")
.field("workspace", &self.workspace)
.finish()
}
}
pub fn sandbox<'a>() -> NetworkBuilder<'a, Sandbox> {
NetworkBuilder::new("sandbox")
}
pub async fn sandbox_with_version<'a>(version: &str) -> Result<Worker<Sandbox>> {
let network_builder = NetworkBuilder::new("sandbox");
let network = Sandbox::from_builder_with_version(network_builder, version).await?;
Ok(Worker::new(network))
}
pub fn testnet<'a>() -> NetworkBuilder<'a, Testnet> {
NetworkBuilder::new("testnet")
}
pub fn testnet_archival<'a>() -> NetworkBuilder<'a, Testnet> {
NetworkBuilder::new("testnet-archival").rpc_addr(crate::network::testnet::ARCHIVAL_URL)
}
pub fn mainnet<'a>() -> NetworkBuilder<'a, Mainnet> {
NetworkBuilder::new("mainnet")
}
pub fn mainnet_archival<'a>() -> NetworkBuilder<'a, Mainnet> {
NetworkBuilder::new("mainnet-archival").rpc_addr(crate::network::mainnet::ARCHIVAL_URL)
}
pub fn betanet<'a>() -> NetworkBuilder<'a, Betanet> {
NetworkBuilder::new("betanet")
}
pub fn custom<'a>(rpc_url: &str) -> NetworkBuilder<'a, Custom> {
NetworkBuilder::new("custom").rpc_addr(rpc_url)
}
pub async fn with_sandbox<F, T>(task: F) -> Result<T::Output>
where
F: Fn(Worker<Sandbox>) -> T + Send + Sync,
T: core::future::Future + Send,
{
Ok(task(sandbox().await?).await)
}
pub async fn with_testnet<F, T>(task: F) -> Result<T::Output>
where
F: Fn(Worker<Testnet>) -> T + Send + Sync,
T: core::future::Future + Send,
{
Ok(task(testnet().await?).await)
}
pub async fn with_testnet_archival<F, T>(task: F) -> Result<T::Output>
where
F: Fn(Worker<Testnet>) -> T + Send + Sync,
T: core::future::Future + Send,
{
Ok(task(testnet_archival().await?).await)
}
pub async fn with_mainnet<F, T>(task: F) -> Result<T::Output>
where
F: Fn(Worker<Mainnet>) -> T + Send + Sync,
T: core::future::Future + Send,
{
Ok(task(mainnet().await?).await)
}
pub async fn with_mainnet_archival<F, T>(task: F) -> Result<T::Output>
where
F: Fn(Worker<Mainnet>) -> T + Send + Sync,
T: core::future::Future + Send,
{
Ok(task(mainnet_archival().await?).await)
}
pub async fn with_betanet<F, T>(task: F) -> Result<T::Output>
where
F: Fn(Worker<Betanet>) -> T + Send + Sync,
T: core::future::Future + Send,
{
Ok(task(betanet().await?).await)
}
#[allow(dead_code)]
pub async fn with_custom<F, T>(task: F, rpc_url: &str) -> Result<T::Output>
where
F: Fn(Worker<Custom>) -> T + Send + Sync,
T: core::future::Future + Send,
{
Ok(task(custom(rpc_url).await?).await)
}