alephium-web3 2.0.7

A Rust library to interact with the Alephium platform
Documentation
// Copyright 2018 - 2022 The Alephium Authors
// This file is part of the alephium project.

use serde::{Deserialize, Serialize};

/// Token information
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Token {
    /// Token ID (contract address)
    pub id: String,
    /// Token amount as string
    pub amount: String,
}

impl Token {
    /// Create a new token
    pub fn new(id: String, amount: String) -> Self {
        Self { id, amount }
    }

    /// Create token with numeric amount
    pub fn with_amount(id: String, amount: u128) -> Self {
        Self {
            id,
            amount: amount.to_string(),
        }
    }
}

/// Fungible token metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FungibleTokenMetadata {
    pub symbol: String,
    pub name: String,
    pub decimals: u8,
    #[serde(rename = "totalSupply")]
    pub total_supply: String,
}

/// NFT token metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NFTMetadata {
    #[serde(rename = "tokenUri")]
    pub token_uri: String,
    #[serde(rename = "collectionId")]
    pub collection_id: Option<String>,
}

/// Token type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TokenType {
    Fungible,
    NonFungible,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_token_creation() {
        let token = Token::new("abc123".to_string(), "1000".to_string());
        assert_eq!(token.id, "abc123");
        assert_eq!(token.amount, "1000");
    }

    #[test]
    fn test_token_with_amount() {
        let token = Token::with_amount("xyz789".to_string(), 5000);
        assert_eq!(token.amount, "5000");
    }

    #[test]
    fn test_token_serialization() {
        let token = Token::new("test_id".to_string(), "100".to_string());
        let json = serde_json::to_string(&token).unwrap();
        assert!(json.contains("test_id"));
        assert!(json.contains("100"));
    }
}