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

Match force-settlements with margin calls at normal margin call fill price #2489

Merged
merged 35 commits into from
Aug 5, 2021

Conversation

abitmore
Copy link
Member

@abitmore abitmore commented Jul 12, 2021

PR for #2481.

1. Force-settlements are matched with margin calls with conditions:

  • no need to wait for the force-settlement delay when margin call appears
  • match price is in favor of force-settle orders, i.e. at current_feed_price.settlement_price / (MSSR-MCFR)
    • if filling at the match price above would trigger a black swan event, fill at 100% CR price instead; after filled, if there are remaining debt in the margin call, trigger black swan event normally
  • no force-settlement fee charged in this case
  • the volume is not counted in the force-settlement volume per maintenance interval
  • the matching takes place after processed limit (buy) orders with better price
  • sell orders whose price are lower than margin calls won't be matched with force-settlements, because force-settlement are meant to improve overall collateral ratio.

2. When a global settlement occurs:

  • the margin calls (whose CR <= MCR) pay a premium (by MSSR-MCFR) and a margin call fee (by MCFR), and
    they are closed at the same price;
  • the debt positions with CR > MCR do not pay premium or margin call fee, and they are closed at a same
    price too.
  • The GS price would close the position with the least CR with no collateral left for the owner,
    but would close other positions with some collateral left (if any) for their owners.
  • Both the premium and the margin call fee paid by the margin calls go to the asset's accumulated fees pool, none will go to the global settlement fund, because
    • if a part of the premium or fees goes to the global settlement fund, it means there would be a
      difference in settlement prices, so traders are incentivized to create new debt in the last minute
      then settle after GS to earn free money;
    • if no premium or fees goes to the global settlement fund, it means debt asset holders would only
      settle for less after GS, so they are incentivized to settle before GS which helps avoid GS.

3. API changes

  • The unused extensions field is removed from asset_settle_cancel_operation
     struct asset_settle_cancel_operation : public base_operation
     {
        struct fee_parameters_type { };
    
        asset_settle_cancel_operation() = default;
        asset_settle_cancel_operation( const force_settlement_id_type& fsid, const account_id_type& aid,
              const asset& a ) : settlement(fsid), account(aid), amount(a) {}
    
        asset           fee;
        force_settlement_id_type settlement;
        /// Account requesting the force settlement. This account pays the fee
        account_id_type account;
        /// Amount of asset to force settle. This must be a market-issued asset
        asset           amount;
    
        account_id_type fee_payer()const { return account; }
        /***
         * This is a virtual operation and should never be placed in a block
         * (i.e. in a proposal)
         */
        void validate() const { FC_ASSERT( !"Virtual operation"); }
    
        share_type calculate_fee(const fee_parameters_type& params)const
        { return 0; }
     };
    

Tasks:

  • cross-hf tests
  • test : disallow adjusting a call order if it would match a force settlement (when there is no limit order) but the call order is not fully filled
  • test : disallow creating a new instantly-called call order if it would match a force settlement (when there is no limit order) but the call order is not fully filled
  • test: if matching a call with a limit order would trigger a blackswan event, match settle orders first, then check for blackswan again
  • test: if there exists a settle order, it is now allowed to create a new call order with CR <= ~MSSP , in other words, DO NOT match the settle order with such undercollateralized call order.
  • test: if there exists a settle order, it is now allowed to update an existing call order so that its CR <= ~MSSP , in other words, DO NOT match the settle order with such undercollateralized call order.
  • rounding tests
    • smaller settle order (price not capped)
    • smaller settle order (price capped)
    • cancel too small settle order(price not capped)
    • cancel too small settle order(price capped)
    • settle receives 0 due to margin call fee, round up to 1 cancel it (updated in Implement alternative black swan response methods #2499)
    • too small call order pays 1 then closes
    • call order pays 1 due to TCR

@abitmore abitmore added this to In Development in Protocol Upgrade Release (6.0.0) via automation Jul 12, 2021
@abitmore abitmore force-pushed the pr-2481-settle-order-margin-call-match branch from 8be1044 to 2045d7c Compare July 13, 2021 10:02
@abitmore abitmore force-pushed the pr-2481-settle-order-margin-call-match branch 3 times, most recently from 4323dd8 to 1cd3f9e Compare July 17, 2021 18:52
- scenario 1: there is at least a settle order, but no limit order that could
  be matched with a call order, the settle order should get matched
- scenario 2: when creating a new call order, if it would trigger a margin
  call but the call order is not fully filled, should throw
- scenario 3: when reducing CR of an existing call order, if it would trigger
  a margin call but the call order is not fully filled, should throw
@abitmore abitmore force-pushed the pr-2481-settle-order-margin-call-match branch 2 times, most recently from 920e1ab to 2024368 Compare July 18, 2021 12:32
@abitmore abitmore force-pushed the pr-2481-settle-order-margin-call-match branch 3 times, most recently from 0ac45cb to e1d4f8d Compare July 19, 2021 15:12
@abitmore abitmore force-pushed the pr-2481-settle-order-margin-call-match branch from a7468b4 to 7a2afc7 Compare July 19, 2021 19:46
@sonarcloud
Copy link

sonarcloud bot commented Jul 27, 2021

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 35 Code Smells

91.0% 91.0% Coverage
0.0% 0.0% Duplication

@abitmore abitmore merged commit c2496e9 into hardfork Aug 5, 2021
Protocol Upgrade Release (6.0.0) automation moved this from In Development to Done Aug 5, 2021
@abitmore abitmore deleted the pr-2481-settle-order-margin-call-match branch August 5, 2021 17:42
@abitmore abitmore linked an issue Aug 5, 2021 that may be closed by this pull request
17 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Match force-settlements with margin calls at normal margin call fill price
1 participant