Understand the exchange protocol on the original MOV chain

Development of decentralized exchange protocols

From Bitshare, Stellar to Etherdelta, Bancor, 0x protocols on Ethereum, decentralized exchange protocols have also undergone several generations of development and exploration of many models. Each generation has been improved and deepened through the pain points of the previous protocol.

mainly divided:

  1. Orderbook on the chain, settlement on the chain;
  2. Orderbook off-chain, settlement on-chain;
  3. Fund pool based on smart contract management;

Orderbook on-chain, on-chain settlement

The earliest successful exploration of the decentralized exchange protocol based on Ethereum is Etherdelta, which once occupied half of the decentralized exchange market. Etherdelta is a relatively complete decentralized model. User recharge, pending orders, eating orders, settlement and withdrawal are all completed on the chain.

The specific operating mechanism is as follows:

Etherdelta's entire operation is completed on the chain. Users keep their private keys. The platform does not touch user assets, ensuring the security and transparency of assets and exchanges. But its disadvantages are also more obvious:

1) As all exchanges are completed on the chain, and every pending order, cancel order, eat order and other operations will consume GAS fees, resulting in high delay and low cost effectiveness.

2) There is a possibility of illegal advance exchange by miners.

Off-chain orderbook, on-chain settlement

In order to solve the problem of low efficiency and low fees on the pure chain, the 0x protocol introduces the concept of relay (relay). All orders are sent to relay, and there is no need to upload. Only transactions will be uploaded.

0x's "off-chain order relay, on-chain final settlement" operation mode is as follows:

The main problem of the 0x protocol is that if orders need to be shared, every transaction on the exchange using the 0x protocol needs to be broadcasted so that other transactions can be known and confirmed. Therefore, using the 0x protocol alone cannot achieve instant transactions; in addition, because The need to convert ETH to WETH also increases exchange costs.

Fund pool based on smart contract management

The most typical funding pool models are Bancor and Kyber. The so-called fund pool can be understood as that the platform uses smart contracts to establish a pool for storing various assets. The provider of assets in the fund pool can be ordinary users or market makers.

Introduction of MOV decentralized exchange protocol

When we examine various exchange protocols, in fact, the pure on-chain exchange protocol is the solution that maximizes the value of the blockchain, but because of the performance problems of public chains such as Ethereum, pure on-chain solutions such as Etherdelta are frustrated, only 0x. An off-chain orderbook appeared to improve performance issues. The root cause is the imperfect infrastructure, resulting in unavoidable changes. Therefore, the original chain MOV started to solve the blockchain performance problem from the beginning.

High-speed sidechain is guaranteed

MOV uses high-speed side chain Vapor pro as the underlying infrastructure. Vapor generates blocks every 0.5s, and each block can accommodate 8000 transactions, that is, 16,000 tps per second. In the case of increasing blocks and upgrading node servers, There is still room for further improvement. This performance can meet the current user demand during off-peak hours, and can be compared to a centralized solution on the shoulder.

At the same time, MOV uses DPoS as the consensus mechanism. Although it has lost some decentralization, it has increased the threshold for matching on the chain. Increasing the threshold for entry can better prevent some "miners" who have bad intentions from conducting pre-transactions. At the same time, because the matching on the chain itself has a certain matching income, this economic incentive can prevent DPoS's block-producing nodes from doing evil to damage the system (the cost of doing evil is higher than the normal income of not doing evil).

Order sharing

In order to solve the performance problem, the 0x protocol uses an off-chain orderbook, but the problem is the fragmentation of the order. Various parties using the 0x protocol will not share their user orders for their own interests, which will affect the The overall transaction depth, and mov uses an orderbook on the chain, all user orders are on the chain, open and transparent, and all consensus nodes participating in the matching can share this depth, thereby enhancing the liquidity of assets on mov.

Advantages of magnetic contracts

Because the original chain is based on the Bitcoin UTXO model, the magnetic contract on the UTXO model has a greater advantage as the exchange protocol, because the UTXO model itself uses assets as the basic unit. Compared with the account model, the operation of assets is Easier and more convenient, let's compare the two processes.

Take 0x as an example, the entire interaction process of 0x:

  • Maker authorizes DEX contract to access its own Token A balance
  • Maker creates an order (the order has a fixed format) and signs it with a private key
  • Maker uses arbitrary communication methods to broadcast orders
  • Taker receives order and is willing to execute
  • Taker authorizes the DEX contract to access its own Token B balance
  • Taker submits order to DEX
  • DEX verifies the legitimacy of the order and transfers between the two accounts at the exchange rate on the order

Then the entire process of the magnetic contract is much simpler:

  • Maker creates a magnetic contract (put your own assets in the magnetic contract and specify the assets and quantity to be exchanged)
  • Taker creates a magnetic contract (put your own assets in the magnetic contract and specify the assets and quantity to be exchanged)
  • The consensus node triggers a matching magnetic contract based on the price and quantity in the contract, and exchanges assets of the two.

Not only is the process simple, but the handling fee will be lower because of the simplification of the process. We only need to charge the handling fee when the user sets the magnetic contract. In fact, we can also try 0 handling fee because the DPoS model is used. The handling fee and the game between the nodes will not be too complicated.

Ecology of cross-chain assets

