background

With the development of blockchain technology, a large number of digital currencies based on blockchain have emerged. These digital currencies are distributed in different blockchain networks and cannot communicate with each other directly, so they are mainly traded through digital currency exchanges. Traditional digital currency exchanges are mostly centralized services, which are challenged in security, privacy and other aspects. Decentralized exchanges running on the chain have attracted more and more attention because of their advantages such as data transparency, privacy guarantee and real-time settlement.

Most of the decentralized exchanges currently proposed only realize token transactions on the same chain, but cannot realize cross-chain digital currency transactions, which limits the application scenarios of decentralized exchanges. This paper proposes a decentralized digital currency exchange solution, using which cross-chain digital currency transactions can be realized. Here we have just outlined the basic idea, the design and development is still in progress.

We call this solution the Canal, because the Canal enables the transport and exchange of goods between cities. The original intention of this solution is that when users publish their own blockchain using one-click chain delivery service, they can use this solution to exchange with other digital currencies on the chain, so as to facilitate the circulation of digital currencies.

Related technologies

Decentralized exchanges

The decentralized exchanges currently proposed fall into two categories.

They are matched

Decentralized exchange systems including 0X [1] and Kyber Network[2] all belong to the type of order matching. Users post their buy or sell orders through smart contracts, and the system helps find the most suitable combination of buy and sell orders to trade. This kind of decentralized exchange can be seen as translocating the trading process of the original centralized exchange to the chain without changing the trading process mechanism.

The advantages of this type of transaction are as follows:

Transaction transparency: full disclosure of billing data; Fund security: Digital currency is managed by smart contract and transferred to user’s account after transaction is completed; Low transaction costs: There are no third-party fees except for blockchain gas. Disadvantages are as follows:

Transactions are done on a blockchain, which is slow; When the market is not active, buyers and sellers find it difficult to reach an agreement, and liquidity is weak. Automatic pricing

Bancor Network[3] realized automatic pricing transactions between users and digital currency capital pools through smart contracts. The transaction process is as follows:

Set up A trading contract and deposit A certain amount of digital currency A and B into the contract as the initial capital pool; When the user needs to exchange A coin for B coin, A coin is first deposited into the capital pool of the contract. Then the system will calculate the conversion rate from A to B according to the stock of A and B coins in the current contract, and transfer the corresponding amount of B coins in the capital pool to the user account. If the user continuously uses A coin to exchange B coin, A coin in the capital pool increases and B coin decreases, thus the conversion rate decreases, that is, the B coin that A coin can exchange decreases. The advantages of automatic pricing transactions are as follows:

There is no need to pay huge fees. Users do not need to trade, can be traded at any time. Ensure liquidity, users do not need to worry about trading depth. Disadvantages are as follows:

The project party is required to pledge part of the currency to the system in advance. Currently, the cross-chain technology of digital currency conversion on the same chain is only supported

Data communication between blockchain networks is not possible, which greatly limits the application space of blockchain. Cross-chain technology aims to solve this problem by enabling data transfer between blockchains. Currently, cross-chain solutions in the market only realize the exchange of value on different blockchains, but do not realize the transaction function. That is, digital currency can only be converted at a predetermined conversion rate, rather than changing its price dynamically with the market. This, to some extent, hinders the free flow of digital currency value and limits the application space of cross-chain technology.

Cross-chain solutions fall into two categories.

relay

One is to use a main chain to connect each side chain to realize data communication between side chains. This main chain is usually called Hub or Relay. For example, Cosmos[4] and Polkadot[5] have the same basic idea:

The main chain adopts dPoS (Delegated Proof of Stake), PoA (Proof of Authority) and other weakly centralized consensus algorithms, that is, the output of the block is completed by a small number of super nodes, orit can be called an alliance chain. The super node of the main chain monitors the data changes of the side chain, and then generates corresponding data in the main chain; Or monitor the change of main chain data, generate corresponding data in the side chain. Thus realize the main chain and side chain data exchange. The advantage of this cross-chain mode is fast and efficient data transmission, and can transmit any form of data on the chain; The disadvantage is centralization, that is, the accuracy and integrity of cross-chain data depend on the super nodes of the federation chain.

Atomic exchange

Atomic cross-chain exchange [6] is a script that uses blockchain to realize atomic exchange of digital currency in two blockchain networks. Atomic here means that two transactions posted on both blockchain systems by both parties must be completed or fail at the same time. The specific steps are as follows:

