use std::sync::LazyLock;
use r402_core::chain::NetworkInfo;
use crate::DEFAULT_TOKEN_DECIMALS;
use crate::chain::{
AptosAddress, AptosChainReference, AptosTokenDeployment, USDC_MAINNET_FA, USDC_TESTNET_FA,
};
pub static APTOS_NETWORKS: &[NetworkInfo] = &[
NetworkInfo {
name: "aptos",
namespace: "aptos",
reference: "1",
},
NetworkInfo {
name: "aptos-testnet",
namespace: "aptos",
reference: "2",
},
];
#[allow(
clippy::expect_used,
reason = "hardcoded constant is infallible; validated by tests"
)]
fn well_known(s: &str) -> AptosAddress {
s.parse().expect("well-known aptos address must be valid")
}
static USDC_DEPLOYMENTS: LazyLock<Vec<AptosTokenDeployment>> = LazyLock::new(|| {
vec![
AptosTokenDeployment::new(
AptosChainReference::MAINNET,
well_known(USDC_MAINNET_FA),
DEFAULT_TOKEN_DECIMALS,
),
AptosTokenDeployment::new(
AptosChainReference::TESTNET,
well_known(USDC_TESTNET_FA),
DEFAULT_TOKEN_DECIMALS,
),
]
});
#[must_use]
pub fn usdc_aptos_deployments() -> &'static [AptosTokenDeployment] {
&USDC_DEPLOYMENTS
}
#[must_use]
pub fn usdc_aptos_deployment(chain: AptosChainReference) -> Option<&'static AptosTokenDeployment> {
USDC_DEPLOYMENTS.iter().find(|d| d.chain_reference == chain)
}
#[derive(Debug, Clone, Copy)]
#[allow(
clippy::upper_case_acronyms,
reason = "USDC is a well-known token ticker"
)]
pub struct USDC;
#[allow(
clippy::missing_panics_doc,
clippy::expect_used,
reason = "static deployment lookups are infallible for built-in data"
)]
impl USDC {
#[must_use]
pub fn on(chain: AptosChainReference) -> Option<&'static AptosTokenDeployment> {
usdc_aptos_deployment(chain)
}
#[must_use]
pub fn all() -> &'static [AptosTokenDeployment] {
usdc_aptos_deployments()
}
#[must_use]
pub fn aptos() -> &'static AptosTokenDeployment {
usdc_aptos_deployment(AptosChainReference::MAINNET)
.expect("built-in USDC deployment for aptos mainnet missing")
}
#[must_use]
pub fn aptos_testnet() -> &'static AptosTokenDeployment {
usdc_aptos_deployment(AptosChainReference::TESTNET)
.expect("built-in USDC deployment for aptos testnet missing")
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "test assertions")]
mod tests {
use super::*;
#[test]
fn usdc_deployments_resolve() {
assert_eq!(USDC::aptos().decimals, DEFAULT_TOKEN_DECIMALS);
assert_eq!(USDC::aptos_testnet().decimals, DEFAULT_TOKEN_DECIMALS);
assert_eq!(USDC::aptos().address.as_str(), USDC_MAINNET_FA);
assert_eq!(USDC::aptos_testnet().address.as_str(), USDC_TESTNET_FA);
assert_eq!(USDC::all().len(), 2);
assert_eq!(APTOS_NETWORKS.len(), 2);
}
}