0x is an important infrastructure for developing crypto economics and allows you to create markets that it did not have to be. As the larger number of assets are token, public blocks provide the ability to create a new financial stack, which will be more efficient, transparent and fair than any other system in the past.
In this guide, we compare trade in centralized cryptocurrency exchanges (for example, BINANCE) and 0x marketing, a decentralized exchange protocol built on Ethereum.
High level primer
All centralized stock exchanges perform three main activities:
- Storage of users of users
- Placement and distribution of orders
- Calculations on transactions
In the protocol 0x, these tasks are performed differently.
- Storage of users of users
Trading on CEX (centralized stock exchange) requires you to deposure your assets in Ethereum, which corresponds to the control of exchange. After that, they fully control your funds and must protect them from hackers, fraudsters and other intruders.
When trading on 0x, your assets should never leave your Ethereum address until the transaction is settled.
- Placement and distribution of orders
0x orders can be placed by anyone, not the only participant placing orders. Any organization that places and distributes 0x orders is called "Repeater". Currently, most repeaters are centralized objects on which the API is located from which orders can be received.
In addition to the relay hosting, the distribution can also be achieved through Mesh, a peer-to-one-based network created specifically to share 0x orders. For more information, see. 0x.Org / mesh .
Witch 0x is a data packet that contains the order details along with the actual signature (for example, a valid signature of an elliptical curve generated from the address of the trader's Ethereum) of its contents.
- Calculations on transactions
When two participants agree with the terms of the transaction, the order 0x is sent to the Ethereum blockchain and is calculated using the 0x Smart Contract.
So that 0x smart contracts can move funds on behalf of traders, traders must first give contracts explicit permission. This is done by installing "tolerance" for a certain number of assets that 0x can then pass from its address Ethereum.
Then, in case the trader cryptographically agrees to the transaction, creating a warrant with a valid signature, an order can be filled and calculated using smart contracts 0x. All these rules are complied with open source smart contracts deployed in the Ethereum blockchain.
In no case, the 0x protocol does not control the closed key of the Ethereum address of the trader, and the resolution can be withdrawn by the trader at any time.
What about coordination of orders?
Today there are two dominant repeater strategies: comparison and open book of orders. Open book repeaters place orders that anyone can fill, directly sending a transaction (with the order items and the desired fill amount) in the Ethereum blockchain.
Mesh, despite the fact that it is not a centralized repeater, also falls into the category of an open book of orders, since it distributes only orders that can be performed by anyone.
When using this model, the order in which the transactions are filled in is determined by the same algorithm that is used by maneers to solve which transactions include in the next block (more about this later).
Appropriate repeaters require traders to create a warrant, reverse 0x to the one they want to fill out, and sent it back to the comparable. Then the corresponding repeater will perform both orders in one atomic transaction in Ethereum on behalf of the trader.
In both models, the repeater never controls the assets of the trader.
1) Generate an order in proper format (for example, encoding / decoding assetdata)
2) Creating the right hash for the order contents
3) Subscribe to your signature in the form of an elliptical curve
For a set of scripts showing the above steps, as well as admission setting, take a look at 0x Starter Project .
Please note that the order SALT field must be installed on the current UNIX temporary tag in milliseconds for full compatibility with the CancelordersUpto function. For details on how the order is created, refer to the orders in the Protocol Specifications.
Distribution of orders
If you have a valid order 0x, you can send it a relay or to Mesh-network. You can use the end point POST / V3 / ORDER standard repeater API (see. SubmitORDERSYNC in 0x Connect) or JSON-RPC MESH_ADDORDERS MESH method (see. AddOrdersasync in @ 0x / mesh-rpc-client).
If you want to receive notifications whenever the trader fills the order, you must also send an order to an instance of the 0x mesh node that you use.
While placing orders for the repeater or MESH is a "push" distribution model, there is also an alternative model "Pull", known as Request for Quote (RFQ). This feature of the 0x API, which implements not only the Standard Relayer API, but also additional resources for liquidity aggregation both from 0x and from sources other than 0x. For RFQ API 0x configured with a list of market meters, and whenever the trader requests a quotation for the best available prices from all sources, the API is associated with these market meters that can be answered by 0x, representing their "quotation". . "If this order exceeds the prices from other sources, it will be transferred to the API client, which can fill it in the same way as any other order 0x.
Installation of orders
Orders can be filled by calling various methods in the Exchange Contract, as described in the "Filling Order" section of the Protocol Specifications. In general, these methods can be divided into categories:
Filling out one order
These methods are useful to perform one-time orders. All these methods have several different refusal conditions:
These methods are useful for performing multiple orders for one transaction. Each method has different conditions of transaction failures and execution:
Appropriate filling order
A special case exists when filling out both purchase orders and for the sale of the same asset pair, when the price of the order for sale is less or equal to the purchase order. These 2 orders can be simultaneously filled with capital requirements, causing the MATCHORDERS function .
Interaction with different models
Note that the process of filling orders is different when interacting with an open book repeater or a suitable repeater. These orders can be distinguished by viewing SenderaDDress and Takeraddress fields in order .
When both fields have a value of 0 (as in the open book of orders) or when simply sendraddRessenely 0 (as in the RFQ-T model), the order can be fill directly by calling the desired fill function in the Exchange Contract. Berking, filling order in this way, wins from the atomicity of the Ethereum transaction and more detailed control of transaction failures . However, in this scenario, the responsibility for paying gas and the choice of specific orders for execution falls on the buyer.
In the map of the mapping model, first sends a signed order to the comparable (where either order, SenderaDDRESSLO TakeRADDRESS is installed on the address of the comparable). Then the comparable chooses which order (s) to compare with the signed warrant of the tantomic, and fills the orders at the same time (usually by calling the batchfillorkillolders were MATCHORDERS). In this scenario, the comparable is responsible for paying the gas and selection of specific orders for comparison.
There are several ways to cancel orders for 0x, interacting with the Exchange Treaty, each of which is described in the "Cancellation of Orders" section of the Protocol Specifications.
This Cancelordersupto approach is perhaps the least straightforward, but also the most effective for market meker. It allows you to cancel an arbitrary number of orders for a fixed amount of gas. Creating orders in which the SALT field is equal to the current SunIx timestamp in milliseconds, you can cancel all orders that were created at a certain time or earlier, for one Cancelordersupto call (fixed size transaction). Please note that any future orders created by using, SALTKOWLY less than the largest CancelordSupto SALTARGUME to your address will be automatically invalid.
The explicit cancellation of orders always requires a transaction in a chain. However, you can create short-term orders and replace overdue orders without any transactions in the chain. Due to the variability of the inclusion of transactions in blocks, it is not recommended to establish extremely aggressive deadlines for orders for orders. (Experienced Most market meters set orders for about 5 minutes.)
Many repeaters using the matching model also offer "soft" cancellation. In this case, since the comparable is the only address that can fill out the order, the comparable can promise not to fulfill the order without creating any transactions in the chain.
Traders should always know that these orders can still be technically executed by the comparatizer without the permission of the trader and must periodically consider them as "hard".
Obtaining orders outside the network
Since 0x orders are out of the network, they need to come from somewhere. Currently there are two places to receive orders:
2) indirectly via P2P network 0x mesh. Many repeaters share liquidity through the MESH P2P network, which means that all of their 0x orders are available to anyone who has a mesh node. Instead of extracting orders from several Relayer APIs, you can deploy the Mesh node and connect to the aggregated flow of orders.
Trading on Ethereum
When trading on a centralized stock exchange, you must integrate exclusively with the CEX shopping API to know the state of the world (for example, the book of orders, the status of filling / cancellation). When trading on 0x, your sources are the truth are Ethereum blockchain and existing offchain-orders. In this section, we will consider the status of the network that you will need.
Control of Ethereum nodes
To keep track of the Ethereum blockchain, you must interact with the Ethereum node. You can either integrate your market meter with the service of placed nodes, such as Infura.Io, or place your own node. When developing your market meker, you can configure the local test network 0x, providing you with a fake Ethereum node, useful for testing purposes.
Because 0x transactions are calculated by switching on miners to the unit, the calculation is subject to the same probability finality as any other state transition in Ethereum.
Probabilistic finality means that no trading calculation is never final 100%. It becomes exponentially more final with each additional Ethereum unit, which is extracted on top of which it was included.
This means that for a certain period of time, the settlement can still resume. This happens when the block is reorganized, as a result of which the next operating unit becomes "not involved" (t. E. No longer part of the main chain). It happens not often, but this is an important extreme case. 0x mesh can help you cope with all possible status changes related to the orders of 0x (including reversion of the state).
Block time stamp
In Ethereum, the only requirement for the TIMESTAMP block - it should be greater than the time label of the previous block. This means that timestamp The next block can be before or after the current Timing Tag Unix.
Since the 0x protocol checks the expiration of the order, using temporary block labels, the order will be considered to be expired only if there is a temporary block label exceeding the order validity period. You can use the current time as the approximate time of the block, but still there is a possibility that the "expired" order will be filled if the next timestamp of the block will be between the label of the last block time and the current time stamp.
In most blocks, Mainer decides what valid transactions include in the next block, which they will try to get. They are expected to be economically rational agents who will give priority to transactions that pay them the greatest commission, but they have complete freedom of action in ordering transactions.
Suppose the trader sends the transaction of cancellation of the order; There are no guarantees regarding order in which this transaction will be included in the block chain. You can try to encourage miners to include it before, increasing the GasPrice (read: Commission) paid for the transaction, but the same can do any other trader trying to fill out an order (read more about this problem in our review, review and dangers. A series of messages in the blog about the virtual settlement). Because of this race status, we recommend that you use a shorter time of the expiration of your orders, and not rely on the cancellation in the chain. In addition, you can use our Cancelordersupto feature to cancel several orders for one transaction.
As a rule, the EtherEUM transaction is either executed or completely completed with an error. This allows the transactor to update the status of the block chain, knowing that status changes will be canceled if there is no later condition within the same transaction.
All calls for the functions that occur within the same Ethereum transaction are performed consistently and atomically. This allows the operator to take advantage of how transaction failures occur, and cause several functions within the transaction, without risking the fact that only a part of the transaction will be successful. It is very useful for simultaneous arbitration or purchase / consumption of tokens.
Market meters can hedge the risk when the order is executed, occupying opposite positions in the same or correlated assets using centralized or decentralized stock exchanges.
Short Sales, Derivatives and Margin Trading
The following incomplete list of protocols can be used for borrowing, lending, sales in shorts, margin trading and creating synthetic assets or derivatives. It is strongly recommended to use these protocols only with a complete understanding of their mechanisms and risks:
Adoption of orders wins from the atomicity of Ethereum transactions, but the hedging of one of your own orders will always be asynchronous. Since EtherEUM transactions currently have probabilistic completion, market meters must closely monitor the state of their own orders before and after hedging. For example, the following scenario is possible (but unlikely):
1) Marketomaker sees the incomplete execution of his own warrant in the membolus.
2) Market Meker is hedgered, occupying the opposite position of the same asset on CEX.
3) The initial performance is not possible due to an insufficient balance or admission of taking, and the meker remains only the hedging part of its position.
Such scenarios may arise due to the reorganization of blocks. There are ways to reduce the risk of this using specialized smart contracts when hedging on other DEX platforms. For example, a smart contract can be conditionally performed by hedging only if this order was executed. This will mean that hedging will not be executed if the original filling will not succeed, and the resulting state will even be canceled in the case of the reorganization of the block that excludes the transaction of the initial fill.
The 0x protocol suggests that the user's assets will be in their own wallet, and not deposited in a contract specifically for DEX. This allows market meters to place orders through several repeaters 0x without having to share their capital in several different places.
Developer Tools 0x
What is the status of the developer's toolkit 0x?
If you are planning to implement a custom infrastructure, you will need at least the following functions:
- Interaction with repeaters through web sockets or HTTPS
- Encoding ASSETDATAKA TAKE using ABIV2
- Keching orders
- Cryptographic signature of orders with the address you want to trade
In addition, it is strongly recommended to connect to the Ethereum node so that you can:
- Track the status of orders
- Receive notifications when changing the order status (subscribing to the relevant events)
- Set discounts for smart contracts 0x for the assets you want to sell
- Place or cancel orders
Examples of projects for 0x
We offer a brief list of examples of marketing projects built on 0x:
Marketomaker Maker on python: https: // GitHub.COM / MakerDao / Market-Maker-Keeper
Hummingbot - Bot to create an open source market: https: // Hummingbot.IO /
Sample bot Radarrelay: https: // GitHub.COM / RADARTECH / SDK-BOT-EXAMPLE
There were many questions? Get answers here.
On Cryptocinoxpert.Info Published reviews of cryptoprojects, technological solutions for IoT, DEFI, DAO, Tochenization of assets Using technology blockchain.