Alice trades her A dollars for Bob’s B dollars. Alice creates A transaction that pays A dollars to an output script that requires A key that Alice knows and Bob’s signature to unlock. After Bob sees the transaction, he creates the transaction as well, paying B coins to an output script that requires the same key and Alice’s signature to unlock. Alice uses the key and signature to unlock the transaction created by Bob and transfers B coins to her account address. The key will be linked up with the transfer operation. After seeing the key on the chain, Bob uses the key and his signature to unlock the transaction created by Alice, and transfers the A-coin to his account address, and the exchange is complete. If Alice and Bob terminate the operation during the process, the two transactions will be returned to Alice and Bob respectively after a certain period of time, and the exchange fails. The advantage of this cross-chain model is complete decentralization, exchange does not depend on any third party; The disadvantage is that an exchange process must rely on four transactions to complete, slow speed and low efficiency.

Algorithm is introduced

This paper proposes a decentralized cross-chain digital currency exchange solution. Its trading process is carried out by a network of alliance chains and multiple smart contracts. Take A scenario where digital currency on side chain A needs to be exchanged for digital currency on side chain B, and we complete the transaction through the main chain as A relay.

The main chain

The main chain adopts alliance chain. The super nodes of the alliance chain are voted on by all users of the exchange, or whatever. Supernodes are responsible for verifying all transactions on the blockchain and generating new blocks. The main chain connects all the side chains that need to be traded, thus ensuring that data can be transferred across the chain. Meanwhile, the main chain handles the smart contracts needed to run digital currency exchanges. The details of the scenario are described below.

Across the chain

The cross-chain part adopts the relay mode and consists of two contracts, one deployed on the main chain, called the main contract, and the other deployed on the side chain, called the sub-contract. The sub-contract is used for charging and withdrawing coins on the side chain by users, which is consistent with the concept of charging and withdrawing coins in centralized exchanges. The master contract is used to synchronize the user’s withdrawals and transaction records, and store the user’s balance.

The cross-chain steps are as follows:

Coin charging users deposit digital currency A to be exchanged into the sub-contract on side chain A. The supernode listens for changes in the amount of digital currency A under the sub-contract address, and then adds the corresponding amount of tokens to the user’s address in the main contract. The transaction user operation invokes the transaction interface with tokens at its own address in the main contract. The interface transfers the converted digital currency B to the user address of the main contract B. The coin withdrawal user initiates the coin withdrawal request to the sub-contract of side chain B. The super node listens to the coin withdrawal request on the side chain B, then destroys the token of the user in the main contract, and transfers the corresponding amount of digital currency B to the user’s address on the side chain B. trading

The transaction uses an automatic pricing model and consists of a smart contract. When the contract is initialized, it needs to fill A certain amount of digital currency A and B as A capital pool to realize the transaction between A and B.

The transaction process is as follows:

The user wants to convert A certain amount of digital currency A into digital currency B. Users will need to convert digital currency A into the contract address to access the pool of funds. The system calculates the number of B that can be converted according to the number of A that needs to be converted and the stock of A and B in the current capital pool. The system transfers the converted B from the system fund pool to the user’s address. The transformation calculation in Step 3 should follow the following principles:

The less A there is in the pool, the higher the conversion rate from A to B. The same goes for B. You cannot use up any of the digital currencies in the pool, otherwise you cannot continue to convert. Based on the above principles, the logic of transformation can be designed in many ways. Such as:

Assume that the stock of digital currency A and B in the capital pool are A0 and B0 (A0 > 0, B0 > 0) respectively. The user wants to convert digital currency A of △ A (△ A > 0) to obtain digital currency B of △ B (△ B > 0). The quantity of B that can be converted is △ B =(B0 *△ A)/(A0 + △ A). It can be seen that △ B is an increasing function of B0 and a subtracting function of A0, in accordance with principle 1 above. It can be seen that △ B < B0, then B in the capital pool will not be used up, which conforms to principle 2 above. implementation

We plan to implement Canal in the future. See the roadmap for more details.

conclusion

In the design process, we had two thoughts. First, we feel that decentralization should not be overemphasized at the expense of transaction efficiency. Because if the transaction speed is too slow, it will affect the user experience; The need for decentralization can be met to some extent by voting for trusted supernodes in a federated chain system. Secondly, the mode of automatic pricing transaction should be significantly better than that of single bill transaction in actual use, because the volume of many newly issued digital currencies is small and liquidity cannot be guaranteed, and the mode of automatic pricing transaction can well solve the problem of liquidity.

reference

[1] 0x protocol. www.0xproject.com/

[2] Kyber network. kyber.network/

[3] Bancor. www.bancor.network/

[4] Cosmos. cosmos.network/

[5] Polkadot. polkadot.network/

[6] Atomic cross-chain Swaps. arxiv.org/abs/1801.09…