Plutus HTLC: Atomic Swap Smart Contract

πŸ“Œ Table of Contents

  1. πŸ” Overview

  2. βš™οΈ Contract Structure

  3. 🧩 HTLCParams – Contract Parameters

  4. 🎯 HTLCRedeemer – User Actions

  5. πŸ§ͺ mkValidator – Core Logic

  6. πŸ“œ Typed Validator & Wrapping

  7. πŸ“« Script Address

  8. πŸ“˜ Glossary of Terms


1️⃣ πŸ” Overview

This contract implements a Hashed Timelock Contract (HTLC), which enables secure, trustless, and time-limited swaps of assets between two parties. The contract has two unlock conditions:

  • βœ… Hashlock: Receiver must reveal the preimage of a given hash before the deadline.

  • ⏳ Timelock: If deadline passes, the sender can reclaim the funds.


2️⃣ βš™οΈ Contract Structure

The contract consists of:

  • πŸ“¦ HTLCParams: The configuration (sender, receiver, hash, and deadline).

  • 🎯 HTLCRedeemer: The action chosen (Redeem with a secret, or Refund).

  • πŸ§ͺ mkValidator: The validator logic.

  • 🧡 Typed validator & script address.


3️⃣ 🧩 HTLCParams β€” Contract Parameters

πŸ” Explanation:

  • sender: Person initiating the swap (can reclaim funds after deadline).

  • receiver: Person claiming funds with the secret.

  • hashlock: A SHA-256 hash of the secret.

  • deadline: Time after which refund is allowed.

πŸš€ Make this value once off-chain, and lift it using Template Haskell:


4️⃣ 🎯 HTLCRedeemer β€” User Actions

✨ Purpose:

  • Redeem secret β€” Receiver provides the secret before the deadline.

  • Refund β€” Sender reclaims after the deadline.

Must be serializable:


5️⃣ πŸ§ͺ mkValidator β€” Core Validation Logic

πŸ” What this does:

πŸ”‘ If Redeem secret:

  • Checks sha2_256(secret) == hashlock

  • Ensures the receiver signed the tx

  • Ensures the current time is before the deadline

πŸ’Έ If Refund:

  • Checks time is after the deadline

  • Ensures the sender signed it

All conditions use traceIfFalse for on-chain debugging.


6️⃣ πŸ“œ Typed Validator & Wrapping

βœ‚οΈ Define the validator types:

🧡 Compile the validator:

πŸ” This connects the raw mkValidator to Plutus's typed validation system.


7️⃣ πŸ“« Script Address

This lets you lock funds at the HTLC address on-chain:


8️⃣ πŸ“˜ Glossary of Terms

Term
Meaning

sha2_256

Hash function used for locking the secret

PubKeyHash

Public key identifier

txSignedBy

Verifies transaction is signed by a given party

scriptContextTxInfo

Retrieves transaction info for validation

contains

Checks if a time range contains a point

traceIfFalse

On-chain debugging that fails with a message

POSIXTime

UTC timestamp used in Plutus timelocks

Validator

A Plutus smart contract function

BuiltinByteString

On-chain byte string

Redeemer

Input that unlocks a validator

TypedValidator

Type-safe validator using Plutus type system


πŸ§ͺ Sample Off-Chain Parameter Setup

You'd pass this into typedValidator and derive the address to fund it.


βœ… Summary

This HTLC Plutus contract allows:

  • Atomic swaps between chains or users

  • Time-bound claim and refund

  • Secure hash-based locking

It's ideal for use cases like:

  • πŸ’± Cross-chain asset exchanges

  • 🧾 Conditional token access

  • πŸ” Trustless payments with refund guarantees


Last updated