Skip to content

shriyashsoni/btc-sol-atomic-swap

Repository files navigation

β‚Ώ ⇄ β—Ž BTC SOL Atomic Swap

Trustless Cross-Chain Swaps Powered by Internet Computer

License: MIT Next.js Internet Computer Rust

Non-custodial β€’ Threshold Signatures β€’ $0.01 Fee β€’ Open Source

πŸš€ Get Started β€’ πŸ“– Documentation β€’ πŸ”’ Security β€’ 🀝 Contribute


Screenshot 2025-10-18 144600

πŸš€ Features

  • Trustless Swaps: No intermediaries or centralized custody
  • Threshold Signatures: ICP canister uses distributed signing (no single private key)
  • Automatic Refunds: Timeout protection if counterparty fails to deposit
  • Real-time Monitoring: Background watchers track deposits and confirmations
  • On-chain Verification: All transactions verifiable on Bitcoin and Solana explorers
  • Wallet Integration: Connect Bitcoin (Leather/Xverse/Unisat) and Solana (Phantom/Solflare) wallets
  • Low Fees: Only $0.01 per swap transaction

πŸ—οΈ Architecture

Frontend (Next.js + React)

  • Modern, professional UI with real-time status updates
  • Wallet connection for Bitcoin and Solana
  • Swap interface with automatic rate calculation
  • Order management and history tracking
  • Responsive design optimized for desktop and mobile
  • Real-time polling (30s intervals) for order updates

Backend (ICP Canister - Rust)

The canister implements:

  • Order creation and management
  • Bitcoin deposit tracking via ICP Bitcoin API
  • Solana deposit tracking via sol-rpc-canister
  • Threshold ECDSA signing for BTC transactions
  • Threshold Ed25519 signing for SOL transactions
  • Timer-based background watchers (polling every 30s)
  • Atomic settlement logic
  • Timeout-based refund mechanisms (24-hour expiration)

Key Technologies

  • Internet Computer: Execution layer and escrow coordinator
  • Bitcoin Integration: ICP's native Bitcoin API for UTXO tracking
  • Solana Integration: sol-rpc-canister for Solana interactions
  • Threshold Cryptography: Distributed signing without single keys
  • Next.js 15: Modern React framework with App Router
  • TailwindCSS v4: Utility-first styling
  • shadcn/ui: High-quality component library

πŸ”’ Security

  1. Non-custodial: Funds held in canister with threshold signatures
  2. Timeout Protection: 24-hour deposit deadline with automatic refunds
  3. Confirmation Requirements: 6 confirmations for BTC, 32 for Solana
  4. Open Source: All code auditable and verifiable
  5. Rate Limiting: Per-swap caps for MVP safety
  6. No Single Point of Failure: Distributed key generation across subnet nodes

πŸ› οΈ Development Setup

Prerequisites

Installation

```bash

Clone the repository

git clone cd btc-sol-atomic-swap

Install dependencies

npm install

Install Rust target for WebAssembly

rustup target add wasm32-unknown-unknown

Start local ICP replica

dfx start --background --clean

Deploy canister

dfx deploy btc_sol_swap_backend

Copy canister ID to environment

echo "NEXT_PUBLIC_CANISTER_ID=$(dfx canister id btc_sol_swap_backend)" > .env.local echo "NEXT_PUBLIC_IC_HOST=http://localhost:8000" >> .env.local

Run frontend

npm run dev ```

Visit http://localhost:3000 to see the app.

Environment Variables

Create a `.env.local` file:

```env

ICP Canister Configuration

NEXT_PUBLIC_CANISTER_ID=your-canister-id-here NEXT_PUBLIC_IC_HOST=http://localhost:8000

For production

NEXT_PUBLIC_IC_HOST=https://ic0.app

Bitcoin Network (testnet or mainnet)

NEXT_PUBLIC_BTC_NETWORK=testnet

Solana Network (devnet or mainnet-beta)

NEXT_PUBLIC_SOL_NETWORK=devnet ```

πŸ“‹ Canister Functions

