Atomic Swaps
Last updated
Last updated
This is intended to be read by people who understands basic computer science and data structure so that they can make their own. If you do not have backgrounds, learn data structure and or take a legitimate computer science course.
Atomic swaps are automated, self-enforcing cryptocurrency exchange contracts that allow cryptocurrencies to be traded peer-to-peer without the need for a trusted third party. Standard Exchange implemented an automated atomic swaps storing them as orders in a set of smart contracts. In each order of Standard Exchange, it includes the demand of wanting base token in trade of quote token and vice versa. If there is buy and sell order with same price, order is matched, and the atomic swap is executed. To store atomic swap agreements in the most efficient way for frequent insertion and deletion, a new data structure is proposed.
In Standard Exchange, automated atomic swaps are facilitated through a system consisting of two linked list matrices for buy and sell orders, along with an order storage mechanism. These components work together within the orderbook smart contract to manage the different states of orders and efficiently segregate users' deposits. The linked list matrices store the indexes of each order, enabling efficient organization and retrieval of order information. The order storage mechanism optimizes the format in which atomic swaps are stored, ensuring the secure and streamlined management of user deposits. This infrastructure allows for smooth and secure automated atomic swaps on the Standard Exchange platform.
To efficiently store orders in the Standard Exchange system, regular developers might consider using arrays as queues. However, Solidity arrays are static and require significant storage cost upfront to initialize spaces for storing orders. Moreover, a large portion of these preoccupied spaces becomes obsolete as buy and sell orders are matched and deleted. To address this storage cost issue, Standard Exchange proposes a solution using a two-dimensional ordered dynamic linked list. This approach organizes prices column-wise and order deposits row-wise. Liquidity availability on each row is calculated by summing the deposits from atomic swaps. Each column contains the order ID, allowing easy navigation to order information.
While some developers might suggest using AI to predict prices and initialize order arrays, or resorting to off-chain clients, these approaches deviate from the decentralized nature of Standard Exchange and atomic swaps. They would transform the system into an unregistered security exchange with dark pools or wholesalers in the dark.
In order to provide a fair, verifiable, and intermediary-free execution of orders in a peer-to-peer trading environment, Standard Exchange remains committed to conducting all trades fully on the blockchain with reasonable fees. This ensures the most transparent and secure execution of orders while maintaining the principles of decentralization and atomic swaps.
In Standard Exchange, an order serves as the fundamental unit of atomic swaps. The design of orders in Standard Exchange prioritizes cost-saving on iterations. Each order is assigned a unique ID for identification purposes. The order itself contains essential information such as the owner's address and the deposit amount. To prevent duplicate records and optimize storage, the price and base/quote information associated with each order are stored in a linked list matrix, with the order ID acting as a reference. This approach ensures efficient organization and retrieval of order details while avoiding unnecessary data duplication.
The order matching algorithm in Standard Exchange follows a transparent and non-dark pool approach, divided into two distinct processes:
When a user initiates a buy or sell transaction, the deposit of the base or quote token is compared with existing stored orders. If the deposit is in the quote token, the order is classified as a buy order. Conversely, if the deposit is in the base token, the order is classified as a sell order. For sell orders, they are first matched with existing buy orders. The order matching process involves traversing the existing atomic swaps, starting from the opposite head price. After each order is matched, bid or gas refunds may occur. To reduce gas costs for order matching, users have the option to manually set the number of orders to be matched.
If the stored orders are unable to match the user's desired price or the matching limit is reached, the user has the option to make an order to fulfill their requirements. In a limit order, the user's order is stored in the order storage along with their specified limit price. On the other hand, in a market order, the user's order is stored in the order storage using the last matched price after executing take orders.
By following these two processes, Standard Exchange ensures a fair and transparent order matching mechanism without resorting to dark pools. Users can participate in the market by executing existing atomic swaps and store new orders on the orderbook according to their specific requirements and desired prices.