Author Topic: How is Matching works?  (Read 2080 times)

0 Members and 1 Guest are viewing this topic.

Offline abit

  • Committee member
  • Hero Member
  • *
  • Posts: 4651
    • View Profile
    • Abit's Hive Blog
  • BitShares: abit
  • GitHub: abitmore
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.
BitShares committee member: abit
BitShares witness: in.abit

Offline alexicon

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 :) ?

 
« Last Edit: March 26, 2018, 09:51:16 am by alexicon »

Offline abit

  • Committee member
  • Hero Member
  • *
  • Posts: 4651
    • View Profile
    • Abit's Hive Blog
  • BitShares: abit
  • GitHub: abitmore
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).
BitShares committee member: abit
BitShares witness: in.abit

Offline alexicon

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)?
« Last Edit: March 22, 2018, 10:49:10 am by alexicon »

Offline MarkoPaasila

Have a look at BSIP 33 - 35. They describe order matching pretty well. https://github.com/bitshares/bsips

Offline alexicon

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 ).