AtomicDEX - crypto exchange without the participation of a third party


AtomicDEX - crypto exchange without the participation of a third party
14.03.2023 12:12

To better understand what's special about AtomicDEX and the underlying Atomic Swap technology, let's first look at how other types of exchanges work.

With a centralized exchange (CEX), the user fully trusts his coins to a third party and in return receives IOU tokens ("I Owe You" or the obligation to return the money owed). These IOUs exist and are exchanged only within one exchange, and the user can only hope that the exchange will withdraw real coins into his wallet in the future. The risks here are obvious, and there are dozens of points of failure - from scam by exchange owners and hacks of exchange wallets, to hacking your account or freezing funds because of KYC, AML, sanctions, etc.

The issues of classic CEXs have been partially solved in DEXs that use Gateways to exchange real coins for IOUs. Examples of such exchanges are BitShares, Binance DEX, Waves. The difference from CEX is that IOU exchange occurs in decentralized blockchain (or conditionally decentralized :)), that is, the exchange has no control over the user's account. But the main disadvantage of CEX remains here - the user still trusts the real coins to a third party, and IOUs can fall in price if the gateway that issued them behaves badly. The most known example is scam, namely the requirement to pass KYC, by CryptoBridge exchange (BitShares gateway) - IOU tokens like bridgeBTC, bridgeETH depreciated very quickly, and it became simply impossible to exchange them to IOUs of other gateways. As a result, withdrawals could only be made after passing KYC, and the gateway itself disappeared in just a couple of months.

The next type of DEX is "everyone's" favorite DeFi swaps like Uniswap or Pancakeswap and their aggregators - Matcha, 1inch, etc. In contrast to the classic asks/bids, AMM (Automated Market Maker) systems are used here, that is, the price of token is determined by the balance of liquidity in the trading pair. Hence there are new risks - remove liquidity (called "rug pull"), and tokens can no longer be sold. In addition, big number of tokens in DeFi are supposedly "wrapped" versions of real coins, that is essentially the same IOUs, the company issuing the token dies - the token dies too. I wrote about the bad smelling DeFi in detail in this article Another pitfall of DeFi is that the whole system is based on smart contracts, but not transaction features, which in turn leads to protocol hacks. Hacking "decentralized" - how about that, Elon?

Another type of DEX removes IOUs altogether, but in trades there is a third party as an arbitrator if something went wrong. The arbitrator is chosen in a decentralized way, examples of such exchanges are Bisq (even fiat exchange is possible), BTDEX.

What if we remove IOUs and the third party shifting the arbitrage to blockchain? And that is where Atomic Swap technology comes in. It has been known for a very long time, I tried it myself about four years ago, but until now there was no full-fledged functional interface to use it in simple way. Atomic Swap is based on the Hash Time Lock Contract (HTLC) - a simple one-time contract in blockchains of exchanged currencies. It has only two functions - either to give the coins to the recipient if he provide a secret hash, or to give the coins back to the sender after a lock time - hence the name "Hash Time Lock".

How does trade works on AtomicDEX?

1) Market Maker places a trade order on AtomicDEX. For example, UFO/LTC pair, selling 40000 UFO at 0.000025 LTC per coin.

2) Taker accepts the offer and pays 0.15% of the total order amount in LTC in favor of AtomicDEX. Taker can also buy not the whole order, but only a part of it.

3) This is where the magic begins - the atomic swap process started up.

Maker sends a Maker Payment in the UFO blockchain to a one-time P2SH address (also for any Bitcoin based coins) or to a smart contract address (for ETH/BNB/MATIC and ERC-20 tokens). Maker Payment is locked by a hash of a secret that the maker generates randomly. Only maker knows the secret, but the hash of that secret is public. The taker can verify that the blocked coins in the contract are sent to him, but he can't spend the funds yet until he knows the secret.

The return of funds to the maker is time-locked. If the taker has not fulfilled his obligations, the Maker Payment will be returned back to the maker after the locking time (4 hours 20 minutes)

4) Taker sees that the maker sent the payment and checks it, then sends Taker Payment to the secure P2SH address, but in the LTC blockchain. Taker Payment is locked by the same hash that the maker used to lock his payment. Taker can use the hash to block the funds, even though he still does not know the secret that the maker used to generate the hash.

The return of funds to the taker is also time-locked, but with a time period equal to half of the one used to lock the Maker Payment. This allows the taker to return his coins if the maker, for whatever reason, does not move on to the next exchange step.

5) Maker sees that taker sent the payment, which is locked by the hash of the secret that the maker generated in step 3. To unlock the funds, maker signs a transaction (Taker Payment Spent), in which he also reveals his secret code, without which it is impossible to spend the locked coins of the taker. As soon as this transaction is broadcasted into the blockchain, the maker's secret becomes publicly available and the maker receives the LTC.

6) Taker sees the maker's transaction and it contains a secret. He can now use the secret to spend the UFOs locked by the maker in step 3 to his advantage (Maker Payment Spent). As a reminder, Maker Payment has double locking time than Taker Payment, meaning that the maker won't be able to get both the Taker Payment and the coins back from the HTLC contract at the same time.

Every step of the atomic swap is verified by both parties, AtomicDEX simply does it automatically. There are no third party, just an elaborate protocol that uses cryptographic techniques to ensure that neither party can cheat. Either the swap happens exactly as described above, or the funds are returned back to the parties of the swap.

Most importantly, during the whole process the coins are held by private key owners and are not transferred to third parties in any way, because the protocol is based on bitcoin transaction features Hash Lock and Time Lock. Yes, in the case of swap for Ethereum based coins, you would have to trust the smart contract already deployed in the blockchain, because Ethereum apparently does not have the above features in transactions. The smart contract is simple and describes the same Hash Lock and Time Lock functions

One of the big advantages of AtomicDEX is that the system is completely open. It's easy to add a coin or token, simply by creating a pull request on GitHub, and the coin you want will appear in the next release.

And now about the inevitable downsides:

1) All swaps, of course, require fees in both blockchains in which the swap is made.

2) Execution time of the swap depends directly on the block time in both blockchains, as we know for BTC - it is on average 10 minutes, that is, the swap with bitcoin can take even an hour.

3) Your wallet must be online to make swaps.

4) Despite the existence of atomic swap protocol for Monero - the essence is the same, but "under the hood" everything is different, AtomicDEX does not include it because of the lack of Hash Lock and Time Lock in XMR transactions.

5) The project is still in beta, so bugs and other joys are inevitable.

But all this is nothing compared to the freedom from third parties and not having to trust anyone when using your own bitcoins)

Post earned 0.00 UFO