sui_rust_operator/
network.rs1use crate::utils::CustomErr;
2use std::{env, error::Error, fmt::Display};
3
4const NETWORK_ENV_NAME: &str = "network";
5
6pub enum Network {
7 Testnet,
8 Mainnet,
9 Devnet,
10 Custom(String),
11}
12
13pub fn default() -> Network {
14 from_env()
15}
16
17pub fn from_env() -> Network {
18 if env::var_os(NETWORK_ENV_NAME).is_some() {
19 if let Ok(value) = env::var(NETWORK_ENV_NAME) {
20 return Network::from_name(value);
21 }
22 }
23 Network::Mainnet
24}
25
26impl Network {
27 pub fn from_name(name: String) -> Self {
28 if name.eq("testnet") {
29 return Network::Testnet;
30 } else if name.eq("devnet") {
31 return Network::Devnet;
32 } else if name.eq("mainnet") {
33 return Network::Mainnet;
34 } else {
35 return Network::Custom(name);
36 }
37 }
38
39 pub fn get_gateway(&self) -> String {
40 match self {
41 Network::Testnet => String::from("https://fullnode.testnet.sui.io:443"),
42 Network::Mainnet => String::from("https://fullnode.mainnet.sui.io:443"),
43 Network::Devnet => String::from("https://fullnode.devnet.sui.io:443"),
44 Network::Custom(url) => url.clone(),
45 }
46 }
47
48 pub fn faucet_url(&self) -> Result<String, Box<dyn Error>> {
49 match self {
50 Network::Devnet => Ok("https://faucet.devnet.sui.io/gas".to_string()),
51 Network::Testnet => Ok("https://faucet.testnet.sui.io/gas".to_string()),
52 Network::Mainnet => Err(Box::new(CustomErr::new("mainnet does not support faucet"))),
53 Network::Custom(url) => Ok(format!("{}/gas", url)),
54 }
55 }
56
57 pub fn to_string(&self) -> String {
58 match self {
59 Network::Testnet => String::from("testnet"),
60 Network::Mainnet => String::from("mainnet"),
61 Network::Devnet => String::from("devnet"),
62 Network::Custom(url) => url.clone(),
63 }
64 }
65
66 pub fn object_link(&self, object_id: &String) -> String {
67 format!(
68 "https://suiexplorer.com/object/{}?network={}",
69 object_id,
70 self.to_string()
71 )
72 }
73
74 pub fn transaction_link(&self, digest: &String) -> String {
75 format!(
76 "https://suiexplorer.com/txblock/{}?network={}",
77 digest,
78 self.to_string()
79 )
80 }
81}
82
83impl Display for Network {
84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85 write!(f, "[{}, {}]", self.to_string(), self.get_gateway())
86 }
87}