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