Ethereum Smart Contracts

Secure hash time lock contracts and liquidity management for cross-chain atomic swaps

Smart Contracts

Core contracts for Ethereum-side atomic swap functionality

HashedTimelock

Core atomic swap contract

The main contract implementing hash time lock functionality for secure cross-chain swaps. Manages the creation, claiming, and refunding of atomic swaps using cryptographic hashes.

  • Hash time lock creation
  • Preimage verification
  • Automatic refund mechanism
  • Event emission for tracking

LiquidityVault

Liquidity management contract

Manages liquidity pools and provides funding for atomic swaps. Handles token deposits, withdrawals, and ensures sufficient liquidity for cross-chain operations.

  • Liquidity pool management
  • Token deposit/withdrawal
  • Balance tracking
  • Access control

HashedTimelock Contract

The core contract that implements atomic swap functionality using hash time locks.

Key Functions
newContract

Creates a new hash time lock contract

claim

Claims funds using preimage

refund

Refunds after timelock expiration

getContract

Retrieves contract details

Contract Structure
struct TimeLockContract {
    address sender;
    address receiver;
    uint256 amount;
    bytes32 hashlock;
    uint256 timelock;
    bool withdrawn;
    bool refunded;
    bytes32 preimage;
}
Security Features
  • Cryptographic hash verification
  • Time-based expiration
  • Access control validation
  • Double-spending prevention

Deployment Guide

Deploy contracts to Ethereum networks

Prerequisites
  • Node.js and npm installed
  • Hardhat development environment
  • Ethereum wallet with test ETH
  • Environment variables configured
Installation
cd ethereum-contracts
npm install
cp .env.example .env
# Configure your .env file
Compilation
npx hardhat compile
# Contracts compiled to artifacts/
Testing
npx hardhat test
# Run all tests
npx hardhat test --grep "HashedTimelock"
# Run specific test suite
Deployment
npx hardhat run scripts/deploy.js --network localhost
# Deploy to local network
npx hardhat run scripts/deploy.js --network sepolia
# Deploy to testnet

Testing

Comprehensive test suite covering all contract functionality and edge cases.

Unit Tests
  • Contract creation
  • Preimage verification
  • Time-based operations
  • Access control
Integration Tests
  • Cross-contract interaction
  • Event emission
  • Gas optimization
  • Error handling