Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BSIP73: Match force-settlement orders with margin calls and limit orders #200

Merged
merged 6 commits into from
Sep 14, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions bsip-0073.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
BSIP: 0073
Title: Match force-settlement orders with margin calls and limit orders
Author: Abit More <https://github.com/abitmore>
Status: Draft
Type: Protocol
Created: 2019-06-09
Discussion: https://github.com/bitshares/bsips/issues/181
Worker: TBD

# Abstract

This BSIP proposes a protocol change to improve user experience (UX) of
force-settlements by trying to fill force-settlement orders at better price
abitmore marked this conversation as resolved.
Show resolved Hide resolved
and optionally fill settle orders before expiration when certain conditions
are met.

# Motivation

Force-settlements were designed for debt asset holders to convert debt asset
abitmore marked this conversation as resolved.
Show resolved Hide resolved
to collateral asset *when there is nobody willing to buy back the debt at
abitmore marked this conversation as resolved.
Show resolved Hide resolved
fair price*.

To mitigate malious behavior and market manipulation, a delay and a price
offset were designed. But the mechanism has flaws.
* Force-settlement requesters always "buy expensive" even when there are
orders "selling low". This also leads to occasional spikes in the market
history charts.
* Force-settlement requesters have to wait for the delay even when there
appear traders selling at fair price in the market.

These flaws has led to certain confusion and anger among market participants.
abitmore marked this conversation as resolved.
Show resolved Hide resolved

# Rationale

Actually, when a margin call appears, it means there is somebody
willing to buy back the debt. Thus, it makes sense to fill the margin calls
with the settle orders.

Similarly, if there are limit orders selling debt asset below feed price,
abitmore marked this conversation as resolved.
Show resolved Hide resolved
it makes sense to match them with the settle orders as well.

When one of these opportunities appears, it makes sense to fill certain
settle orders immediately if it's desired by the owners of the settle orders.

These changes would improve user experience (UX).

# Specifications

## `settle_order_object`

The `settle_order_object` stores current status of a force-settlement order.

Need to add a new field into it:
abitmore marked this conversation as resolved.
Show resolved Hide resolved

* `bool fill_asap;`

By default this field is set to `false`, which means the order will be
processed after the delay defined in bitasset option, which is current
abitmore marked this conversation as resolved.
Show resolved Hide resolved
behavior.

If this field is set to `true`, the order will be filled or partially filled
when a debt position enters margin call territory, it will also be filled or
abitmore marked this conversation as resolved.
Show resolved Hide resolved
partially filled when someone placed a limit order selling collateral asset
abitmore marked this conversation as resolved.
Show resolved Hide resolved
below feed price.
abitmore marked this conversation as resolved.
Show resolved Hide resolved

If multiple settle orders with this field as `true` exist in the market,
when filling before the delay, the order which is created first will be
abitmore marked this conversation as resolved.
Show resolved Hide resolved
filled first.

## `asset_settle_operation`

The `asset_settle_operation` is used to request a force-settlement. It has
an `extensions` field:

* `extensions_type extensions;`

Need to override data type of this field so it can include the new `fill_asap`
abitmore marked this conversation as resolved.
Show resolved Hide resolved
option.

## `asset_settle_evaluator`

The `asset_settle_evaluator` is used to evaluate and apply the
`asset_settle_operation`. Need to add logic:
abitmore marked this conversation as resolved.
Show resolved Hide resolved
* only allow `fill_asap` option to be set after the hard fork;
* if `fill_asap` option is specified in `asset_settle_operation`, when
creating a `settle_order_object` (note: it implies some conditions E.G. the
asset is not globally settled), assign the value of the operation's
`fill_asap` field to the object's `fill_asap` field.
* if `fill_asap` is set to `true`, and if the feed price is valid,
after created the settle order object, try to match it against the order
abitmore marked this conversation as resolved.
Show resolved Hide resolved
book immediately, in other words, treat it as a taker limit order buying
abitmore marked this conversation as resolved.
Show resolved Hide resolved
at feed price.
abitmore marked this conversation as resolved.
Show resolved Hide resolved

## `proposal_create_evaluator`

The `proposal_create_evaluator` is used to evaluate and apply the
`proposal_create_operation`, which can contain zero or more
`asset_settle_operation` objects. Need to add logic:
abitmore marked this conversation as resolved.
Show resolved Hide resolved
* only allow `fill_asap` to be set after the hard fork.

## Matching and filling settle orders before the delay

### When a new limit order is created

If the new limit order is selling the collateral asset for debt asset, and
abitmore marked this conversation as resolved.
Show resolved Hide resolved
its price is below feed price (which implies feed price is valid),
abitmore marked this conversation as resolved.
Show resolved Hide resolved
* current logic is to only match it with the limit orders on the opposite;
abitmore marked this conversation as resolved.
Show resolved Hide resolved
* after the hard fork, the new logic would be:
* firstly match it with the limit orders on the opposite whose buy prices
are higher than feed price;
abitmore marked this conversation as resolved.
Show resolved Hide resolved
* secondly match it with the settle orders with `fill_asap` set to `true`,
the matching price would be feed price, in other words, treat the settle
abitmore marked this conversation as resolved.
Show resolved Hide resolved
orders as maker limit orders buying at feed price;
abitmore marked this conversation as resolved.
Show resolved Hide resolved
* lastly match it with remaining limit orders on the opposite.

