DeFiChain: The World's First Decentralized Trading with Bitcoin
One of the most anticipated innovations by our users is the introduction of the Interchain Exchange (ICX) as well as Atomic Swaps, which enable a truly decentralized exchange from the DeFiChain ecosystem into the Bitcoin ecosystem. In the following article we’ll give you an overview of the design goals and constraints behind each concept, the building blocks behind them and how each would function on DeFiChain.
Design Goals & Constraints
The key design goal when it comes to implementing and operating an ICX and Atomic Swaps on DeFiChain, is to allow users to seamlessly and trustlessly swap between DST (DeFiChain Standard Token) and BTC on the Bitcoin Blockchain. The focus here lies on the following three design principles exactly in the order given below:
- User experience and
Firstly, DeFiChain does not want to compromise security and won’t allow one party to lose their tokens while another receives them. Ultimately, the token swap has to be atomic, meaning that both parties have to receive what they initially agreed upon, or neither parties get anything.
Secondly, where DeFiChain really has a trump card up its sleeve compared to its competitors is when it comes to the user experience. The focus hereby lies on a simple-to-use interface that allows anyone to carry out swaps intuitively with just a few clicks.
The last integral component is seamlessness. The entire process is designed to be automatic and natural so that everybody is receiving what was initially agreed upon. The most important factor here is, once you get the swap going, you don’t have to think about the underlying process anymore — it’s enough to have an intention to swap and from which the outcome will naturally and automatically evolve and complete.
Before we delve into the main content of this article, we’d like to lay the cryptographic foundation in order to better understand how ICX and Atomic Swaps work. As part of this, we would like to introduce you to the following three key cryptographic principles:
- Hash and hash function
- Hash time lock contract
- SPV Wallet
Hash & Hash Function
In cryptography, the word “hash” refers to the output produced by a hash function after a piece of data is submitted through it. In order to better understand what a hash is, we first must consider the concept of a hash function. Hash functions are mathematical algorithms which convert an input value of any size into an output (or hash) of fixed size.
Different cryptocurrencies use different hash functions. Bitcoin, for instance, uses SHA256 hash function, which maps data of any size into a hexadecimal output (hash) of a fixed size. Do note that this output hash value is always the same for the same input. Consequently, it doesn’t matter who is hashing the input or how often it’s happening — the output doesn’t change. On the other hand though, any minor change to the input data will cause the hash function to return a completely different output (hash).
Hash Time Lock Contract
The term Hash Time Lock Contract (HTLC) refers to a feature used in certain smart contracts and allows for the modification of a payment channel. Adding the HTLC feature enables the implementation of time-bound transactions between two users. Practically speaking, the recipient of a HTLC transaction has a pre-set and ex ante specified timeframe in which they have to accept the payment by submitting a secret to the hash — which is essentially just a form of cryptographic proof.
The two most important components of the HTLC are the Hashlock and the Timelock. A Hashlock refers to a function that restricts the spending of locked-away funds until the cryptographic proof occurs. On the other hand, though, a Timelock restricts the spending of those funds until a certain block height in the future.
If the Timelock doesn’t get triggered, then the funds will be returned to the original sender. So both parties actually get a money-back guarantee in case prerequisites aren’t met.
SPV stands for Simplified Payment Verification and is a light Bitcoin node which runs alongside the DeFiChain node. Currently, it is used for anchor validation and to allow the node to trustlessly verify the anchor on the Bitcoin blockchain. Simply put, it’s the counterpart of running a full node, without sacrificing any layers of security.
Most importantly — since DeFiChain shares the same root as Bitcoin — the extended private key you use on DeFiChain can be the same key which you also use on Bitcoin. Hence, you can use the very same DeFiChain seed phrase to unlock the Bitcoins on your DeFiChain wallet. In the end the SPV wallet, even though it’s light and trustless, is no less insecure to the end user than operating a full node.
Atomic Swaps are smart contracts which facilitate the trustless and decentralized exchange of cryptocurrencies between two distinct blockchains without having to place reliance on any intermediaries.
DeFiChain users are then able to exchange bitcoin, sitting on the DeFiChain blockchain in the form of DBTC (DeFi Asset Token backed by bitcoin), for bitcoins, sitting on the Bitcoin blockchain, without relying on any middlemen or centralized institution. Instead, users will be able to intuitively transfer coins to and from the Bitcoin blockchain directly in the DeFiChain app. This opens up the possibility of allowing all assets on DeFiChain to be easily moved into and out of the DeFiChain blockchain and the Bitcoin blockchain with high liquidity.
Here’s a short example on how Atomic Swaps on DeFiChain work.
Let’s assume Alice and Bob want to carry out an atomic swap trade over 1 BTC. Alice then sends her 1 BTC using HTLC to Bob, whereas Bob is not provided with the secret to unlock it — at least not yet. But at the same time Bob holds a guarantee which states that if he had the secret, he would be able to unlock it. Alice, on the other hand, has a money-back guarantee in case Bob fails to carry out his part of the deal.
In the next step, Bob sends the payment to Alice with the same Hash used by Alice and Alice redeems Bob’s payment by revealing the secret. The secret has to be published publicly so that Bob is then also able to claim his payment by using the same secret. Since both parties have received what they were intended to receive, without someone betraying the other, Alice and Bob have successfully carried out their first atomic swap deal.
Interchain Exchange (ICX) is a new term coined by DeFiChain for its state-of-the-art trustless order book exchange on the DeFiChain blockchain. It’s different from the decentralized exchange (DEX) — which most of you are already familiar with — in a way that users are able to enter and exit the DeFiChain ecosystem. You can think about it almost like “on ramping” or entering into the DeFiChain ecosystem vs. “off ramping” or exiting DeFiChain into the Bitcoin ecosystem.
In contrast to the DEX’s liquidity providing an automated market maker feature, the ICX is designed like other traditional exchanges that you might know, whereby you can make a bid and ask for any asset. As the name implies, the ICX deals with an exchange of assets on two distinct blockchains. In our case, it’s the DFI token on the DeFiChain blockchain and BTC on the Bitcoin blockchain.
Due to its non-Turing complete blockchain architecture, the ICX will be implemented as a base blockchain consensus and not as a dApp. This means that every single trade is broadcasted to and validated by all nodes, which is also a reason the synchronization of the App might sometimes take a little longer.
The main goals being addressed with the ICX are the matchmaking between buyers and sellers, and the facilitation of price discovery. After buyers and sellers have been matched, the ICX facilitates the creation of a so-called Interchain Swap Contract between the two parties.
Another important aspect to fully understand matchmaking is to be aware of the following market participants. On one hand, we have buyers and sellers, and on the other hand we have makers and takers. In the context of Atomic Swaps and the ICX, buyers and sellers are simply users who want to buy and sell (i.e. Bitcoin). A maker however, is someone who submits a bid or ask order to an order book, whereas a taker takes an order from the order book. The easiest way to see this is if you’d like to sell Bitcoins into the market, then you are a maker, but if you see an order in the order book — which you’d like to ‘buy’ — then you are a taker.
In the following example we’ll explain the process through which Alice, as a maker, wants to exit DeFiChain by putting up an order onto the ICX. In other words, she wants to sell her DST (DeFiChain Standard Token) for BTC on the Bitcoin blockchain. Bob on the other hand, is the taker, and wants to enter into DeFiChain by buying DST with BTC. Please note that a DST can be any token on the DeFiChain blockchain.
The whole coin swap process can be broken down into six steps (Figure 1):
- Order Creation
Firstly, Alice as the maker has to put up the following five inputs to be able to sell her 10 Tesla DSTs for BTC (Tesla is just an example of a future DST; see Figure 2 and Figure 3):
- For sale: 10 TSLA DSTs
- Unit price: 0.02 (BTC - $800 each for BTC at $40k)
- Expiry: 2880 blocks
- Option fee per unit: 8 DFI
- BTC address
As the first input variable, Alice has to name the unit price she wants to sell her Tesla stocks (Tesla/BTC) for — in our example it is 0.02 (US$ 800 / US$ 40,000). In order to protect herself from excessive price fluctuations and from the market potentially turning against her, she has to specify an expiry date of her swap as well. In the next step, Alice sets an option fee to prevent any party (maker or taker) from getting into a trade and backing out of it later without being penalized for it. Think about it as a guarantee fee — the price Bob has to (initially) pay to get into the trade, but once the trade goes through, he will get the option fee back. Lastly, Alice has to name her BTC address where she wants to receive her BTC. Please note that nothing has been locked so far — Alice just entered the trading order.
2. Acceptance Offer
It can now take some time until a taker wants to buy just a few or all of Alice’s Tesla DST. Let’s assume Bob now wants to buy just 2.5 TSLA DSTs. He would then make an acceptance offer to indicate that he actually wants to buy Alice’s DSTs along with specifying a receiving address in order to be able to get the Tesla DSTs ‘delivered’. Lastly, Bob also has to pay the option fee of 8 * 2.5 = 20 DFI along with his offered price as an indicator that he is honest and wants to go through with the deal.
3. Acceptance of the Offer
Alice now accepts the offer and generates a secret ‘S’ while simultaneously calculating the hash of this secret by using H = Hash (S). In the next step Alice moves 2.5 TSLA DSTs from her selling order into an Interchain Swap Contract (SC) which is set to expire in 3 hours. This SC is similar to a HTLC in the way it functions, but it deals with a token rather than the coin itself. Bob now has 3 hours to fulfill his part of the deal. If he doesn’t then he will lose his locked away option fee and the whole deal falls apart.
4. Verification Process of the Taker
Bob now sees that Alice has accepted his initial ‘’acceptance’ offer. He now verifies that the amount (= 2.5), the token (= TSLA DST), the recipient (= address where Alice locks the tokens), and the time locked (= 3 hours) is accurate. Even though no notary or centralized institution is needed to check if the deal is done correctly, Bob is waiting for sufficient blockchain confirmations to ensure finality in order to have a high degree of certainty that the contract isn’t forked out and eventually becomes non-existent. After Bob has waited sufficient time, he now fulfills his part of the deal and locks away 2.5 * 0.02 = 0.05 BTC to a HTLC using the same hash ‘H’ Alice has used before. The key thing here is that Bob’s SC contract has to expire before Alice’s SC contract.
5. Acceptance Process of the Maker
Now it’s time for Alice to confirm the accuracy of Bob’s HTLC by checking the amount, time and the recipient. Alice now also waits for sufficient confirmations to ensure finality. In the last and most important step, Alice claims the Bitcoins by revealing the secret ‘S’, such that H = Hash(S).
6. Acceptance Process of Taker
Bob now sees that Alice has claimed his Bitcoin and also revealed the secret ‘S’. Bob now also claims the TSLA DSTs by using the same secret ‘S’, because it’s the same hash in the end. Once the TSLA DSTs have been claimed from the Swap Contract, Bob also gets his option fee refunded.
The above swap has occurred under perfect conditions. But what will happen if one of the participants plays foul? Let’s consider that by focusing on the following two examples:
- Let’s assume Alice doesn’t accept Bob’s offer due to any reason, then the offer is short-lived and Bob will get his option fee returned. In the end no-one loses anything except his time.
- Let’s assume Bob doesn’t honor his BTC side of the deal, then Alice gets her TSLA DSTs back after the timeout. Alice has lost valuable time for locking in her TSLA and is ultimately compensated with the option fee, which Bob is now forfeiting for his bad behavior.
Yet the most important aspect of a well functioning ICX is a healthy and liquid order book. It is essential that a DBTC into BTC transfer or vice versa is carried out in due time and without spending time on price discovery. Hence, a DFIP will be put up, in which the masternode holders will be able to vote for an incentivized both sides of the trade. As a result, the user shall be incentivized to create orders for 1:1 swap between BTC DST and Bitcoin BTC. The whole concept is quite similar to liquidity mining, just that it happens on the ICX and not on the DEX, and specifically only for parity BTC DST - BTC orders. Yet keep in mind, the ICX isn’t automatically matching the orders — you have to be a taker or a maker in order to do so.
The whole concept of an ICX and Atomic Swaps on DeFiChain works without relying on any counterparty, escrow or notary (node). Consequently, DeFiChain users do not have to trust anyone — it’s 100% trustless. Users can also anticipate a seamless user experience when interacting with the newly designed app. All technical concepts outlined above will be hidden behind a visually appealing interface where it is easy to carry out any swap from any DST to BTC. Do expect the introduction of the ICX with the next upcoming update, the BTC SPV may even get introduced even earlier.
To stay up to date with the latest information and news, feel free to follow us on Twitter or join our Telegram group.
Your DeFiChain Team