rust_x402/types/
network.rs

1//! Network configuration types
2
3/// Network configuration for x402 payments
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Network {
6    Mainnet,
7    Testnet,
8}
9
10/// Network configuration with chain-specific details
11#[derive(Debug, Clone)]
12pub struct NetworkConfig {
13    /// Chain ID for the network
14    pub chain_id: u64,
15    /// USDC contract address
16    pub usdc_contract: String,
17    /// Network name
18    pub name: String,
19    /// Whether this is a testnet
20    pub is_testnet: bool,
21}
22
23impl NetworkConfig {
24    /// Base mainnet configuration
25    pub fn base_mainnet() -> Self {
26        Self {
27            chain_id: 8453,
28            usdc_contract: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913".to_string(),
29            name: "base".to_string(),
30            is_testnet: false,
31        }
32    }
33
34    /// Base Sepolia testnet configuration
35    pub fn base_sepolia() -> Self {
36        Self {
37            chain_id: 84532,
38            usdc_contract: "0x036CbD53842c5426634e7929541eC2318f3dCF7e".to_string(),
39            name: "base-sepolia".to_string(),
40            is_testnet: true,
41        }
42    }
43
44    /// Get network config by name
45    pub fn from_name(name: &str) -> Option<Self> {
46        match name {
47            "base" => Some(Self::base_mainnet()),
48            "base-sepolia" => Some(Self::base_sepolia()),
49            _ => None,
50        }
51    }
52}
53
54impl Network {
55    /// Get the network identifier string
56    pub fn as_str(&self) -> &'static str {
57        match self {
58            Network::Mainnet => "base",
59            Network::Testnet => "base-sepolia",
60        }
61    }
62
63    /// Get the USDC contract address for this network
64    pub fn usdc_address(&self) -> &'static str {
65        match self {
66            Network::Mainnet => "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
67            Network::Testnet => "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
68        }
69    }
70
71    /// Get the USDC token name for this network
72    pub fn usdc_name(&self) -> &'static str {
73        match self {
74            Network::Mainnet => "USD Coin",
75            Network::Testnet => "USDC",
76        }
77    }
78}