BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface software
db_market.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
26 
29 #include <graphene/chain/hardfork.hpp>
32 
33 #include <fc/uint128.hpp>
34 
35 namespace graphene { namespace chain {
36 
37 namespace detail {
38 
39  share_type calculate_percent(const share_type& value, uint16_t percent)
40  {
41  fc::uint128_t a(value.value);
42  a *= percent;
44  FC_ASSERT( a <= GRAPHENE_MAX_SHARE_SUPPLY, "overflow when calculating percent" );
45  return static_cast<int64_t>(a);
46  }
47 
48 } //detail
49 
50 bool database::check_for_blackswan( const asset_object& mia, bool enable_black_swan,
51  const asset_bitasset_data_object* bitasset_ptr )
52 {
53  if( !mia.is_market_issued() ) return false;
54 
55  const asset_bitasset_data_object& bitasset = bitasset_ptr ? *bitasset_ptr : mia.bitasset_data(*this);
56  if( bitasset.has_settlement() ) return true; // already force settled
57  auto settle_price = bitasset.current_feed.settlement_price;
58  if( settle_price.is_null() ) return false; // no feed
59 
60  asset_id_type debt_asset_id = bitasset.asset_id;
61 
62  auto maint_time = get_dynamic_global_properties().next_maintenance_time;
63  bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME ); // call price caching issue
64  bool after_core_hardfork_2481 = HARDFORK_CORE_2481_PASSED( maint_time ); // Match settle orders with margin calls
65 
66  // After core-2481 hard fork, if there are force-settlements, match call orders with them first
67  if( after_core_hardfork_2481 )
68  {
69  const auto& settlement_index = get_index_type<force_settlement_index>().indices().get<by_expiration>();
70  auto lower_itr = settlement_index.lower_bound( debt_asset_id );
71  if( lower_itr != settlement_index.end() && lower_itr->balance.asset_id == debt_asset_id )
72  return false;
73  }
74 
75  // Find the call order with the least collateral ratio
76  const call_order_object* call_ptr = find_least_collateralized_short( bitasset, false );
77  if( !call_ptr ) // no call order
78  return false;
79 
80  const limit_order_index& limit_index = get_index_type<limit_order_index>();
81  const auto& limit_price_index = limit_index.indices().get<by_price>();
82 
83  // looking for limit orders selling the most USD for the least CORE
84  auto highest_possible_bid = price::max( debt_asset_id, bitasset.options.short_backing_asset );
85  // stop when limit orders are selling too little USD for too much CORE
86  auto lowest_possible_bid = price::min( debt_asset_id, bitasset.options.short_backing_asset );
87 
88  FC_ASSERT( highest_possible_bid.base.asset_id == lowest_possible_bid.base.asset_id );
89  // NOTE limit_price_index is sorted from greatest to least
90  auto limit_itr = limit_price_index.lower_bound( highest_possible_bid );
91  auto limit_end = limit_price_index.upper_bound( lowest_possible_bid );
92 
93  price call_pays_price;
94  if( limit_itr != limit_end )
95  {
96  call_pays_price = limit_itr->sell_price;
97  if( after_core_hardfork_2481 )
98  {
99  // due to margin call fee, we check with MCPP (margin call pays price) here
100  call_pays_price = call_pays_price * bitasset.get_margin_call_pays_ratio();
101  }
102  }
103 
105  const auto bsrm = bitasset.get_black_swan_response_method();
106 
107  // when BSRM is individual settlement, we loop multiple times
108  bool settled_some = false;
109  while( true )
110  {
111  settle_price = bitasset.current_feed.settlement_price;
112  price highest = settle_price;
113  // Due to #338, we won't check for black swan on incoming limit order, so need to check with MSSP here
114  // * If BSRM is individual_settlement_to_fund, check with median_feed to decide whether to settle.
115  // * If BSRM is no_settlement, check with current_feed to NOT trigger global settlement.
116  // * If BSRM is global_settlement or individual_settlement_to_order, median_feed == current_feed.
117  if( bsrm_type::individual_settlement_to_fund == bsrm )
118  highest = bitasset.median_feed.max_short_squeeze_price();
119  else if( !before_core_hardfork_1270 )
120  highest = bitasset.current_feed.max_short_squeeze_price();
121  else if( maint_time > HARDFORK_CORE_338_TIME )
123  // else do nothing
124 
125  if( limit_itr != limit_end )
126  {
127  FC_ASSERT( highest.base.asset_id == limit_itr->sell_price.base.asset_id );
128  if( bsrm_type::individual_settlement_to_fund != bsrm )
129  highest = std::max( call_pays_price, highest );
130  // for individual_settlement_to_fund, if call_pays_price < current_feed.max_short_squeeze_price(),
131  // we don't match the least collateralized short with the limit order
132  // even if call_pays_price >= median_feed.max_short_squeeze_price()
133  else if( call_pays_price >= bitasset.current_feed.max_short_squeeze_price() )
134  highest = call_pays_price;
135  // else highest is median_feed.max_short_squeeze_price()
136  }
137 
138  // The variable `highest` after hf_338:
139  // * if no limit order, it is expected to be the black swan price; if the call order with the least CR
140  // has CR below or equal to the black swan price, we trigger GS,
141  // * if there exists at least one limit order and the price is higher, we use the limit order's price,
142  // which means we will match the margin call orders with the limit order first.
143  //
144  // However, there was a bug: after hf_bsip74 and before hf_2481, margin call fee was not considered
145  // when calculating highest, which means some blackswans weren't got caught here. Fortunately they got
146  // caught by an additional check in check_call_orders().
147  // This bug is fixed in hf_2481. Actually, after hf_2481,
148  // * if there is a force settlement, we totally rely on the additional checks in check_call_orders(),
149  // * if there is no force settlement, we check here with margin call fee in consideration.
150 
151  auto least_collateral = call_ptr->collateralization();
152  // Note: strictly speaking, even when the call order's collateralization is lower than ~highest,
153  // if the matching limit order is smaller, due to rounding, it is still possible that the
154  // call order's collateralization would increase and become higher than ~highest after matched.
155  // However, for simplicity, we only compare the prices here.
156  bool is_blackswan = after_core_hardfork_2481 ? ( ~least_collateral > highest )
157  : ( ~least_collateral >= highest );
158  if( !is_blackswan )
159  return settled_some;
160 
161  wdump( (*call_ptr) );
162  elog( "Black Swan detected on asset ${symbol} (${id}) at block ${b}: \n"
163  " Least collateralized call: ${lc} ${~lc}\n"
164  // " Highest Bid: ${hb} ${~hb}\n"
165  " Settle Price: ${~sp} ${sp}\n"
166  " Max: ${~h} ${h}\n",
167  ("id",mia.id)("symbol",mia.symbol)("b",head_block_num())
168  ("lc",least_collateral.to_real())("~lc",(~least_collateral).to_real())
169  // ("hb",limit_itr->sell_price.to_real())("~hb",(~limit_itr->sell_price).to_real())
170  ("sp",settle_price.to_real())("~sp",(~settle_price).to_real())
171  ("h",highest.to_real())("~h",(~highest).to_real()) );
172  edump((enable_black_swan));
173  FC_ASSERT( enable_black_swan,
174  "Black swan was detected during a margin update which is not allowed to trigger a blackswan" );
175 
176  if( bsrm_type::individual_settlement_to_fund == bsrm || bsrm_type::individual_settlement_to_order == bsrm )
177  {
178  individually_settle( bitasset, *call_ptr );
179  call_ptr = find_least_collateralized_short( bitasset, true );
180  if( !call_ptr ) // no call order
181  return true;
182  settled_some = true;
183  continue;
184  }
185  // Global settlement or no settlement, but we should not be here if BSRM is no_settlement
186  else if( after_core_hardfork_2481 )
187  {
188  if( bsrm_type::no_settlement == bsrm ) // this should not happen, be defensive here
189  wlog( "Internal error: BSRM is no_settlement but undercollateralization occurred" );
190  // After hf_2481, when a global settlement occurs,
191  // * the margin calls (whose CR <= MCR) pay a premium (by MSSR-MCFR) and a margin call fee (by MCFR), and
192  // they are closed at the same price,
193  // * the debt positions with CR > MCR do not pay premium or margin call fee, and they are closed at a same
194  // price too.
195  // * The GS price would close the position with the least CR with no collateral left for the owner,
196  // but would close other positions with some collateral left (if any) for their owners.
197  // * Both the premium and the margin call fee paid by the margin calls go to the asset owner, none will go
198  // to the global settlement fund, because
199  // - if a part of the premium or fees goes to the global settlement fund, it means there would be a
200  // difference in settlement prices, so traders are incentivized to create new debt in the last minute
201  // then settle after GS to earn free money,
202  // - if no premium or fees goes to the global settlement fund, it means debt asset holders would only
203  // settle for less after GS, so they are incentivized to settle before GS which helps avoid GS.
204  globally_settle_asset(mia, ~least_collateral, true );
205  }
206  else if( maint_time > HARDFORK_CORE_338_TIME && ~least_collateral <= settle_price )
207  // global settle at feed price if possible
208  globally_settle_asset(mia, settle_price );
209  else
210  globally_settle_asset(mia, ~least_collateral );
211  return true;
212  }
213 }
214 
221 void database::globally_settle_asset( const asset_object& mia, const price& settlement_price,
222  bool check_margin_calls )
223 {
224  auto maint_time = get_dynamic_global_properties().next_maintenance_time;
225  bool before_core_hardfork_1669 = ( maint_time <= HARDFORK_CORE_1669_TIME ); // whether to use call_price
226 
227  if( before_core_hardfork_1669 )
228  {
229  globally_settle_asset_impl( mia, settlement_price,
230  get_index_type<call_order_index>().indices().get<by_price>(),
231  check_margin_calls );
232  }
233  else
234  {
235  // Note: it is safe to iterate here even if there is no call order due to individual settlements
236  globally_settle_asset_impl( mia, settlement_price,
237  get_index_type<call_order_index>().indices().get<by_collateral>(),
238  check_margin_calls );
239  }
240 }
241 
242 template<typename IndexType>
243 void database::globally_settle_asset_impl( const asset_object& mia,
244  const price& settlement_price,
245  const IndexType& call_index,
246  bool check_margin_calls )
247 { try {
248  const asset_bitasset_data_object& bitasset = mia.bitasset_data(*this);
249  FC_ASSERT( !bitasset.has_settlement(), "black swan already occurred, it should not happen again" );
250 
251  asset collateral_gathered( 0, bitasset.options.short_backing_asset );
252 
253  const asset_dynamic_data_object& mia_dyn = mia.dynamic_asset_data_id(*this);
254  auto original_mia_supply = mia_dyn.current_supply;
255 
256  auto maint_time = get_dynamic_global_properties().next_maintenance_time;
257  bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME ); // better rounding
258 
259  // cancel all call orders and accumulate it into collateral_gathered
260  auto call_itr = call_index.lower_bound( price::min( bitasset.options.short_backing_asset, bitasset.asset_id ) );
261  auto call_end = call_index.upper_bound( price::max( bitasset.options.short_backing_asset, bitasset.asset_id ) );
262 
263  auto margin_end = call_end;
264  bool is_margin_call = false;
265  price call_pays_price = settlement_price;
266  price fund_receives_price = settlement_price;
267  if( check_margin_calls )
268  {
269  margin_end = call_index.upper_bound( bitasset.current_maintenance_collateralization );
270  // Note: settlement_price is in debt / collateral, here the fund gets less collateral
271  fund_receives_price = settlement_price * ratio_type( bitasset.current_feed.maximum_short_squeeze_ratio,
273  if( call_itr != margin_end )
274  is_margin_call = true;
275  }
276  asset margin_call_fee( 0, bitasset.options.short_backing_asset );
277 
278  asset pays;
279  while( call_itr != call_end )
280  {
281  if( is_margin_call && call_itr == margin_end )
282  {
283  is_margin_call = false;
284  call_pays_price = fund_receives_price;
285  }
286 
287  const call_order_object& order = *call_itr;
288  ++call_itr;
289 
290  auto order_debt = order.get_debt();
291  if( before_core_hardfork_342 )
292  pays = order_debt * call_pays_price; // round down, in favor of call order
293  else
294  pays = order_debt.multiply_and_round_up( call_pays_price ); // round up in favor of global-settle fund
295 
296  if( pays > order.get_collateral() )
297  pays = order.get_collateral();
298 
299  if( is_margin_call )
300  {
301  auto fund_receives = order_debt.multiply_and_round_up( fund_receives_price );
302  if( fund_receives > pays )
303  fund_receives = pays;
304  margin_call_fee = pays - fund_receives;
305  collateral_gathered += fund_receives;
306  }
307  else
308  {
309  margin_call_fee.amount = 0;
310  collateral_gathered += pays;
311  }
312 
313  // call order is maker
314  FC_ASSERT( fill_call_order( order, pays, order_debt, fund_receives_price, true, margin_call_fee, false ),
315  "Internal error: unable to close margin call ${o}", ("o", order) );
316  }
317 
318  // Move the individual settlement order to the GS fund
319  const limit_order_object* limit_ptr = find_settled_debt_order( bitasset.asset_id );
320  if( limit_ptr )
321  {
322  collateral_gathered.amount += limit_ptr->for_sale;
323  remove( *limit_ptr );
324  }
325 
326  // Move individual settlement fund to the GS fund
327  collateral_gathered.amount += bitasset.individual_settlement_fund;
328 
329  modify( bitasset, [&mia,&original_mia_supply,&collateral_gathered]( asset_bitasset_data_object& obj ){
330  obj.options.extensions.value.black_swan_response_method.reset(); // Update BSRM to GS
331  obj.current_feed = obj.median_feed; // reset current feed price if was capped
334  obj.settlement_price = mia.amount(original_mia_supply) / collateral_gathered;
335  obj.settlement_fund = collateral_gathered.amount;
336  });
337 
338 } FC_CAPTURE_AND_RETHROW( (mia)(settlement_price) ) }
339 
340 void database::individually_settle( const asset_bitasset_data_object& bitasset, const call_order_object& order )
341 {
342  FC_ASSERT( bitasset.asset_id == order.debt_type(), "Internal error: asset type mismatch" );
343 
345  const auto bsrm = bitasset.get_black_swan_response_method();
346  FC_ASSERT( bsrm_type::individual_settlement_to_fund == bsrm || bsrm_type::individual_settlement_to_order == bsrm,
347  "Internal error: Invalid BSRM" );
348 
349  auto order_debt = order.get_debt();
350  auto order_collateral = order.get_collateral();
351  auto fund_receives_price = (~order.collateralization()) / bitasset.get_margin_call_pays_ratio();
352  auto fund_receives = order_debt.multiply_and_round_up( fund_receives_price );
353  if( fund_receives.amount > order.collateral ) // should not happen, just be defensive
354  fund_receives.amount = order.collateral;
355 
356  auto margin_call_fee = order_collateral - fund_receives;
357 
358  if( bsrm_type::individual_settlement_to_fund == bsrm ) // settle to fund
359  {
360  modify( bitasset, [&order,&fund_receives]( asset_bitasset_data_object& obj ){
361  obj.individual_settlement_debt += order.debt;
362  obj.individual_settlement_fund += fund_receives.amount;
363  });
364  }
365  else // settle to order
366  {
367  const limit_order_object* limit_ptr = find_settled_debt_order( bitasset.asset_id );
368  if( limit_ptr )
369  {
370  modify( *limit_ptr, [&order,&fund_receives]( limit_order_object& obj ) {
371  obj.for_sale += fund_receives.amount;
372  obj.sell_price.base.amount = obj.for_sale;
373  obj.sell_price.quote.amount += order.debt;
374  } );
375  }
376  else
377  {
378  create< limit_order_object >( [&order_debt,&fund_receives]( limit_order_object& obj ) {
379  obj.expiration = time_point_sec::maximum();
380  obj.seller = GRAPHENE_NULL_ACCOUNT;
381  obj.for_sale = fund_receives.amount;
382  obj.sell_price = fund_receives / order_debt;
383  obj.is_settled_debt = true;
384  } );
385  }
386  // Note: CORE asset in settled debt is not counted in account_stats.total_core_in_orders
387  }
388 
389  // call order is maker
390  FC_ASSERT( fill_call_order( order, order_collateral, order_debt,
391  fund_receives_price, true, margin_call_fee, false ),
392  "Internal error: unable to close margin call ${o}", ("o", order) );
393 
394  // Update current feed if needed
395  if( bsrm_type::individual_settlement_to_fund == bsrm )
396  update_bitasset_current_feed( bitasset, true );
397 
398 }
399 
401 { try {
402  FC_ASSERT( bitasset.is_market_issued() );
403  FC_ASSERT( bitasset.id == bad.asset_id );
404  FC_ASSERT( bad.has_settlement() );
407 
408  const asset_dynamic_data_object& bdd = bitasset.dynamic_asset_data_id(*this);
409  if( bdd.current_supply > 0 )
410  {
411  // Create + execute a "bid" with 0 additional collateral
412  const collateral_bid_object& pseudo_bid = create<collateral_bid_object>(
413  [&bitasset,&bad,&bdd](collateral_bid_object& bid) {
414  bid.bidder = bitasset.issuer;
415  bid.inv_swan_price = asset(0, bad.options.short_backing_asset)
416  / asset(bdd.current_supply, bad.asset_id);
417  });
418  execute_bid( pseudo_bid, bdd.current_supply, bad.settlement_fund, bad.current_feed );
419  } else
420  FC_ASSERT( bad.settlement_fund == 0 );
421 
422  _cancel_bids_and_revive_mpa( bitasset, bad );
423 } FC_CAPTURE_AND_RETHROW( (bitasset) ) }
424 
425 void database::_cancel_bids_and_revive_mpa( const asset_object& bitasset, const asset_bitasset_data_object& bad )
426 { try {
427  FC_ASSERT( bitasset.is_market_issued() );
428  FC_ASSERT( bad.has_settlement() );
430 
431  // cancel remaining bids
432  const auto& bid_idx = get_index_type< collateral_bid_index >().indices().get<by_price>();
433  auto itr = bid_idx.lower_bound( bad.asset_id );
434  const auto end = bid_idx.upper_bound( bad.asset_id );
435  while( itr != end )
436  {
437  const collateral_bid_object& bid = *itr;
438  ++itr;
439  cancel_bid( bid );
440  }
441 
442  // revive
443  modify( bad, []( asset_bitasset_data_object& obj ){
444  obj.settlement_price = price();
445  obj.settlement_fund = 0;
446  });
447 } FC_CAPTURE_AND_RETHROW( (bitasset) ) }
448 
449 void database::cancel_bid(const collateral_bid_object& bid, bool create_virtual_op)
450 {
451  adjust_balance(bid.bidder, bid.inv_swan_price.base);
452 
453  if( create_virtual_op )
454  {
456  vop.bidder = bid.bidder;
459  push_applied_operation( vop );
460  }
461  remove(bid);
462 }
463 
465  share_type collateral_from_fund, const price_feed& current_feed )
466 {
467  const call_order_object& call_obj = create<call_order_object>(
468  [&bid, &debt_covered, &collateral_from_fund, &current_feed, this](call_order_object& call ){
469  call.borrower = bid.bidder;
470  call.collateral = bid.inv_swan_price.base.amount + collateral_from_fund;
471  call.debt = debt_covered;
472  // don't calculate call_price after core-1270 hard fork
473  if( get_dynamic_global_properties().next_maintenance_time > HARDFORK_CORE_1270_TIME )
474  // bid.inv_swan_price is in collateral / debt
475  call.call_price = price( asset( 1, bid.inv_swan_price.base.asset_id ),
476  asset( 1, bid.inv_swan_price.quote.asset_id ) );
477  else
478  call.call_price = price::call_price( asset(debt_covered, bid.inv_swan_price.quote.asset_id),
479  asset(call.collateral, bid.inv_swan_price.base.asset_id),
480  current_feed.maintenance_collateral_ratio );
481  });
482 
483  // Note: CORE asset in collateral_bid_object is not counted in account_stats.total_core_in_orders
484  if( bid.inv_swan_price.base.asset_id == asset_id_type() )
485  modify( get_account_stats_by_owner(bid.bidder), [&call_obj](account_statistics_object& stats) {
486  stats.total_core_in_orders += call_obj.collateral;
487  });
488 
489  push_applied_operation( execute_bid_operation( bid.bidder,
490  asset( debt_covered, bid.inv_swan_price.quote.asset_id ),
491  asset( call_obj.collateral, bid.inv_swan_price.base.asset_id ) ) );
492 
493  remove(bid);
494 }
495 
497 {
498  adjust_balance(order.owner, order.balance);
499 
500  push_applied_operation( asset_settle_cancel_operation( order.get_id(), order.owner, order.balance ) );
501 
502  remove(order);
503 }
504 
505 void database::cancel_limit_order( const limit_order_object& order, bool create_virtual_op, bool skip_cancel_fee )
506 {
507  // if need to create a virtual op, try deduct a cancellation fee here.
508  // there are two scenarios when order is cancelled and need to create a virtual op:
509  // 1. due to expiration: always deduct a fee if there is any fee deferred
510  // 2. due to cull_small: deduct a fee after hard fork 604, but not before (will set skip_cancel_fee)
511  const account_statistics_object* seller_acc_stats = nullptr;
512  const asset_dynamic_data_object* fee_asset_dyn_data = nullptr;
514  share_type deferred_fee = order.deferred_fee;
515  asset deferred_paid_fee = order.deferred_paid_fee;
516  if( create_virtual_op )
517  {
518  vop.order = order.id;
519  vop.fee_paying_account = order.seller;
520  // only deduct fee if not skipping fee, and there is any fee deferred
521  if( !skip_cancel_fee && deferred_fee > 0 )
522  {
523  asset core_cancel_fee = current_fee_schedule().calculate_fee( vop );
524  // cap the fee
525  if( core_cancel_fee.amount > deferred_fee )
526  core_cancel_fee.amount = deferred_fee;
527  // if there is any CORE fee to deduct, redirect it to referral program
528  if( core_cancel_fee.amount > 0 )
529  {
530  seller_acc_stats = &get_account_stats_by_owner( order.seller );
531  modify( *seller_acc_stats, [&core_cancel_fee, this]( account_statistics_object& obj ) {
532  obj.pay_fee( core_cancel_fee.amount, get_global_properties().parameters.cashback_vesting_threshold );
533  } );
534  deferred_fee -= core_cancel_fee.amount;
535  // handle originally paid fee if any:
536  // to_deduct = round_up( paid_fee * core_cancel_fee / deferred_core_fee_before_deduct )
537  if( deferred_paid_fee.amount == 0 )
538  {
539  vop.fee = core_cancel_fee;
540  }
541  else
542  {
543  fc::uint128_t fee128( deferred_paid_fee.amount.value );
544  fee128 *= core_cancel_fee.amount.value;
545  // to round up
546  fee128 += order.deferred_fee.value;
547  fee128 -= 1;
548  fee128 /= order.deferred_fee.value;
549  share_type cancel_fee_amount = static_cast<int64_t>(fee128);
550  // cancel_fee should be positive, pay it to asset's accumulated_fees
551  fee_asset_dyn_data = &deferred_paid_fee.asset_id(*this).dynamic_asset_data_id(*this);
552  modify( *fee_asset_dyn_data, [&cancel_fee_amount](asset_dynamic_data_object& addo) {
553  addo.accumulated_fees += cancel_fee_amount;
554  });
555  // cancel_fee should be no more than deferred_paid_fee
556  deferred_paid_fee.amount -= cancel_fee_amount;
557  vop.fee = asset( cancel_fee_amount, deferred_paid_fee.asset_id );
558  }
559  }
560  }
561  }
562 
563  // refund funds in order
564  auto refunded = order.amount_for_sale();
565  if( refunded.asset_id == asset_id_type() )
566  {
567  if( !seller_acc_stats )
568  seller_acc_stats = &get_account_stats_by_owner( order.seller );
569  modify( *seller_acc_stats, [&refunded]( account_statistics_object& obj ) {
570  obj.total_core_in_orders -= refunded.amount;
571  });
572  }
573  adjust_balance(order.seller, refunded);
574 
575  // refund fee
576  // could be virtual op or real op here
577  if( order.deferred_paid_fee.amount == 0 )
578  {
579  // be here, order.create_time <= HARDFORK_CORE_604_TIME, or fee paid in CORE, or no fee to refund.
580  // if order was created before hard fork 604 then cancelled no matter before or after hard fork 604,
581  // see it as fee paid in CORE, deferred_fee should be refunded to order owner but not fee pool
582  adjust_balance( order.seller, deferred_fee );
583  }
584  else // need to refund fee in originally paid asset
585  {
586  adjust_balance(order.seller, deferred_paid_fee);
587  // be here, must have: fee_asset != CORE
588  if( !fee_asset_dyn_data )
589  fee_asset_dyn_data = &deferred_paid_fee.asset_id(*this).dynamic_asset_data_id(*this);
590  modify( *fee_asset_dyn_data, [&](asset_dynamic_data_object& addo) {
591  addo.fee_pool += deferred_fee;
592  });
593  }
594 
595  if( create_virtual_op )
596  push_applied_operation( vop );
597 
598  remove(order);
599 }
600 
602 {
613  if( order.amount_to_receive().amount == 0 )
614  {
615  if( order.deferred_fee > 0 && db.head_block_time() <= HARDFORK_CORE_604_TIME )
616  {
617  db.cancel_limit_order( order, true, true );
618  }
619  else
620  db.cancel_limit_order( order );
621  return true;
622  }
623  return false;
624 }
625 
626 // Note: optimizations have been done in apply_order(...)
628 {
629  auto order_id = new_order_object.id;
630  const asset_object& sell_asset = get(new_order_object.amount_for_sale().asset_id);
631  const asset_object& receive_asset = get(new_order_object.amount_to_receive().asset_id);
632 
633  // Possible optimization: We only need to check calls if both are true:
634  // - The new order is at the front of the book
635  // - The new order is below the call limit price
636  bool called_some = check_call_orders(sell_asset, true, true); // the first time when checking, call order is maker
637  bool called_some_else = check_call_orders(receive_asset, true, true); // the other side, same as above
638  if( ( called_some || called_some_else ) && !find_object(order_id) ) // then we were filled by call order
639  return true;
640 
641  const auto& limit_price_idx = get_index_type<limit_order_index>().indices().get<by_price>();
642 
643  // it should be possible to simply check the NEXT/PREV iterator after new_order_object to
644  // determine whether or not this order has "changed the book" in a way that requires us to
645  // check orders. For now I just lookup the lower bound and check for equality... this is log(n) vs
646  // constant time check. Potential optimization.
647 
648  auto max_price = ~new_order_object.sell_price;
649  auto limit_itr = limit_price_idx.lower_bound(max_price.max());
650  auto limit_end = limit_price_idx.upper_bound(max_price);
651 
652  bool finished = false;
653  while( !finished && limit_itr != limit_end )
654  {
655  auto old_limit_itr = limit_itr;
656  ++limit_itr;
657  // match returns 2 when only the old order was fully filled. In this case, we keep matching; otherwise, we stop.
658  finished = ( match(new_order_object, *old_limit_itr, old_limit_itr->sell_price)
659  != match_result_type::only_maker_filled );
660  }
661 
662  // Possible optimization: only check calls if the new order completely filled some old order.
663  // Do I need to check both assets?
664  check_call_orders(sell_asset); // after the new limit order filled some orders on the book,
665  // if a call order matches another order, the call order is taker
666  check_call_orders(receive_asset); // the other side, same as above
667 
668  const limit_order_object* updated_order_object = find< limit_order_object >( order_id );
669  if( !updated_order_object )
670  return true;
671  if( head_block_time() <= HARDFORK_555_TIME )
672  return false;
673  // before #555 we would have done maybe_cull_small_order() logic as a result of fill_order()
674  // being called by match() above
675  // however after #555 we need to get rid of small orders -- #555 hardfork defers logic that
676  // was done too eagerly before, and
677  // this is the point it's deferred to.
678  return maybe_cull_small_order( *this, *updated_order_object );
679 }
680 
681 /***
682  * @brief apply a new limit_order_object to the market, matching with existing limit orders or
683  * margin call orders where possible, leaving remainder on the book if not fully matched.
684  * @detail Called from limit_order_create_evaluator::do_apply() in market_evaluator.cpp in
685  * response to a limit_order_create operation. If we're not at the front of the book, we
686  * return false early and do nothing else, since there's nothing we can match. If we are at
687  * the front of the book, then we first look for matching limit orders that are more
688  * favorable than the margin call price, then we search through active margin calls, then
689  * finaly the remaining limit orders, until we either fully consume the order or can no
690  * longer match and must leave the remainder on the book.
691  * @return Returns true if limit order is completely consumed by matching, else false if it
692  * remains on the book.
693  * @param new_order_object the new limit order (read only ref, though the corresponding db
694  * object is modified as we match and deleted if filled completely)
695  */
696 bool database::apply_order(const limit_order_object& new_order_object)
697 {
698  auto order_id = new_order_object.id;
699  asset_id_type sell_asset_id = new_order_object.sell_asset_id();
700  asset_id_type recv_asset_id = new_order_object.receive_asset_id();
701 
702  // We only need to check if the new order will match with others if it is at the front of the book
703  const auto& limit_price_idx = get_index_type<limit_order_index>().indices().get<by_price>();
704  auto limit_itr = limit_price_idx.iterator_to( new_order_object );
705  if( limit_itr != limit_price_idx.begin() )
706  {
707  --limit_itr;
708  if( limit_itr->sell_asset_id() == sell_asset_id && limit_itr->receive_asset_id() == recv_asset_id )
709  return false;
710  }
711 
712  // this is the opposite side (on the book)
713  auto max_price = ~new_order_object.sell_price;
714  limit_itr = limit_price_idx.lower_bound( max_price.max() );
715  auto limit_end = limit_price_idx.upper_bound( max_price );
716 
717  // Order matching should be in favor of the taker.
718  // When a new limit order is created, e.g. an ask, need to check if it will match the highest bid.
719  // We were checking call orders first. However, due to MSSR (maximum_short_squeeze_ratio),
720  // effective price of call orders may be worse than limit orders, so we should also check limit orders here.
721 
722  // Question: will a new limit order trigger a black swan event?
723  //
724  // 1. as of writing, it's possible due to the call-order-and-limit-order overlapping issue:
725  // https://github.com/bitshares/bitshares-core/issues/606 .
726  // when it happens, a call order can be very big but don't match with the opposite,
727  // even when price feed is too far away, further than swan price,
728  // if the new limit order is in the same direction with the call orders, it can eat up all the opposite,
729  // then the call order will lose support and trigger a black swan event.
730  // 2. after issue 606 is fixed, there will be no limit order on the opposite side "supporting" the call order,
731  // so a new order in the same direction with the call order won't trigger a black swan event.
732  // 3. calling is one direction. if the new limit order is on the opposite direction,
733  // no matter if matches with the call, it won't trigger a black swan event.
734  // (if a match at MSSP caused a black swan event, it means the call order is already undercollateralized,
735  // which should trigger a black swan event earlier.)
736  //
737  // Since it won't trigger a black swan, no need to check here.
738 
739  // currently we don't do cross-market (triangle) matching.
740  // the limit order will only match with a call order if meet all of these:
741  // 1. it's buying collateral, which means sell_asset is the MIA, receive_asset is the backing asset.
742  // 2. sell_asset is not a prediction market
743  // 3. sell_asset is not globally settled
744  // 4. sell_asset has a valid price feed
745  // 5. the call order's collateral ratio is below or equals to MCR
746  // 6. the limit order provided a good price
747 
748  auto maint_time = get_dynamic_global_properties().next_maintenance_time;
749  bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME ); // call price caching issue
750 
751  bool to_check_call_orders = false;
752  const asset_object& sell_asset = sell_asset_id( *this );
753  const asset_bitasset_data_object* sell_abd = nullptr;
754  price call_match_price; // Price at which margin calls sit on the books. Prior to BSIP-74 this price is
755  // same as the MSSP. After, it is the MCOP, which may deviate from MSSP due to MCFR.
756  price call_pays_price; // Price margin call actually relinquishes collateral at. Equals the MSSP and it may
757  // differ from call_match_price if there is a Margin Call Fee.
758  if( sell_asset.is_market_issued() )
759  {
760  sell_abd = &sell_asset.bitasset_data( *this );
761  if( sell_abd->options.short_backing_asset == recv_asset_id
762  && !sell_abd->is_prediction_market
763  && !sell_abd->has_settlement()
764  && !sell_abd->current_feed.settlement_price.is_null() )
765  {
766  if( before_core_hardfork_1270 ) {
767  call_match_price = ~sell_abd->current_feed.max_short_squeeze_price_before_hf_1270();
768  call_pays_price = call_match_price;
769  } else {
770  call_match_price = ~sell_abd->get_margin_call_order_price();
771  call_pays_price = ~sell_abd->current_feed.max_short_squeeze_price();
772  }
773  if( ~new_order_object.sell_price <= call_match_price ) // If new limit order price is good enough to
774  to_check_call_orders = true; // match a call, then check if there are calls.
775  }
776  }
777 
778  bool finished = false; // whether the new order is gone
779  bool feed_price_updated = false; // whether current_feed.settlement_price has been updated
780  if( to_check_call_orders )
781  {
782  // check limit orders first, match the ones with better price in comparison to call orders
783  auto limit_itr_after_call = limit_price_idx.lower_bound( call_match_price );
784  while( !finished && limit_itr != limit_itr_after_call )
785  {
786  const limit_order_object& matching_limit_order = *limit_itr;
787  ++limit_itr;
788  // match returns 2 when only the old order was fully filled.
789  // In this case, we keep matching; otherwise, we stop.
790  finished = ( match( new_order_object, matching_limit_order, matching_limit_order.sell_price )
791  != match_result_type::only_maker_filled );
792  }
793 
794  auto call_min = price::min( recv_asset_id, sell_asset_id );
795  if( !finished && !before_core_hardfork_1270 ) // TODO refactor or cleanup duplicate code after core-1270 hf
796  {
797  // check if there are margin calls
798  // Note: it is safe to iterate here even if there is no call order due to individual settlements
799  const auto& call_collateral_idx = get_index_type<call_order_index>().indices().get<by_collateral>();
800  // Note: when BSRM is no_settlement, current_feed can change after filled a call order,
801  // so we recalculate inside the loop
803  auto bsrm = sell_abd->get_black_swan_response_method();
804  bool update_call_price = ( bsrm_type::no_settlement == bsrm && sell_abd->is_current_feed_price_capped() );
805  auto old_current_feed_price = sell_abd->current_feed.settlement_price;
806  while( !finished )
807  {
808  // hard fork core-343 and core-625 took place at same time,
809  // always check call order with least collateral ratio
810  auto call_itr = call_collateral_idx.lower_bound( call_min );
811  if( call_itr == call_collateral_idx.end()
812  || call_itr->debt_type() != sell_asset_id
813  // feed protected https://github.com/cryptonomex/graphene/issues/436
814  || call_itr->collateralization() > sell_abd->current_maintenance_collateralization )
815  break;
816  // hard fork core-338 and core-625 took place at same time, not checking HARDFORK_CORE_338_TIME here.
817  const auto match_result = match( new_order_object, *call_itr, call_match_price,
818  *sell_abd, call_pays_price );
819  // match returns 1 or 3 when the new order was fully filled.
820  // In this case, we stop matching; otherwise keep matching.
821  // since match can return 0 due to BSIP38 (hf core-834), we no longer only check if the result is 2.
822  if( match_result_type::only_taker_filled == match_result
823  || match_result_type::both_filled == match_result )
824  finished = true;
825  else if( update_call_price )
826  {
827  call_match_price = ~sell_abd->get_margin_call_order_price();
828  call_pays_price = ~sell_abd->current_feed.max_short_squeeze_price();
829  update_call_price = sell_abd->is_current_feed_price_capped();
830  // Since current feed price (in debt/collateral) can only decrease after updated, if there still
831  // exists a call order in margin call territory, it would be on the top of the order book,
832  // so no need to check if the current limit (buy) order would match another limit (sell) order atm.
833  // On the other hand, the current limit order is on the top of the other side of the order book.
834  }
835  } // while !finished
836  if( bsrm_type::no_settlement == bsrm && sell_abd->current_feed.settlement_price != old_current_feed_price )
837  feed_price_updated = true;
838  } // if after core-1270 hf
839  else if( !finished ) // and before core-1270 hard fork
840  {
841  // check if there are margin calls
842  const auto& call_price_idx = get_index_type<call_order_index>().indices().get<by_price>();
843  while( !finished )
844  {
845  // assume hard fork core-343 and core-625 will take place at same time,
846  // always check call order with least call_price
847  auto call_itr = call_price_idx.lower_bound( call_min );
848  if( call_itr == call_price_idx.end()
849  || call_itr->debt_type() != sell_asset_id
850  // feed protected https://github.com/cryptonomex/graphene/issues/436
851  || call_itr->call_price > ~sell_abd->current_feed.settlement_price )
852  break;
853  // assume hard fork core-338 and core-625 will take place at same time,
854  // not checking HARDFORK_CORE_338_TIME here.
855  const auto match_result = match( new_order_object, *call_itr, call_match_price, *sell_abd );
856  // match returns 1 or 3 when the new order was fully filled.
857  // In this case, we stop matching; otherwise keep matching.
858  // since match can return 0 due to BSIP38 (hard fork core-834),
859  // we no longer only check if the result is 2.
860  if( match_result_type::only_taker_filled == match_result
861  || match_result_type::both_filled == match_result )
862  finished = true;
863  } // while !finished
864  } // if before core-1270 hf
865  } // if to check call
866 
867  // still need to check limit orders
868  while( !finished && limit_itr != limit_end )
869  {
870  const limit_order_object& matching_limit_order = *limit_itr;
871  ++limit_itr;
872  // match returns 2 when only the old order was fully filled. In this case, we keep matching; otherwise, we stop.
873  finished = ( match( new_order_object, matching_limit_order, matching_limit_order.sell_price )
874  != match_result_type::only_maker_filled );
875  }
876 
877  bool limit_order_is_gone = true;
878  const limit_order_object* updated_order_object = find< limit_order_object >( order_id );
879  if( updated_order_object )
880  // before #555 we would have done maybe_cull_small_order() logic as a result of fill_order()
881  // being called by match() above
882  // however after #555 we need to get rid of small orders -- #555 hardfork defers logic that
883  // was done too eagerly before, and
884  // this is the point it's deferred to.
885  limit_order_is_gone = maybe_cull_small_order( *this, *updated_order_object );
886 
887  if( limit_order_is_gone && feed_price_updated )
888  {
889  // If current_feed got updated, and the new limit order is gone,
890  // it is possible that other limit orders are able to get filled,
891  // so we need to call check_call_orders()
892  check_call_orders( sell_asset, true, false, sell_abd );
893  }
894 
895  return limit_order_is_gone;
896 }
897 
899  const asset_bitasset_data_object& bitasset,
900  const asset_object& asset_obj )
901 {
902  // Defensive checks
903  auto maint_time = get_dynamic_global_properties().next_maintenance_time;
904  FC_ASSERT( HARDFORK_CORE_2481_PASSED( maint_time ), "Internal error: hard fork core-2481 not passed" );
905  FC_ASSERT( new_settlement.balance.asset_id == bitasset.asset_id, "Internal error: asset type mismatch" );
906  FC_ASSERT( !bitasset.is_prediction_market, "Internal error: asset is a prediction market" );
907  FC_ASSERT( !bitasset.has_settlement(), "Internal error: asset is globally settled already" );
908  FC_ASSERT( !bitasset.current_feed.settlement_price.is_null(), "Internal error: no sufficient price feeds" );
909 
910  auto head_time = head_block_time();
911  bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time ); // Price feed issues
912 
913  auto new_obj_id = new_settlement.id;
914 
915  // Price at which margin calls sit on the books.
916  // It is the MCOP, which may deviate from MSSP due to MCFR.
917  price call_match_price = bitasset.get_margin_call_order_price();
918  // Price margin call actually relinquishes collateral at. Equals the MSSP and it may
919  // differ from call_match_price if there is a Margin Call Fee.
920  price call_pays_price = bitasset.current_feed.max_short_squeeze_price();
921 
922  // Note: when BSRM is no_settlement, current_feed can change after filled a call order,
923  // so we recalculate inside the loop
925  auto bsrm = bitasset.get_black_swan_response_method();
926  bool update_call_price = ( bsrm_type::no_settlement == bsrm && bitasset.is_current_feed_price_capped() );
927 
928  bool finished = false; // whether the new order is gone
929 
930  // check if there are margin calls
931  // Note: it is safe to iterate here even if there is no call order due to individual settlements
932  const auto& call_collateral_idx = get_index_type<call_order_index>().indices().get<by_collateral>();
933  auto call_min = price::min( bitasset.options.short_backing_asset, new_settlement.balance.asset_id );
934  while( !finished )
935  {
936  // always check call order with the least collateral ratio
937  auto call_itr = call_collateral_idx.lower_bound( call_min );
938  // Note: we don't precalculate an iterator with upper_bound() before entering the loop,
939  // because the upper bound can change after a call order got filled
940  if( call_itr == call_collateral_idx.end()
941  || call_itr->debt_type() != new_settlement.balance.asset_id
942  // feed protected https://github.com/cryptonomex/graphene/issues/436
943  || call_itr->collateralization() > bitasset.current_maintenance_collateralization )
944  break;
945  // TCR applies here
946  auto settle_price = after_core_hardfork_2582 ? bitasset.median_feed.settlement_price
947  : bitasset.current_feed.settlement_price;
948  asset max_debt_to_cover( call_itr->get_max_debt_to_cover( call_pays_price,
949  settle_price,
952  new_settlement.balance.asset_id );
953 
954  match( new_settlement, *call_itr, call_pays_price, bitasset, max_debt_to_cover, call_match_price, true );
955 
956  // Check whether the new order is gone
957  finished = ( nullptr == find_object( new_obj_id ) );
958 
959  if( update_call_price )
960  {
961  // when current_feed is updated, it is possible that there are limit orders able to get filled,
962  // so we need to call check_call_orders(), but skip matching call orders with force settlements
963  check_call_orders( asset_obj, true, false, &bitasset, false, true );
964  if( !finished )
965  {
966  call_match_price = bitasset.get_margin_call_order_price();
967  call_pays_price = bitasset.current_feed.max_short_squeeze_price();
968  update_call_price = bitasset.is_current_feed_price_capped();
969  }
970  }
971  }
972 
973 }
974 
976 static database::match_result_type get_match_result( bool taker_filled, bool maker_filled )
977 {
978  int8_t result = 0;
979  if( maker_filled )
980  result += static_cast<int8_t>( database::match_result_type::only_maker_filled );
981  if( taker_filled )
982  result += static_cast<int8_t>( database::match_result_type::only_taker_filled );
983  return static_cast<database::match_result_type>( result );
984 }
985 
996 database::match_result_type database::match( const limit_order_object& taker, const limit_order_object& maker,
997  const price& match_price )
998 {
1001  FC_ASSERT( taker.for_sale > 0 && maker.for_sale > 0 );
1002 
1003  return maker.is_settled_debt ? match_limit_settled_debt( taker, maker, match_price )
1004  : match_limit_normal_limit( taker, maker, match_price );
1005 }
1006 
1007 database::match_result_type database::match_limit_normal_limit( const limit_order_object& taker,
1008  const limit_order_object& maker, const price& match_price )
1009 {
1010  FC_ASSERT( !maker.is_settled_debt, "Internal error: maker is settled debt" );
1011 
1012  auto taker_for_sale = taker.amount_for_sale();
1013  auto maker_for_sale = maker.amount_for_sale();
1014 
1015  asset taker_pays;
1016  asset taker_receives;
1017  asset maker_pays;
1018  asset maker_receives;
1019 
1020  auto maint_time = get_dynamic_global_properties().next_maintenance_time;
1021  bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME ); // better rounding
1022 
1023  bool cull_taker = false;
1024  if( taker_for_sale <= ( maker_for_sale * match_price ) ) // rounding down here should be fine
1025  {
1026  taker_receives = taker_for_sale * match_price; // round down, in favor of bigger order
1027 
1028  // Be here, it's possible that taker is paying something for nothing due to partially filled in last loop.
1029  // In this case, we see it as filled and cancel it later
1030  if( taker_receives.amount == 0 && maint_time > HARDFORK_CORE_184_TIME )
1031  return match_result_type::only_taker_filled;
1032 
1033  if( before_core_hardfork_342 )
1034  maker_receives = taker_for_sale;
1035  else
1036  {
1037  // The remaining amount in order `taker` would be too small,
1038  // so we should cull the order in fill_limit_order() below.
1039  // The order would receive 0 even at `match_price`, so it would receive 0 at its own price,
1040  // so calling maybe_cull_small() will always cull it.
1041  maker_receives = taker_receives.multiply_and_round_up( match_price );
1042  cull_taker = true;
1043  }
1044  }
1045  else
1046  {
1047  //This line once read: assert( maker_for_sale < taker_for_sale * match_price ); // check
1048  //This assert is not always true -- see trade_amount_equals_zero in operation_tests.cpp
1049  //Although taker_for_sale is greater than maker_for_sale * match_price,
1050  // maker_for_sale == taker_for_sale * match_price
1051  //Removing the assert seems to be safe -- apparently no asset is created or destroyed.
1052 
1053  // The maker won't be paying something for nothing, since if it would, it would have been cancelled already.
1054  maker_receives = maker_for_sale * match_price; // round down, in favor of bigger order
1055  if( before_core_hardfork_342 )
1056  taker_receives = maker_for_sale;
1057  else
1058  // The remaining amount in order `maker` would be too small,
1059  // so the order will be culled in fill_limit_order() below
1060  taker_receives = maker_receives.multiply_and_round_up( match_price );
1061  }
1062 
1063  maker_pays = taker_receives;
1064  taker_pays = maker_receives;
1065 
1066  if( before_core_hardfork_342 )
1067  FC_ASSERT( taker_pays == taker.amount_for_sale() ||
1068  maker_pays == maker.amount_for_sale() );
1069 
1070  // the first param of match() is taker
1071  bool taker_filled = fill_limit_order( taker, taker_pays, taker_receives, cull_taker, match_price, false );
1072  // the second param of match() is maker
1073  bool maker_filled = fill_limit_order( maker, maker_pays, maker_receives, true, match_price, true );
1074 
1075  match_result_type result = get_match_result( taker_filled, maker_filled );
1076  FC_ASSERT( result != match_result_type::none_filled );
1077  return result;
1078 }
1079 
1080 // When matching a limit order against settled debt, the maker actually behaviors like a call order
1081 database::match_result_type database::match_limit_settled_debt( const limit_order_object& taker,
1082  const limit_order_object& maker, const price& match_price )
1083 {
1084  FC_ASSERT( maker.is_settled_debt, "Internal error: maker is not settled debt" );
1085 
1086  bool cull_taker = false;
1087  bool maker_filled = false;
1088 
1089  auto usd_for_sale = taker.amount_for_sale();
1090  auto usd_to_buy = maker.sell_price.quote;
1091 
1092  asset call_receives;
1093  asset order_receives;
1094  if( usd_to_buy > usd_for_sale )
1095  { // fill taker limit order
1096  order_receives = usd_for_sale * match_price; // round down here, in favor of call order
1097 
1098  // Be here, it's possible that taker is paying something for nothing due to partially filled in last loop.
1099  // In this case, we see it as filled and cancel it later
1100  if( order_receives.amount == 0 )
1101  return match_result_type::only_taker_filled;
1102 
1103  // The remaining amount in the limit order could be too small,
1104  // so we should cull the order in fill_limit_order() below.
1105  // If the order would receive 0 even at `match_price`, it would receive 0 at its own price,
1106  // so calling maybe_cull_small() will always cull it.
1107  call_receives = order_receives.multiply_and_round_up( match_price );
1108  cull_taker = true;
1109  }
1110  else
1111  { // fill maker "call order"
1112  call_receives = usd_to_buy;
1113  order_receives = maker.amount_for_sale();
1114  maker_filled = true;
1115  }
1116 
1117  // seller, pays, receives, ...
1118  bool taker_filled = fill_limit_order( taker, call_receives, order_receives, cull_taker, match_price, false );
1119 
1120  // Reduce current supply
1121  const asset_dynamic_data_object& mia_ddo = call_receives.asset_id(*this).dynamic_asset_data_id(*this);
1122  modify( mia_ddo, [&call_receives]( asset_dynamic_data_object& ao ){
1123  ao.current_supply -= call_receives.amount;
1124  });
1125 
1126  // Push fill_order vitual operation
1127  // id, seller, pays, receives, ...
1128  push_applied_operation( fill_order_operation( maker.id, maker.seller, order_receives, call_receives,
1129  asset(0, call_receives.asset_id), match_price, true ) );
1130 
1131  // Update the maker order
1132  // Note: CORE asset in settled debt is not counted in account_stats.total_core_in_orders
1133  if( maker_filled )
1134  remove( maker );
1135  else
1136  {
1137  modify( maker, [&order_receives,&call_receives]( limit_order_object& obj ) {
1138  obj.for_sale -= order_receives.amount;
1139  obj.sell_price.base.amount = obj.for_sale;
1140  obj.sell_price.quote.amount -= call_receives.amount;
1141  });
1142  }
1143 
1144  match_result_type result = get_match_result( taker_filled, maker_filled );
1145  return result;
1146 }
1147 
1148 database::match_result_type database::match( const limit_order_object& bid, const call_order_object& ask,
1149  const price& match_price,
1150  const asset_bitasset_data_object& bitasset,
1151  const price& call_pays_price )
1152 {
1153  FC_ASSERT( bid.sell_asset_id() == ask.debt_type() );
1154  FC_ASSERT( bid.receive_asset_id() == ask.collateral_type() );
1155  FC_ASSERT( bid.for_sale > 0 && ask.debt > 0 && ask.collateral > 0 );
1156 
1157  bool cull_taker = false;
1158 
1159  auto maint_time = get_dynamic_global_properties().next_maintenance_time;
1160  bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME ); // call price caching issue
1161  bool after_core_hardfork_2481 = HARDFORK_CORE_2481_PASSED( maint_time ); // Match settle orders with margin calls
1162 
1163  auto head_time = head_block_time();
1164  bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time ); // Price feed issues
1165 
1166  const auto& feed_price = after_core_hardfork_2582 ? bitasset.median_feed.settlement_price
1167  : bitasset.current_feed.settlement_price;
1168  const auto& maintenance_collateral_ratio = bitasset.current_feed.maintenance_collateral_ratio;
1169  optional<price> maintenance_collateralization;
1170  if( !before_core_hardfork_1270 )
1171  maintenance_collateralization = bitasset.current_maintenance_collateralization;
1172 
1173  asset usd_for_sale = bid.amount_for_sale();
1174  asset usd_to_buy( ask.get_max_debt_to_cover( call_pays_price, feed_price, maintenance_collateral_ratio,
1175  maintenance_collateralization ),
1176  ask.debt_type() );
1177 
1178  asset call_pays;
1179  asset call_receives;
1180  asset order_pays;
1181  asset order_receives;
1182  if( usd_to_buy > usd_for_sale )
1183  { // fill limit order
1184  order_receives = usd_for_sale * match_price; // round down here, in favor of call order
1185 
1186  // Be here, it's possible that taker is paying something for nothing due to partially filled in last loop.
1187  // In this case, we see it as filled and cancel it later
1188  if( order_receives.amount == 0 )
1189  return match_result_type::only_taker_filled;
1190 
1191  call_receives = order_receives.multiply_and_round_up( match_price );
1192  if( after_core_hardfork_2481 )
1193  call_pays = call_receives * call_pays_price; // calculate with updated call_receives
1194  else
1195  // TODO add tests about CR change
1196  call_pays = usd_for_sale * call_pays_price; // (same as match_price until BSIP-74)
1197 
1198  // The remaining amount (if any) in the limit order would be too small,
1199  // so we should cull the order in fill_limit_order() below.
1200  // The order would receive 0 even at `match_price`, so it would receive 0 at its own price,
1201  // so calling maybe_cull_small() will always cull it.
1202  cull_taker = true;
1203  }
1204  else
1205  { // fill call order
1206  call_receives = usd_to_buy;
1207  order_receives = usd_to_buy.multiply_and_round_up( match_price ); // round up here, in favor of limit order
1208  call_pays = usd_to_buy.multiply_and_round_up( call_pays_price );
1209  // Note: here we don't re-assign call_receives with (orders_receives * match_price) to receive more
1210  // debt asset, it means the call order could be receiving a bit too much less than its value.
1211  // It is a sad thing for the call order, but it is the rule -- when a call order is margin called,
1212  // it does not get more than it borrowed.
1213  // On the other hand, if the call order is not being closed (due to TCR),
1214  // it means get_max_debt_to_cover() did not return a perfect result, probably we can improve it.
1215  }
1216  order_pays = call_receives;
1217 
1218  // Compute margin call fee (BSIP74). Difference between what the call order pays and the limit order
1219  // receives is the margin call fee that is paid by the call order owner to the asset issuer.
1220  // Margin call fee should equal = X*MCFR/settle_price, to within rounding error.
1221  FC_ASSERT(call_pays >= order_receives);
1222  const asset margin_call_fee = call_pays - order_receives;
1223 
1224  bool taker_filled = fill_limit_order( bid, order_pays, order_receives, cull_taker, match_price, false );
1225  bool maker_filled = fill_call_order( ask, call_pays, call_receives, match_price, true, margin_call_fee );
1226 
1227  // Update current_feed after filled call order if needed
1228  if( bitasset_options::black_swan_response_type::no_settlement == bitasset.get_black_swan_response_method() )
1229  update_bitasset_current_feed( bitasset, true );
1230 
1231  // Note: result can be none_filled when call order has target_collateral_ratio option set.
1232  match_result_type result = get_match_result( taker_filled, maker_filled );
1233  return result;
1234 }
1235 
1236 
1237 asset database::match( const force_settlement_object& settle,
1238  const call_order_object& call,
1239  const price& match_price,
1240  const asset_bitasset_data_object& bitasset,
1241  const asset& max_settlement,
1242  const price& fill_price,
1243  bool is_margin_call )
1244 {
1245  return match_impl( settle, call, match_price, bitasset, max_settlement, fill_price, is_margin_call, true );
1246 }
1247 
1248 asset database::match( const call_order_object& call,
1249  const force_settlement_object& settle,
1250  const price& match_price,
1251  const asset_bitasset_data_object& bitasset,
1252  const asset& max_settlement,
1253  const price& fill_price )
1254 {
1255  return match_impl( settle, call, match_price, bitasset, max_settlement, fill_price, true, false );
1256 }
1257 
1258 asset database::match_impl( const force_settlement_object& settle,
1259  const call_order_object& call,
1260  const price& p_match_price,
1261  const asset_bitasset_data_object& bitasset,
1262  const asset& max_settlement,
1263  const price& p_fill_price,
1264  bool is_margin_call,
1265  bool settle_is_taker )
1266 { try {
1267  FC_ASSERT(call.get_debt().asset_id == settle.balance.asset_id );
1268  FC_ASSERT(call.debt > 0 && call.collateral > 0 && settle.balance.amount > 0);
1269 
1270  auto maint_time = get_dynamic_global_properties().next_maintenance_time;
1271  bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME ); // better rounding
1272 
1273  auto settle_for_sale = std::min(settle.balance, max_settlement);
1274  auto call_debt = call.get_debt();
1275  auto call_collateral = call.get_collateral();
1276 
1277  price match_price = p_match_price;
1278  price fill_price = p_fill_price;
1279 
1280  asset call_receives = std::min(settle_for_sale, call_debt);
1281  asset call_pays = call_receives * match_price; // round down here, in favor of call order, for first check
1282  // TODO possible optimization: check need to round up
1283  // or down first
1284 
1285  // Note: when is_margin_call == true, the call order is being margin called,
1286  // match_price is the price that the call order pays,
1287  // fill_price is the price that the settle order receives,
1288  // the difference is the margin-call fee
1289 
1290  asset settle_receives = call_pays;
1291  asset settle_pays = call_receives;
1292 
1293  // Be here, the call order may be paying nothing.
1294  bool cull_settle_order = false; // whether need to cancel dust settle order
1295  if( maint_time > HARDFORK_CORE_184_TIME && call_pays.amount == 0 )
1296  {
1297  if( call_receives == call_debt ) // the call order is smaller than or equal to the settle order
1298  {
1299  call_pays.amount = 1;
1300  settle_receives.amount = 1; // Note: no margin-call fee in this case even if is_margin_call
1301  }
1302  else if( call_receives == settle.balance ) // the settle order is smaller
1303  {
1304  cancel_settle_order( settle );
1305  // If the settle order is canceled, we just return, since nothing else can be done
1306  return asset( 0, call_debt.asset_id );
1307  }
1308  // be here, neither order will be completely filled, perhaps due to max_settlement too small
1309  else if( !is_margin_call )
1310  {
1311  // If the call order is not being margin called, we simply return and continue outside
1312  return asset( 0, call_debt.asset_id );
1313  }
1314  else
1315  {
1316  // Be here, the call order is being margin called, and it is not being fully covered due to TCR,
1317  // and the settle order is big enough.
1318  // So the call order is considered as the smaller one, and we should round up call_pays.
1319  // We have ( call_receives == max_settlement == call_order.get_max_debt_to_cover() ).
1320  // It is guaranteed by call_order.get_max_debt_to_cover() that rounding up call_pays
1321  // would not reduce CR of the call order, but would push it to be above MCR.
1322  call_pays.amount = 1;
1323  settle_receives.amount = 1; // Note: no margin-call fee in this case
1324  }
1325  } // end : if after the core-184 hf and call_pays.amount == 0
1326  else if( !before_core_hardfork_342 && call_pays.amount != 0 )
1327  {
1328  auto margin_call_pays_ratio = bitasset.get_margin_call_pays_ratio();
1329  // be here, the call order is not paying nothing,
1330  // but it is still possible that the settle order is paying more than minimum required due to rounding
1331  if( call_receives == call_debt ) // the call order is smaller than or equal to the settle order
1332  {
1333  call_pays = call_receives.multiply_and_round_up( match_price ); // round up here, in favor of settle order
1334  if( is_margin_call ) // implies hf core-2481
1335  {
1336  if( call_pays.amount > call.collateral ) // CR too low
1337  {
1338  call_pays.amount = call.collateral;
1339  match_price = call_debt / call_collateral;
1340  fill_price = match_price / margin_call_pays_ratio;
1341  }
1342  settle_receives = call_receives.multiply_and_round_up( fill_price );
1343  }
1344  else // be here, we should have: call_pays <= call_collateral
1345  {
1346  settle_receives = call_pays; // Note: fill_price is not used in calculation when is_margin_call is false
1347  }
1348  }
1349  else // the call order is not completely filled, due to max_settlement too small or settle order too small
1350  {
1351  // be here, call_pays has been rounded down
1352  if( !is_margin_call )
1353  {
1354  // it was correct to round down call_pays.
1355  // round up here to mitigate rounding issues (hf core-342).
1356  // It is important to understand the math that the newly rounded-up call_receives won't be greater than
1357  // the old call_receives. And rounding up here would NOT make CR lower.
1358  call_receives = call_pays.multiply_and_round_up( match_price );
1359  }
1360  // the call order is a margin call, implies hf core-2481
1361  else if( settle_pays == max_settlement ) // the settle order is larger, but the call order has TCR
1362  {
1363  // Note: here settle_pays == call_receives
1364  call_pays = call_receives.multiply_and_round_up( match_price ); // round up, in favor of settle order
1365  settle_receives = call_receives.multiply_and_round_up( fill_price ); // round up
1366  // Note: here we do NOT stabilize call_receives since it is done in get_max_debt_to_cover(),
1367  // and it is already the maximum value
1368  }
1369  else // the call order is a margin call, and the settle order is smaller
1370  {
1371  // It was correct to round down call_pays. However, it is not the final result.
1372  // For margin calls, due to margin call fee, it is fairer to calculate with fill_price first
1373  const auto& calculate = [&settle_receives,&settle_pays,&fill_price,&call_receives,&call_pays,&match_price]
1374  {
1375  settle_receives = settle_pays * fill_price; // round down here, in favor of call order
1376  if( settle_receives.amount != 0 )
1377  {
1378  // round up to mitigate rounding issues (hf core-342)
1379  call_receives = settle_receives.multiply_and_round_up( fill_price );
1380  // round down
1381  call_pays = call_receives * match_price;
1382  }
1383  };
1384 
1385  calculate();
1386  if( settle_receives.amount == 0 )
1387  {
1388  cancel_settle_order( settle );
1389  // If the settle order is canceled, we just return, since nothing else can be done
1390  return asset( 0, call_debt.asset_id );
1391  }
1392 
1393  // check whether the call order can be filled at match_price
1394  bool cap_price = false;
1395  if( call_pays.amount >= call.collateral ) // CR too low, normally won't be true, just be defensive here
1396  cap_price = true;
1397  else
1398  {
1399  auto new_collateral = call_collateral - call_pays;
1400  auto new_debt = call_debt - call_receives; // the result is positive due to math
1401  if( ( new_collateral / new_debt ) < call.collateralization() ) // if CR would decrease
1402  cap_price = true;
1403  }
1404 
1405  if( cap_price ) // match_price is not good, update match price and fill price, then calculate again
1406  {
1407  match_price = call_debt / call_collateral;
1408  fill_price = match_price / margin_call_pays_ratio;
1409  calculate();
1410  if( settle_receives.amount == 0 )
1411  {
1412  // Note: when it is a margin call, max_settlement is max_debt_to_cover.
1413  // if need to cap price here, max_debt_to_cover should be equal to call_debt.
1414  // if call pays 0, it means the settle order is really small.
1415  cancel_settle_order( settle );
1416  // If the settle order is canceled, we just return, since nothing else can be done
1417  return asset( 0, call_debt.asset_id );
1418  }
1419  }
1420  } // end : if is_margin_call, else ...
1421 
1422  // be here, we should have: call_pays <= call_collateral
1423 
1424  // if the settle order is too small, mark it to be culled
1425  if( settle_pays == settle.balance && call_receives != settle.balance )
1426  cull_settle_order = true;
1427  // else do nothing, since we can't cull the settle order, or it is already fully filled
1428 
1429  settle_pays = call_receives;
1430  }
1431  } // end : if after the core-342 hf and call_pays.amount != 0
1432  // else : before the core-184 hf or the core-342 hf, do nothing
1433 
1441  if( before_core_hardfork_342 )
1442  {
1443  GRAPHENE_ASSERT( call_pays < call_collateral, black_swan_exception, "" );
1444 
1445  assert( settle_pays == settle_for_sale || call_receives == call.get_debt() );
1446  }
1447  // else do nothing, since black swan event won't happen, and the assertion is no longer true
1448 
1449  asset margin_call_fee = call_pays - settle_receives;
1450 
1451  fill_call_order( call, call_pays, call_receives, fill_price, settle_is_taker, margin_call_fee );
1452  // do not pay force-settlement fee if the call is being margin called
1453  fill_settle_order( settle, settle_pays, settle_receives, fill_price, !settle_is_taker, !is_margin_call );
1454 
1455  // Update current_feed after filled call order if needed
1456  if( bitasset_options::black_swan_response_type::no_settlement == bitasset.get_black_swan_response_method() )
1457  update_bitasset_current_feed( bitasset, true );
1458 
1459  if( cull_settle_order )
1460  cancel_settle_order( settle );
1461 
1462  return call_receives;
1463 } FC_CAPTURE_AND_RETHROW( (p_match_price)(max_settlement)(p_fill_price)(is_margin_call)(settle_is_taker) ) }
1464 
1465 bool database::fill_limit_order( const limit_order_object& order, const asset& pays, const asset& receives,
1466  bool cull_if_small, const price& fill_price, const bool is_maker)
1467 { try {
1468  if( head_block_time() < HARDFORK_555_TIME )
1469  cull_if_small = true;
1470 
1471  FC_ASSERT( order.amount_for_sale().asset_id == pays.asset_id );
1472  FC_ASSERT( pays.asset_id != receives.asset_id );
1473 
1474  const account_object& seller = order.seller(*this);
1475 
1476  const auto issuer_fees = pay_market_fees(&seller, receives.asset_id(*this), receives, is_maker);
1477 
1478  pay_order( seller, receives - issuer_fees, pays );
1479 
1480  assert( pays.asset_id != receives.asset_id );
1481  push_applied_operation( fill_order_operation( order.id, order.seller, pays, receives,
1482  issuer_fees, fill_price, is_maker ) );
1483 
1484  // BSIP85: Maker order creation fee discount, https://github.com/bitshares/bsips/blob/master/bsip-0085.md
1485  // if the order creation fee was paid in BTS,
1486  // return round_down(deferred_fee * maker_fee_discount_percent) to the owner,
1487  // then process the remaining deferred fee as before;
1488  // if the order creation fee was paid in another asset,
1489  // return round_down(deferred_paid_fee * maker_fee_discount_percent) to the owner,
1490  // return round_down(deferred_fee * maker_fee_discount_percent) to the fee pool of the asset,
1491  // then process the remaining deferred fee and deferred paid fee as before.
1492  const uint16_t maker_discount_percent = get_global_properties().parameters.get_maker_fee_discount_percent();
1493 
1494  // Save local copies for calculation
1495  share_type deferred_fee = order.deferred_fee;
1496  share_type deferred_paid_fee = order.deferred_paid_fee.amount;
1497 
1498  // conditional because cheap integer comparison may allow us to avoid two expensive modify() and object lookups
1499  if( order.deferred_paid_fee.amount > 0 ) // implies head_block_time() > HARDFORK_CORE_604_TIME
1500  {
1501  share_type fee_pool_refund = 0;
1502  if( is_maker && maker_discount_percent > 0 )
1503  {
1504  share_type refund = detail::calculate_percent( deferred_paid_fee, maker_discount_percent );
1505  // Note: it's possible that the deferred_paid_fee is very small,
1506  // which can result in a zero refund due to rounding issue,
1507  // in this case, no refund to the fee pool
1508  if( refund > 0 )
1509  {
1510  FC_ASSERT( refund <= deferred_paid_fee, "Internal error" );
1511  adjust_balance( order.seller, asset(refund, order.deferred_paid_fee.asset_id) );
1512  deferred_paid_fee -= refund;
1513 
1514  // deferred_fee might be positive too
1515  FC_ASSERT( deferred_fee > 0, "Internal error" );
1516  fee_pool_refund = detail::calculate_percent( deferred_fee, maker_discount_percent );
1517  FC_ASSERT( fee_pool_refund <= deferred_fee, "Internal error" );
1518  deferred_fee -= fee_pool_refund;
1519  }
1520  }
1521 
1522  const auto& fee_asset_dyn_data = order.deferred_paid_fee.asset_id(*this).dynamic_asset_data_id(*this);
1523  modify( fee_asset_dyn_data, [deferred_paid_fee,fee_pool_refund](asset_dynamic_data_object& addo) {
1524  addo.accumulated_fees += deferred_paid_fee;
1525  addo.fee_pool += fee_pool_refund;
1526  });
1527  }
1528 
1529  if( order.deferred_fee > 0 )
1530  {
1531  if( order.deferred_paid_fee.amount <= 0 // paid in CORE, or before HF 604
1532  && is_maker && maker_discount_percent > 0 )
1533  {
1534  share_type refund = detail::calculate_percent( deferred_fee, maker_discount_percent );
1535  if( refund > 0 )
1536  {
1537  FC_ASSERT( refund <= deferred_fee, "Internal error" );
1538  adjust_balance( order.seller, asset(refund, asset_id_type()) );
1539  deferred_fee -= refund;
1540  }
1541  }
1542  // else do nothing here, because we have already processed it above, or no need to process
1543 
1544  if( deferred_fee > 0 )
1545  {
1546  modify( seller.statistics(*this), [deferred_fee,this]( account_statistics_object& statistics )
1547  {
1548  statistics.pay_fee( deferred_fee, get_global_properties().parameters.cashback_vesting_threshold );
1549  } );
1550  }
1551  }
1552 
1553  if( pays == order.amount_for_sale() )
1554  {
1555  remove( order );
1556  return true;
1557  }
1558  else
1559  {
1560  modify( order, [&pays]( limit_order_object& b ) {
1561  b.for_sale -= pays.amount;
1562  b.deferred_fee = 0;
1563  b.deferred_paid_fee.amount = 0;
1564  });
1565  if( cull_if_small )
1566  return maybe_cull_small_order( *this, order );
1567  return false;
1568  }
1569 } FC_CAPTURE_AND_RETHROW( (pays)(receives) ) }
1570 
1571 /***
1572  * @brief fill a call order in the specified amounts
1573  * @param order the call order
1574  * @param pays What the call order will give to the other party (collateral)
1575  * @param receives what the call order will receive from the other party (debt)
1576  * @param fill_price the price at which the call order will execute
1577  * @param is_maker TRUE if the call order is the maker, FALSE if it is the taker
1578  * @param margin_call_fee Margin call fees paid in collateral asset
1579  * @returns TRUE if the call order was completely filled
1580  */
1581 bool database::fill_call_order( const call_order_object& order, const asset& pays, const asset& receives,
1582  const price& fill_price, const bool is_maker, const asset& margin_call_fee, bool reduce_current_supply )
1583 { try {
1584  FC_ASSERT( order.debt_type() == receives.asset_id );
1585  FC_ASSERT( order.collateral_type() == pays.asset_id );
1586  FC_ASSERT( order.collateral >= pays.amount );
1587 
1588  // TODO pass in mia and bitasset_data for better performance
1589  const asset_object& mia = receives.asset_id(*this);
1590  FC_ASSERT( mia.is_market_issued() );
1591  const asset_bitasset_data_object& bitasset = mia.bitasset_data(*this);
1592 
1593  optional<asset> collateral_freed;
1594  // adjust the order
1595  modify( order, [&]( call_order_object& o ) {
1596  o.debt -= receives.amount;
1597  o.collateral -= pays.amount;
1598  if( o.debt == 0 ) // is the whole debt paid?
1599  {
1600  collateral_freed = o.get_collateral();
1601  o.collateral = 0;
1602  }
1603  else // the debt was not completely paid
1604  {
1605  auto maint_time = get_dynamic_global_properties().next_maintenance_time;
1606  // update call_price after core-343 hard fork,
1607  // but don't update call_price after core-1270 hard fork
1608  if( maint_time <= HARDFORK_CORE_1270_TIME && maint_time > HARDFORK_CORE_343_TIME )
1609  {
1610  o.call_price = price::call_price( o.get_debt(), o.get_collateral(),
1611  bitasset.current_feed.maintenance_collateral_ratio );
1612  }
1613  }
1614  });
1615 
1616  // update current supply
1617  if( reduce_current_supply )
1618  {
1619  const asset_dynamic_data_object& mia_ddo = mia.dynamic_asset_data_id(*this);
1620  modify( mia_ddo, [&receives]( asset_dynamic_data_object& ao ){
1621  ao.current_supply -= receives.amount;
1622  });
1623  }
1624 
1625  // If the whole debt is paid, adjust borrower's collateral balance
1626  if( collateral_freed.valid() )
1627  adjust_balance( order.borrower, *collateral_freed );
1628 
1629  // Update account statistics. We know that order.collateral_type() == pays.asset_id
1630  if( pays.asset_id == asset_id_type() )
1631  {
1632  modify( get_account_stats_by_owner(order.borrower), [&collateral_freed,&pays]( account_statistics_object& b ){
1633  b.total_core_in_orders -= pays.amount;
1634  if( collateral_freed.valid() )
1635  b.total_core_in_orders -= collateral_freed->amount;
1636  });
1637  }
1638 
1639  // BSIP74: Accumulate the collateral-denominated fee
1640  if (margin_call_fee.amount.value != 0)
1641  mia.accumulate_fee(*this, margin_call_fee);
1642 
1643  // virtual operation for account history
1644  push_applied_operation( fill_order_operation( order.id, order.borrower, pays, receives,
1645  margin_call_fee, fill_price, is_maker ) );
1646 
1647  // Call order completely filled, remove it
1648  if( collateral_freed.valid() )
1649  remove( order );
1650 
1651  return collateral_freed.valid();
1652 } FC_CAPTURE_AND_RETHROW( (pays)(receives) ) }
1653 
1654 /***
1655  * @brief fullfill a settle order in the specified amounts
1656  *
1657  * @details Called from database::match(), this coordinates exchange of debt asset X held in the
1658  * settle order for collateral asset Y held in a call order, and routes fees. Note that we
1659  * don't touch the call order directly, as match() handles this via a separate call to
1660  * fill_call_order(). We are told exactly how much X and Y to exchange, based on details of
1661  * order matching determined higher up the call chain. Thus it is possible that the settle
1662  * order is not completely satisfied at the conclusion of this function.
1663  *
1664  * @param settle the force_settlement object
1665  * @param pays the quantity of market-issued debt asset X which the settler will yield in this
1666  * round (may be less than the full amount indicated in settle object)
1667  * @param receives the quantity of collateral asset Y which the settler will receive in
1668  * exchange for X
1669  * @param fill_price the price at which the settle order will execute (not used - passed through
1670  * to virtual operation)
1671  * @param is_maker TRUE if the settle order is the maker, FALSE if it is the taker (passed
1672  * through to virtual operation)
1673  * @returns TRUE if the settle order was completely filled, FALSE if only partially filled
1674  */
1675 bool database::fill_settle_order( const force_settlement_object& settle, const asset& pays, const asset& receives,
1676  const price& fill_price, bool is_maker, bool pay_force_settle_fee )
1677 { try {
1678  bool filled = false;
1679 
1680  const account_object* settle_owner_ptr = nullptr;
1681  // The owner of the settle order pays market fees to the issuer of the collateral asset.
1682  // After HF core-1780, these fees are shared to the referral program, which is flagged to
1683  // pay_market_fees by setting settle_owner_ptr non-null.
1684  //
1685  // TODO Check whether the HF check can be removed after the HF.
1686  // Note: even if logically it can be removed, perhaps the removal will lead to a small performance
1687  // loss. Needs testing.
1688  if( head_block_time() >= HARDFORK_CORE_1780_TIME )
1689  settle_owner_ptr = &settle.owner(*this);
1690  // Compute and pay the market fees:
1691  asset market_fees = pay_market_fees( settle_owner_ptr, get(receives.asset_id), receives, is_maker );
1692 
1693  // Issuer of the settled smartcoin asset lays claim to a force-settlement fee (BSIP87), but
1694  // note that fee is denominated in collateral asset, not the debt asset. Asset object of
1695  // debt asset is passed to the pay function so it knows where to put the fee. Note that
1696  // amount of collateral asset upon which fee is assessed is reduced by market_fees already
1697  // paid to prevent the total fee exceeding total collateral.
1698  asset force_settle_fees = pay_force_settle_fee
1699  ? pay_force_settle_fees( get(pays.asset_id), receives - market_fees )
1700  : asset( 0, receives.asset_id );
1701 
1702  auto total_collateral_denominated_fees = market_fees + force_settle_fees;
1703 
1704  // If we don't consume entire settle order:
1705  if( pays < settle.balance )
1706  {
1707  modify(settle, [&pays](force_settlement_object& s) {
1708  s.balance -= pays;
1709  });
1710  } else {
1711  filled = true;
1712  }
1713  // Give released collateral not already taken as fees to settle order owner:
1714  adjust_balance(settle.owner, receives - total_collateral_denominated_fees);
1715 
1716  assert( pays.asset_id != receives.asset_id );
1717  push_applied_operation( fill_order_operation( settle.id, settle.owner, pays, receives,
1718  total_collateral_denominated_fees, fill_price, is_maker ) );
1719 
1720  if (filled)
1721  remove(settle);
1722 
1723  return filled;
1724 
1725 } FC_CAPTURE_AND_RETHROW( (pays)(receives) ) }
1726 
1745 bool database::check_call_orders( const asset_object& mia, bool enable_black_swan, bool for_new_limit_order,
1746  const asset_bitasset_data_object* bitasset_ptr,
1747  bool mute_exceptions, bool skip_matching_settle_orders )
1748 { try {
1749  const auto& dyn_prop = get_dynamic_global_properties();
1750  auto maint_time = dyn_prop.next_maintenance_time;
1751  if( for_new_limit_order )
1752  FC_ASSERT( maint_time <= HARDFORK_CORE_625_TIME ); // `for_new_limit_order` is only true before HF 338 / 625
1753 
1754  if( !mia.is_market_issued() ) return false;
1755 
1756  const asset_bitasset_data_object& bitasset = ( bitasset_ptr ? *bitasset_ptr : mia.bitasset_data(*this) );
1757 
1758  // price feeds can cause black swans in prediction markets
1759  // The hardfork check may be able to be removed after the hardfork date
1760  // if check_for_blackswan never triggered a black swan on a prediction market.
1761  // NOTE: check_for_blackswan returning true does not always mean a black
1762  // swan was triggered.
1763  if ( maint_time >= HARDFORK_CORE_460_TIME && bitasset.is_prediction_market )
1764  return false;
1765 
1767  const auto bsrm = bitasset.get_black_swan_response_method();
1768 
1769  // Only check for black swan here if BSRM is not individual settlement
1770  if( bsrm_type::individual_settlement_to_fund != bsrm
1771  && bsrm_type::individual_settlement_to_order != bsrm
1772  && check_for_blackswan( mia, enable_black_swan, &bitasset ) )
1773  return false;
1774 
1775  if( bitasset.is_prediction_market ) return false;
1776  if( bitasset.current_feed.settlement_price.is_null() ) return false;
1777 
1778  const limit_order_index& limit_index = get_index_type<limit_order_index>();
1779  const auto& limit_price_index = limit_index.indices().get<by_price>();
1780 
1781  bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME ); // call price caching issue
1782  bool after_core_hardfork_2481 = HARDFORK_CORE_2481_PASSED( maint_time ); // Match settle orders with margin calls
1783 
1784  // Looking for limit orders selling the most USD for the least CORE.
1785  auto max_price = price::max( bitasset.asset_id, bitasset.options.short_backing_asset );
1786  // Stop when limit orders are selling too little USD for too much CORE.
1787  // Note that since BSIP74, margin calls offer somewhat less CORE per USD
1788  // if the issuer claims a Margin Call Fee.
1789  auto min_price = before_core_hardfork_1270 ?
1791  : bitasset.get_margin_call_order_price();
1792 
1793  // NOTE limit_price_index is sorted from greatest to least
1794  auto limit_itr = limit_price_index.lower_bound( max_price );
1795  auto limit_end = limit_price_index.upper_bound( min_price );
1796 
1797  // Before the core-2481 hf, only check limit orders
1798  if( !after_core_hardfork_2481 && limit_itr == limit_end )
1799  return false;
1800 
1801  const call_order_index& call_index = get_index_type<call_order_index>();
1802  const auto& call_price_index = call_index.indices().get<by_price>();
1803  // Note: it is safe to iterate here even if there is no call order due to individual settlements
1804  const auto& call_collateral_index = call_index.indices().get<by_collateral>();
1805 
1806  auto call_min = price::min( bitasset.options.short_backing_asset, bitasset.asset_id );
1807  auto call_max = price::max( bitasset.options.short_backing_asset, bitasset.asset_id );
1808 
1809  auto call_price_itr = call_price_index.begin();
1810  auto call_price_end = call_price_itr;
1811  auto call_collateral_itr = call_collateral_index.begin();
1812  auto call_collateral_end = call_collateral_itr;
1813 
1814  if( before_core_hardfork_1270 )
1815  {
1816  call_price_itr = call_price_index.lower_bound( call_min );
1817  call_price_end = call_price_index.upper_bound( call_max );
1818  }
1819  else
1820  {
1821  call_collateral_itr = call_collateral_index.lower_bound( call_min );
1822  call_collateral_end = call_collateral_index.upper_bound( call_max );
1823  }
1824 
1825  bool filled_limit = false;
1826  bool margin_called = false; // toggles true once/if we actually execute a margin call
1827 
1828  auto head_time = head_block_time();
1829  auto head_num = head_block_num();
1830 
1831  bool before_hardfork_615 = ( head_time < HARDFORK_615_TIME );
1832  bool after_hardfork_436 = ( head_time > HARDFORK_436_TIME );
1833 
1834  bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME ); // better rounding
1835  bool before_core_hardfork_343 = ( maint_time <= HARDFORK_CORE_343_TIME ); // update call_price on partial fill
1836  bool before_core_hardfork_453 = ( maint_time <= HARDFORK_CORE_453_TIME ); // multiple matching issue
1837  bool before_core_hardfork_606 = ( maint_time <= HARDFORK_CORE_606_TIME ); // feed always trigger call
1838  bool before_core_hardfork_834 = ( maint_time <= HARDFORK_CORE_834_TIME ); // target collateral ratio option
1839 
1840  bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time ); // Price feed issues
1841 
1842  auto has_call_order = [ before_core_hardfork_1270,
1843  &call_collateral_itr,&call_collateral_end,
1844  &call_price_itr,&call_price_end ]()
1845  {
1846  return before_core_hardfork_1270 ? ( call_price_itr != call_price_end )
1847  : ( call_collateral_itr != call_collateral_end );
1848  };
1849 
1850  bool update_current_feed = ( bsrm_type::no_settlement == bsrm && bitasset.is_current_feed_price_capped() );
1851 
1852  const auto& settlement_index = get_index_type<force_settlement_index>().indices().get<by_expiration>();
1853 
1854  while( has_call_order() )
1855  {
1856  // check for blackswan first // TODO perhaps improve performance by passing in iterators
1857  bool settled_some = check_for_blackswan( mia, enable_black_swan, &bitasset );
1858  if( bitasset.has_settlement() )
1859  return margin_called;
1860 
1861  if( settled_some ) // which implies that BSRM is individual settlement to fund or to order
1862  {
1863  call_collateral_itr = call_collateral_index.lower_bound( call_min );
1864  if( call_collateral_itr == call_collateral_end )
1865  return true;
1866  margin_called = true;
1867  if( bsrm_type::individual_settlement_to_fund == bsrm )
1868  limit_end = limit_price_index.upper_bound( bitasset.get_margin_call_order_price() );
1869  }
1870 
1871  // be here, there exists at least one call order
1872  const call_order_object& call_order = ( before_core_hardfork_1270 ? *call_price_itr : *call_collateral_itr );
1873 
1874  // Feed protected (don't call if CR>MCR) https://github.com/cryptonomex/graphene/issues/436
1875  bool feed_protected = before_core_hardfork_1270 ?
1876  ( after_hardfork_436 && bitasset.current_feed.settlement_price > ~call_order.call_price )
1877  : ( bitasset.current_maintenance_collateralization < call_order.collateralization() );
1878  if( feed_protected )
1879  return margin_called;
1880 
1881  // match call orders with limit orders
1882  if( limit_itr != limit_end )
1883  {
1884  const limit_order_object& limit_order = *limit_itr;
1885 
1886  price match_price = limit_order.sell_price;
1887  // There was a check `match_price.validate();` here, which is removed now because it always passes
1888 
1889  // Old rule: margin calls can only buy high https://github.com/bitshares/bitshares-core/issues/606
1890  if( before_core_hardfork_606 && match_price > ~call_order.call_price )
1891  return margin_called;
1892 
1893  margin_called = true;
1894 
1895  price call_pays_price = match_price * bitasset.get_margin_call_pays_ratio();
1896  // Since BSIP74, the call "pays" a bit more collateral per debt than the match price, with the
1897  // excess being kept by the asset issuer as a margin call fee. In what follows, we use
1898  // call_pays_price for the black swan check, and for the TCR, but we still use the match_price,
1899  // of course, to determine what the limit order receives. Note margin_call_pays_ratio() returns
1900  // 1/1 if margin_call_fee_ratio is unset (i.e. before BSIP74), so hardfork check is implicit.
1901 
1902  // Although we checked for black swan above, we do one more check to ensure the call order can
1903  // pay the amount of collateral which we intend to take from it (including margin call fee).
1904  // TODO refactor code for better performance and readability, perhaps extract the new logic to a new
1905  // function and call it after hf_1270, hf_bsip74 or hf_2481.
1906  auto usd_to_buy = call_order.get_debt();
1907  if( !after_core_hardfork_2481 && ( usd_to_buy * call_pays_price ) > call_order.get_collateral() )
1908  {
1909  // Trigger black swan
1910  elog( "black swan detected on asset ${symbol} (${id}) at block ${b}",
1911  ("id",bitasset.asset_id)("symbol",mia.symbol)("b",head_num) );
1912  edump((enable_black_swan));
1913  FC_ASSERT( enable_black_swan );
1914  globally_settle_asset(mia, bitasset.current_feed.settlement_price );
1915  return true;
1916  }
1917 
1918  if( !before_core_hardfork_1270 )
1919  {
1920  auto settle_price = after_core_hardfork_2582 ? bitasset.median_feed.settlement_price
1921  : bitasset.current_feed.settlement_price;
1922  usd_to_buy.amount = call_order.get_max_debt_to_cover( call_pays_price,
1923  settle_price,
1926  }
1927  else if( !before_core_hardfork_834 )
1928  {
1929  usd_to_buy.amount = call_order.get_max_debt_to_cover( call_pays_price,
1930  bitasset.current_feed.settlement_price,
1932  }
1933 
1934  asset usd_for_sale = limit_order.amount_for_sale();
1935  asset call_pays, call_receives, limit_pays, limit_receives;
1936 
1937  struct UndercollateralizationException {};
1938  try { // throws UndercollateralizationException if the call order is undercollateralized
1939 
1940  bool filled_call = false;
1941 
1942  if( usd_to_buy > usd_for_sale )
1943  { // fill order
1944  limit_receives = usd_for_sale * match_price; // round down, in favor of call order
1945 
1946  // Be here, the limit order won't be paying something for nothing, since if it would, it would have
1947  // been cancelled elsewhere already (a maker limit order won't be paying something for nothing):
1948  // * after hard fork core-625, the limit order will be always a maker if entered this function;
1949  // * before hard fork core-625,
1950  // * when the limit order is a taker, it could be paying something for nothing only when
1951  // the call order is smaller and is too small
1952  // * when the limit order is a maker, it won't be paying something for nothing
1953 
1954  if( before_core_hardfork_342 )
1955  call_receives = usd_for_sale;
1956  else
1957  // The remaining amount in the limit order would be too small,
1958  // so we should cull the order in fill_limit_order() below.
1959  // The order would receive 0 even at `match_price`, so it would receive 0 at its own price,
1960  // so calling maybe_cull_small() will always cull it.
1961  call_receives = limit_receives.multiply_and_round_up( match_price );
1962 
1963  if( !after_core_hardfork_2481 )
1964  // TODO add tests about CR change
1965  call_pays = usd_for_sale * call_pays_price; // (same as match_price until BSIP-74)
1966  else
1967  {
1968  call_pays = call_receives * call_pays_price; // calculate with updated call_receives
1969  if( call_pays.amount >= call_order.collateral )
1970  throw UndercollateralizationException();
1971  auto new_collateral = call_order.get_collateral() - call_pays;
1972  auto new_debt = call_order.get_debt() - call_receives; // the result is positive due to math
1973  if( ( new_collateral / new_debt ) < call_order.collateralization() ) // if CR would decrease
1974  throw UndercollateralizationException();
1975  }
1976 
1977  filled_limit = true;
1978 
1979  } else { // fill call, could be partial fill due to TCR
1980  call_receives = usd_to_buy;
1981 
1982  if( before_core_hardfork_342 )
1983  {
1984  limit_receives = usd_to_buy * match_price; // round down, in favor of call order
1985  call_pays = limit_receives;
1986  } else {
1987  call_pays = usd_to_buy.multiply_and_round_up( call_pays_price ); // BSIP74; excess is fee.
1988  // Note: Due to different rounding, this could potentialy be
1989  // one satoshi more than the blackswan check above
1990  if( call_pays.amount > call_order.collateral )
1991  {
1992  if( after_core_hardfork_2481 )
1993  throw UndercollateralizationException();
1994  if( mute_exceptions )
1995  call_pays.amount = call_order.collateral;
1996  }
1997  // Note: if it is a partial fill due to TCR, the math guarantees that the new CR will be higher
1998  // than the old CR, so no additional check for potential blackswan here
1999 
2000  limit_receives = usd_to_buy.multiply_and_round_up( match_price ); // round up, favors limit order
2001  if( limit_receives.amount > call_order.collateral ) // implies !after_hf_2481
2002  limit_receives.amount = call_order.collateral;
2003  // Note: here we don't re-assign call_receives with (orders_receives * match_price) to receive more
2004  // debt asset, it means the call order could be receiving a bit too much less than its value.
2005  // It is a sad thing for the call order, but it is the rule
2006  // -- when a call order is margin called, it does not get more than it borrowed.
2007  // On the other hand, if the call order is not being closed (due to TCR),
2008  // it means get_max_debt_to_cover() did not return a perfect result, maybe we can improve it.
2009  }
2010 
2011  filled_call = true; // this is safe, since BSIP38 (hard fork core-834) depends on BSIP31 (hf core-343)
2012 
2013  if( usd_to_buy == usd_for_sale )
2014  filled_limit = true;
2015  else if( filled_limit && before_hardfork_615 )
2016  //NOTE: Multiple limit match problem (see issue 453, yes this happened)
2017  _issue_453_affected_assets.insert( bitasset.asset_id );
2018  }
2019  limit_pays = call_receives;
2020 
2021  // BSIP74: Margin call fee
2022  FC_ASSERT(call_pays >= limit_receives);
2023  const asset margin_call_fee = call_pays - limit_receives;
2024 
2025  if( filled_call && before_core_hardfork_343 )
2026  ++call_price_itr;
2027 
2028  // when for_new_limit_order is true, the call order is maker, otherwise the call order is taker
2029  fill_call_order( call_order, call_pays, call_receives, match_price, for_new_limit_order, margin_call_fee);
2030 
2031  // Update current_feed after filled call order if needed
2032  if( update_current_feed )
2033  {
2034  update_bitasset_current_feed( bitasset, true );
2035  limit_end = limit_price_index.upper_bound( bitasset.get_margin_call_order_price() );
2036  update_current_feed = bitasset.is_current_feed_price_capped();
2037  }
2038 
2039  if( !before_core_hardfork_1270 )
2040  call_collateral_itr = call_collateral_index.lower_bound( call_min );
2041  else if( !before_core_hardfork_343 )
2042  call_price_itr = call_price_index.lower_bound( call_min );
2043 
2044  auto next_limit_itr = std::next( limit_itr );
2045  // when for_new_limit_order is true, the limit order is taker, otherwise the limit order is maker
2046  bool really_filled = fill_limit_order( limit_order, limit_pays, limit_receives, true,
2047  match_price, !for_new_limit_order );
2048  if( really_filled || ( filled_limit && before_core_hardfork_453 ) )
2049  limit_itr = next_limit_itr;
2050 
2051  continue; // check for blackswan again
2052 
2053  } catch( const UndercollateralizationException& ) {
2054  // Nothing to do here
2055  }
2056  } // if there is a matching limit order
2057 
2058  // be here, it is unable to fill a limit order due to undercollateralization (and there is a force settlement),
2059  // or there is no matching limit order due to MSSR, or no limit order at all
2060 
2061  // If no need to process force settlements, we return
2062  // Note: before core-2481/2467 hf, or BSRM is no_settlement and processing a new force settlement
2063  if( skip_matching_settle_orders || !after_core_hardfork_2481 )
2064  return margin_called;
2065 
2066  // If no force settlements, we return
2067  auto settle_itr = settlement_index.lower_bound( bitasset.asset_id );
2068  if( settle_itr == settlement_index.end() || settle_itr->balance.asset_id != bitasset.asset_id )
2069  return margin_called;
2070 
2071  // Check margin calls against force settlements
2072  // Note: we always need to recheck limit orders after processed call-settle match,
2073  // in case when the least collateralized short was undercollateralized.
2074  if( match_force_settlements( bitasset ) )
2075  {
2076  margin_called = true;
2077  call_collateral_itr = call_collateral_index.lower_bound( call_min );
2078  if( update_current_feed )
2079  {
2080  // Note: we do not call update_bitasset_current_feed() here,
2081  // because it's called in match_impl() in match() in match_force_settlements()
2082  limit_end = limit_price_index.upper_bound( bitasset.get_margin_call_order_price() );
2083  update_current_feed = bitasset.is_current_feed_price_capped();
2084  }
2085  }
2086  // else : no more force settlements, or feed protected, both will be handled in the next loop
2087  } // while there exists a call order
2088  return margin_called;
2090 
2091 bool database::match_force_settlements( const asset_bitasset_data_object& bitasset )
2092 {
2093  // Defensive checks
2094  auto maint_time = get_dynamic_global_properties().next_maintenance_time;
2095  FC_ASSERT( HARDFORK_CORE_2481_PASSED( maint_time ), "Internal error: hard fork core-2481 not passed" );
2096  FC_ASSERT( !bitasset.is_prediction_market, "Internal error: asset is a prediction market" );
2097  FC_ASSERT( !bitasset.has_settlement(), "Internal error: asset is globally settled already" );
2098  FC_ASSERT( !bitasset.current_feed.settlement_price.is_null(), "Internal error: no sufficient price feeds" );
2099 
2100  auto head_time = head_block_time();
2101  bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time ); // Price feed issues
2102 
2103  const auto& settlement_index = get_index_type<force_settlement_index>().indices().get<by_expiration>();
2104  auto settle_itr = settlement_index.lower_bound( bitasset.asset_id );
2105  auto settle_end = settlement_index.upper_bound( bitasset.asset_id );
2106 
2107  // Note: it is safe to iterate here even if there is no call order due to individual settlements
2108  const auto& call_collateral_index = get_index_type<call_order_index>().indices().get<by_collateral>();
2109  auto call_min = price::min( bitasset.options.short_backing_asset, bitasset.asset_id );
2110  auto call_max = price::max( bitasset.options.short_backing_asset, bitasset.asset_id );
2111  auto call_itr = call_collateral_index.lower_bound( call_min );
2112  auto call_end = call_collateral_index.upper_bound( call_max );
2113 
2114  // Price at which margin calls sit on the books.
2115  // It is the MCOP, which may deviate from MSSP due to MCFR.
2116  // It is in debt/collateral .
2117  price call_match_price = bitasset.get_margin_call_order_price();
2118  // Price margin call actually relinquishes collateral at. Equals the MSSP and it may
2119  // differ from call_match_price if there is a Margin Call Fee.
2120  // It is in debt/collateral .
2121  price call_pays_price = bitasset.current_feed.max_short_squeeze_price();
2122 
2123  while( settle_itr != settle_end && call_itr != call_end )
2124  {
2125  const force_settlement_object& settle_order = *settle_itr;
2126  const call_order_object& call_order = *call_itr;
2127 
2128  // Feed protected (don't call if CR>MCR) https://github.com/cryptonomex/graphene/issues/436
2129  if( bitasset.current_maintenance_collateralization < call_order.collateralization() )
2130  return false;
2131 
2132  // TCR applies here
2133  auto settle_price = after_core_hardfork_2582 ? bitasset.median_feed.settlement_price
2134  : bitasset.current_feed.settlement_price;
2135  asset max_debt_to_cover( call_order.get_max_debt_to_cover( call_pays_price,
2136  settle_price,
2139  bitasset.asset_id );
2140 
2141  // Note: if the call order's CR is too low, it is probably unable to fill at call_pays_price.
2142  // In this case, the call order pays at its CR, the settle order may receive less due to margin call fee.
2143  // It is processed inside the function.
2144  auto result = match( call_order, settle_order, call_pays_price, bitasset, max_debt_to_cover, call_match_price );
2145 
2146  // if result.amount > 0, it means the call order got updated or removed
2147  // in this case, we need to check limit orders first, so we return
2148  if( result.amount > 0 )
2149  return true;
2150  // else : result.amount == 0, it means the settle order got canceled directly and the call order did not change
2151 
2152  settle_itr = settlement_index.lower_bound( bitasset.asset_id );
2153  call_itr = call_collateral_index.lower_bound( call_min );
2154  }
2155  return false;
2156 }
2157 
2158 void database::pay_order( const account_object& receiver, const asset& receives, const asset& pays )
2159 {
2160  if( pays.asset_id == asset_id_type() )
2161  {
2162  const auto& stats = receiver.statistics(*this);
2163  modify( stats, [&pays]( account_statistics_object& b ){
2164  b.total_core_in_orders -= pays.amount;
2165  });
2166  }
2167  adjust_balance(receiver.get_id(), receives);
2168 }
2169 
2170 asset database::calculate_market_fee( const asset_object& trade_asset, const asset& trade_amount,
2171  const bool& is_maker )const
2172 {
2173  assert( trade_asset.id == trade_amount.asset_id );
2174 
2175  if( !trade_asset.charges_market_fees() )
2176  return trade_asset.amount(0);
2177  // Optimization: The fee is zero if the order is a maker, and the maker fee percent is 0%
2178  if( is_maker && trade_asset.options.market_fee_percent == 0 )
2179  return trade_asset.amount(0);
2180 
2181  // Optimization: The fee is zero if the order is a taker, and the taker fee percent is 0%
2182  const optional<uint16_t>& taker_fee_percent = trade_asset.options.extensions.value.taker_fee_percent;
2183  if(!is_maker && taker_fee_percent.valid() && *taker_fee_percent == 0)
2184  return trade_asset.amount(0);
2185 
2186  uint16_t fee_percent;
2187  if (is_maker) {
2188  // Maker orders are charged the maker fee percent
2189  fee_percent = trade_asset.options.market_fee_percent;
2190  } else {
2191  // Taker orders are charged the taker fee percent if they are valid. Otherwise, the maker fee percent.
2192  fee_percent = taker_fee_percent.valid() ? *taker_fee_percent : trade_asset.options.market_fee_percent;
2193  }
2194 
2195  auto value = detail::calculate_percent(trade_amount.amount, fee_percent);
2196  asset percent_fee = trade_asset.amount(value);
2197 
2198  if( percent_fee.amount > trade_asset.options.max_market_fee )
2199  percent_fee.amount = trade_asset.options.max_market_fee;
2200 
2201  return percent_fee;
2202 }
2203 
2204 
2205 asset database::pay_market_fees(const account_object* seller, const asset_object& recv_asset, const asset& receives,
2206  const bool& is_maker, const optional<asset>& calculated_market_fees )
2207 {
2208  const auto market_fees = ( calculated_market_fees.valid() ? *calculated_market_fees
2209  : calculate_market_fee( recv_asset, receives, is_maker ) );
2210  auto issuer_fees = market_fees;
2211  FC_ASSERT( issuer_fees <= receives, "Market fee shouldn't be greater than receives");
2212  //Don't dirty undo state if not actually collecting any fees
2213  if ( issuer_fees.amount > 0 )
2214  {
2215  // Share market fees to the network
2216  const uint16_t network_percent = get_global_properties().parameters.get_market_fee_network_percent();
2217  if( network_percent > 0 )
2218  {
2219  const auto network_fees_amt = detail::calculate_percent( issuer_fees.amount, network_percent );
2220  FC_ASSERT( network_fees_amt <= issuer_fees.amount,
2221  "Fee shared to the network shouldn't be greater than total market fee" );
2222  if( network_fees_amt > 0 )
2223  {
2224  const asset network_fees = recv_asset.amount( network_fees_amt );
2225  deposit_market_fee_vesting_balance( GRAPHENE_COMMITTEE_ACCOUNT, network_fees );
2226  issuer_fees -= network_fees;
2227  }
2228  }
2229  }
2230 
2231  // Process the remaining fees
2232  if ( issuer_fees.amount > 0 )
2233  {
2234  // calculate and pay rewards
2235  asset reward = recv_asset.amount(0);
2236 
2237  auto is_rewards_allowed = [&recv_asset, seller]() {
2238  if ( !seller )
2239  return false;
2241  return ( !white_list || (*white_list).empty()
2242  || ( (*white_list).find(seller->registrar) != (*white_list).end() ) );
2243  };
2244 
2245  if ( is_rewards_allowed() )
2246  {
2247  const auto reward_percent = recv_asset.options.extensions.value.reward_percent;
2248  if ( reward_percent.valid() && (*reward_percent) > 0 )
2249  {
2250  const auto reward_value = detail::calculate_percent(issuer_fees.amount, *reward_percent);
2251  if ( reward_value > 0 && is_authorized_asset(*this, seller->registrar(*this), recv_asset) )
2252  {
2253  reward = recv_asset.amount(reward_value);
2254  // TODO after hf_1774, remove the `if` check, keep the code in `else`
2255  if( head_block_time() < HARDFORK_1774_TIME ){
2256  FC_ASSERT( reward < issuer_fees, "Market reward should be less than issuer fees");
2257  }
2258  else{
2259  FC_ASSERT( reward <= issuer_fees, "Market reward should not be greater than issuer fees");
2260  }
2261  // cut referrer percent from reward
2262  auto registrar_reward = reward;
2263 
2264  auto registrar = seller->registrar;
2265  auto referrer = seller->referrer;
2266 
2267  // After HF core-1800, for funds going to temp-account, redirect to committee-account
2268  if( head_block_time() >= HARDFORK_CORE_1800_TIME )
2269  {
2270  if( registrar == GRAPHENE_TEMP_ACCOUNT )
2271  registrar = GRAPHENE_COMMITTEE_ACCOUNT;
2272  if( referrer == GRAPHENE_TEMP_ACCOUNT )
2273  referrer = GRAPHENE_COMMITTEE_ACCOUNT;
2274  }
2275 
2276  if( referrer != registrar )
2277  {
2278  const auto referrer_rewards_value = detail::calculate_percent( reward.amount,
2279  seller->referrer_rewards_percentage );
2280 
2281  if ( referrer_rewards_value > 0 && is_authorized_asset(*this, referrer(*this), recv_asset) )
2282  {
2283  FC_ASSERT ( referrer_rewards_value <= reward.amount.value,
2284  "Referrer reward shouldn't be greater than total reward" );
2285  const asset referrer_reward = recv_asset.amount(referrer_rewards_value);
2286  registrar_reward -= referrer_reward;
2287  deposit_market_fee_vesting_balance(referrer, referrer_reward);
2288  }
2289  }
2290  if( registrar_reward.amount > 0 )
2291  deposit_market_fee_vesting_balance(registrar, registrar_reward);
2292  }
2293  }
2294  }
2295 
2296  if( issuer_fees.amount > reward.amount )
2297  {
2298  const auto& recv_dyn_data = recv_asset.dynamic_asset_data_id(*this);
2299  modify( recv_dyn_data, [&issuer_fees, &reward]( asset_dynamic_data_object& obj ){
2300  obj.accumulated_fees += issuer_fees.amount - reward.amount;
2301  });
2302  }
2303  }
2304 
2305  return market_fees;
2306 }
2307 
2308 /***
2309  * @brief Calculate force-settlement fee and give it to issuer of the settled asset
2310  * @param collecting_asset the smart asset object which should receive the fee
2311  * @param collat_receives the amount of collateral the settler would expect to receive absent this fee
2312  * (fee is computed as a percentage of this amount)
2313  * @return asset denoting the amount of fee collected
2314  */
2315 asset database::pay_force_settle_fees(const asset_object& collecting_asset, const asset& collat_receives)
2316 {
2317  FC_ASSERT( collecting_asset.get_id() != collat_receives.asset_id );
2318 
2319  const bitasset_options& collecting_bitasset_opts = collecting_asset.bitasset_data(*this).options;
2320 
2321  if( !collecting_bitasset_opts.extensions.value.force_settle_fee_percent.valid()
2322  || *collecting_bitasset_opts.extensions.value.force_settle_fee_percent == 0 )
2323  return asset{ 0, collat_receives.asset_id };
2324 
2325  auto value = detail::calculate_percent(collat_receives.amount,
2326  *collecting_bitasset_opts.extensions.value.force_settle_fee_percent);
2327  asset settle_fee( value, collat_receives.asset_id );
2328 
2329  // Deposit fee in asset's dynamic data object:
2330  if( value > 0) {
2331  collecting_asset.accumulate_fee(*this, settle_fee);
2332  }
2333  return settle_fee;
2334 }
2335 
2336 } }
#define GRAPHENE_COLLATERAL_RATIO_DENOM
Definition: config.hpp:113
asset_id_type asset_id
The asset this object belong to.
share_type max_market_fee
Market fees calculated as market_fee_percent of the traded volume are capped to this value...
Definition: asset_ops.hpp:58
#define GRAPHENE_TEMP_ACCOUNT
Represents the canonical account with WILDCARD authority (anybody can access funds in temp account) ...
Definition: config.hpp:148
fc::optional< flat_set< account_id_type > > whitelist_market_fee_sharing
Definition: asset_ops.hpp:34
asset additional_collateral
the amount of collateral to bid for the debt
Definition: market.hpp:186
price get_margin_call_order_price() const
Get margin call order price (MCOP) of this bitasset.
bool maybe_cull_small_order(database &db, const limit_order_object &order)
Definition: db_market.cpp:601
uint16_t maximum_short_squeeze_ratio
Definition: asset.hpp:189
contains properties that only apply to bitassets (market issued assets)
black_swan_response_type
Defines how a BitAsset would respond to black swan events.
Definition: asset_ops.hpp:112
bool is_authorized_asset(const database &d, const account_object &acct, const asset_object &asset_obj)
const index_type & indices() const
void apply_force_settlement(const force_settlement_object &new_settlement, const asset_bitasset_data_object &bitasset, const asset_object &asset_obj)
Process a new force-settlement request.
Definition: db_market.cpp:898
boost::rational< int32_t > ratio_type
Definition: types.hpp:150
time_point_sec head_block_time() const
Definition: db_getter.cpp:67
This class represents an account on the object graphAccounts are the primary unit of authority on the...
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
bool is_settled_debt
Whether this order is an individual settlement fund.
price min() const
Definition: asset.hpp:125
bitasset_options::black_swan_response_type get_black_swan_response_method() const
Get the effective black swan response method of this bitasset.
Definition: api.cpp:48
#define elog(FORMAT,...)
Definition: logger.hpp:129
price max_short_squeeze_price_before_hf_1270() const
Definition: asset.cpp:282
tracks debt and call price information
bool apply_order_before_hardfork_625(const limit_order_object &new_order_object)
Process a new limit order through the markets.
Definition: db_market.cpp:627
account_id_type bidder
pays fee and additional collateral
Definition: market.hpp:185
price call_price
Collateral / Debt.
void accumulate_fee(DB &db, const asset &fee) const
asset_id_type collateral_type() const
asset_id_type sell_asset_id() const
#define wlog(FORMAT,...)
Definition: logger.hpp:123
asset pay_market_fees(const account_object *seller, const asset_object &recv_asset, const asset &receives, const bool &is_maker, const optional< asset > &calculated_market_fees={})
Definition: db_market.cpp:2205
void revive_bitasset(const asset_object &bitasset, const asset_bitasset_data_object &bad)
Definition: db_market.cpp:400
share_type for_sale
asset id is sell_price.base.asset_id
share_type calculate_percent(const share_type &value, uint16_t percent)
Definition: db_market.cpp:39
asset multiply_and_round_up(const price &p) const
Multiply and round up.
Definition: asset.cpp:77
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
price max() const
Definition: asset.hpp:124
void execute_bid(const collateral_bid_object &bid, share_type debt_covered, share_type collateral_from_fund, const price_feed &current_feed)
Definition: db_market.cpp:464
const asset_bitasset_data_object & bitasset_data(const DB &db) const
asset amount(share_type a) const
Helper function to get an asset object with the given amount in this asset&#39;s type.
bool check_call_orders(const asset_object &mia, bool enable_black_swan=true, bool for_new_limit_order=false, const asset_bitasset_data_object *bitasset_ptr=nullptr, bool mute_exceptions=false, bool skip_matching_settle_orders=false)
Definition: db_market.cpp:1745
bool is_prediction_market
True if this asset implements a Prediction Market.
share_type debt
call_price.quote.asset_id, access via get_debt
void cancel_bid(const collateral_bid_object &bid, bool create_virtual_op=true)
Definition: db_market.cpp:449
fc::optional< uint16_t > reward_percent
Definition: asset_ops.hpp:33
static time_point_sec maximum()
Definition: time.hpp:86
#define wdump(SEQ)
Definition: logger.hpp:174
object_id< SpaceID, TypeID > get_id() const
Definition: object.hpp:113
object_id_type id
Definition: object.hpp:69
uint16_t maintenance_collateral_ratio
Definition: asset.hpp:186
ratio_type get_margin_call_pays_ratio() const
Get margin call pays ratio (MCPR) of this bitasset.
asset_id_type receive_asset_id() const
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:108
#define edump(SEQ)
Definition: logger.hpp:182
#define GRAPHENE_100_PERCENT
Definition: config.hpp:102
void cancel_limit_order(const limit_order_object &order, bool create_virtual_op=true, bool skip_cancel_fee=false)
Definition: db_market.cpp:505
bool valid() const
Definition: optional.hpp:186
#define GRAPHENE_COMMITTEE_ACCOUNT
Definition: config.hpp:140
asset_dynamic_data_id_type dynamic_asset_data_id
Current supply, fee pool, and collected fees are stored in a separate object as they change frequentl...
string symbol
Ticker symbol for this asset, i.e. "USD".
tracks bitassets scheduled for force settlement at some point in the future.
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:479
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
price_feed_with_icr current_feed
This is the currently active price feed, calculated from median_feed and other parameters.
tracks the asset information that changes frequentlyBecause the asset_object is very large it doesn&#39;t...
void globally_settle_asset(const asset_object &bitasset, const price &settle_price, bool check_margin_calls=false)
Definition: db_market.cpp:221
#define GRAPHENE_NULL_ACCOUNT
Represents the canonical account with NO authority (nobody can access funds in null account) ...
Definition: config.hpp:146
accounts must be whitelisted in order to hold or transact this asset
Definition: types.hpp:195
bids of collateral for debt after a black swan
double to_real() const
Definition: asset.hpp:127
void pay_fee(share_type core_fee, share_type cashback_vesting_threshold)
bool apply_order(const limit_order_object &new_order_object)
Definition: db_market.cpp:696
share_type individual_settlement_fund
Amount of collateral which is available for force settlement due to individual settlements.
share_type accumulated_fees
fees accumulate to be paid out over time
tracks the parameters of an assetAll assets have a globally unique symbol name that controls how they...
asset pay_force_settle_fees(const asset_object &collecting_asset, const asset &collat_receives)
Definition: db_market.cpp:2315
defines market parameters for margin positions
Definition: asset.hpp:160
share_type collateral
call_price.base.asset_id, access via get_collateral
asset calculate_market_fee(const asset_object &trade_asset, const asset &trade_amount, const bool &is_maker) const
Calculate the market fee that is to be taken.
Definition: db_market.cpp:2170
asset_id_type asset_id
Definition: asset.hpp:37
bitasset_options options
The tunable options for BitAssets are stored in this field.
account_statistics_id_type statistics
fc::optional< uint16_t > taker_fee_percent
Definition: asset_ops.hpp:36
constexpr int64_t GRAPHENE_MAX_SHARE_SUPPLY(1000000000000000LL)
#define GRAPHENE_ASSERT(expr, exc_type, FORMAT,...)
Definition: exceptions.hpp:28
account_id_type registrar
The account that paid the fee to register this account. Receives a percentage of referral rewards...
share_type get_max_debt_to_cover(price match_price, price feed_price, const uint16_t maintenance_collateral_ratio, const optional< price > &maintenance_collateralization=optional< price >()) const
share_type current_supply
The number of shares currently in existence.
price max_short_squeeze_price() const
Definition: asset.cpp:300
share_type deferred_fee
fee converted to CORE
asset deferred_paid_fee
originally paid fee
bool is_null() const
Definition: asset.cpp:229
additional_asset_options_t extensions
Definition: asset_ops.hpp:93
static price call_price(const asset &debt, const asset &collateral, uint16_t collateral_ratio)
Definition: asset.cpp:216
account_id_type referrer
The account credited as referring this account. Receives a percentage of referral rewards...
void cancel_settle_order(const force_settlement_object &order)
Definition: db_market.cpp:496
share_type settlement_fund
Amount of collateral which is available for force settlement due to global settlement.
The bitasset_options struct contains configurable options available only to BitAssets.
Definition: asset_ops.hpp:109
asset debt_covered
the amount of debt to take over
Definition: market.hpp:187
T value
Definition: safe.hpp:22
price_feed_with_icr median_feed
This is the median of values from the currently active feeds.
an offer to sell an amount of an asset at a specified exchange rate by a certain timeThe objects are ...