Skip to main content

r402_aptos/
networks.rs

1//! Well-known Aptos network definitions and token deployments.
2
3use std::sync::LazyLock;
4
5use r402_core::chain::NetworkInfo;
6
7use crate::DEFAULT_TOKEN_DECIMALS;
8use crate::chain::{
9    AptosAddress, AptosChainReference, AptosTokenDeployment, USDC_MAINNET_FA, USDC_TESTNET_FA,
10};
11
12/// Well-known Aptos networks with their names and CAIP-2 identifiers.
13pub static APTOS_NETWORKS: &[NetworkInfo] = &[
14    NetworkInfo {
15        name: "aptos",
16        namespace: "aptos",
17        reference: "1",
18    },
19    NetworkInfo {
20        name: "aptos-testnet",
21        namespace: "aptos",
22        reference: "2",
23    },
24];
25
26/// Parses a well-known Aptos address, panicking on malformed input.
27#[allow(
28    clippy::expect_used,
29    reason = "hardcoded constant is infallible; validated by tests"
30)]
31fn well_known(s: &str) -> AptosAddress {
32    s.parse().expect("well-known aptos address must be valid")
33}
34
35/// Well-known USDC FA deployments on Aptos networks.
36static USDC_DEPLOYMENTS: LazyLock<Vec<AptosTokenDeployment>> = LazyLock::new(|| {
37    vec![
38        AptosTokenDeployment::new(
39            AptosChainReference::MAINNET,
40            well_known(USDC_MAINNET_FA),
41            DEFAULT_TOKEN_DECIMALS,
42        ),
43        AptosTokenDeployment::new(
44            AptosChainReference::TESTNET,
45            well_known(USDC_TESTNET_FA),
46            DEFAULT_TOKEN_DECIMALS,
47        ),
48    ]
49});
50
51/// Returns all known USDC deployments on Aptos chains.
52#[must_use]
53pub fn usdc_aptos_deployments() -> &'static [AptosTokenDeployment] {
54    &USDC_DEPLOYMENTS
55}
56
57/// Returns the USDC deployment for a specific Aptos chain, if known.
58#[must_use]
59pub fn usdc_aptos_deployment(chain: AptosChainReference) -> Option<&'static AptosTokenDeployment> {
60    USDC_DEPLOYMENTS.iter().find(|d| d.chain_reference == chain)
61}
62
63/// Ergonomic accessors for USDC token deployments on well-known Aptos chains.
64///
65/// Combine with [`AptosTokenDeployment::amount`] for a fluent pricing API:
66///
67/// ```ignore
68/// use r402_aptos::{AptosExact, USDC};
69///
70/// let tag = AptosExact::price_tag(pay_to, USDC::aptos_testnet().amount(1_000_000u64));
71/// ```
72#[derive(Debug, Clone, Copy)]
73#[allow(
74    clippy::upper_case_acronyms,
75    reason = "USDC is a well-known token ticker"
76)]
77pub struct USDC;
78
79#[allow(
80    clippy::missing_panics_doc,
81    clippy::expect_used,
82    reason = "static deployment lookups are infallible for built-in data"
83)]
84impl USDC {
85    /// Looks up a USDC deployment by chain reference.
86    #[must_use]
87    pub fn on(chain: AptosChainReference) -> Option<&'static AptosTokenDeployment> {
88        usdc_aptos_deployment(chain)
89    }
90
91    /// Returns all known USDC deployments on Aptos chains.
92    #[must_use]
93    pub fn all() -> &'static [AptosTokenDeployment] {
94        usdc_aptos_deployments()
95    }
96
97    /// USDC on Aptos mainnet (`aptos:1`).
98    #[must_use]
99    pub fn aptos() -> &'static AptosTokenDeployment {
100        usdc_aptos_deployment(AptosChainReference::MAINNET)
101            .expect("built-in USDC deployment for aptos mainnet missing")
102    }
103
104    /// USDC on Aptos testnet (`aptos:2`).
105    #[must_use]
106    pub fn aptos_testnet() -> &'static AptosTokenDeployment {
107        usdc_aptos_deployment(AptosChainReference::TESTNET)
108            .expect("built-in USDC deployment for aptos testnet missing")
109    }
110}
111
112#[cfg(test)]
113#[allow(clippy::unwrap_used, reason = "test assertions")]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn usdc_deployments_resolve() {
119        assert_eq!(USDC::aptos().decimals, DEFAULT_TOKEN_DECIMALS);
120        assert_eq!(USDC::aptos_testnet().decimals, DEFAULT_TOKEN_DECIMALS);
121        assert_eq!(USDC::aptos().address.as_str(), USDC_MAINNET_FA);
122        assert_eq!(USDC::aptos_testnet().address.as_str(), USDC_TESTNET_FA);
123        assert_eq!(USDC::all().len(), 2);
124        assert_eq!(APTOS_NETWORKS.len(), 2);
125    }
126}