95 lines
2.3 KiB
Rust
95 lines
2.3 KiB
Rust
//! ERC-20 Token辅助模块
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// ERC-20 Token信息
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ERC20Token {
|
|
/// 合约地址
|
|
pub address: String,
|
|
/// Token符号
|
|
pub symbol: String,
|
|
/// Token名称
|
|
pub name: String,
|
|
/// 小数位数
|
|
pub decimals: u8,
|
|
/// 总供应量
|
|
pub total_supply: Option<u128>,
|
|
}
|
|
|
|
impl ERC20Token {
|
|
/// 创建新的ERC-20 Token
|
|
pub fn new(
|
|
address: String,
|
|
symbol: String,
|
|
name: String,
|
|
decimals: u8,
|
|
) -> Self {
|
|
Self {
|
|
address,
|
|
symbol,
|
|
name,
|
|
decimals,
|
|
total_supply: None,
|
|
}
|
|
}
|
|
|
|
/// 常见的ERC-20 Token列表
|
|
pub fn common_tokens() -> Vec<ERC20Token> {
|
|
vec![
|
|
ERC20Token::new(
|
|
"0xdac17f958d2ee523a2206206994597c13d831ec7".to_string(),
|
|
"USDT".to_string(),
|
|
"Tether USD".to_string(),
|
|
6,
|
|
),
|
|
ERC20Token::new(
|
|
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".to_string(),
|
|
"USDC".to_string(),
|
|
"USD Coin".to_string(),
|
|
6,
|
|
),
|
|
ERC20Token::new(
|
|
"0x6b175474e89094c44da98b954eedeac495271d0f".to_string(),
|
|
"DAI".to_string(),
|
|
"Dai Stablecoin".to_string(),
|
|
18,
|
|
),
|
|
ERC20Token::new(
|
|
"0x2260fac5e5542a773aa44fbcfedf7c193bc2c599".to_string(),
|
|
"WBTC".to_string(),
|
|
"Wrapped BTC".to_string(),
|
|
8,
|
|
),
|
|
]
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_erc20_token_creation() {
|
|
let token = ERC20Token::new(
|
|
"0x1234...".to_string(),
|
|
"TEST".to_string(),
|
|
"Test Token".to_string(),
|
|
18,
|
|
);
|
|
|
|
assert_eq!(token.symbol, "TEST");
|
|
assert_eq!(token.decimals, 18);
|
|
}
|
|
|
|
#[test]
|
|
fn test_common_tokens() {
|
|
let tokens = ERC20Token::common_tokens();
|
|
assert!(tokens.len() >= 4);
|
|
|
|
let usdt = tokens.iter().find(|t| t.symbol == "USDT");
|
|
assert!(usdt.is_some());
|
|
assert_eq!(usdt.unwrap().decimals, 6);
|
|
}
|
|
}
|