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};

/// Block header information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockHeader {
    pub hash: String,
    pub timestamp: u64,
    pub height: u32,
    #[serde(rename = "chainFrom")]
    pub chain_from: u8,
    #[serde(rename = "chainTo")]
    pub chain_to: u8,
    #[serde(rename = "parentHash")]
    pub parent_hash: String,
    pub nonce: String,
    pub version: u8,
    #[serde(rename = "depStateHash")]
    pub dep_state_hash: String,
    #[serde(rename = "txsHash")]
    pub txs_hash: String,
    pub target: String,
}

/// Block with transactions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Block {
    pub hash: String,
    pub timestamp: u64,
    pub height: u32,
    #[serde(rename = "chainFrom")]
    pub chain_from: u8,
    #[serde(rename = "chainTo")]
    pub chain_to: u8,
    pub transactions: Vec<BlockTransaction>,
    #[serde(rename = "parentHash")]
    pub parent_hash: String,
    pub nonce: String,
}

/// Transaction in a block
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockTransaction {
    pub hash: String,
    pub inputs: Vec<TransactionInput>,
    pub outputs: Vec<TransactionOutput>,
    #[serde(rename = "gasAmount")]
    pub gas_amount: u32,
    #[serde(rename = "gasPrice")]
    pub gas_price: String,
}

/// Transaction input
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionInput {
    #[serde(rename = "outputRef")]
    pub output_ref: OutputRef,
    #[serde(rename = "unlockScript")]
    pub unlock_script: String,
}

/// Output reference
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputRef {
    pub key: String,
    pub hint: i32,
}

/// Transaction output
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionOutput {
    #[serde(rename = "type")]
    pub output_type: String,
    pub address: String,
    #[serde(rename = "attoAlphAmount")]
    pub atto_alph_amount: String,
    pub tokens: Option<Vec<TokenOutput>>,
    #[serde(rename = "lockTime")]
    pub lock_time: Option<u64>,
    pub message: Option<String>,
}

/// Token in output
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenOutput {
    pub id: String,
    pub amount: String,
}

impl Block {
    /// Get transaction count
    pub fn transaction_count(&self) -> usize {
        self.transactions.len()
    }

    /// Check if block is from specific chain
    pub fn is_from_chain(&self, chain: u8) -> bool {
        self.chain_from == chain
    }

    /// Check if block is to specific chain
    pub fn is_to_chain(&self, chain: u8) -> bool {
        self.chain_to == chain
    }
}

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

    #[test]
    fn test_block_transaction_count() {
        let block = Block {
            hash: "test_hash".to_string(),
            timestamp: 1234567890,
            height: 100,
            chain_from: 0,
            chain_to: 1,
            transactions: vec![],
            parent_hash: "parent_hash".to_string(),
            nonce: "nonce".to_string(),
        };
        assert_eq!(block.transaction_count(), 0);
    }

    #[test]
    fn test_block_chain_check() {
        let block = Block {
            hash: "test_hash".to_string(),
            timestamp: 1234567890,
            height: 100,
            chain_from: 0,
            chain_to: 1,
            transactions: vec![],
            parent_hash: "parent_hash".to_string(),
            nonce: "nonce".to_string(),
        };
        assert!(block.is_from_chain(0));
        assert!(!block.is_from_chain(1));
        assert!(block.is_to_chain(1));
        assert!(!block.is_to_chain(0));
    }

    #[test]
    fn test_block_serialization() {
        let block = Block {
            hash: "abc123".to_string(),
            timestamp: 1234567890,
            height: 100,
            chain_from: 0,
            chain_to: 1,
            transactions: vec![],
            parent_hash: "parent".to_string(),
            nonce: "nonce_value".to_string(),
        };
        let json = serde_json::to_string(&block).unwrap();
        assert!(json.contains("abc123"));
        assert!(json.contains("1234567890"));
    }
}