xaya / taurion_gsp

Taurion - A Fully Decentralised MMO built for the Xaya Platform.
https://taurion.io
GNU General Public License v3.0
23 stars 10 forks source link

Implement DEX trading #187

Closed domob1812 closed 4 years ago

domob1812 commented 4 years ago

This implements DEX trading inside each building. With a new set of commands, users can transfer assets inside a building between accounts, place buy/sell limit orders and cancel those orders again. Building owners can also configure a new option, namely the DEX fee to charge sellers (out of their profit in Cubits) for trades inside their building. This fee can be chosen between 0% and 30%, and is on top of a base fee of 3% that all trades incur and that is burnt.

domob1812 commented 4 years ago

The DEX operations are sent as moves with the new x top-level field. It accepts an array of individual operations to perform. For transferring assets (10 units of foo inside building 42 to domob in the example), the move is this:

{"x": [{"b": 42, "i": "foo", "n": 10, "t": "domob"}]}

To create buy (bp) or sell (ap) orders at a certain price in Cubits per unit:

{"x": [{"b": 42, "i": "foo", "n": 10, "bp": 2}]}
{"x": [{"b": 42, "i": "foo", "n": 10, "ap": 100}]}

These orders are matched immediately if possible, and the unmatched parts will be placed onto the order book. Orders on the book can be cancelled again by their owner account via ID:

{"x": [{"c": 123}]}

All open orders inside a building are now returned as part of the building game-state JSON, in a new field orderbook.

When a buy order is created, the Cubits necessary for it to fill are reserved out of the account's balance. To reflect this, the balance inside an account JSON now returns three balances:

"balance": {
  "available": 10,
  "reserved": 2,
  "total": 12
}

In this case, 2 Cubits are reserved in open buy orders, and only 10 are normally available (as balance for all other purposes in the game). If the buy orders are cancelled or the building in which they are is destroyed, the reserved Cubits will be refunded.

Similarly, any items required to fill open sell orders are also reserved. Each building has a new inventory of reserved balances that is returned in addition to the "normal" inventories field in reserved. If a sell order is cancelled, the reserved items become available again. If the building is destroyed, then reserved inventories become part of the normal loot dropped with 30% chance.

domob1812 commented 4 years ago

Each trade is subject to a 3% base fee (deducted from the Cubits the buyer pays before the seller gets them), which is burnt. In addition to that, the building owner can configure a DEX fee between 0% and 30% (denominated in basis points), e.g.

{"b": {"id": 42, "xf": 500}}

This move by the owner of building 42 would set the building-owner fee to 5%.

domob1812 commented 4 years ago

Trade history (i.e. all executed trades on any DEX) is recorded as well (but not consensus relevant). It can be queried with a new RPC method gettradehistory (item="foo", building=42) for a particular item in a particular building, e.g. to show a price chart.