unc_workspaces/network/
mainnet.rs

1use crate::network::{Info, NetworkClient, NetworkInfo};
2use crate::result::Result;
3use crate::rpc::client::Client;
4use std::path::PathBuf;
5
6use super::builder::{FromNetworkBuilder, NetworkBuilder};
7
8/// URL to the mainnet RPC node provided by unc.org.
9pub const RPC_URL: &str = "https://rpc.mainnet.unc.org";
10
11/// URL to the mainnet archival RPC node provided by unc.org.
12pub const ARCHIVAL_URL: &str = "https://archival-rpc.mainnet.unc.org";
13
14/// Mainnet related configuration for interacting with mainnet. Look at
15/// [`workspaces::mainnet`] and [`workspaces::mainnet_archival`] for how to
16/// spin up a [`Worker`] that can be used to interact with mainnet. Note that
17/// mainnet account creation is not currently supported, and these calls into
18/// creating a mainnet worker is meant for retrieving data and/or making
19/// queries only.
20///
21/// [`workspaces::mainnet`]: crate::mainnet
22/// [`workspaces::mainnet_archival`]: crate::mainnet_archival
23/// [`Worker`]: crate::Worker
24pub struct Mainnet {
25    client: Client,
26    info: Info,
27}
28
29#[async_trait::async_trait]
30impl FromNetworkBuilder for Mainnet {
31    async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> Result<Self> {
32        let rpc_url = build.rpc_addr.unwrap_or_else(|| RPC_URL.into());
33        let client = Client::new(&rpc_url, build.api_key)?;
34        client.wait_for_rpc().await?;
35
36        Ok(Self {
37            client,
38            info: Info {
39                name: build.name.into(),
40                root_id: "unc".parse().unwrap(),
41                keystore_path: PathBuf::from(".unc-credentials/mainnet/"),
42                rpc_url: url::Url::parse(&rpc_url).expect("url is hardcoded"),
43            },
44        })
45    }
46}
47
48impl std::fmt::Debug for Mainnet {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        f.debug_struct("Mainnet")
51            .field("root_id", &self.info.root_id)
52            .field("rpc_url", &self.info.rpc_url)
53            .finish()
54    }
55}
56
57impl NetworkClient for Mainnet {
58    fn client(&self) -> &Client {
59        &self.client
60    }
61}
62
63impl NetworkInfo for Mainnet {
64    fn info(&self) -> &Info {
65        &self.info
66    }
67}