Skip to main content

Crate avalanche_atomic_swap_daemon

Crate avalanche_atomic_swap_daemon 

Source
Expand description

§Avalanche Atomic Swap Daemon

A production-ready HTLC (Hash Time-Locked Contract) atomic swap daemon for bidirectional swaps between Avalanche C-Chain and any Subnet-EVM chain.

§Features

  • Bidirectional Swaps: Supports both C-Chain → Subnet and Subnet → C-Chain atomic swaps
  • Automatic Recovery: Recovers in-flight swaps on startup by scanning recent blocks
  • Transaction Finality: Validates transaction finality before proceeding with swaps
  • Prometheus Metrics: Built-in metrics endpoint for monitoring swap activity
  • Configurable Thresholds: Set minimum swap amounts and polling intervals

§Quick Start

cargo install avalanche-atomic-swap-daemon
avalanche-atomic-swap-daemon --help

§Configuration

Configure via environment variables or command-line arguments:

export SUBNET_RPC="https://subnet.example.com/rpc"
export DAEMON_PRIVATE_KEY="0x..."
export HTLC_CCHAIN="0x..."
export HTLC_SUBNET="0x..."
avalanche-atomic-swap-daemon

§Architecture

The daemon monitors HTLC contracts on both chains:

  1. Initiation: User locks funds on Chain A with a hashlock
  2. Mirror Lock: Daemon detects the lock and mirrors it on Chain B
  3. Claim: User reveals the secret on Chain B to claim funds
  4. Complete: Daemon uses the revealed secret to claim funds on Chain A

§Library Usage

While primarily a binary application, the library exposes core components for building custom atomic swap solutions:

use avalanche_atomic_swap_daemon::{
    clients::{CChainClient, SubnetClient},
    watcher::SwapWatcher,
    config::Config,
};
use std::sync::Arc;

// Configuration would typically come from Config::parse()

// Create swap watcher
let watcher = SwapWatcher::new(cchain, subnet, config.min_amount).await;

// Recover in-flight swaps
watcher.recover_state(200).await?;

// Start monitoring (this runs forever)
watcher.run().await;

Re-exports§

pub use crate::clients::cchain::CChainClient;
pub use crate::clients::subnet::SubnetClient;
pub use config::Config;
pub use state::SwapDirection;
pub use state::SwapState;
pub use traits::AvalancheChain;
pub use traits::SwapClaimedEvent;
pub use traits::SwapInitiatedEvent;
pub use watcher::SwapWatcher;

Modules§

clients
Client implementations for C-Chain and Subnet-EVM chains
config
Configuration parsing and management
htlc
HTLC contract bindings generated from Solidity
metrics
Prometheus metrics server and collectors
state
Swap state definitions and types
traits
Trait definitions for chain interactions
watcher
Main swap watcher and orchestration logic