1use 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
12pub 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#[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
35static 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#[must_use]
53pub fn usdc_aptos_deployments() -> &'static [AptosTokenDeployment] {
54 &USDC_DEPLOYMENTS
55}
56
57#[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#[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 #[must_use]
87 pub fn on(chain: AptosChainReference) -> Option<&'static AptosTokenDeployment> {
88 usdc_aptos_deployment(chain)
89 }
90
91 #[must_use]
93 pub fn all() -> &'static [AptosTokenDeployment] {
94 usdc_aptos_deployments()
95 }
96
97 #[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 #[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}