Skip to main content

alephium_web3/token/
mod.rs

1// Copyright 2018 - 2022 The Alephium Authors
2// This file is part of the alephium project.
3
4use serde::{Deserialize, Serialize};
5
6/// Token information
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8pub struct Token {
9    /// Token ID (contract address)
10    pub id: String,
11    /// Token amount as string
12    pub amount: String,
13}
14
15impl Token {
16    /// Create a new token
17    pub fn new(id: String, amount: String) -> Self {
18        Self { id, amount }
19    }
20
21    /// Create token with numeric amount
22    pub fn with_amount(id: String, amount: u128) -> Self {
23        Self {
24            id,
25            amount: amount.to_string(),
26        }
27    }
28}
29
30/// Fungible token metadata
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct FungibleTokenMetadata {
33    pub symbol: String,
34    pub name: String,
35    pub decimals: u8,
36    #[serde(rename = "totalSupply")]
37    pub total_supply: String,
38}
39
40/// NFT token metadata
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct NFTMetadata {
43    #[serde(rename = "tokenUri")]
44    pub token_uri: String,
45    #[serde(rename = "collectionId")]
46    pub collection_id: Option<String>,
47}
48
49/// Token type
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
51pub enum TokenType {
52    Fungible,
53    NonFungible,
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_token_creation() {
62        let token = Token::new("abc123".to_string(), "1000".to_string());
63        assert_eq!(token.id, "abc123");
64        assert_eq!(token.amount, "1000");
65    }
66
67    #[test]
68    fn test_token_with_amount() {
69        let token = Token::with_amount("xyz789".to_string(), 5000);
70        assert_eq!(token.amount, "5000");
71    }
72
73    #[test]
74    fn test_token_serialization() {
75        let token = Token::new("test_id".to_string(), "100".to_string());
76        let json = serde_json::to_string(&token).unwrap();
77        assert!(json.contains("test_id"));
78        assert!(json.contains("100"));
79    }
80}