apex_sdk_types/lib.rs
1//! # Apex SDK Types
2//!
3//! Common types and data structures used across the Apex SDK.
4//!
5//! This crate provides fundamental types for representing blockchain entities
6//! across different chain types (Substrate, EVM, Hybrid).
7//!
8//! ## Core Types
9//!
10//! - **Chain**: Enumeration of supported blockchain networks
11//! - **ChainType**: Classification of chains (Substrate, EVM, Hybrid)
12//! - **Address**: Generic address type supporting multiple formats
13//! - **TransactionStatus**: Unified transaction status representation
14//! - **CrossChainTransaction**: Cross-chain transaction information
15//!
16//! ## Example
17//!
18//! ```rust
19//! use apex_sdk_types::{Chain, ChainType, Address};
20//!
21//! // Create addresses for different chains
22//! let eth_addr = Address::evm("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb7");
23//! let dot_addr = Address::substrate("15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5");
24//!
25//! // Check chain types
26//! assert_eq!(Chain::Ethereum.chain_type(), ChainType::Evm);
27//! assert_eq!(Chain::Polkadot.chain_type(), ChainType::Substrate);
28//! assert_eq!(Chain::Moonbeam.chain_type(), ChainType::Hybrid);
29//! ```
30
31use serde::{Deserialize, Serialize};
32
33/// Blockchain types
34#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
35pub enum ChainType {
36 /// Substrate-based chain
37 Substrate,
38 /// EVM-based chain
39 Evm,
40 /// Hybrid chain (both Substrate and EVM)
41 Hybrid,
42}
43
44/// Supported blockchain networks
45#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
46pub enum Chain {
47 /// Polkadot relay chain
48 Polkadot,
49 /// Kusama relay chain
50 Kusama,
51 /// Ethereum mainnet
52 Ethereum,
53 /// Binance Smart Chain
54 BinanceSmartChain,
55 /// Polygon
56 Polygon,
57 /// Avalanche C-Chain
58 Avalanche,
59 /// Moonbeam (Polkadot parachain with EVM)
60 Moonbeam,
61 /// Astar (Polkadot parachain with EVM)
62 Astar,
63}
64
65impl Chain {
66 /// Get the chain type
67 pub fn chain_type(&self) -> ChainType {
68 match self {
69 Chain::Polkadot | Chain::Kusama => ChainType::Substrate,
70 Chain::Ethereum | Chain::BinanceSmartChain | Chain::Polygon | Chain::Avalanche => {
71 ChainType::Evm
72 }
73 Chain::Moonbeam | Chain::Astar => ChainType::Hybrid,
74 }
75 }
76
77 /// Get the chain name
78 pub fn name(&self) -> &str {
79 match self {
80 Chain::Polkadot => "Polkadot",
81 Chain::Kusama => "Kusama",
82 Chain::Ethereum => "Ethereum",
83 Chain::BinanceSmartChain => "Binance Smart Chain",
84 Chain::Polygon => "Polygon",
85 Chain::Avalanche => "Avalanche",
86 Chain::Moonbeam => "Moonbeam",
87 Chain::Astar => "Astar",
88 }
89 }
90}
91
92/// Generic address type for different chains
93#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
94pub enum Address {
95 /// Substrate SS58 address
96 Substrate(String),
97 /// EVM hex address (0x...)
98 Evm(String),
99}
100
101impl Address {
102 /// Create a Substrate address
103 pub fn substrate(addr: impl Into<String>) -> Self {
104 Address::Substrate(addr.into())
105 }
106
107 /// Create an EVM address
108 pub fn evm(addr: impl Into<String>) -> Self {
109 Address::Evm(addr.into())
110 }
111
112 /// Get the address as a string
113 pub fn as_str(&self) -> &str {
114 match self {
115 Address::Substrate(s) | Address::Evm(s) => s,
116 }
117 }
118}
119
120/// Transaction status
121#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
122pub enum TransactionStatus {
123 /// Transaction is pending
124 Pending,
125 /// Transaction is confirmed
126 Confirmed {
127 /// Block number where transaction was included
128 block_number: u64,
129 /// Number of confirmations
130 confirmations: u32,
131 },
132 /// Transaction failed
133 Failed {
134 /// Error message
135 error: String,
136 },
137 /// Transaction status unknown
138 Unknown,
139}
140
141/// Cross-chain transaction info
142#[derive(Debug, Clone, Serialize, Deserialize)]
143pub struct CrossChainTransaction {
144 /// Transaction ID
145 pub id: String,
146 /// Source chain
147 pub source_chain: Chain,
148 /// Destination chain
149 pub destination_chain: Chain,
150 /// Source transaction hash
151 pub source_tx_hash: Option<String>,
152 /// Destination transaction hash
153 pub destination_tx_hash: Option<String>,
154 /// Transaction status
155 pub status: TransactionStatus,
156 /// Timestamp
157 pub timestamp: u64,
158}
159
160#[cfg(test)]
161mod tests {
162 use super::*;
163
164 #[test]
165 fn test_chain_type() {
166 assert_eq!(Chain::Polkadot.chain_type(), ChainType::Substrate);
167 assert_eq!(Chain::Ethereum.chain_type(), ChainType::Evm);
168 assert_eq!(Chain::Moonbeam.chain_type(), ChainType::Hybrid);
169 }
170
171 #[test]
172 fn test_address_creation() {
173 let sub_addr = Address::substrate("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY");
174 assert!(matches!(sub_addr, Address::Substrate(_)));
175
176 let evm_addr = Address::evm("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb7");
177 assert!(matches!(evm_addr, Address::Evm(_)));
178 }
179}