This article is a report on my participation in the second stage of the hackathon, organized by
karma.red . You can read more about the hackathon on a
special page .
For those who like to read the code, not the article -
the project repository .
')
Video lovers are also not offended :)
In this video, you can listen to a brief summary of the article, see how the exchange is implemented step by step and see its work live.
An article by another hackathon participant from Vladivostok (the three organizers from Vladik and one from Ulan-Ude, the organizers of the online stage paid for flying and living in Moscow).
The photos in the article - Vadim Frantsev, the pictures are mine, from a quick presentation of the project to protect the project :) So, with the formalities over, let's go.
After issuing
assignments , I had a choice - either to take up a decentralized exchange, or for an arbitration court. In the task with the court, it would be necessary to create a system with a reputation, to program the simulation. It would be too simple and a bit like my project
in the first stage .
The work of the decentralized exchange is based on atomic interblockchain exchanges (swaps), I have long wanted to understand them, but all hands did not reach. After giving myself an hour to research (if I couldn’t figure it out, I’d have to go to court) I went to read. After reading the
documentation for the implementation of exchanges for several blockchains, I realized that everything was easy and took over the exchange.
What is this decentralized exchange:
- Engine This can be your own blockchain or smart contract on another blockchain. Responsibilities include accepting orders (order, order) for buying / selling, their matching (matching, matching)
- Blockchains with support for blocking coins with unlocking by time / secret . Such as ethereum, bitcoin and its fork, eos, etc.
- Interface (optional) . To view the glass, submit orders, notifications, etc.
For simplicity, exchanges can be exchanged only through the currency of the exchange. In this case, the exchange will be able to initiate atomic exchanges in the blockchain of the exchange.
Since besides ethereum, there are no other blockchains with smart contacts now, so I chose it for prototyping. Its use in the military stock exchange seems unlikely due to the price of gas and its limitations. In this regard, there is some hope for EOS.
Atomic exchanges
You can read detailed documentation
here . Very brief retelling:
- Dasha has 1 bitcoin, Masha has 10 esters. They want to exchange them so that no one can fool each other.
- Dasha generates a secret and a hash from the secret
- (Initiate step) Dasha blocks 1 bitcoin so that only Masha can get it, indicating the secret, or Dasha herself two days after blocking
- Dasha passes the transaction ID with a lock and hash from the secret to Masha
- (Step participate) Masha checks the Dasha's transaction and, if everything is correct, Masha, using a smart contract, blocks 10 airs so that only Dasha can receive them, specifying a secret, or Masha herself a day after blocking
- Masha hands over the transaction with the funds blocking to Dasha
- (Redeem step) Dasha checks the transaction and, if the amount and date of the block is correct, takes 10 airs, specifying the secret
- (Redeem step) Masha learns the secret and takes bitcoin with it
Putting it all together
- Dasha sends an order to sell 1 bitcoin at the price of 10 esters per bitcoin
- Masha makes a deposit in 20 airs on an exchange contract
- Masha sends an order to buy 2 bitcoins for the price of 10 esters per bitcoin
- Exchange Matches Orders
- An order is created for Masha to buy 1 bitcoin at the price of 10 airs per bitcoin
- Atomic metabolism is initiated. In this case, 10 ethers are removed from the balance of Masha and are blocked in the contract of atomic exchange. Since For initiation, you need a secret and a hash from a secret, then the buyer (in this case, Masha) must deposit a stock of hashes of secrets on the exchange in order for each initiated exchange to have a new hash.
- Then all the actions described in the previous section take place, regardless of the exchange.
My implementation
I took this implementation as the basis for the atomic swap contract:
github.com/AltCoinExchange/ethatomicswap . The peculiarity of this implementation is that for a new atomic exchange there is no need to deploy a new contract. Everything happens in it, as in a certain registry of atomic exchanges.
In this contract, I added the ability to manually specify the initiator and the second participant of the exchange, in order to specify them when initiating the exchange from the exchange contract. The final version is
here .
The following has been implemented in
the exchange engine :
- Adding stock hashes
- Deposit and Withdrawal
- Submission of orders for the purchase and sale , but with some restrictions due to the prototypical nature of the contract and the time limit:
- There are no gas limit checks: cycles are not limited, in combat contracts you cannot do that
- For comparisons of orders, a counter-order is taken, not with the best price, but the first
- Only submitted applications are split when compared with smaller ones. For example, if there was an application for the purchase of 10 air, and you are applying for the sale of 2x, then the orders will not wind up.
- Initiation of atomic metabolism in the contract of the registry of atomic exchanges
In order not to write the interface, I wrapped the contracts in the
designers for the
smartz.io platform and secured the contracts through these designers, quickly obtaining an interface for managing the contracts.
I didn’t have enough time to demonstrate the exchange of the broadcast to bitcoin, so I showed the exchange of the broadcast from the rinkeby test network to the broadcast of their test network kovan:
A couple of pictures and links
Links to control panels for contracts signed by me (you need to install the browser extension metamask to access the ethereum blockchain from the browser). If you wish, you can enclose your
Pictures for unwilling to put extensions: