Skip to main content

avalanche_atomic_swap_daemon/
lib.rs

1//! # Avalanche Atomic Swap Daemon
2//!
3//! A production-ready HTLC (Hash Time-Locked Contract) atomic swap daemon for bidirectional
4//! swaps between Avalanche C-Chain and any Subnet-EVM chain.
5//!
6//! ## Features
7//!
8//! - **Bidirectional Swaps**: Supports both C-Chain → Subnet and Subnet → C-Chain atomic swaps
9//! - **Automatic Recovery**: Recovers in-flight swaps on startup by scanning recent blocks
10//! - **Transaction Finality**: Validates transaction finality before proceeding with swaps
11//! - **Prometheus Metrics**: Built-in metrics endpoint for monitoring swap activity
12//! - **Configurable Thresholds**: Set minimum swap amounts and polling intervals
13//!
14//! ## Quick Start
15//!
16//! ```bash
17//! cargo install avalanche-atomic-swap-daemon
18//! avalanche-atomic-swap-daemon --help
19//! ```
20//!
21//! ## Configuration
22//!
23//! Configure via environment variables or command-line arguments:
24//!
25//! ```bash
26//! export SUBNET_RPC="https://subnet.example.com/rpc"
27//! export DAEMON_PRIVATE_KEY="0x..."
28//! export HTLC_CCHAIN="0x..."
29//! export HTLC_SUBNET="0x..."
30//! avalanche-atomic-swap-daemon
31//! ```
32//!
33//! ## Architecture
34//!
35//! The daemon monitors HTLC contracts on both chains:
36//!
37//! 1. **Initiation**: User locks funds on Chain A with a hashlock
38//! 2. **Mirror Lock**: Daemon detects the lock and mirrors it on Chain B
39//! 3. **Claim**: User reveals the secret on Chain B to claim funds
40//! 4. **Complete**: Daemon uses the revealed secret to claim funds on Chain A
41//!
42//! ## Library Usage
43//!
44//! While primarily a binary application, the library exposes core components
45//! for building custom atomic swap solutions:
46//!
47//! ```rust,no_run
48//! use avalanche_atomic_swap_daemon::{
49//!     clients::{CChainClient, SubnetClient},
50//!     watcher::SwapWatcher,
51//!     config::Config,
52//! };
53//! use std::sync::Arc;
54//!
55//! # async fn example() -> eyre::Result<()> {
56//! // Configuration would typically come from Config::parse()
57//! # let config = Config::parse();
58//! # let signer = "0x1234".parse().unwrap();
59//! # let cchain = Arc::new(CChainClient::new(config.cchain_rpc, config.htlc_cchain, signer.clone()).await?);
60//! # let subnet = Arc::new(SubnetClient::new(config.subnet_rpc, config.htlc_subnet, signer).await?);
61//!
62//! // Create swap watcher
63//! let watcher = SwapWatcher::new(cchain, subnet, config.min_amount).await;
64//!
65//! // Recover in-flight swaps
66//! watcher.recover_state(200).await?;
67//!
68//! // Start monitoring (this runs forever)
69//! watcher.run().await;
70//! # Ok(())
71//! # }
72//! ```
73
74#![doc(html_root_url = "https://docs.rs/avalanche-atomic-swap-daemon/0.2.1")]
75#![allow(missing_docs)]
76
77/// Client implementations for C-Chain and Subnet-EVM chains
78pub mod clients;
79
80/// Configuration parsing and management
81pub mod config;
82
83/// HTLC contract bindings generated from Solidity
84pub mod htlc;
85
86/// Prometheus metrics server and collectors
87pub mod metrics;
88
89/// Swap state definitions and types
90pub mod state;
91
92/// Trait definitions for chain interactions
93pub mod traits;
94
95/// Main swap watcher and orchestration logic
96pub mod watcher;
97
98// Re-export commonly used types
99pub use crate::clients::cchain::CChainClient;
100pub use crate::clients::subnet::SubnetClient;
101pub use config::Config;
102pub use state::{SwapDirection, SwapState};
103pub use traits::{AvalancheChain, SwapClaimedEvent, SwapInitiatedEvent};
104pub use watcher::SwapWatcher;