We observe the current decentralized exchange protocol on Ethereum, and it still stays on the ecology of Ethereum itself. Although the ecology of Ethereum cannot be denied, the outside world is actually the larger world. Of course, cross-chain is the follow-up theme. Both Cosmos and Polkdot want to do cross-chain things, so MOV considered cross-chain things from the beginning, and OFMF mapped assets outside the original chain to the original chain, and then formed all digital assets. In the big ecology, users experience the same experience as centralization in MOV. They can trade multiple assets, and these assets are not alone in the ecology of a certain chain.

Detailed MOV magnetic contract

Let's expand the MOV magnetic contract in detail here to see how it is implemented.

The MOV magnetic contract is essentially a pending order contract. Both Taker and Maker need to generate such a contract. In fact, it does not distinguish between Maker and Taker. It only distinguishes Maker and Taker according to the order of pending orders. This has increased the transaction depth, and in fact can be considered as Maker.

The pending order trading contract is an advanced version of the currency trading contract. The essential purpose of the contract is to lock any number of assets A and is willing to exchange asset B at a specific exchange rate. There should be four constants inside the contract (the ID of asset A does not need to be stored because the contract locks on asset A): the ID of asset B that is expected to be exchanged, and the exchange rate that is expected to be converted (using the numerator and denominator to solve the floating-point support problem) And the public key of the pending order user, the pending order user accepts the address of asset B. Contracts can be unlocked in three modes:

Unlock all: Asset A in all contracts is converted into Asset B and transferred to the address of the pending order user.

Partial solution: part of the contract's asset A is converted into asset B and transferred to the address of the pending order user, and the remaining asset A is re-locked back to the contract itself (newly generated UTXO) through the recursive contract mode.

Cancel pending order: The pending order user uses the private key signature to transfer all the assets A in the contract back to their own address.

The code of the magnetic contract Equity is as follows:

MagneticContract source code: contract MagneticContract(requestedAsset: Asset, ratioNumerator: Integer, ratioDenominator: Integer, sellerProgram: Program, standardProgram: Program, sellerKey: PublicKey) locks valueAmount of valueAsset { clause partialTrade(exchangeAmount: Amount) { define actualAmount: Integer = exchangeAmount * ratioDenominator / ratioNumerator verify actualAmount > 0 && actualAmount < valueAmount lock exchangeAmount of requestedAsset with sellerProgram lock valueAmount-actualAmount of valueAsset with standardProgram unlock actualAmount of valueAsset } clause fullTrade() { define requestedAmount: Integer = valueAmount * ratioNumerator / ratioDenominator verify requestedAmount > 0 lock requestedAmount of requestedAsset with sellerProgram unlock valueAmount of valueAsset } clause cancel(sellerSig: Signature) { verify checkTxSig(sellerKey, sellerSig) unlock valueAmount of valueAsset } 

fullTrade () is the full unlock method; partialTrade () is the partial unlock. When the partial unlock is triggered, it will talk about putting the unlocked asset into a newly generated magnetic contract to wait for the next match; the cancel () method will put the user's The asset is returned to its own address and the contract is cancelled.

We are looking at the input parameters of the magnetic contract:

 type MagneticContractArgs struct { RequestedAsset bc.AssetID RatioMolecule int64 RatioDenominator int64 SellerProgram []byte SellerKey []byte } 

RequestedAsset is the asset you want to exchange. RatioMolecule and RatioDenominator are the exchange rate of the asset you want to convert (RatioMolecule / RatioDenominator is the exchange rate). Because BVM currently does not support floating point, this parameter is additionally used as the ratio. SellerProgram and SellerKey are contract creations. The owner ’s own contract and address, the target asset is locked into the contract creator ’s own account.

Attentive friends may find that there is one parameter missing from the Equity contract, that is, standardProgram, then this parameter does not need to be entered by the user, and the system will fill it in by default. StandardProgram actually represents the original contract, because some matching will make some assets still not available. When used, it is still returned to the contract.

Finally, a picture is used to describe the magnetic contract more directly:

to sum up

Let's compare the current decentralized exchange protocols:


The earliest fully decentralized exchange protocol, Etherdelta, has the least interference to the exchange, but the fully on-chain mechanism causes high cost consumption and poor experience. The following types of decentralized exchange protocols can be said to be a trade-off between fish and bear's paw: the reserve pool model represented by Bancor and kyber, and the administrator has a high degree of participation in the entire process. Higher authority. For example, Bancor can remove user assets before, which will pose a threat to the user's funds. The two processes are relatively simple, the cost control performance is good, and the transaction efficiency is high. It is only a functional exchange agreement with an orderbook. Compared to slightly inferior. 0x relay mode, the platform does not touch user assets, relatively speaking, the degree of decentralization is relatively high, but this also results in relatively low cost-effectiveness; the overall delivery experience is good, but if you need to share orders, you cannot achieve instantaneous Deal.

On the basis of combining these previous generations, MOV has improved the infrastructure performance, enhanced the entry threshold for matching through DPoS, and realized on-chain order sharing, which has also greatly improved the user experience, in addition to sacrificing some of it through DPoS. In addition to centralization, it has been improved in other aspects. With the further development and improvement of MOV, the advantages of this solution will be brought into play, so that the blockchain can play a great value in the field of asset exchange and decentralization. Landing of the exchange of assets.