### When a debt position entered margin call territory

If a margin call order appears, either due to feed price changed, or due to
abitmore marked this conversation as resolved.
Show resolved Hide resolved
collateral or debt changed (which implies feed price is valid),
abitmore marked this conversation as resolved.
Show resolved Hide resolved
* current logic is to only match it with the limit orders on the opposite;
* after the hard fork, the new logic would be:
* firstly match it with the limit orders on the opposite whose buy prices
are higher than feed price;
abitmore marked this conversation as resolved.
Show resolved Hide resolved
* secondly match it with the settle orders with `fill_asap` set to `true`,
abitmore marked this conversation as resolved.
Show resolved Hide resolved
the matching price would be feed price, in other words, treat the settle
abitmore marked this conversation as resolved.
Show resolved Hide resolved
orders as maker limit orders buying at feed price;
abitmore marked this conversation as resolved.
Show resolved Hide resolved
* lastly match it with remaining limit orders on the opposite.

### When feed price changed
abitmore marked this conversation as resolved.
Show resolved Hide resolved

When feed price changed, either due to a new price is published, or due
abitmore marked this conversation as resolved.
Show resolved Hide resolved
to an old price expired, or due to asset options changed, if the new feed
abitmore marked this conversation as resolved.
Show resolved Hide resolved
price is valid, and if the new feed price in the direction of "X debt asset
per collateral asset" is higher than the old feed price or the old feed
price was invalid,
* current logic doesn't handle settle orders,
* after the hard fork, the new logic would be:
* if there are limit orders selling collateral asset below the new feed
abitmore marked this conversation as resolved.
Show resolved Hide resolved
price, and there are settle orders whose `fill_asap` is `true`,
match the settle orders with the limit orders, in other words, treat
abitmore marked this conversation as resolved.
Show resolved Hide resolved
those settle orders as taker limit orders buying at the new feed price.

### Do not update `force_settled_volume` before the delay

When a settle order is matched and (partially) filled before the delay,
it doesn't affect `force_settled_volume` (which indicates how much debt
abitmore marked this conversation as resolved.
Show resolved Hide resolved
asset has been force-settled in current maintenance interval).
abitmore marked this conversation as resolved.
Show resolved Hide resolved

## Processing settle orders after the delay

Currently, when filling a settle order after the delay (note: it implies
some conditions E.G. price feed is valid and total settled volume in current
abitmore marked this conversation as resolved.
Show resolved Hide resolved
maitenance interval doesn't exceed maximum allowed volume),
abitmore marked this conversation as resolved.
Show resolved Hide resolved
the settle order will be matched against the debt position with the least
collateral ratio, the fill price in the direction of "X debt asset
per collateral asset" would be
`fill_price = feed_price * (1 + foce_settlement_offset)`.

After the hard fork, when processing a settle order after the delay,
* firstly try to match it with the margin calls and limit orders selling
below `fill_price`, in other words, treat it as a taker limit order buying
abitmore marked this conversation as resolved.
Show resolved Hide resolved
at `fill_price`.
Note: this step doesn't affect `force_settled_volume`.
* if the settle order still exists, process it with the logic before the
hard fork.
Note: this step does affect `force_settled_volume` as before.

## API

APIs which return `settle_order_object` need to return the new `fill_asap`
field.

APIs which return combined order book can combine settle orders whose
abitmore marked this conversation as resolved.
Show resolved Hide resolved
`fill_asap` is true with limit orders in the same direction.

## CLI

There was a `settle_asset` command in CLI.

Need to add a new command E.G. `settle_asset_ext` in CLI so that users would
abitmore marked this conversation as resolved.
Show resolved Hide resolved
pmconrad marked this conversation as resolved.
Show resolved Hide resolved
be able to create force-settlement orders with the new `fill_asap` option.

## GUI/UX

pmconrad marked this conversation as resolved.
Show resolved Hide resolved
The new `fill_asap` option need to be presented and can be used in UI after
abitmore marked this conversation as resolved.
Show resolved Hide resolved
the hard fork.

When there are settle orders with `fill_asap` set to `true`, UI can show them
abitmore marked this conversation as resolved.
Show resolved Hide resolved
as special buy orders which are buying at feed price in the order book.
abitmore marked this conversation as resolved.
Show resolved Hide resolved

# Discussion

With this BSIP, we provided a tool that can be used by debt asset holders to
convert their debt asset to collateral asset more conveniently and flexibly.
abitmore marked this conversation as resolved.
Show resolved Hide resolved
However, it's not guaranteed that a settle order will be filled at better
abitmore marked this conversation as resolved.
Show resolved Hide resolved
price if the owner chose to fill it "as soon as possible". Market paticipants
should always make their own decisions on whether to use the new tool.

pmconrad marked this conversation as resolved.
Show resolved Hide resolved
# Summary for Shareholders

TBD

# Copyright

This document is placed in the public domain.

# See Also

* https://github.com/bitshares/bsips/issues/181
* https://github.com/bitshares/bitshares-ui/issues/1711