QuantumSwap: Trustless ETH-BTC atomic swaps using HTLCs, no intermediaries, no custody, pure DeFi.
QuantumSwap: Trustless Cross-Chain Atomic Swaps
What is QuantumSwap?
QuantumSwap is a complete implementation of trustless atomic swaps between Ethereum and Bitcoin using Hash Time-Locked Contracts (HTLCs). Built for ETHGlobal, it enables users to exchange cryptocurrencies across different blockchains without requiring any trusted intermediaries, custodians, or centralized exchanges.
Core Technology: HTLCs (Hash Time-Locked Contracts)
How HTLCs Work:
Security Guarantees:
Project Architecture:
Key Features:
Trustless Operation:
Multi-Chain Support:
Advanced Features:
User Experience:
Technical Implementation:
Frontend (React + TypeScript):
Backend (Node.js + TypeScript):
Smart Contracts (Solidity):
Bitcoin Scripts:
Security Considerations:
Cryptographic Security:
Smart Contract Security:
Network Security:
Use Cases:
Individual Users:
DeFi Protocols:
Institutional Users:
Development and Deployment:
Local Development:
Production Deployment:
Environment Configuration:
Future Enhancements:
Planned Features:
Technical Improvements:
Conclusion:
QuantumSwap represents a complete, production-ready implementation of trustless atomic swaps, demonstrating how blockchain technology can enable truly decentralized cross-chain trading. By eliminating the need for trusted intermediaries while maintaining security and usability, it provides a foundation for the future of decentralized finance where users have full control over their assets and trading activities.
The project showcases advanced blockchain development techniques, including smart contract design, Bitcoin script programming, cross-chain communication, and modern web development practices, serving as both a practical tool for users and a reference implementation for developers looking to build similar systems.
How it's made:
QuantumSwap was built as a comprehensive full-stack application combining smart contracts, Bitcoin scripts, cross-chain communication, and modern web technologies. Here's the technical deep dive:
Smart Contract Architecture: The Ethereum side uses two main contracts: ETHHTLC.sol for basic atomic swaps and FusionHTLC.sol for advanced order matching. The contracts implement the core HTLC logic with lock(), redeem(), and refund() functions. A particularly hacky but effective approach was using keccak256 hashing of random data to generate unique order IDs, ensuring no collisions while maintaining gas efficiency. The contracts emit comprehensive events (Locked, Redeemed, Refunded) that the relayer service monitors for cross-chain coordination.
Bitcoin Script Implementation: The Bitcoin side required custom P2SH scripts using bitcoinjs-lib. The most challenging part was implementing the HTLC script with OP_SHA256, OP_CLTV, and OP_CHECKSIG operations. We had to manually construct the redeem script and serialize it properly for P2SH addresses. The hacky part here was using a custom script builder that generates both the redeem path (with secret) and refund path (with timelock) in a single script, allowing for atomic execution.
Cross-Chain Relayer Service: The relayer is a Node.js service that monitors both Ethereum and Bitcoin networks simultaneously. It uses ethers.js for Ethereum interaction and electrum-client for Bitcoin. The most complex part was implementing the secret extraction mechanism - when a redeem event is detected on one chain, the relayer extracts the secret and automatically triggers the corresponding redemption on the other chain. This required careful event parsing and transaction construction.
Frontend Development: Built with React + TypeScript + Vite for optimal development experience. The frontend integrates multiple wallet providers: MetaMask for Ethereum and Unisat/Hiro/Xverse for Bitcoin. A particularly hacky solution was implementing a dual input system where users can specify both send and receive amounts, with real-time calculation based on live exchange rates. The exchange rate system uses multiple API sources (CoinGecko, Binance) with fallback mechanisms to ensure reliability.
1inch Fusion+ Integration: The project integrates with 1inch Fusion+ protocol for advanced order matching and partial fills. This integration allows users to create orders that can be matched by multiple counterparties, enabling better liquidity and more efficient trading. The Fusion+ integration required implementing order creation, matching, and execution logic that works seamlessly with our HTLC system. The most complex part was ensuring that Fusion+ orders maintain the same security guarantees as direct HTLC swaps while providing additional flexibility.
Real-Time Exchange Rates: Implemented a sophisticated exchange rate system that fetches from multiple sources with automatic fallback. The hacky part was using a combination of CoinGecko's free API and Binance's public endpoints, with local caching and rate limiting to avoid API restrictions. When all external APIs fail, it falls back to estimated rates to maintain functionality.
Wallet Integration: The most challenging part was integrating multiple Bitcoin wallet providers (Unisat, Hiro, Xverse) with different APIs. We implemented a provider detection system that checks for wallet availability and provides a unified interface. The hacky solution was using window object detection and dynamic API calls based on which wallet is available.
Production Deployment: Deployed on AWS EC2 with Nginx reverse proxy and Let's Encrypt SSL. The deployment process involved creating systemd services for both frontend and relayer, with proper environment variable management. A particularly hacky but effective approach was using npx for service execution to avoid path issues with globally installed packages.
Database and State Management: Instead of a traditional database, we implemented a JSON-based logging system for swap status tracking. This was a deliberate choice for simplicity and transparency - all swap data is stored in human-readable JSON files. The hacky part was implementing file-based locking to prevent race conditions when multiple processes access the same log files.
CLI Tools: Built comprehensive CLI tools using TypeScript and Commander.js. The tools can create orders, monitor status, and execute HTLC operations directly. The most complex part was implementing partial fill support for large orders, which required careful UTXO management and change address handling.
Security Implementation: Implemented comprehensive input validation using Zod schemas throughout the application. The most critical security feature is the cryptographic secret generation using window.crypto.getRandomValues() for true randomness. Private keys are never stored on servers - all signing happens in user wallets.
Cross-Chain Communication: The most technically challenging part was implementing reliable cross-chain communication. The relayer service polls both networks every 5 seconds, parsing events and transaction data. When it detects a redeem event on one chain, it extracts the secret and constructs the corresponding transaction for the other chain. This required deep understanding of both Ethereum and Bitcoin transaction formats.
Error Handling and Monitoring: Implemented comprehensive error handling with graceful degradation. The system continues to function even if external APIs fail or networks are congested. Real-time status updates are provided through polling mechanisms, with proper error states and user feedback.
The entire system is designed to be stateless and fault-tolerant, with each component able to recover from failures independently. The most hacky but effective approach was using a combination of file-based logging, in-memory caching, and real-time polling to maintain system reliability without complex infrastructure requirements.