Smart contracts have revolutionized the way we think about transactions in digital space. They allowed decentralized networks to exist without the involvement of third parties. Despite the original Nich Szhabo’s core ideas being somewhat lost in the mainstream, “smart contracts” work and do well. Among the most popular types of smart contracts is the Hash Time Locked Contract (HTLC). This contract allows parties to exchange funds or assets without needing a trusted third party. Instead, the terms of the contract are enforced automatically through programmable code. The hash time-locked contract is beneficial when parties need to exchange assets simultaneously or in a very short period.
HTLC was initially called “atomic transfer” and was later named “atomic swap.” Even today, such services like boltz.exchange still use exactly this definition, and it will remain with us forever because atomic swaps do not just provide the exchange with short-living “digital assets” but also function for liquidity transfers between Bitcoin main chain and Lightning Network or Liquid sidechain.
The “atomic transfer” was first introduced by Tier Nolan on the BitcoinTalk forums in 2013. Nolan described the basic principles for cross-chain cryptocurrency swaps by using simple cryptocurrency trades across different types of blockchains. He pointed to possible Bitcoin Scripts that could facilitate proposed transactions. The idea wasn’t practically implemented until September 2017, when Litecoin founder Charlie Lee announced the successful execution of an atomic swap between Litecoin and Bitcoin over Twitter. Litecoin is an “altcoin” and inherits from Bitcoin all architectural features like Unspent Transaction Outputs and tree-like transaction structures with Bitcoin Scripts executing on their branches. That means implementation of Atomic Swaps between two similar chains may be considered relatively simple and straightforward, therefore secure.
Atomic swaps haven’t received considerable attention, and between 2013 and 2017, during the era of altcoins, which were primarily forks of the Bitcoin Core codebase, centralized exchanges mainly facilitated trading functions for exchanging cryptocurrencies and tokens. With the introduction of Taproot, so-called coin swaps came instead of atomic swaps with fairly special transaction patterns (the reason they are considered less private). However, the total dominance of centralized exchanges over ways of moving and exchanging value in a decentralized fashion couldn’t lower the importance of HTLC contracts.
A cornerstone of the Lightning Network
Hashed time lock contracts enable the existence of payment channels, which are more general concepts enabling the transfer of value via payment routing.
First of all, payment channels enable direct atomic payment between two neighbors that share a channel. This direct atomic payment settles around payment hash and payment preimage such that
payment hash = sha256(payment preimage).
Lightning network payment may fail with a timeout, and the failure will enable an alternative “branch” of the HTLC related to lock time. Therefore, through the channel, HTLC payment either goes or not. This is why it is called direct atomic payment.
Secondly, on the higher level, nodes build a network of channels, and payment in the network resembles the situation of indirect payments with correspondent banking accounts. The payment settles as a result of the execution of chained HTLCs between several peers. The contract itself allows enforcing onchain settlement if there is no coordination between peers and they can’t agree on amounts on each other’s side of the channel.
Why the Most?
In Ethereum, HTLC implementation exists and can be searched through Etherscan. Search results for a basic contract HashedTimelockERC20.sol deliver around 300000 records; however, HTLC is a concept that is unlikely to become popular among Ethereum projects. This contract is oversimplified for most use cases in Ethereum or may be incorporated or even replaced with just Solidity smart-contract in a very general form.
However, are the Ethereum smart contracts the only meaningful indicator here? Let us consider the Lightning network, which currently has 74261 public channels between 17994 nodes (https://amboss.space/). These numbers do not include mobile wallets and privacy-cautious operators, which may use only unannounced channels that become visible only when closing.
According to the protocol specification, each channel can have 483 “in-flight” HTLC. That means if the number of pending payments equals that amount, the channel may be considered “frozen.” It couldn’t route any new payments until some pending HTLC resolves with a known preimage or timeout. 74261 active channels may have 74261*483 = 35868063 or approximately 36 million pending contracts could be enforced by Bitcoin Proof-of-Work at any time.
Of course, many limitations make this number only a theoretical limit of operations. The point is that any share we could take as a “safe margin” makes the Lightning network very competitive compared to legacy financial payment systems and HTLC if compared to any contract running on any possible smart-contract platform.
Tips are welcome firstname.lastname@example.org.