use std::sync::LazyLock;
use r402_core::chain::NetworkInfo;
use crate::chain::{TvmAddress, TvmChainReference, TvmTokenDeployment};
use crate::{DEFAULT_TOKEN_DECIMALS, USDT_MAINNET_MINTER, USDT_TESTNET_MINTER};
pub static TVM_NETWORKS: &[NetworkInfo] = &[
NetworkInfo {
name: "ton",
namespace: "tvm",
reference: "-239",
},
NetworkInfo {
name: "ton-testnet",
namespace: "tvm",
reference: "-3",
},
];
#[allow(
clippy::expect_used,
reason = "hardcoded constant is infallible; validated by tests"
)]
fn well_known(s: &str) -> TvmAddress {
s.parse().expect("well-known tvm address must be valid")
}
static USDT_DEPLOYMENTS: LazyLock<Vec<TvmTokenDeployment>> = LazyLock::new(|| {
vec![
TvmTokenDeployment::new(
TvmChainReference::MAINNET,
well_known(USDT_MAINNET_MINTER),
DEFAULT_TOKEN_DECIMALS,
),
TvmTokenDeployment::new(
TvmChainReference::TESTNET,
well_known(USDT_TESTNET_MINTER),
DEFAULT_TOKEN_DECIMALS,
),
]
});
#[must_use]
pub fn usdt_tvm_deployments() -> &'static [TvmTokenDeployment] {
&USDT_DEPLOYMENTS
}
#[must_use]
pub fn usdt_tvm_deployment(chain: TvmChainReference) -> Option<&'static TvmTokenDeployment> {
USDT_DEPLOYMENTS.iter().find(|d| d.chain_reference == chain)
}
#[derive(Debug, Clone, Copy)]
#[allow(
clippy::upper_case_acronyms,
reason = "USDT is a well-known token ticker"
)]
pub struct USDT;
#[allow(
clippy::doc_markdown,
clippy::missing_panics_doc,
clippy::expect_used,
reason = "static deployment lookups are infallible for built-in data"
)]
impl USDT {
#[must_use]
pub fn on(chain: TvmChainReference) -> Option<&'static TvmTokenDeployment> {
usdt_tvm_deployment(chain)
}
#[must_use]
pub fn all() -> &'static [TvmTokenDeployment] {
usdt_tvm_deployments()
}
#[must_use]
pub fn tvm() -> &'static TvmTokenDeployment {
usdt_tvm_deployment(TvmChainReference::MAINNET)
.expect("built-in USDT deployment for tvm mainnet missing")
}
#[must_use]
pub fn tvm_testnet() -> &'static TvmTokenDeployment {
usdt_tvm_deployment(TvmChainReference::TESTNET)
.expect("built-in USDT deployment for tvm testnet missing")
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "test assertions")]
mod tests {
use super::*;
#[test]
fn usdt_deployments_resolve() {
assert_eq!(USDT::tvm().decimals, DEFAULT_TOKEN_DECIMALS);
assert_eq!(USDT::tvm_testnet().decimals, DEFAULT_TOKEN_DECIMALS);
assert_eq!(USDT::tvm().address.as_str(), USDT_MAINNET_MINTER);
assert_eq!(USDT::tvm_testnet().address.as_str(), USDT_TESTNET_MINTER);
assert_eq!(USDT::all().len(), 2);
assert_eq!(TVM_NETWORKS.len(), 2);
assert_eq!(TVM_NETWORKS.first().map(|n| n.reference), Some("-239"));
assert_eq!(TVM_NETWORKS.get(1).map(|n| n.reference), Some("-3"));
}
}