Task: Build an Algorand Smart Contract

Hash Time Lock Contract (HTLC) for Algorand Standard Assets (ASA)

Functionality

This contract implements a hashed timelock contract (HTLC) for holding Algorand Standard Assets (ASA). It is intended for use in cross-chain atomic swaps of an ASA for an asset on a foreign chain using the same preimage.

The contract will approve transactions from itself under three scenarios:

  1. If txn is an “opt-in” transaction for TMPL_ASA_ID.
  2. If txn.FirstValid is greater than TMPL_TIMEOUT, then funds may be closed out to TMPL_OWN.
  3. If an argument arg_0 is passed to the script such that TMPL_HASHFN(arg_0) is equal to TMPL_HASHIMG, then funds may be closed out to TMPL_RCV.

Code overview

( (Scenario 1) OR  ( (        Scenario 2           ) OR  (      Scenario 3     ) ) AND Fee)
( (  Opt-in  ) bnz ( ( Time Lock: ( ASA || Algos ) ) bnz ( Hash Lock: ASA Only ) ) && Fee )

TEAL Template Code

Template Details and Walkthrough

Next Steps

Please make use of the template and provide feedback. Pull Requests are welcome :slight_smile:

Footnotes

All code is offered “AS-IS” and “WITHOUT WARRANTY” into the PUBLIC DOMAIN.

The only caveat I am aware of is overfunding the escrow account with Algos, as anyone may send an “opt-in” transaction which could consume all Algos upto the minimum balance requirement. Either party may send Algos to the escrow along with their HTLC transaction to pay for fees just-in-time, so the contract cannot be halted.

Eligible account to receive TEAL Badge:

FT43JWVPZQ4U3M5BL5DFENHG3LXWXD7Q34YZP6SWWQCEB7OBREMDM3RKC4
3 Likes