use std::sync::LazyLock;
use r402_core::chain::NetworkInfo;
use crate::DEFAULT_TOKEN_DECIMALS;
use crate::chain::{NearAddress, NearChainReference, NearTokenDeployment};
pub static NEAR_NETWORKS: &[NetworkInfo] = &[
NetworkInfo {
name: "near",
namespace: "near",
reference: "mainnet",
},
NetworkInfo {
name: "near-testnet",
namespace: "near",
reference: "testnet",
},
];
#[allow(
clippy::expect_used,
reason = "hardcoded constant is infallible; validated by tests"
)]
fn well_known(s: &str) -> NearAddress {
s.parse().expect("well-known near address must be valid")
}
static USDC_DEPLOYMENTS: LazyLock<Vec<NearTokenDeployment>> = LazyLock::new(|| {
vec![
NearTokenDeployment::new(
NearChainReference::MAINNET,
well_known("17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1"),
DEFAULT_TOKEN_DECIMALS,
),
NearTokenDeployment::new(
NearChainReference::TESTNET,
well_known("3e2210e1184b45b64c8a434c0a7e7b23cc04ea7eb7a6c3c32520d03d4afcb8af"),
DEFAULT_TOKEN_DECIMALS,
),
]
});
#[must_use]
pub fn usdc_near_deployments() -> &'static [NearTokenDeployment] {
&USDC_DEPLOYMENTS
}
#[must_use]
pub fn usdc_near_deployment(chain: NearChainReference) -> Option<&'static NearTokenDeployment> {
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::doc_markdown,
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: NearChainReference) -> Option<&'static NearTokenDeployment> {
usdc_near_deployment(chain)
}
#[must_use]
pub fn all() -> &'static [NearTokenDeployment] {
usdc_near_deployments()
}
#[must_use]
pub fn near() -> &'static NearTokenDeployment {
usdc_near_deployment(NearChainReference::MAINNET)
.expect("built-in USDC deployment for near mainnet missing")
}
#[must_use]
pub fn near_testnet() -> &'static NearTokenDeployment {
usdc_near_deployment(NearChainReference::TESTNET)
.expect("built-in USDC deployment for near testnet missing")
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "test assertions")]
mod tests {
use super::*;
#[test]
fn usdc_deployments_resolve() {
assert_eq!(USDC::near().decimals, DEFAULT_TOKEN_DECIMALS);
assert_eq!(USDC::near_testnet().decimals, DEFAULT_TOKEN_DECIMALS);
assert_eq!(USDC::all().len(), 2);
assert_eq!(NEAR_NETWORKS.len(), 2);
}
}