What are Atomic Swaps?
An Atomic Swap is a series of blockchain transactions that allows two parties to perform trustless cross-chain trading across blockchains.
For example, if Alice would like to trade 20 ETH in return for 1 BTC, and the counterparty, Bob, would like to receive 20 ETH in return for 1 BTC, Alice will have to choose between one of two options:
Alice would have to first send Bob 20 ETH and trust that Bob will send her 1 BTC back
Alice and Bob would have to trust a third-party to conduct the trade on their behalf
With Atomic Swaps, a cross-chain trade can be completed without the need for a third-party and without any risk of loss due to the counterparty not honoring the deal. To make an Atomic Swap trade, the users would proceed as follows:
Alice sends an ETH blockchain transaction with the instructions, “Lock 20 ETH from my account for Bob, with the following password hash:
After waiting for Alice’s transaction to appear on the blockchain, Bob sends a BTC transaction using the same password hash, the instruction reads as “Lock 1 BTC from my account for Alice, with the following password hash:
Alice sees that Bob has initiated a transaction, and claims her 1 BTC, using the secret password (gtMcn7XGlIbq) known only to her. Alice claims the BTC with the blockchain transaction: “Claim 1 BTC from Bob, the secret password is (gtMcn7XGlIbq) for the password hash:
The password is verified on the BTC blockchain to match the password hash, and the funds are transferred to Alice.
Bob sees the secret password from Alice’s blockchain transaction. He can then make the claim of 20 ETH from Alice by sending a blockchain transaction with the following instructions: “Claim 20 ETH from Alice, the secret password is (gtMcn7XGlIbq) for the password hash:
The password is verified on the ETH blockchain to match the password hash, and the funds are transferred to Bob.
Atomic Swaps on Switcheo Exchange
While an Atomic Swap allows for trustless cross-chain trading, in practice, it is troublesome due to both parties (the person who initiates the cross-chain trade and the counterparty) having to closely monitor the blockchain and send their transactions at the appropriate times.
Through the combination of atomic swaps and the use of an algorithmic market maker, Switcheo is able to provide users with a seamless trading experience, by simplifying the steps to make a cross-chain trade.
In the example that was used above, the algorithmic market maker represents Alice, and Bob represents a Switcheo User.
If the trading pair selected is SWTH / ETH, then the user will specify how many SWTH he would like to trade, and Switcheo’s algorithmic market maker will determine the amount of ETH that should be used to match the trade. The proposed ETH amount is then shown to the user, and the user can confirm the order if the amount is acceptable.
What does it mean to select the option, “Skip Waiting”?
When an order is submitted, Switcheo Exchange will send the first Atomic Swap transaction (see step 1 above) to promise funds to the user. While this transaction is immediately sent, it takes some time for it to appear on blockchain explorers.
By choosing to Skip Waiting, the user trusts that Switcheo has confirmed that this transaction was sent, and proceeds to send their Atomic Swap transaction (see step 2 above).
Algorithmic Market Maker Pricing Calculations
To decide on how much ETH should be asked for or given for an amount of SWTH, Switcheo uses the Constant Product market making formula:
SWTH_To_Give = SWTH_Balance - (SWTH_Balance * ETH_Balance) / (ETH_Balance + ETH_Received)
For example, if the algorithmic market maker has 100 ETH and 3.6 million SWTH, and if a user wants to buy SWTH using 1 ETH:
SWTH_To_Give = 3.6 million - 360 million / (100 + 1) = 35643.56436 SWTH
As the algorithmic market maker allocates more SWTH to users, the formula will result in less SWTH being given for a similar amount of ETH, resulting in the price of SWTH rising against ETH. On the other hand, if the algorithmic market maker gives more ETH to users, then the price of ETH will increase against SWTH. This provides fair pricing for users by allowing the price to be determined by the relative market demand of each asset.