29 #include <graphene/chain/hardfork.hpp> 35 namespace graphene {
namespace chain {
41 fc::uint128_t a(value.
value);
45 return static_cast<int64_t
>(a);
50 bool database::check_for_blackswan(
const asset_object& mia,
bool enable_black_swan,
58 if( settle_price.is_null() )
return false;
60 asset_id_type debt_asset_id = bitasset.
asset_id;
62 auto maint_time = get_dynamic_global_properties().next_maintenance_time;
63 bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME );
64 bool after_core_hardfork_2481 = HARDFORK_CORE_2481_PASSED( maint_time );
67 if( after_core_hardfork_2481 )
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 )
76 const call_order_object* call_ptr = find_least_collateralized_short( bitasset,
false );
81 const auto& limit_price_index = limit_index.
indices().get<by_price>();
88 FC_ASSERT( highest_possible_bid.base.asset_id == lowest_possible_bid.base.asset_id );
90 auto limit_itr = limit_price_index.lower_bound( highest_possible_bid );
91 auto limit_end = limit_price_index.upper_bound( lowest_possible_bid );
93 price call_pays_price;
94 if( limit_itr != limit_end )
96 call_pays_price = limit_itr->sell_price;
97 if( after_core_hardfork_2481 )
108 bool settled_some =
false;
112 price highest = settle_price;
117 if( bsrm_type::individual_settlement_to_fund == bsrm )
119 else if( !before_core_hardfork_1270 )
121 else if( maint_time > HARDFORK_CORE_338_TIME )
125 if( limit_itr != limit_end )
128 if( bsrm_type::individual_settlement_to_fund != bsrm )
129 highest = std::max( call_pays_price, highest );
134 highest = call_pays_price;
156 bool is_blackswan = after_core_hardfork_2481 ? ( ~least_collateral > highest )
157 : ( ~least_collateral >= highest );
161 wdump( (*call_ptr) );
162 elog(
"Black Swan detected on asset ${symbol} (${id}) at block ${b}: \n" 163 " Least collateralized call: ${lc} ${~lc}\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())
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));
174 "Black swan was detected during a margin update which is not allowed to trigger a blackswan" );
176 if( bsrm_type::individual_settlement_to_fund == bsrm || bsrm_type::individual_settlement_to_order == bsrm )
178 individually_settle( bitasset, *call_ptr );
179 call_ptr = find_least_collateralized_short( bitasset,
true );
186 else if( after_core_hardfork_2481 )
188 if( bsrm_type::no_settlement == bsrm )
189 wlog(
"Internal error: BSRM is no_settlement but undercollateralization occurred" );
204 globally_settle_asset(mia, ~least_collateral,
true );
206 else if( maint_time > HARDFORK_CORE_338_TIME && ~least_collateral <= settle_price )
208 globally_settle_asset(mia, settle_price );
210 globally_settle_asset(mia, ~least_collateral );
222 bool check_margin_calls )
224 auto maint_time = get_dynamic_global_properties().next_maintenance_time;
225 bool before_core_hardfork_1669 = ( maint_time <= HARDFORK_CORE_1669_TIME );
227 if( before_core_hardfork_1669 )
229 globally_settle_asset_impl( mia, settlement_price,
230 get_index_type<call_order_index>().indices().get<by_price>(),
231 check_margin_calls );
236 globally_settle_asset_impl( mia, settlement_price,
237 get_index_type<call_order_index>().indices().get<by_collateral>(),
238 check_margin_calls );
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 )
256 auto maint_time = get_dynamic_global_properties().next_maintenance_time;
257 bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME );
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 )
273 if( call_itr != margin_end )
274 is_margin_call =
true;
279 while( call_itr != call_end )
281 if( is_margin_call && call_itr == margin_end )
283 is_margin_call =
false;
284 call_pays_price = fund_receives_price;
291 if( before_core_hardfork_342 )
292 pays = order_debt * call_pays_price;
302 if( fund_receives > pays )
303 fund_receives = pays;
304 margin_call_fee = pays - fund_receives;
305 collateral_gathered += fund_receives;
309 margin_call_fee.
amount = 0;
310 collateral_gathered += pays;
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) );
322 collateral_gathered.amount += limit_ptr->
for_sale;
323 remove( *limit_ptr );
346 FC_ASSERT( bsrm_type::individual_settlement_to_fund == bsrm || bsrm_type::individual_settlement_to_order == bsrm,
347 "Internal error: Invalid BSRM" );
352 auto fund_receives = order_debt.multiply_and_round_up( fund_receives_price );
356 auto margin_call_fee = order_collateral - fund_receives;
358 if( bsrm_type::individual_settlement_to_fund == bsrm )
371 obj.
for_sale += fund_receives.amount;
378 create< limit_order_object >( [&order_debt,&fund_receives](
limit_order_object& obj ) {
381 obj.for_sale = fund_receives.amount;
382 obj.sell_price = fund_receives / order_debt;
383 obj.is_settled_debt =
true;
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) );
395 if( bsrm_type::individual_settlement_to_fund == bsrm )
396 update_bitasset_current_feed( bitasset,
true );
409 if( bdd.current_supply > 0 )
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);
418 execute_bid( pseudo_bid, bdd.current_supply, bad.settlement_fund, bad.current_feed );
422 _cancel_bids_and_revive_mpa( bitasset, bad );
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 );
453 if( create_virtual_op )
459 push_applied_operation( vop );
468 [&bid, &debt_covered, &collateral_from_fund, ¤t_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;
473 if( get_dynamic_global_properties().next_maintenance_time > HARDFORK_CORE_1270_TIME )
475 call.call_price =
price(
asset( 1, bid.inv_swan_price.base.asset_id ),
476 asset( 1, bid.inv_swan_price.quote.asset_id ) );
479 asset(call.collateral, bid.inv_swan_price.base.asset_id),
480 current_feed.maintenance_collateral_ratio );
484 if( bid.inv_swan_price.base.asset_id == asset_id_type() )
486 stats.total_core_in_orders += call_obj.
collateral;
490 asset( debt_covered, bid.inv_swan_price.quote.asset_id ),
516 if( create_virtual_op )
521 if( !skip_cancel_fee && deferred_fee > 0 )
523 asset core_cancel_fee = current_fee_schedule().calculate_fee( vop );
525 if( core_cancel_fee.
amount > deferred_fee )
526 core_cancel_fee.
amount = deferred_fee;
528 if( core_cancel_fee.
amount > 0 )
530 seller_acc_stats = &get_account_stats_by_owner( order.
seller );
532 obj.
pay_fee( core_cancel_fee.
amount, get_global_properties().parameters.cashback_vesting_threshold );
534 deferred_fee -= core_cancel_fee.
amount;
537 if( deferred_paid_fee.
amount == 0 )
539 vop.
fee = core_cancel_fee;
543 fc::uint128_t fee128( deferred_paid_fee.
amount.
value );
549 share_type cancel_fee_amount =
static_cast<int64_t
>(fee128);
551 fee_asset_dyn_data = &deferred_paid_fee.
asset_id(*this).dynamic_asset_data_id(*
this);
556 deferred_paid_fee.
amount -= cancel_fee_amount;
565 if( refunded.asset_id == asset_id_type() )
567 if( !seller_acc_stats )
568 seller_acc_stats = &get_account_stats_by_owner( order.
seller );
573 adjust_balance(order.
seller, refunded);
582 adjust_balance( order.
seller, deferred_fee );
586 adjust_balance(order.
seller, deferred_paid_fee);
588 if( !fee_asset_dyn_data )
589 fee_asset_dyn_data = &deferred_paid_fee.
asset_id(*this).dynamic_asset_data_id(*
this);
595 if( create_virtual_op )
596 push_applied_operation( vop );
629 auto order_id = new_order_object.
id;
636 bool called_some = check_call_orders(sell_asset,
true,
true);
637 bool called_some_else = check_call_orders(receive_asset,
true,
true);
638 if( ( called_some || called_some_else ) && !find_object(order_id) )
641 const auto& limit_price_idx = get_index_type<limit_order_index>().indices().get<by_price>();
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);
652 bool finished =
false;
653 while( !finished && limit_itr != limit_end )
655 auto old_limit_itr = limit_itr;
658 finished = ( match(new_order_object, *old_limit_itr, old_limit_itr->
sell_price)
659 != match_result_type::only_maker_filled );
664 check_call_orders(sell_asset);
666 check_call_orders(receive_asset);
668 const limit_order_object* updated_order_object = find< limit_order_object >( order_id );
669 if( !updated_order_object )
671 if( head_block_time() <= HARDFORK_555_TIME )
698 auto order_id = new_order_object.
id;
699 asset_id_type sell_asset_id = new_order_object.
sell_asset_id();
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() )
708 if( limit_itr->sell_asset_id() == sell_asset_id && limit_itr->receive_asset_id() == recv_asset_id )
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 );
748 auto maint_time = get_dynamic_global_properties().next_maintenance_time;
749 bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME );
751 bool to_check_call_orders =
false;
752 const asset_object& sell_asset = sell_asset_id( *
this );
754 price call_match_price;
756 price call_pays_price;
766 if( before_core_hardfork_1270 ) {
768 call_pays_price = call_match_price;
773 if( ~new_order_object.
sell_price <= call_match_price )
774 to_check_call_orders =
true;
778 bool finished =
false;
779 bool feed_price_updated =
false;
780 if( to_check_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 )
790 finished = ( match( new_order_object, matching_limit_order, matching_limit_order.
sell_price )
791 != match_result_type::only_maker_filled );
794 auto call_min =
price::min( recv_asset_id, sell_asset_id );
795 if( !finished && !before_core_hardfork_1270 )
799 const auto& call_collateral_idx = get_index_type<call_order_index>().indices().get<by_collateral>();
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
817 const auto match_result = match( new_order_object, *call_itr, call_match_price,
818 *sell_abd, call_pays_price );
822 if( match_result_type::only_taker_filled == match_result
823 || match_result_type::both_filled == match_result )
825 else if( update_call_price )
837 feed_price_updated =
true;
842 const auto& call_price_idx = get_index_type<call_order_index>().indices().get<by_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
855 const auto match_result = match( new_order_object, *call_itr, call_match_price, *sell_abd );
860 if( match_result_type::only_taker_filled == match_result
861 || match_result_type::both_filled == match_result )
868 while( !finished && limit_itr != limit_end )
873 finished = ( match( new_order_object, matching_limit_order, matching_limit_order.
sell_price )
874 != match_result_type::only_maker_filled );
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 )
887 if( limit_order_is_gone && feed_price_updated )
892 check_call_orders( sell_asset,
true,
false, sell_abd );
895 return limit_order_is_gone;
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" );
910 auto head_time = head_block_time();
911 bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time );
913 auto new_obj_id = new_settlement.
id;
928 bool finished =
false;
932 const auto& call_collateral_idx = get_index_type<call_order_index>().indices().get<by_collateral>();
937 auto call_itr = call_collateral_idx.lower_bound( call_min );
940 if( call_itr == call_collateral_idx.end()
948 asset max_debt_to_cover( call_itr->get_max_debt_to_cover( call_pays_price,
954 match( new_settlement, *call_itr, call_pays_price, bitasset, max_debt_to_cover, call_match_price,
true );
957 finished = (
nullptr == find_object( new_obj_id ) );
959 if( update_call_price )
963 check_call_orders( asset_obj,
true,
false, &bitasset,
false,
true );
997 const price& match_price )
1003 return maker.
is_settled_debt ? match_limit_settled_debt( taker, maker, match_price )
1004 : match_limit_normal_limit( taker, maker, match_price );
1016 asset taker_receives;
1018 asset maker_receives;
1020 auto maint_time = get_dynamic_global_properties().next_maintenance_time;
1021 bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME );
1023 bool cull_taker =
false;
1024 if( taker_for_sale <= ( maker_for_sale * match_price ) )
1026 taker_receives = taker_for_sale * match_price;
1030 if( taker_receives.
amount == 0 && maint_time > HARDFORK_CORE_184_TIME )
1031 return match_result_type::only_taker_filled;
1033 if( before_core_hardfork_342 )
1034 maker_receives = taker_for_sale;
1054 maker_receives = maker_for_sale * match_price;
1055 if( before_core_hardfork_342 )
1056 taker_receives = maker_for_sale;
1063 maker_pays = taker_receives;
1064 taker_pays = maker_receives;
1066 if( before_core_hardfork_342 )
1071 bool taker_filled = fill_limit_order( taker, taker_pays, taker_receives, cull_taker, match_price,
false );
1073 bool maker_filled = fill_limit_order( maker, maker_pays, maker_receives,
true, match_price,
true );
1076 FC_ASSERT( result != match_result_type::none_filled );
1086 bool cull_taker =
false;
1087 bool maker_filled =
false;
1092 asset call_receives;
1093 asset order_receives;
1094 if( usd_to_buy > usd_for_sale )
1096 order_receives = usd_for_sale * match_price;
1100 if( order_receives.
amount == 0 )
1101 return match_result_type::only_taker_filled;
1112 call_receives = usd_to_buy;
1114 maker_filled =
true;
1118 bool taker_filled = fill_limit_order( taker, call_receives, order_receives, cull_taker, match_price,
false );
1149 const price& match_price,
1151 const price& call_pays_price )
1157 bool cull_taker =
false;
1159 auto maint_time = get_dynamic_global_properties().next_maintenance_time;
1160 bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME );
1161 bool after_core_hardfork_2481 = HARDFORK_CORE_2481_PASSED( maint_time );
1163 auto head_time = head_block_time();
1164 bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time );
1170 if( !before_core_hardfork_1270 )
1175 maintenance_collateralization ),
1179 asset call_receives;
1181 asset order_receives;
1182 if( usd_to_buy > usd_for_sale )
1184 order_receives = usd_for_sale * match_price;
1188 if( order_receives.
amount == 0 )
1189 return match_result_type::only_taker_filled;
1192 if( after_core_hardfork_2481 )
1193 call_pays = call_receives * call_pays_price;
1196 call_pays = usd_for_sale * call_pays_price;
1206 call_receives = usd_to_buy;
1216 order_pays = call_receives;
1222 const asset margin_call_fee = call_pays - order_receives;
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 );
1229 update_bitasset_current_feed( bitasset,
true );
1239 const price& match_price,
1241 const asset& max_settlement,
1242 const price& fill_price,
1243 bool is_margin_call )
1245 return match_impl( settle, call, match_price, bitasset, max_settlement, fill_price, is_margin_call,
true );
1250 const price& match_price,
1252 const asset& max_settlement,
1253 const price& fill_price )
1255 return match_impl( settle, call, match_price, bitasset, max_settlement, fill_price,
true,
false );
1260 const price& p_match_price,
1262 const asset& max_settlement,
1263 const price& p_fill_price,
1264 bool is_margin_call,
1265 bool settle_is_taker )
1270 auto maint_time = get_dynamic_global_properties().next_maintenance_time;
1271 bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME );
1273 auto settle_for_sale = std::min(settle.
balance, max_settlement);
1277 price match_price = p_match_price;
1278 price fill_price = p_fill_price;
1280 asset call_receives = std::min(settle_for_sale, call_debt);
1281 asset call_pays = call_receives * match_price;
1290 asset settle_receives = call_pays;
1291 asset settle_pays = call_receives;
1294 bool cull_settle_order =
false;
1295 if( maint_time > HARDFORK_CORE_184_TIME && call_pays.
amount == 0 )
1297 if( call_receives == call_debt )
1300 settle_receives.
amount = 1;
1302 else if( call_receives == settle.
balance )
1304 cancel_settle_order( settle );
1306 return asset( 0, call_debt.asset_id );
1309 else if( !is_margin_call )
1312 return asset( 0, call_debt.asset_id );
1323 settle_receives.
amount = 1;
1326 else if( !before_core_hardfork_342 && call_pays.
amount != 0 )
1331 if( call_receives == call_debt )
1334 if( is_margin_call )
1339 match_price = call_debt / call_collateral;
1340 fill_price = match_price / margin_call_pays_ratio;
1346 settle_receives = call_pays;
1352 if( !is_margin_call )
1361 else if( settle_pays == max_settlement )
1373 const auto& calculate = [&settle_receives,&settle_pays,&fill_price,&call_receives,&call_pays,&match_price]
1375 settle_receives = settle_pays * fill_price;
1376 if( settle_receives.
amount != 0 )
1381 call_pays = call_receives * match_price;
1386 if( settle_receives.
amount == 0 )
1388 cancel_settle_order( settle );
1390 return asset( 0, call_debt.asset_id );
1394 bool cap_price =
false;
1399 auto new_collateral = call_collateral - call_pays;
1400 auto new_debt = call_debt - call_receives;
1407 match_price = call_debt / call_collateral;
1408 fill_price = match_price / margin_call_pays_ratio;
1410 if( settle_receives.
amount == 0 )
1415 cancel_settle_order( settle );
1417 return asset( 0, call_debt.asset_id );
1425 if( settle_pays == settle.
balance && call_receives != settle.
balance )
1426 cull_settle_order =
true;
1429 settle_pays = call_receives;
1441 if( before_core_hardfork_342 )
1443 GRAPHENE_ASSERT( call_pays < call_collateral, black_swan_exception,
"" );
1445 assert( settle_pays == settle_for_sale || call_receives == call.
get_debt() );
1449 asset margin_call_fee = call_pays - settle_receives;
1451 fill_call_order( call, call_pays, call_receives, fill_price, settle_is_taker, margin_call_fee );
1453 fill_settle_order( settle, settle_pays, settle_receives, fill_price, !settle_is_taker, !is_margin_call );
1457 update_bitasset_current_feed( bitasset,
true );
1459 if( cull_settle_order )
1460 cancel_settle_order( settle );
1462 return call_receives;
1463 }
FC_CAPTURE_AND_RETHROW( (p_match_price)(max_settlement)(p_fill_price)(is_margin_call)(settle_is_taker) ) }
1466 bool cull_if_small,
const price& fill_price,
const bool is_maker)
1468 if( head_block_time() < HARDFORK_555_TIME )
1469 cull_if_small =
true;
1476 const auto issuer_fees = pay_market_fees(&seller, receives.
asset_id(*
this), receives, is_maker);
1478 pay_order( seller, receives - issuer_fees, pays );
1482 issuer_fees, fill_price, is_maker ) );
1492 const uint16_t maker_discount_percent = get_global_properties().parameters.get_maker_fee_discount_percent();
1502 if( is_maker && maker_discount_percent > 0 )
1510 FC_ASSERT( refund <= deferred_paid_fee,
"Internal error" );
1512 deferred_paid_fee -= refund;
1515 FC_ASSERT( deferred_fee > 0,
"Internal error" );
1517 FC_ASSERT( fee_pool_refund <= deferred_fee,
"Internal error" );
1518 deferred_fee -= fee_pool_refund;
1532 && is_maker && maker_discount_percent > 0 )
1537 FC_ASSERT( refund <= deferred_fee,
"Internal error" );
1538 adjust_balance( order.
seller,
asset(refund, asset_id_type()) );
1539 deferred_fee -= refund;
1544 if( deferred_fee > 0 )
1548 statistics.pay_fee( deferred_fee, get_global_properties().parameters.cashback_vesting_threshold );
1582 const price& fill_price,
const bool is_maker,
const asset& margin_call_fee,
bool reduce_current_supply )
1600 collateral_freed = o.get_collateral();
1605 auto maint_time = get_dynamic_global_properties().next_maintenance_time;
1608 if( maint_time <= HARDFORK_CORE_1270_TIME && maint_time > HARDFORK_CORE_343_TIME )
1610 o.call_price = price::call_price( o.get_debt(), o.get_collateral(),
1611 bitasset.current_feed.maintenance_collateral_ratio );
1617 if( reduce_current_supply )
1626 if( collateral_freed.valid() )
1627 adjust_balance( order.
borrower, *collateral_freed );
1630 if( pays.
asset_id == asset_id_type() )
1633 b.total_core_in_orders -= pays.amount;
1634 if( collateral_freed.valid() )
1635 b.total_core_in_orders -= collateral_freed->amount;
1645 margin_call_fee, fill_price, is_maker ) );
1648 if( collateral_freed.valid() )
1651 return collateral_freed.valid();
1676 const price& fill_price,
bool is_maker,
bool pay_force_settle_fee )
1678 bool filled =
false;
1688 if( head_block_time() >= HARDFORK_CORE_1780_TIME )
1689 settle_owner_ptr = &settle.
owner(*
this);
1691 asset market_fees = pay_market_fees( settle_owner_ptr,
get(receives.
asset_id), receives, is_maker );
1698 asset force_settle_fees = pay_force_settle_fee
1699 ? pay_force_settle_fees(
get(pays.
asset_id), receives - market_fees )
1702 auto total_collateral_denominated_fees = market_fees + force_settle_fees;
1714 adjust_balance(settle.
owner, receives - total_collateral_denominated_fees);
1718 total_collateral_denominated_fees, fill_price, is_maker ) );
1747 bool mute_exceptions,
bool skip_matching_settle_orders )
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 );
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 ) )
1779 const auto& limit_price_index = limit_index.
indices().get<by_price>();
1781 bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME );
1782 bool after_core_hardfork_2481 = HARDFORK_CORE_2481_PASSED( maint_time );
1789 auto min_price = before_core_hardfork_1270 ?
1794 auto limit_itr = limit_price_index.lower_bound( max_price );
1795 auto limit_end = limit_price_index.upper_bound( min_price );
1798 if( !after_core_hardfork_2481 && limit_itr == limit_end )
1802 const auto& call_price_index = call_index.
indices().get<by_price>();
1804 const auto& call_collateral_index = call_index.
indices().get<by_collateral>();
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;
1814 if( before_core_hardfork_1270 )
1816 call_price_itr = call_price_index.lower_bound( call_min );
1817 call_price_end = call_price_index.upper_bound( call_max );
1821 call_collateral_itr = call_collateral_index.lower_bound( call_min );
1822 call_collateral_end = call_collateral_index.upper_bound( call_max );
1825 bool filled_limit =
false;
1826 bool margin_called =
false;
1828 auto head_time = head_block_time();
1829 auto head_num = head_block_num();
1831 bool before_hardfork_615 = ( head_time < HARDFORK_615_TIME );
1832 bool after_hardfork_436 = ( head_time > HARDFORK_436_TIME );
1834 bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME );
1835 bool before_core_hardfork_343 = ( maint_time <= HARDFORK_CORE_343_TIME );
1836 bool before_core_hardfork_453 = ( maint_time <= HARDFORK_CORE_453_TIME );
1837 bool before_core_hardfork_606 = ( maint_time <= HARDFORK_CORE_606_TIME );
1838 bool before_core_hardfork_834 = ( maint_time <= HARDFORK_CORE_834_TIME );
1840 bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time );
1842 auto has_call_order = [ before_core_hardfork_1270,
1843 &call_collateral_itr,&call_collateral_end,
1844 &call_price_itr,&call_price_end ]()
1846 return before_core_hardfork_1270 ? ( call_price_itr != call_price_end )
1847 : ( call_collateral_itr != call_collateral_end );
1852 const auto& settlement_index = get_index_type<force_settlement_index>().indices().get<
by_expiration>();
1854 while( has_call_order() )
1857 bool settled_some = check_for_blackswan( mia, enable_black_swan, &bitasset );
1859 return margin_called;
1863 call_collateral_itr = call_collateral_index.lower_bound( call_min );
1864 if( call_collateral_itr == call_collateral_end )
1866 margin_called =
true;
1867 if( bsrm_type::individual_settlement_to_fund == bsrm )
1872 const call_order_object& call_order = ( before_core_hardfork_1270 ? *call_price_itr : *call_collateral_itr );
1875 bool feed_protected = before_core_hardfork_1270 ?
1878 if( feed_protected )
1879 return margin_called;
1882 if( limit_itr != limit_end )
1890 if( before_core_hardfork_606 && match_price > ~call_order.
call_price )
1891 return margin_called;
1893 margin_called =
true;
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() )
1910 elog(
"black swan detected on asset ${symbol} (${id}) at block ${b}",
1912 edump((enable_black_swan));
1918 if( !before_core_hardfork_1270 )
1927 else if( !before_core_hardfork_834 )
1935 asset call_pays, call_receives, limit_pays, limit_receives;
1937 struct UndercollateralizationException {};
1940 bool filled_call =
false;
1942 if( usd_to_buy > usd_for_sale )
1944 limit_receives = usd_for_sale * match_price;
1954 if( before_core_hardfork_342 )
1955 call_receives = usd_for_sale;
1963 if( !after_core_hardfork_2481 )
1965 call_pays = usd_for_sale * call_pays_price;
1968 call_pays = call_receives * call_pays_price;
1970 throw UndercollateralizationException();
1972 auto new_debt = call_order.
get_debt() - call_receives;
1974 throw UndercollateralizationException();
1977 filled_limit =
true;
1980 call_receives = usd_to_buy;
1982 if( before_core_hardfork_342 )
1984 limit_receives = usd_to_buy * match_price;
1985 call_pays = limit_receives;
1992 if( after_core_hardfork_2481 )
1993 throw UndercollateralizationException();
1994 if( mute_exceptions )
2013 if( usd_to_buy == usd_for_sale )
2014 filled_limit =
true;
2015 else if( filled_limit && before_hardfork_615 )
2017 _issue_453_affected_assets.insert( bitasset.
asset_id );
2019 limit_pays = call_receives;
2023 const asset margin_call_fee = call_pays - limit_receives;
2025 if( filled_call && before_core_hardfork_343 )
2029 fill_call_order( call_order, call_pays, call_receives, match_price, for_new_limit_order, margin_call_fee);
2032 if( update_current_feed )
2034 update_bitasset_current_feed( bitasset,
true );
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 );
2044 auto next_limit_itr = std::next( limit_itr );
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;
2053 }
catch(
const UndercollateralizationException& ) {
2063 if( skip_matching_settle_orders || !after_core_hardfork_2481 )
2064 return margin_called;
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;
2074 if( match_force_settlements( bitasset ) )
2076 margin_called =
true;
2077 call_collateral_itr = call_collateral_index.lower_bound( call_min );
2078 if( update_current_feed )
2088 return margin_called;
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" );
2100 auto head_time = head_block_time();
2101 bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time );
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 );
2108 const auto& call_collateral_index = get_index_type<call_order_index>().indices().get<by_collateral>();
2111 auto call_itr = call_collateral_index.lower_bound( call_min );
2112 auto call_end = call_collateral_index.upper_bound( call_max );
2123 while( settle_itr != settle_end && call_itr != call_end )
2144 auto result = match( call_order, settle_order, call_pays_price, bitasset, max_debt_to_cover, call_match_price );
2148 if( result.amount > 0 )
2152 settle_itr = settlement_index.lower_bound( bitasset.
asset_id );
2153 call_itr = call_collateral_index.lower_bound( call_min );
2160 if( pays.
asset_id == asset_id_type() )
2162 const auto& stats = receiver.
statistics(*
this);
2167 adjust_balance(receiver.
get_id(), receives);
2171 const bool& is_maker )
const 2173 assert( trade_asset.
id == trade_amount.
asset_id );
2176 return trade_asset.
amount(0);
2179 return trade_asset.
amount(0);
2183 if(!is_maker && taker_fee_percent.
valid() && *taker_fee_percent == 0)
2184 return trade_asset.
amount(0);
2186 uint16_t fee_percent;
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");
2213 if ( issuer_fees.amount > 0 )
2216 const uint16_t network_percent = get_global_properties().parameters.get_market_fee_network_percent();
2217 if( network_percent > 0 )
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 )
2224 const asset network_fees = recv_asset.
amount( network_fees_amt );
2226 issuer_fees -= network_fees;
2232 if ( issuer_fees.amount > 0 )
2237 auto is_rewards_allowed = [&recv_asset, seller]() {
2241 return ( !
white_list || (*white_list).empty()
2242 || ( (*white_list).find(seller->
registrar) != (*white_list).end() ) );
2245 if ( is_rewards_allowed() )
2248 if ( reward_percent.valid() && (*reward_percent) > 0 )
2253 reward = recv_asset.
amount(reward_value);
2255 if( head_block_time() < HARDFORK_1774_TIME ){
2256 FC_ASSERT( reward < issuer_fees,
"Market reward should be less than issuer fees");
2259 FC_ASSERT( reward <= issuer_fees,
"Market reward should not be greater than issuer fees");
2262 auto registrar_reward = reward;
2268 if( head_block_time() >= HARDFORK_CORE_1800_TIME )
2276 if( referrer != registrar )
2281 if ( referrer_rewards_value > 0 &&
is_authorized_asset(*
this, referrer(*
this), recv_asset) )
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);
2290 if( registrar_reward.amount > 0 )
2291 deposit_market_fee_vesting_balance(registrar, registrar_reward);
2296 if( issuer_fees.amount > reward.
amount )
2321 if( !collecting_bitasset_opts.extensions.value.force_settle_fee_percent.valid()
2322 || *collecting_bitasset_opts.extensions.value.force_settle_fee_percent == 0 )
2326 *collecting_bitasset_opts.extensions.
value.force_settle_fee_percent);
extension< ext > extensions
price collateralization() const
bool has_settlement() const
bool charges_market_fees() const
#define GRAPHENE_COLLATERAL_RATIO_DENOM
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...
#define GRAPHENE_TEMP_ACCOUNT
Represents the canonical account with WILDCARD authority (anybody can access funds in temp account) ...
fc::optional< flat_set< account_id_type > > whitelist_market_fee_sharing
asset additional_collateral
the amount of collateral to bid for the debt
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)
asset amount_to_receive() const
uint16_t maximum_short_squeeze_ratio
contains properties that only apply to bitassets (market issued assets)
black_swan_response_type
Defines how a BitAsset would respond to black swan events.
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.
boost::rational< int32_t > ratio_type
time_point_sec head_block_time() const
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
bool is_settled_debt
Whether this order is an individual settlement fund.
bitasset_options::black_swan_response_type get_black_swan_response_method() const
Get the effective black swan response method of this bitasset.
price max_short_squeeze_price_before_hf_1270() const
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.
account_id_type bidder
pays fee and additional collateral
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
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={})
void revive_bitasset(const asset_object &bitasset, const asset_bitasset_data_object &bad)
share_type for_sale
asset id is sell_price.base.asset_id
share_type calculate_percent(const share_type &value, uint16_t percent)
asset multiply_and_round_up(const price &p) const
Multiply and round up.
provides stack-based nullable value similar to boost::optional
void execute_bid(const collateral_bid_object &bid, share_type debt_covered, share_type collateral_from_fund, const price_feed ¤t_feed)
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'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)
bool is_prediction_market
True if this asset implements a Prediction Market.
share_type debt
call_price.quote.asset_id, access via get_debt
asset get_collateral() const
void cancel_bid(const collateral_bid_object &bid, bool create_virtual_op=true)
fc::optional< uint16_t > reward_percent
share_type individual_settlement_debt
price current_maintenance_collateralization
static time_point_sec maximum()
object_id< SpaceID, TypeID > get_id() const
uint16_t maintenance_collateral_ratio
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.
#define GRAPHENE_100_PERCENT
void cancel_limit_order(const limit_order_object &order, bool create_virtual_op=true, bool skip_cancel_fee=false)
#define GRAPHENE_COMMITTEE_ACCOUNT
uint16_t market_fee_percent
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...
bool is_market_issued() const
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(...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
price_feed_with_icr current_feed
This is the currently active price feed, calculated from median_feed and other parameters.
asset_id_type debt_type() const
tracks the asset information that changes frequentlyBecause the asset_object is very large it doesn't...
void globally_settle_asset(const asset_object &bitasset, const price &settle_price, bool check_margin_calls=false)
#define GRAPHENE_NULL_ACCOUNT
Represents the canonical account with NO authority (nobody can access funds in null account) ...
accounts must be whitelisted in order to hold or transact this asset
bids of collateral for debt after a black swan
void pay_fee(share_type core_fee, share_type cashback_vesting_threshold)
bool apply_order(const limit_order_object &new_order_object)
share_type individual_settlement_fund
Amount of collateral which is available for force settlement due to individual settlements.
share_type fee_pool
in core asset
asset amount_for_sale() const
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)
defines market parameters for margin positions
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.
bitasset_options options
The tunable options for BitAssets are stored in this field.
limit_order_id_type order
account_statistics_id_type statistics
fc::optional< uint16_t > taker_fee_percent
constexpr int64_t GRAPHENE_MAX_SHARE_SUPPLY(1000000000000000LL)
#define GRAPHENE_ASSERT(expr, exc_type, FORMAT,...)
uint16_t referrer_rewards_percentage
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
bool is_current_feed_price_capped() const
share_type deferred_fee
fee converted to CORE
asset deferred_paid_fee
originally paid fee
additional_asset_options_t extensions
account_id_type fee_paying_account
static price call_price(const asset &debt, const asset &collateral, uint16_t collateral_ratio)
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)
asset_id_type short_backing_asset
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.
share_type total_core_in_orders
asset debt_covered
the amount of debt to take over
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 ...