Cross-Chain Resolver

TypeScript service for coordinating atomic swaps between Ethereum and Internet Computer

Resolver Services

Core services for cross-chain coordination and swap management

Swap Coordinator

Main swap orchestration service

Coordinates the entire atomic swap process between Ethereum and ICP chains. Manages swap state, validates transactions, and ensures atomicity.

  • Swap state management
  • Cross-chain validation
  • Transaction monitoring
  • Error recovery

WebSocket Server

Real-time communication service

Provides real-time updates to frontend applications about swap status, transaction confirmations, and network events.

  • Real-time updates
  • Event broadcasting
  • Connection management
  • Error handling

Service Architecture

The resolver implements a microservices architecture for scalable cross-chain operations.

Core Components
Handler

Main request handler and routing

Resolver

Core swap resolution logic

Relayer

Transaction relaying service

Swap

Swap execution service

Service Integration
// Main resolver service
export class CrossChainResolver {
  private ethereumService: ETHLiquidityService;
  private icpService: ICPLiquidityService;
  private webSocketServer: WebSocketServer;
  
  async createSwap(params: SwapParams): Promise {
    // Validate parameters
    // Create swap on both chains
    // Monitor for completion
    // Return result
  }
  
  async monitorSwap(swapId: string): Promise {
    // Check status on both chains
    // Update WebSocket clients
    // Return current status
  }
}
WebSocket Communication
// WebSocket server for real-time updates
export class FusionWebSocket {
  private wss: WebSocketServer;
  private clients: Set = new Set();
  
  broadcast(event: string, data: any) {
    this.clients.forEach(client => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(JSON.stringify({ event, data }));
      }
    });
  }
  
  handleSwapUpdate(swapId: string, status: SwapStatus) {
    this.broadcast('swap_update', { swapId, status });
  }
}
Service Features
  • Cross-chain validation
  • Real-time monitoring
  • Automatic retry logic
  • Error recovery
Integration Points
  • Ethereum RPC
  • ICP Canister calls
  • Frontend WebSocket
  • State persistence

Setup Guide

Deploy the cross-chain resolver service

Prerequisites
  • Node.js 16+ and npm
  • Ethereum node access
  • ICP network access
  • Environment configuration
Installation
cd crosschain-resolver
npm install
cp .env.example .env
# Configure environment variables
Configuration
# Ethereum configuration
ETHEREUM_RPC_URL=https://eth-mainnet.alchemyapi.io/v2/YOUR_KEY
ETHEREUM_PRIVATE_KEY=your_private_key

# ICP configuration
ICP_CANISTER_ID=your_canister_id
ICP_NETWORK=ic

# WebSocket configuration
WS_PORT=8080
WS_HOST=localhost
Development
npm run dev
# Start development server
npm run build
# Build for production
npm start
# Start production server
Testing
npm test
# Run all tests
npm run test:watch
# Run in watch mode
npm run test:coverage
# Run with coverage

API Reference

Complete service API documentation

REST API Endpoints
Method Endpoint Description Parameters
POST /api/swap/create Create new atomic swap SwapParams
GET /api/swap/:id Get swap status swapId
POST /api/swap/:id/claim Claim swap with preimage ClaimParams
POST /api/swap/:id/refund Refund expired swap RefundParams
WebSocket Events
Event Description Data Format
swap_created New swap created { swapId, status }
swap_updated Swap status updated { swapId, status, details }
swap_completed Swap successfully completed { swapId, result }
swap_failed Swap failed or expired { swapId, error }

Usage Examples

Common integration patterns

Creating a Swap
// Create atomic swap
const swapParams = {
  fromChain: 'ethereum',
  toChain: 'icp',
  fromToken: 'ETH',
  toToken: 'ICP',
  amount: '1.0',
  recipient: 'user-address',
  timelock: Date.now() + 3600000 // 1 hour
};

const response = await fetch('/api/swap/create', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(swapParams)
});

const result = await response.json();
console.log('Swap created:', result.swapId);
WebSocket Connection
// Connect to WebSocket for real-time updates
const ws = new WebSocket('ws://localhost:8080');

ws.onopen = () => {
  console.log('Connected to resolver');
};

ws.onmessage = (event) => {
  const { event: eventType, data } = JSON.parse(event.data);
  
  switch (eventType) {
    case 'swap_updated':
      console.log('Swap updated:', data);
      break;
    case 'swap_completed':
      console.log('Swap completed:', data);
      break;
  }
};
Monitoring Swap Status
// Monitor swap status
const checkSwapStatus = async (swapId: string) => {
  const response = await fetch(`/api/swap/${swapId}`);
  const status = await response.json();
  
  console.log('Swap status:', status);
  
  if (status.state === 'pending') {
    // Continue monitoring
    setTimeout(() => checkSwapStatus(swapId), 5000);
  }
};