Plutus HTLC: Atomic Swap Smart Contract
π Table of Contents
π Overview
βοΈ Contract Structure
π§© HTLCParams β Contract Parameters
π― HTLCRedeemer β User Actions
π§ͺ mkValidator β Core Logic
π Typed Validator & Wrapping
π« Script Address
π 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
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
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
mkValidator β Core Validation Logicπ What this does:
π If Redeem secret:
Checks
sha2_256(secret) == hashlockEnsures 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
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