TypeScript service for coordinating atomic swaps between Ethereum and Internet Computer
Core services for cross-chain coordination and swap management
Coordinates the entire atomic swap process between Ethereum and ICP chains. Manages swap state, validates transactions, and ensures atomicity.
Provides real-time updates to frontend applications about swap status, transaction confirmations, and network events.
The resolver implements a microservices architecture for scalable cross-chain operations.
Main request handler and routing
Core swap resolution logic
Transaction relaying service
Swap execution service
// 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 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 });
}
}
Deploy the cross-chain resolver service
cd crosschain-resolver
npm install
cp .env.example .env
# Configure environment variables
# 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
npm run dev
# Start development server
npm run build
# Build for production
npm start
# Start production server
npm test
# Run all tests
npm run test:watch
# Run in watch mode
npm run test:coverage
# Run with coverage
Complete service API documentation
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 |
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 } |
Common integration patterns
// 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);
// 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;
}
};
// 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);
}
};