Public Methods

  • `create_order()` - Create new swap order with BTC/SOL amounts
  • `get_order(order_id)` - Fetch order details and status
  • `notify_deposit(order_id, tx_id, asset)` - Notify deposit completion
  • `request_refund(order_id)` - Request timeout refund after 24h
  • `get_my_orders()` - List all orders for current user
  • `get_btc_address()` - Get canister's Bitcoin address
  • `get_sol_address()` - Get canister's Solana address

Internal Methods

  • `check_btc_confirmations()` - Verify Bitcoin confirmations via ICP API
  • `check_sol_confirmations()` - Verify Solana confirmations via RPC
  • `update_order_confirmations()` - Update order confirmation count
  • `settle_order()` - Execute atomic settlement with threshold signatures
  • `expire_order()` - Mark order as expired after timeout

Background Watchers

Uses `ic_cdk_timers::set_timer_interval()` to poll every 30 seconds:

  • Check pending deposits on both chains
  • Update confirmation counts
  • Trigger settlements when both deposits confirmed
  • Process timeout refunds for expired orders

πŸ§ͺ Testing

```bash

Test canister functions locally

dfx canister call btc_sol_swap_backend create_order '(record { from_asset = variant { BTC }; to_asset = variant { SOL }; from_amount = 5000000:nat64; to_amount = 162500000000:nat64; })'

Get order status

dfx canister call btc_sol_swap_backend get_order '("order_1234567890")'

Get all orders

dfx canister call btc_sol_swap_backend get_my_orders '()' ```

πŸ“Š Swap Flow

  1. Connect Wallets: User connects Bitcoin and Solana wallets
  2. Create Order: User specifies BTC amount to swap for SOL
  3. Deposit Addresses: Canister generates unique deposit addresses using threshold crypto
  4. Deposit Phase: User deposits BTC to provided address
  5. Verification: Canister monitors Bitcoin network for confirmations (6 required)
  6. Settlement: Once confirmed, canister atomically releases SOL to user
  7. Completion: Transaction complete with $0.01 fee

Order States

``` PendingDeposit β†’ AwaitingConfirmations β†’ ReadyToSettle β†’ Settling β†’ Completed ↓ Alternative: PendingDeposit β†’ (24h timeout) β†’ Expired β†’ Refunded ```

πŸ’° Fee Structure

Transaction Fee: $0.01 per swap

Breakdown:

  • ICP canister cycles: ~$0.005
  • Bitcoin network fee: ~$0.003
  • Solana network fee: ~$0.002

Fees are automatically deducted from the output amount.

πŸš€ Deployment

See DEPLOYMENT.md for detailed deployment instructions.

Quick Deploy to IC Mainnet

```bash

Deploy to mainnet

dfx deploy --network ic btc_sol_swap_backend

Update frontend environment

echo "NEXT_PUBLIC_CANISTER_ID=$(dfx canister id btc_sol_swap_backend --network ic)" > .env.production echo "NEXT_PUBLIC_IC_HOST=https://ic0.app" >> .env.production

Deploy frontend to Vercel

vercel --prod ```

πŸ“š Documentation

πŸ”„ Future Enhancements

  1. Order Book: Match makers and takers for better liquidity
  2. Partial Fills: Split large orders into smaller chunks
  3. More Assets: Add ETH, USDC, and other tokens
  4. Lightning Network: Instant BTC settlements
  5. True HTLC: Peer-to-peer hash-locked contracts
  6. Governance: DAO for protocol upgrades
  7. Oracle Integration: Real-time exchange rates

🀝 Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

πŸ“„ License

MIT License - Open Source

πŸ†˜ Support

  • GitHub Issues: Report bugs and request features
  • Discord: Join our community
  • Documentation: Read the full docs

Built with ❀️ on Internet Computer

Transaction Fee: Only $0.01 per swap | Non-custodial | Trustless | Open Source

About

A trustless, non-custodial atomic swap platform for exchanging Bitcoin and Solana, powered by the Internet Computer (ICP).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors