Hash Time-Locked Contracts (HTLC)
How HTLCs enable trustless atomic swaps
Hash Time-Locked Contracts (HTLCs) are the foundation of LendaSwap's atomic swap protocol. They enable trustless, conditional payments across blockchains.
What is an HTLC?
An HTLC is a smart contract that locks funds with two conditions:
- Hash Lock: Funds can be claimed by revealing a secret (preimage)
- Time Lock: Funds are automatically refunded after a timeout
This combination enables atomic swaps without requiring trust between parties.
How It Works
Hash Lock
secret = random 32 bytes
hash_lock = SHA256(secret)- Only the party with the secret can claim funds
- Contract verifies:
SHA256(provided_secret) == hash_lock - 32-byte secret = 2^256 possible values (cryptographically secure)
Time Lock
timelock = current_time + 10 minutes- Before timeout: Only recipient can claim with secret
- After timeout: Sender can refund (no secret needed)
LendaSwap HTLC Flow
Service Creates HTLC
Service locks WBTC in the Polygon smart contract with:
- Recipient address (user)
- Hash lock (SHA256 of secret)
- Timelock (10 minutes)
- Minimum USDC output (1% slippage protection)
User Claims USDC
User reveals the secret on-chain:
- Contract verifies hash matches
- WBTC swaps to USDC via Uniswap V3
- USDC transfers to user's wallet
- Gasless via Gelato Relay
Service Extracts Secret
Service reads the revealed secret from the blockchain and can now claim the BTC payment (if using HTLC on Bitcoin side).
Gasless Execution
LendaSwap uses Gelato Relay and ERC-2771 meta-transactions so users never pay gas:
- User signs claim intent (off-chain)
- Gelato Relay submits transaction (pays gas)
- Contract processes claim
- User receives USDC with zero gas cost
Cost to service: ~$0.38 per claim
Security
| Protection | How |
|---|---|
| Atomic | Either both parties get funds, or neither does |
| Trustless | No third party controls funds |
| Time-bounded | Automatic refund if unclaimed |
| Slippage protection | Minimum output amount enforced |
| Reentrancy safe | State updated before external calls |
Timelock Safety
Polygon HTLC timeout: 10 minutes
Bitcoin timeout: 30 minutes (minimum)This ensures the service has time to claim BTC after the user reveals the secret on Polygon.
Smart Contract Interface
// Create swap (service)
function createSwap(
bytes32 swapId,
address recipient,
uint256 amountWBTCIn,
uint256 minAmountOut,
bytes32 hashLock,
uint256 timelock
) external;
// Claim swap (user, gasless via Gelato)
function claimSwap(bytes32 swapId, bytes32 secret) external;
// Refund after timeout (service)
function refundSwap(bytes32 swapId) external;