BitShares Forum

Main => Technical Support => Topic started by: alexicon on March 21, 2018, 02:46:22 pm

Title: How is Matching works?
Post by: alexicon on March 21, 2018, 02:46:22 pm
Hi, All
Can anybody explain in more technical details how the DEX matching works?
I search through the code and found graphene::chain::database::apply_order(...) function which then call to fill_order() function and so on... But i do not understand how DEX is actually change user balances after orders matching (i mean, i do not understand how the blockchain is working here ).
Title: Re: How is Matching works?
Post by: MarkoPaasila on March 21, 2018, 08:08:43 pm
Have a look at BSIP 33 - 35. They describe order matching pretty well. https://github.com/bitshares/bsips
Title: Re: How is Matching works?
Post by: alexicon on March 22, 2018, 10:39:31 am
Have a look at BSIP 33 - 35. They describe order matching pretty well. https://github.com/bitshares/bsips

Thank you for answer! :)

It looks like i didn't describe my problem correctly.
I understand how the matching algorithm is working, but i do not understand how all this orders processes through blockchain? Where order book is stored and how ? Is it stored in ElasticSearch DB or directly in blokchain? What happens when two orders matched and how they stored in graphene? How users balances change when orders automatically matched (form blockchain perspective)?
Title: Re: How is Matching works?
Post by: abit on March 22, 2018, 01:29:16 pm
Have a look at BSIP 33 - 35. They describe order matching pretty well. https://github.com/bitshares/bsips

Thank you for answer! :)

It looks like i didn't describe my problem correctly.
I understand how the matching algorithm is working, but i do not understand how all this orders processes through blockchain? Where order book is stored and how ? Is it stored in ElasticSearch DB or directly in blokchain? What happens when two orders matched and how they stored in graphene? How users balances change when orders automatically matched (form blockchain perspective)?
There are some basic questions about blockchain.

The BitShares "blockchain" itself only stores blocks with transactions inside with operations inside.

All (witness) nodes read and explain/execute the transactions according to consensus, and save the results as "state" in a in-memory database. Accounts, balances, orders and other objects are all parts of the state. When orders placed, or filled, the state would change (e.g. new order object created or removed, balances increased or decreased according to the consensus).
Title: Re: How is Matching works?
Post by: alexicon on March 26, 2018, 08:11:54 am
Hi, abit! Thanks a lot for your reply! I'm now have better understanding of bitshares working process.
And now i have some more questions, maybe simple, but interesting:

 How bitshares will work in this situation: Bob put his order to buy 10 bitUSD for 1.7 BTS, then Alice and Dan, simultaneously put their orders to sell 10 bitUSD for 1.7.  Alice and Dan using two different nodes(witnesses) to put orders to the network. What will happen? Which order will be chosen to fill Bobs order and why?
How will Alice and Dan orders be processed through transactions chain? Who decide which order will fill if they created orders absolutely simultaneously :) ?

 
Title: Re: How is Matching works?
Post by: abit on April 03, 2018, 04:55:58 pm
Hi, abit! Thanks a lot for your reply! I'm now have better understanding of bitshares working process.
And now i have some more questions, maybe simple, but interesting:

 How bitshares will work in this situation: Bob put his order to buy 10 bitUSD for 1.7 BTS, then Alice and Dan, simultaneously put their orders to sell 10 bitUSD for 1.7.  Alice and Dan using two different nodes(witnesses) to put orders to the network. What will happen? Which order will be chosen to fill Bobs order and why?
How will Alice and Dan orders be processed through transactions chain? Who decide which order will fill if they created orders absolutely simultaneously :) ?
There is no "simultaneous" thing in blockchain, one of them will be processed first. In BitShares, the witnesses (block producers) decide which order is processed first. It's described here: https://github.com/bitshares/bsips/blob/master/bsip-0035.md#the-match-price. The second order will either become a maker order, or get matched with another order, or be dropped if have the "fill-or-kill" flag set.