Atomic swaps, or atomic cross-chain trading, are cryptographic protocols that allow two parties to exchange their coins in a trustless transaction through the utilization of smart contracts.
Let’s look at a generic example to illustrate how all of this might work:
Suppose Alice has 1 BTC and wants to trade it for 100 LTC, whereas Bob has 100 LTC and wants to trade them for 1 BTC.
In normal circumstances, Alice and Bob would have to deposit their coins to a centralized exchange prior to trading them. Since Alice and Bob have heard about exchange platform going bust after being hacked, they are concerned by the deposit process and would rather avoid it. With atomic swaps, however, it is possible for Alice and Bob to trade without counterparty risk because the exchange platform never has the custody of the coins. The coins are simply escrowed in smart contracts, one paying Alice, the other paying Bob. The contracts are coded in such a way that:
1) Either both contracts execute the expected payments or none of the payments happen;
2) If none of the payments are made, each party can recover its funds after the expiry date set in the contract.
There are different methods for how atomic swaps can be used and implemented. In this article, we outline how users of Blockchain.io exchange platform will be able to utilize atomic swaps for decentralized settlement of their trade orders.
Blockchain.io: Atomic Swaps and Decentralized Settlements
At Blockchain.io, we are using a combination of centralized and decentralized features to make atomic swaps simpler, easier, and more efficient to use.
● Blockchain.io’s order book will be centralized to ensure high liquidity.
● With the option available to trade with a decentralized settlement, this provides a secure and trustless option for users that trade on our exchange.
The coins that are traded under the decentralized settlement option are never deposited into the custody of Blockchain.io.
Instead, they are securely deposited and locked in an escrow smart contract for a given period of time, during which, the tokens can be freely traded off-chain. When users have completed their trading session, they can withdraw from the smart contract the amount of coins owed to them.
Let’s take a closer look at how this process works, breaking it down step-by-step:
Trading Using Blockchain.io’s Decentralized Settlement Feature
1. Users have the option to select “Decentralized Settlement” before beginning to trade on our platform. This option provides a trustless and safe trading environment for users as they always maintain control and custody over their funds at all times, both during and after trading.
2. Upon selecting “Decentralized Settlement”, the user will be prompted for their public key and to send coins to a timelock escrow smart contract address.
● The escrow address will contain the hash of a script with two primary features:
1. Before the expiration of the contract, funds can only be sent when both signatures have been signed, the user’s signature with the other being Blockchain.io;
2. After expiration time of the contract, the user will be the only entity that can recover the funds using their signature; and
3. Once the blockchain has confirmed that funds have been deposited into the escrow address, the user can begin trading on the Blockchain.io centralized order book. All trades conducted on the exchange are done through the centralized order book to provide more liquidity by merging all orders (both custodial and non-custodial orders).
● The decentralized trading protocol is based on Request For Quote (RFQ) trading. Let’s say Alice wishes to trade 1 BTC for LTC, the platform would respond with a quote for the equivalent amount of LTC she is able to receive for her 1 BTC (We assume that the going rate for 100 LTC is 1 BTC). The quote will also mention its period of validity, could be 3 minutes or longer.
● Once Alice has accepted the quote and deposited the required amount in the escrow address, she will be able to trade on the centralized order book, up to the amount of the escrow, with all trades being conducted off-chain through the Blockchain.io trading hub.
4. When the escrow smart contract lapses 24 hours, the settlement of all trades conducted on BCIO’s platform will take place on-chain, using the final state of the escrow contract to determine the amounts owed to each party. This way, only one on-chain transaction is completed every 24 hours for the unlimited amount of off-chain trades that can be conducted on our platform.
● BCIO countersigns and broadcasts the cash out transactions to complete and close the process, taking into account all the trades that have been executed by the user.
● If there are no trades, the user can simply recover her funds after the expiry of the escrow, using her private key.
Decentralized settlement represents a big departure from traditional markets and is a major step for the Internet of Value. Blockchain.io offers a new atomic swap protocol, inspired by Tumblebit and optimized for real-time trading purposes.
The settlement consists of the following two operations:
● Escrow Transaction (escrow tx); and
● Cashout Transaction (cashout tx).
Escrow tx is the first transaction that the user performs to begin trading on the exchange, whilst cashout tx settles on chain the final state of trades executed on the platform.
The mechanics of how the atomic swap protocol works on Blockchain.io is similar to how the Lightning Network functions. Hashed timelock contracts are utilized to open up payment channels between parties. Within this payment channel, participants can conduct an unlimited number of transactions. When the specified time period is over, the final state of all transactions recorded is executed on-chain.
Similarly, during an atomic swap, users can trade as much as they want on the exchange during the given time limit for the escrow address. Once the time limit expires, the final state is taken into account to settle the trades on-chain, through the cash-out transaction. The settlement is the final amount of coins that a user ends up with, post trading.
What are the advantages of this system?
● Firstly, by trading in a centralized environment, it makes the whole process extremely time-efficient and also provides far greater liquidity than a fully decentralized exchange.
● Secondly, the protocol designed specifically for trading and offered by Blockchain.io avoids known issues that affect fully decentralized exchanges, such as front-running and grieving.
BCIO also has several anti-failure mechanisms in place to ensure the system doesn’t fail in the event of downtime or say a malicious attack, to ensure user’s assets are always protected. Let’s briefly consider some of the anti-failure mechanisms that are in place:
● Multiple, sophisticated, autonomous monitoring systems will trigger automatic alerts early if there are any inconsistencies in the order book or suspicious behavior being conducted.
● Users are able to cash out what they are owed from escrow smart contract addresses at any time.
● Only signed transactions are sent and executed by the decentralized trading protocol. This makes it impossible for hackers to invalidate someone’s signature and hence, modify the withdrawal destination addresses.
Blockchain.io’s decentralized settlement feature is designed to ensure that users are in complete control of their funds at all times. With this in mind, the control that comes with this feature means there are extra responsibilities. This is similar to any cryptocurrency holder who holds tokens in their own personal address; if they lose their private key, it will be difficult for them to recover their funds.
In our case, however, this issue isn’t as severe as users are responsible for closing and withdrawing their funds from escrow smart contracts in a timely manner. Users will be incentivized through fees to close their exchange escrows when they have finished trading. The client software run by users will also automatically close any open exchange escrows before the expiration time.