Skip to main content

near_kit/types/
network.rs

1//! Network identification for NEAR Protocol.
2
3use std::fmt;
4
5/// The NEAR network the client is connected to.
6///
7/// This is used to resolve network-specific addresses for known tokens
8/// and other network-aware operations.
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
10pub enum Network {
11    /// NEAR mainnet (production network).
12    #[default]
13    Mainnet,
14    /// NEAR testnet (testing network).
15    Testnet,
16    /// Local sandbox network for development.
17    Sandbox,
18    /// Custom network with unknown token mappings.
19    Custom,
20}
21
22impl Network {
23    /// Returns true if this is mainnet.
24    pub fn is_mainnet(&self) -> bool {
25        matches!(self, Network::Mainnet)
26    }
27
28    /// Returns true if this is testnet.
29    pub fn is_testnet(&self) -> bool {
30        matches!(self, Network::Testnet)
31    }
32
33    /// Returns true if this is a sandbox network.
34    pub fn is_sandbox(&self) -> bool {
35        matches!(self, Network::Sandbox)
36    }
37
38    /// Returns the network identifier string.
39    pub fn as_str(&self) -> &'static str {
40        match self {
41            Network::Mainnet => "mainnet",
42            Network::Testnet => "testnet",
43            Network::Sandbox => "sandbox",
44            Network::Custom => "custom",
45        }
46    }
47}
48
49impl fmt::Display for Network {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        f.write_str(self.as_str())
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_network_display() {
61        assert_eq!(Network::Mainnet.to_string(), "mainnet");
62        assert_eq!(Network::Testnet.to_string(), "testnet");
63        assert_eq!(Network::Sandbox.to_string(), "sandbox");
64        assert_eq!(Network::Custom.to_string(), "custom");
65    }
66
67    #[test]
68    fn test_network_predicates() {
69        assert!(Network::Mainnet.is_mainnet());
70        assert!(!Network::Mainnet.is_testnet());
71
72        assert!(Network::Testnet.is_testnet());
73        assert!(!Network::Testnet.is_mainnet());
74
75        assert!(Network::Sandbox.is_sandbox());
76    }
77
78    #[test]
79    fn test_default_is_mainnet() {
80        assert_eq!(Network::default(), Network::Mainnet);
81    }
82}