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,
51 const asset_bitasset_data_object* bitasset_ptr )
53 if( !mia.is_market_issued() )
return false;
55 const asset_bitasset_data_object& bitasset = bitasset_ptr ? *bitasset_ptr : mia.bitasset_data(*
this);
56 if( bitasset.is_globally_settled() )
return true;
57 auto settle_price = bitasset.current_feed.settlement_price;
58 if( settle_price.is_null() )
return false;
60 asset_id_type debt_asset_id = bitasset.asset_id;
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 )
81 const auto& limit_price_index = limit_index.indices().get<by_price>();
84 auto highest_possible_bid =
price::max( debt_asset_id, bitasset.options.short_backing_asset );
86 auto lowest_possible_bid =
price::min( debt_asset_id, bitasset.options.short_backing_asset );
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 )
100 call_pays_price = call_pays_price * bitasset.get_margin_call_pays_ratio();
105 const auto bsrm = bitasset.get_black_swan_response_method();
108 bool settled_some =
false;
111 settle_price = bitasset.current_feed.settlement_price;
112 price highest = settle_price;
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 )
122 highest = bitasset.current_feed.max_short_squeeze_price_before_hf_1270();
125 if( limit_itr != limit_end )
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 );
133 else if( call_pays_price >= bitasset.current_feed.max_short_squeeze_price() )
134 highest = call_pays_price;
151 auto least_collateral = call_ptr->collateralization();
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",
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 );
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" );
206 else if( maint_time > HARDFORK_CORE_338_TIME && ~least_collateral <= settle_price )
222 bool check_margin_calls )
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 )
260 bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME );
266 auto margin_end = call_end;
267 bool is_margin_call =
false;
268 price call_pays_price = settlement_price;
269 price fund_receives_price = settlement_price;
270 if( check_margin_calls )
276 if( call_itr != margin_end )
277 is_margin_call =
true;
282 while( call_itr != call_end )
284 if( is_margin_call && call_itr == margin_end )
286 is_margin_call =
false;
287 call_pays_price = fund_receives_price;
290 const call_order_object& order = *call_itr;
293 auto order_debt = order.get_debt();
294 if( before_core_hardfork_342 )
295 pays = order_debt * call_pays_price;
297 pays = order_debt.multiply_and_round_up( call_pays_price );
299 if( pays > order.get_collateral() )
300 pays = order.get_collateral();
304 auto fund_receives = order_debt.multiply_and_round_up( fund_receives_price );
305 if( fund_receives > pays )
306 fund_receives = pays;
307 margin_call_fee = pays - fund_receives;
308 collateral_gathered += fund_receives;
312 margin_call_fee.amount = 0;
313 collateral_gathered += pays;
317 FC_ASSERT( fill_call_order( order, pays, order_debt, fund_receives_price,
true, margin_call_fee,
false ),
318 "Internal error: unable to close margin call ${o}", (
"o", order) );
329 modify( bitasset, [&mia,&original_mia_supply,&collateral_gathered]( asset_bitasset_data_object& obj ){
330 obj.options.extensions.value.black_swan_response_method.reset();
331 obj.current_feed = obj.median_feed;
332 obj.individual_settlement_debt = 0;
333 obj.individual_settlement_fund = 0;
334 obj.settlement_price = mia.
amount(original_mia_supply) / collateral_gathered;
335 obj.settlement_fund = collateral_gathered.
amount;
340 void database::individually_settle(
const asset_bitasset_data_object& bitasset,
const call_order_object& order )
342 FC_ASSERT( bitasset.asset_id == order.debt_type(),
"Internal error: asset type mismatch" );
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" );
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 )
354 fund_receives.amount = order.collateral;
356 auto margin_call_fee = order_collateral - fund_receives;
358 modify( bitasset, [&order,&fund_receives]( asset_bitasset_data_object& obj ){
359 obj.individual_settlement_debt += order.debt;
360 obj.individual_settlement_fund += fund_receives.amount;
363 if( bsrm_type::individual_settlement_to_order == bsrm )
366 bool after_core_hardfork_2591 = HARDFORK_CORE_2591_PASSED( head_time );
371 modify( *limit_ptr, [after_core_hardfork_2591,&bitasset]( limit_order_object& obj ) {
373 bool sell_all =
true;
374 if( after_core_hardfork_2591 )
376 obj.sell_price = ~bitasset.get_margin_call_order_price();
377 asset settled_debt( bitasset.individual_settlement_debt, obj.receive_asset_id() );
380 obj.for_sale = settled_debt.multiply_and_round_up( obj.sell_price ).amount;
382 if( obj.for_sale <= bitasset.individual_settlement_fund )
393 obj.for_sale = bitasset.individual_settlement_fund;
394 obj.sell_price = ~bitasset.get_individual_settlement_price();
400 create< limit_order_object >( [&order_debt,&fund_receives]( limit_order_object& obj ) {
403 obj.for_sale = fund_receives.amount;
404 obj.sell_price = fund_receives / order_debt;
405 obj.is_settled_debt =
true;
412 FC_ASSERT( fill_call_order( order, order_collateral, order_debt,
413 fund_receives_price,
true, margin_call_fee,
false ),
414 "Internal error: unable to close margin call ${o}", (
"o", order) );
417 if( bsrm_type::individual_settlement_to_fund == bsrm )
447 _cancel_bids_and_revive_mpa( bitasset, bad );
460 const auto& bid_idx = get_index_type< collateral_bid_index >().indices().get<by_price>();
461 auto itr = bid_idx.lower_bound( bad.
asset_id );
462 const auto end = bid_idx.upper_bound( bad.
asset_id );
471 modify( bad, []( asset_bitasset_data_object& obj ){
472 obj.settlement_price = price();
473 obj.settlement_fund = 0;
481 if( create_virtual_op )
496 [&bid, &debt_covered, &collateral_from_fund, ¤t_feed,
this](
call_order_object& call ){
499 call.
debt = debt_covered;
514 stats.total_core_in_orders += call_obj.collateral;
544 if( create_virtual_op )
549 if( !skip_cancel_fee && deferred_fee > 0 )
553 if( core_cancel_fee.
amount > deferred_fee )
554 core_cancel_fee.
amount = deferred_fee;
556 if( core_cancel_fee.
amount > 0 )
562 deferred_fee -= core_cancel_fee.
amount;
565 if( deferred_paid_fee.
amount == 0 )
567 vop.
fee = core_cancel_fee;
571 fc::uint128_t fee128( deferred_paid_fee.
amount.
value );
577 share_type cancel_fee_amount =
static_cast<int64_t
>(fee128);
579 deferred_fee_asset_dyn_data = &deferred_paid_fee.
asset_id(*this).dynamic_asset_data_id(*
this);
584 deferred_paid_fee.
amount -= cancel_fee_amount;
593 if( refunded.asset_id == asset_id_type() )
595 if( !seller_acc_stats )
616 if( !deferred_fee_asset_dyn_data )
617 deferred_fee_asset_dyn_data = &deferred_paid_fee.
asset_id(*this).dynamic_asset_data_id(*
this);
623 if( create_virtual_op )
629 cleanup_and_remove_limit_order( order );
674 auto order_id = new_order_object.
id;
683 if( ( called_some || called_some_else ) && !
find_object(order_id) )
686 const auto& limit_price_idx = get_index_type<limit_order_index>().indices().get<by_price>();
693 auto max_price = ~new_order_object.
sell_price;
694 auto limit_itr = limit_price_idx.lower_bound(max_price.max());
695 auto limit_end = limit_price_idx.upper_bound(max_price);
697 bool finished =
false;
698 while( !finished && limit_itr != limit_end )
700 auto old_limit_itr = limit_itr;
703 finished = ( match(new_order_object, *old_limit_itr, old_limit_itr->
sell_price)
713 const limit_order_object* updated_order_object = find< limit_order_object >( order_id );
714 if( !updated_order_object )
743 auto order_id = new_order_object.
id;
744 asset_id_type sell_asset_id = new_order_object.
sell_asset_id();
748 const auto& limit_price_idx = get_index_type<limit_order_index>().indices().get<by_price>();
749 auto limit_itr = limit_price_idx.iterator_to( new_order_object );
750 if( limit_itr != limit_price_idx.begin() )
753 if( limit_itr->sell_asset_id() == sell_asset_id && limit_itr->receive_asset_id() == recv_asset_id )
758 auto max_price = ~new_order_object.
sell_price;
759 limit_itr = limit_price_idx.lower_bound( max_price.max() );
760 auto limit_end = limit_price_idx.upper_bound( max_price );
794 bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME );
796 bool to_check_call_orders =
false;
797 const asset_object& sell_asset = sell_asset_id( *
this );
799 price call_match_price;
801 price call_pays_price;
811 if( before_core_hardfork_1270 ) {
813 call_pays_price = call_match_price;
818 if( ~new_order_object.
sell_price <= call_match_price )
819 to_check_call_orders =
true;
823 bool finished =
false;
824 bool feed_price_updated =
false;
825 if( to_check_call_orders )
828 auto limit_itr_after_call = limit_price_idx.lower_bound( call_match_price );
829 while( !finished && limit_itr != limit_itr_after_call )
835 finished = ( match( new_order_object, matching_limit_order, matching_limit_order.
sell_price )
839 auto call_min =
price::min( recv_asset_id, sell_asset_id );
840 if( !finished && !before_core_hardfork_1270 )
844 const auto& call_collateral_idx = get_index_type<call_order_index>().indices().get<by_collateral>();
855 auto call_itr = call_collateral_idx.lower_bound( call_min );
856 if( call_itr == call_collateral_idx.end()
857 || call_itr->debt_type() != sell_asset_id
862 const auto match_result = match( new_order_object, *call_itr, call_match_price,
863 *sell_abd, call_pays_price );
870 else if( update_call_price )
882 feed_price_updated =
true;
887 const auto& call_price_idx = get_index_type<call_order_index>().indices().get<by_price>();
892 auto call_itr = call_price_idx.lower_bound( call_min );
893 if( call_itr == call_price_idx.end()
894 || call_itr->debt_type() != sell_asset_id
900 const auto match_result = match( new_order_object, *call_itr, call_match_price, *sell_abd );
913 while( !finished && limit_itr != limit_end )
918 finished = ( match( new_order_object, matching_limit_order, matching_limit_order.
sell_price )
922 bool limit_order_is_gone =
true;
923 const limit_order_object* updated_order_object = find< limit_order_object >( order_id );
924 if( updated_order_object )
932 if( limit_order_is_gone && feed_price_updated )
940 return limit_order_is_gone;
951 FC_ASSERT( HARDFORK_CORE_2481_PASSED( maint_time ),
"Internal error: hard fork core-2481 not passed" );
959 bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time );
961 auto new_obj_id = new_settlement.
id;
976 bool finished =
false;
980 const auto& call_collateral_idx = get_index_type<call_order_index>().indices().get<by_collateral>();
985 auto call_itr = call_collateral_idx.lower_bound( call_min );
988 if( call_itr == call_collateral_idx.end()
996 asset max_debt_to_cover( call_itr->get_max_debt_to_cover( call_pays_price,
1002 match( new_settlement, *call_itr, call_pays_price, bitasset, max_debt_to_cover, call_match_price,
true );
1005 finished = (
nullptr ==
find_object( new_obj_id ) );
1007 if( update_call_price )
1040 const price& match_price )
1044 FC_ASSERT( taker.sell_price.quote.asset_id == maker.sell_price.base.asset_id );
1045 FC_ASSERT( taker.sell_price.base.asset_id == maker.sell_price.quote.asset_id );
1046 FC_ASSERT( taker.for_sale > 0 && maker.for_sale > 0 );
1049 return maker.is_settled_debt ? match_limit_settled_debt( taker, maker, match_price )
1050 : match_limit_normal_limit( taker, maker, match_price );
1055 const limit_order_object& maker,
const price& match_price )
1059 FC_ASSERT( !maker.is_settled_debt,
"Internal error: maker is settled debt" );
1060 FC_ASSERT( !taker.is_settled_debt,
"Internal error: taker is settled debt" );
1063 auto taker_for_sale = taker.amount_for_sale();
1064 auto maker_for_sale = maker.amount_for_sale();
1067 asset taker_receives;
1069 asset maker_receives;
1072 bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME );
1074 bool cull_taker =
false;
1075 if( taker_for_sale <= ( maker_for_sale * match_price ) )
1077 taker_receives = taker_for_sale * match_price;
1081 if( taker_receives.amount == 0 && maint_time > HARDFORK_CORE_184_TIME )
1084 if( before_core_hardfork_342 )
1085 maker_receives = taker_for_sale;
1092 maker_receives = taker_receives.multiply_and_round_up( match_price );
1105 maker_receives = maker_for_sale * match_price;
1106 if( before_core_hardfork_342 )
1107 taker_receives = maker_for_sale;
1111 taker_receives = maker_receives.multiply_and_round_up( match_price );
1114 maker_pays = taker_receives;
1115 taker_pays = maker_receives;
1117 if( before_core_hardfork_342 )
1118 FC_ASSERT( taker_pays == taker.amount_for_sale() ||
1119 maker_pays == maker.amount_for_sale() );
1122 bool taker_filled = fill_limit_order( taker, taker_pays, taker_receives, cull_taker, match_price,
false );
1124 bool maker_filled = fill_limit_order( maker, maker_pays, maker_receives,
true, match_price,
true );
1133 const limit_order_object& maker,
const price& match_price )
1137 FC_ASSERT( maker.is_settled_debt,
"Internal error: maker is not settled debt" );
1138 FC_ASSERT( !taker.is_settled_debt,
"Internal error: taker is settled debt" );
1141 bool cull_taker =
false;
1142 bool maker_filled =
false;
1144 const auto& mia = maker.receive_asset_id()(*this);
1147 auto usd_for_sale = taker.amount_for_sale();
1148 auto usd_to_buy = asset( bitasset.individual_settlement_debt, maker.receive_asset_id() );
1150 asset call_receives;
1151 asset order_receives;
1152 if( usd_to_buy > usd_for_sale )
1154 order_receives = usd_for_sale * match_price;
1158 if( order_receives.amount == 0 )
1165 call_receives = order_receives.multiply_and_round_up( match_price );
1170 call_receives = usd_to_buy;
1171 order_receives = maker.amount_for_sale();
1172 maker_filled =
true;
1176 bool taker_filled = fill_limit_order( taker, call_receives, order_receives, cull_taker, match_price,
false );
1179 bool after_core_hardfork_2591 = HARDFORK_CORE_2591_PASSED( head_time );
1181 asset call_pays = order_receives;
1183 call_pays.amount = bitasset.individual_settlement_fund;
1184 else if( maker.for_sale != bitasset.individual_settlement_fund )
1185 call_pays = call_receives * bitasset.get_individual_settlement_price();
1186 if( call_pays < order_receives )
1188 wlog(
"Unexpected scene: call_pays < order_receives" );
1189 call_pays = order_receives;
1191 asset collateral_fee = call_pays - order_receives;
1195 modify( mia_ddo, [&call_receives,&collateral_fee]( asset_dynamic_data_object& ao ){
1196 ao.current_supply -= call_receives.amount;
1197 ao.accumulated_collateral_fees += collateral_fee.amount;
1203 collateral_fee, match_price,
true ) );
1206 modify( bitasset, [&call_receives,&call_pays]( asset_bitasset_data_object& obj ){
1207 obj.individual_settlement_debt -= call_receives.amount;
1208 obj.individual_settlement_fund -= call_pays.amount;
1217 modify( maker, [after_core_hardfork_2591,&bitasset]( limit_order_object& obj ) {
1218 if( after_core_hardfork_2591 )
1221 asset settled_debt( bitasset.individual_settlement_debt, obj.receive_asset_id() );
1222 obj.for_sale = settled_debt.multiply_and_round_up( obj.sell_price ).amount;
1223 if( obj.for_sale > bitasset.individual_settlement_fund )
1225 wlog(
"Unexpected scene: obj.for_sale > bitasset.individual_settlement_fund" );
1226 obj.for_sale = bitasset.individual_settlement_fund;
1227 obj.sell_price = ~bitasset.get_individual_settlement_price();
1232 obj.for_sale = bitasset.individual_settlement_fund;
1233 obj.sell_price = ~bitasset.get_individual_settlement_price();
1249 const limit_order_object& maker,
const price& match_price )
1253 FC_ASSERT( !maker.is_settled_debt,
"Internal error: maker is settled debt" );
1254 FC_ASSERT( taker.is_settled_debt,
"Internal error: taker is not settled debt" );
1257 bool taker_filled =
false;
1259 const auto& mia = taker.receive_asset_id()(*this);
1262 auto usd_for_sale = maker.amount_for_sale();
1263 auto usd_to_buy = asset( bitasset.individual_settlement_debt, taker.receive_asset_id() );
1265 asset call_receives;
1266 asset order_receives;
1267 if( usd_to_buy > usd_for_sale )
1269 order_receives = usd_for_sale * match_price;
1274 call_receives = order_receives.multiply_and_round_up( match_price );
1278 call_receives = usd_to_buy;
1279 order_receives = call_receives.multiply_and_round_up( match_price );
1280 taker_filled =
true;
1283 asset call_pays = order_receives;
1285 call_pays.amount = bitasset.individual_settlement_fund;
1286 else if( taker.for_sale != bitasset.individual_settlement_fund )
1287 call_pays = call_receives * bitasset.get_individual_settlement_price();
1288 if( call_pays < order_receives )
1290 wlog(
"Unexpected scene: call_pays < order_receives" );
1291 call_pays = order_receives;
1293 asset collateral_fee = call_pays - order_receives;
1297 modify( mia_ddo, [&call_receives,&collateral_fee]( asset_dynamic_data_object& ao ){
1298 ao.current_supply -= call_receives.amount;
1299 ao.accumulated_collateral_fees += collateral_fee.amount;
1305 collateral_fee, match_price,
false ) );
1308 modify( bitasset, [&call_receives,&call_pays]( asset_bitasset_data_object& obj ){
1309 obj.individual_settlement_debt -= call_receives.amount;
1310 obj.individual_settlement_fund -= call_pays.amount;
1319 modify( taker, [&bitasset]( limit_order_object& obj ) {
1321 asset settled_debt( bitasset.individual_settlement_debt, obj.receive_asset_id() );
1322 obj.for_sale = settled_debt.multiply_and_round_up( obj.sell_price ).amount;
1323 if( obj.for_sale > bitasset.individual_settlement_fund )
1325 wlog(
"Unexpected scene: obj.for_sale > bitasset.individual_settlement_fund" );
1326 obj.for_sale = bitasset.individual_settlement_fund;
1327 obj.sell_price = ~bitasset.get_individual_settlement_price();
1334 bool maker_filled = fill_limit_order( maker, call_receives, order_receives,
true, match_price,
true );
1342 const price& match_price,
1343 const asset_bitasset_data_object& bitasset,
1344 const price& call_pays_price )
1346 FC_ASSERT( bid.sell_asset_id() == ask.debt_type() );
1347 FC_ASSERT( bid.receive_asset_id() == ask.collateral_type() );
1348 FC_ASSERT( bid.for_sale > 0 && ask.debt > 0 && ask.collateral > 0 );
1350 bool cull_taker =
false;
1353 bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME );
1354 bool after_core_hardfork_2481 = HARDFORK_CORE_2481_PASSED( maint_time );
1357 bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time );
1359 const auto& feed_price = after_core_hardfork_2582 ? bitasset.median_feed.settlement_price
1360 : bitasset.current_feed.settlement_price;
1361 const auto& maintenance_collateral_ratio = bitasset.current_feed.maintenance_collateral_ratio;
1362 optional<price> maintenance_collateralization;
1363 if( !before_core_hardfork_1270 )
1364 maintenance_collateralization = bitasset.current_maintenance_collateralization;
1366 asset usd_for_sale = bid.amount_for_sale();
1367 asset usd_to_buy( ask.get_max_debt_to_cover( call_pays_price, feed_price, maintenance_collateral_ratio,
1368 maintenance_collateralization ),
1372 asset call_receives;
1374 asset order_receives;
1375 if( usd_to_buy > usd_for_sale )
1377 order_receives = usd_for_sale * match_price;
1381 if( order_receives.amount == 0 )
1384 call_receives = order_receives.multiply_and_round_up( match_price );
1385 if( after_core_hardfork_2481 )
1386 call_pays = call_receives * call_pays_price;
1389 call_pays = usd_for_sale * call_pays_price;
1399 call_receives = usd_to_buy;
1400 order_receives = usd_to_buy.multiply_and_round_up( match_price );
1401 call_pays = usd_to_buy.multiply_and_round_up( call_pays_price );
1409 order_pays = call_receives;
1415 const asset margin_call_fee = call_pays - order_receives;
1417 bool taker_filled = fill_limit_order( bid, order_pays, order_receives, cull_taker, match_price,
false );
1418 bool maker_filled = fill_call_order( ask, call_pays, call_receives, match_price,
true, margin_call_fee );
1421 if( bitasset_options::black_swan_response_type::no_settlement == bitasset.get_black_swan_response_method() )
1430 asset database::match(
const force_settlement_object& settle,
1431 const call_order_object& call,
1432 const price& match_price,
1433 const asset_bitasset_data_object& bitasset,
1434 const asset& max_settlement,
1435 const price& fill_price,
1436 bool is_margin_call )
1438 return match_impl( settle, call, match_price, bitasset, max_settlement, fill_price, is_margin_call,
true );
1441 asset database::match(
const call_order_object& call,
1442 const force_settlement_object& settle,
1443 const price& match_price,
1444 const asset_bitasset_data_object& bitasset,
1445 const asset& max_settlement,
1446 const price& fill_price )
1448 return match_impl( settle, call, match_price, bitasset, max_settlement, fill_price,
true,
false );
1451 asset database::match_impl(
const force_settlement_object& settle,
1452 const call_order_object& call,
1453 const price& p_match_price,
1454 const asset_bitasset_data_object& bitasset,
1455 const asset& max_settlement,
1456 const price& p_fill_price,
1457 bool is_margin_call,
1458 bool settle_is_taker )
1460 FC_ASSERT(call.get_debt().asset_id == settle.balance.asset_id );
1461 FC_ASSERT(call.debt > 0 && call.collateral > 0 && settle.balance.amount > 0);
1464 bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME );
1466 auto settle_for_sale = std::min(settle.balance, max_settlement);
1467 auto call_debt = call.get_debt();
1468 auto call_collateral = call.get_collateral();
1470 price match_price = p_match_price;
1471 price fill_price = p_fill_price;
1473 asset call_receives = std::min(settle_for_sale, call_debt);
1474 asset call_pays = call_receives * match_price;
1483 asset settle_receives = call_pays;
1484 asset settle_pays = call_receives;
1487 bool cull_settle_order =
false;
1488 if( maint_time > HARDFORK_CORE_184_TIME && call_pays.amount == 0 )
1490 if( call_receives == call_debt )
1492 call_pays.amount = 1;
1493 settle_receives.amount = 1;
1495 else if( call_receives == settle.balance )
1499 return asset( 0, call_debt.asset_id );
1502 else if( !is_margin_call )
1505 return asset( 0, call_debt.asset_id );
1515 call_pays.amount = 1;
1516 settle_receives.amount = 1;
1519 else if( !before_core_hardfork_342 && call_pays.amount != 0 )
1521 auto margin_call_pays_ratio = bitasset.get_margin_call_pays_ratio();
1524 if( call_receives == call_debt )
1526 call_pays = call_receives.multiply_and_round_up( match_price );
1527 if( is_margin_call )
1529 if( call_pays.amount > call.collateral )
1531 call_pays.amount = call.collateral;
1532 match_price = call_debt / call_collateral;
1533 fill_price = match_price / margin_call_pays_ratio;
1535 settle_receives = call_receives.multiply_and_round_up( fill_price );
1539 settle_receives = call_pays;
1545 if( !is_margin_call )
1551 call_receives = call_pays.multiply_and_round_up( match_price );
1554 else if( settle_pays == max_settlement )
1557 call_pays = call_receives.multiply_and_round_up( match_price );
1558 settle_receives = call_receives.multiply_and_round_up( fill_price );
1566 const auto& calculate = [&settle_receives,&settle_pays,&fill_price,&call_receives,&call_pays,&match_price]
1568 settle_receives = settle_pays * fill_price;
1569 if( settle_receives.amount != 0 )
1572 call_receives = settle_receives.multiply_and_round_up( fill_price );
1574 call_pays = call_receives * match_price;
1579 if( settle_receives.amount == 0 )
1583 return asset( 0, call_debt.asset_id );
1587 bool cap_price =
false;
1588 if( call_pays.amount >= call.collateral )
1592 auto new_collateral = call_collateral - call_pays;
1593 auto new_debt = call_debt - call_receives;
1594 if( ( new_collateral / new_debt ) < call.collateralization() )
1600 match_price = call_debt / call_collateral;
1601 fill_price = match_price / margin_call_pays_ratio;
1603 if( settle_receives.amount == 0 )
1610 return asset( 0, call_debt.asset_id );
1618 if( settle_pays == settle.balance && call_receives != settle.balance )
1619 cull_settle_order =
true;
1622 settle_pays = call_receives;
1634 if( before_core_hardfork_342 )
1636 GRAPHENE_ASSERT( call_pays < call_collateral, black_swan_exception,
"" );
1638 assert( settle_pays == settle_for_sale || call_receives == call.get_debt() );
1642 asset margin_call_fee = call_pays - settle_receives;
1644 fill_call_order( call, call_pays, call_receives, fill_price, settle_is_taker, margin_call_fee );
1646 fill_settle_order( settle, settle_pays, settle_receives, fill_price, !settle_is_taker, !is_margin_call );
1649 if( bitasset_options::black_swan_response_type::no_settlement == bitasset.get_black_swan_response_method() )
1652 if( cull_settle_order )
1655 return call_receives;
1657 (is_margin_call)(settle_is_taker) ) }
1659 optional<limit_order_id_type> database::process_limit_order_on_fill(
const limit_order_object& order,
1660 const asset& order_receives )
1662 optional<limit_order_id_type> result;
1663 if( order.on_fill.empty() )
1666 const auto& take_profit_action = order.get_take_profit_action();
1668 fc::uint128_t amount128( order_receives.amount.value );
1669 amount128 *= take_profit_action.size_percent;
1674 if( amount128 <= 0 )
1678 asset for_sale(
static_cast<int64_t
>( amount128 ), order_receives.asset_id );
1680 if( order.take_profit_order_id.valid() )
1682 limit_order_update_operation op;
1683 op.seller = order.seller;
1684 op.order = *order.take_profit_order_id;
1685 op.delta_amount_to_sell = for_sale;
1688 op.new_expiration =
head_block_time() + take_profit_action.expiration_seconds;
1694 if( take_profit_action.fee_asset_id == asset_id_type() )
1698 take_profit_action.fee_asset_id(*this).options.core_exchange_rate );
1700 if( *order.take_profit_order_id > order.get_id() )
1703 const auto& take_profit_order = (*order.take_profit_order_id)(*
this);
1704 for_sale.
amount += take_profit_order.for_sale;
1707 auto new_min_to_receive = for_sale.multiply_and_round_up( sell_price );
1708 op.new_price = for_sale / new_min_to_receive;
1714 FC_ASSERT( !op.new_price || ( ~(*op.new_price) > order.sell_price ),
1715 "Internal error: the take profit order should not match the current order" );
1718 transaction_evaluation_state eval_state(
this);
1719 eval_state.skip_limit_order_price_check =
true;
1721 try_push_virtual_operation( eval_state, op );
1728 wlog(
"At block ${n}, failed to process on_fill for limit order ${order}, "
1729 "automatic action (maybe incomplete) was ${op}, exception was ${e}",
1736 limit_order_create_operation op;
1737 op.seller = order.seller;
1738 op.amount_to_sell = for_sale;
1740 op.expiration =
head_block_time() + take_profit_action.expiration_seconds;
1743 if( take_profit_action.repeat )
1744 op.extensions.value.on_fill = order.on_fill;
1748 if( take_profit_action.fee_asset_id == asset_id_type() )
1752 take_profit_action.fee_asset_id(*this).options.core_exchange_rate );
1756 op.min_to_receive = for_sale.multiply_and_round_up( sell_price );
1760 FC_ASSERT( ~op.get_price() > order.sell_price,
1761 "Internal error: the take profit order should not match the current order" );
1764 transaction_evaluation_state eval_state(
this);
1766 auto op_result = try_push_virtual_operation( eval_state, op );
1767 result = limit_order_id_type( op_result.get<object_id_type>() );
1774 wlog(
"At block ${n}, failed to process on_fill for limit order ${order}, "
1775 "automatic action (maybe incomplete) was ${op}, exception was ${e}",
1784 bool database::fill_limit_order(
const limit_order_object& order,
const asset& pays,
const asset& receives,
1785 bool cull_if_small,
const price& fill_price,
const bool is_maker)
1788 cull_if_small =
true;
1792 FC_ASSERT( order.amount_for_sale().asset_id == pays.asset_id );
1793 FC_ASSERT( pays.asset_id != receives.asset_id );
1796 const account_object& seller = order.seller(*
this);
1798 const auto issuer_fees =
pay_market_fees(&seller, receives.asset_id(*
this), receives, is_maker);
1800 auto order_receives = receives - issuer_fees;
1801 pay_order( seller, order_receives, pays );
1804 issuer_fees, fill_price, is_maker ) );
1817 share_type deferred_fee = order.deferred_fee;
1818 share_type deferred_paid_fee = order.deferred_paid_fee.amount;
1821 if( order.deferred_paid_fee.amount > 0 )
1824 if( is_maker && maker_discount_percent > 0 )
1832 FC_ASSERT( refund <= deferred_paid_fee,
"Internal error" );
1833 adjust_balance( order.seller, asset(refund, order.deferred_paid_fee.asset_id) );
1834 deferred_paid_fee -= refund;
1837 FC_ASSERT( deferred_fee > 0,
"Internal error" );
1839 FC_ASSERT( fee_pool_refund <= deferred_fee,
"Internal error" );
1840 deferred_fee -= fee_pool_refund;
1844 const auto& fee_asset_dyn_data = order.deferred_paid_fee.asset_id(*this).dynamic_asset_data_id(*
this);
1845 modify( fee_asset_dyn_data, [deferred_paid_fee,fee_pool_refund](asset_dynamic_data_object& addo) {
1846 addo.accumulated_fees += deferred_paid_fee;
1847 addo.fee_pool += fee_pool_refund;
1851 if( order.deferred_fee > 0 )
1853 if( order.deferred_paid_fee.amount <= 0
1854 && is_maker && maker_discount_percent > 0 )
1859 FC_ASSERT( refund <= deferred_fee,
"Internal error" );
1861 deferred_fee -= refund;
1866 if( deferred_fee > 0 )
1868 modify( seller.statistics(*
this), [deferred_fee,
this]( account_statistics_object& statistics )
1870 statistics.pay_fee( deferred_fee, get_global_properties().parameters.cashback_vesting_threshold );
1876 optional<limit_order_id_type> new_take_profit_order_id = process_limit_order_on_fill( order, order_receives );
1879 if( pays == order.amount_for_sale() )
1881 cleanup_and_remove_limit_order( order );
1886 if( new_take_profit_order_id.valid() )
1888 modify( (*new_take_profit_order_id)(*
this), [&order]( limit_order_object& loo ) {
1889 loo.take_profit_order_id = order.get_id();
1892 modify( order, [&pays,&new_take_profit_order_id]( limit_order_object& b ) {
1893 b.for_sale -= pays.amount;
1894 b.filled_amount += pays.amount.value;
1896 b.deferred_paid_fee.amount = 0;
1897 if( new_take_profit_order_id.valid() )
1898 b.take_profit_order_id = *new_take_profit_order_id;
1915 bool database::fill_call_order(
const call_order_object& order,
const asset& pays,
const asset& receives,
1916 const price& fill_price,
const bool is_maker,
const asset& margin_call_fee,
bool reduce_current_supply )
1918 FC_ASSERT( order.debt_type() == receives.asset_id );
1919 FC_ASSERT( order.collateral_type() == pays.asset_id );
1920 FC_ASSERT( order.collateral >= pays.amount );
1923 const asset_object& mia = receives.asset_id(*
this);
1925 const asset_bitasset_data_object& bitasset = mia.bitasset_data(*
this);
1927 optional<asset> collateral_freed;
1929 modify( order, [&]( call_order_object& o ) {
1930 o.debt -= receives.amount;
1931 o.collateral -= pays.amount;
1934 collateral_freed = o.get_collateral();
1939 auto maint_time = get_dynamic_global_properties().next_maintenance_time;
1942 if( maint_time <= HARDFORK_CORE_1270_TIME && maint_time > HARDFORK_CORE_343_TIME )
1944 o.call_price = price::call_price( o.get_debt(), o.get_collateral(),
1945 bitasset.current_feed.maintenance_collateral_ratio );
1951 if( reduce_current_supply )
1953 const asset_dynamic_data_object& mia_ddo = mia.dynamic_asset_data_id(*
this);
1954 modify( mia_ddo, [&receives]( asset_dynamic_data_object& ao ){
1955 ao.current_supply -= receives.amount;
1960 if( collateral_freed.valid() )
1964 if( pays.asset_id == asset_id_type() )
1967 b.total_core_in_orders -= pays.amount;
1968 if( collateral_freed.valid() )
1969 b.total_core_in_orders -= collateral_freed->amount;
1974 if (margin_call_fee.amount.value != 0)
1975 mia.accumulate_fee(*
this, margin_call_fee);
1979 margin_call_fee, fill_price, is_maker ) );
1982 if( collateral_freed.valid() )
1985 return collateral_freed.valid();
2009 bool database::fill_settle_order(
const force_settlement_object& settle,
const asset& pays,
const asset& receives,
2010 const price& fill_price,
bool is_maker,
bool pay_force_settle_fee )
2012 bool filled =
false;
2014 const account_object* settle_owner_ptr =
nullptr;
2022 if( head_block_time() >= HARDFORK_CORE_1780_TIME )
2023 settle_owner_ptr = &settle.owner(*
this);
2025 asset market_fees = pay_market_fees( settle_owner_ptr, get(receives.asset_id), receives, is_maker );
2032 asset force_settle_fees = pay_force_settle_fee
2033 ? pay_force_settle_fees( get(pays.asset_id), receives - market_fees )
2034 : asset( 0, receives.asset_id );
2036 auto total_collateral_denominated_fees = market_fees + force_settle_fees;
2039 if( pays < settle.balance )
2041 modify(settle, [&pays](force_settlement_object& s) {
2048 adjust_balance(settle.owner, receives - total_collateral_denominated_fees);
2050 assert( pays.asset_id != receives.asset_id );
2051 push_applied_operation( fill_order_operation( settle.id, settle.owner, pays, receives,
2052 total_collateral_denominated_fees, fill_price, is_maker ) );
2079 bool database::check_call_orders(
const asset_object& mia,
bool enable_black_swan,
bool for_new_limit_order,
2081 bool mute_exceptions,
bool skip_matching_settle_orders )
2083 const auto& dyn_prop = get_dynamic_global_properties();
2084 auto maint_time = dyn_prop.next_maintenance_time;
2085 if( for_new_limit_order )
2086 FC_ASSERT( maint_time <= HARDFORK_CORE_625_TIME );
2104 if( bsrm_type::individual_settlement_to_fund != bsrm
2105 && bsrm_type::individual_settlement_to_order != bsrm
2106 && check_for_blackswan( mia, enable_black_swan, &bitasset ) )
2113 const auto& limit_price_index = limit_index.
indices().get<by_price>();
2115 bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME );
2116 bool after_core_hardfork_2481 = HARDFORK_CORE_2481_PASSED( maint_time );
2123 auto min_price = before_core_hardfork_1270 ?
2128 auto limit_itr = limit_price_index.lower_bound( max_price );
2129 auto limit_end = limit_price_index.upper_bound( min_price );
2132 if( !after_core_hardfork_2481 && limit_itr == limit_end )
2136 const auto& call_price_index = call_index.
indices().get<by_price>();
2138 const auto& call_collateral_index = call_index.
indices().get<by_collateral>();
2143 auto call_price_itr = call_price_index.begin();
2144 auto call_price_end = call_price_itr;
2145 auto call_collateral_itr = call_collateral_index.begin();
2146 auto call_collateral_end = call_collateral_itr;
2148 if( before_core_hardfork_1270 )
2150 call_price_itr = call_price_index.lower_bound( call_min );
2151 call_price_end = call_price_index.upper_bound( call_max );
2155 call_collateral_itr = call_collateral_index.lower_bound( call_min );
2156 call_collateral_end = call_collateral_index.upper_bound( call_max );
2159 bool filled_limit =
false;
2160 bool margin_called =
false;
2162 auto head_time = head_block_time();
2163 auto head_num = head_block_num();
2165 bool before_hardfork_615 = ( head_time < HARDFORK_615_TIME );
2166 bool after_hardfork_436 = ( head_time > HARDFORK_436_TIME );
2168 bool before_core_hardfork_342 = ( maint_time <= HARDFORK_CORE_342_TIME );
2169 bool before_core_hardfork_343 = ( maint_time <= HARDFORK_CORE_343_TIME );
2170 bool before_core_hardfork_453 = ( maint_time <= HARDFORK_CORE_453_TIME );
2171 bool before_core_hardfork_606 = ( maint_time <= HARDFORK_CORE_606_TIME );
2172 bool before_core_hardfork_834 = ( maint_time <= HARDFORK_CORE_834_TIME );
2174 bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time );
2176 auto has_call_order = [ before_core_hardfork_1270,
2177 &call_collateral_itr,&call_collateral_end,
2178 &call_price_itr,&call_price_end ]()
2180 return before_core_hardfork_1270 ? ( call_price_itr != call_price_end )
2181 : ( call_collateral_itr != call_collateral_end );
2186 const auto& settlement_index = get_index_type<force_settlement_index>().indices().get<
by_expiration>();
2188 while( has_call_order() )
2191 bool settled_some = check_for_blackswan( mia, enable_black_swan, &bitasset );
2193 return margin_called;
2197 call_collateral_itr = call_collateral_index.lower_bound( call_min );
2198 if( call_collateral_itr == call_collateral_end )
2200 check_settled_debt_order( bitasset );
2203 margin_called =
true;
2204 if( bsrm_type::individual_settlement_to_fund == bsrm )
2209 const call_order_object& call_order = ( before_core_hardfork_1270 ? *call_price_itr : *call_collateral_itr );
2212 bool feed_protected = before_core_hardfork_1270 ?
2215 if( feed_protected )
2217 check_settled_debt_order( bitasset );
2218 return margin_called;
2222 if( limit_itr != limit_end )
2230 if( before_core_hardfork_606 && match_price > ~call_order.
call_price )
2231 return margin_called;
2233 margin_called =
true;
2246 auto usd_to_buy = call_order.
get_debt();
2247 if( !after_core_hardfork_2481 && ( usd_to_buy * call_pays_price ) > call_order.
get_collateral() )
2250 elog(
"black swan detected on asset ${symbol} (${id}) at block ${b}",
2252 edump((enable_black_swan));
2258 if( !before_core_hardfork_1270 )
2267 else if( !before_core_hardfork_834 )
2275 asset call_pays, call_receives, limit_pays, limit_receives;
2277 struct UndercollateralizationException {};
2280 bool filled_call =
false;
2282 if( usd_to_buy > usd_for_sale )
2284 limit_receives = usd_for_sale * match_price;
2294 if( before_core_hardfork_342 )
2295 call_receives = usd_for_sale;
2303 if( !after_core_hardfork_2481 )
2305 call_pays = usd_for_sale * call_pays_price;
2308 call_pays = call_receives * call_pays_price;
2310 throw UndercollateralizationException();
2312 auto new_debt = call_order.
get_debt() - call_receives;
2314 throw UndercollateralizationException();
2317 filled_limit =
true;
2320 call_receives = usd_to_buy;
2322 if( before_core_hardfork_342 )
2324 limit_receives = usd_to_buy * match_price;
2325 call_pays = limit_receives;
2332 if( after_core_hardfork_2481 )
2333 throw UndercollateralizationException();
2334 if( mute_exceptions )
2353 if( usd_to_buy == usd_for_sale )
2354 filled_limit =
true;
2355 else if( filled_limit && before_hardfork_615 )
2357 _issue_453_affected_assets.insert( bitasset.
asset_id );
2359 limit_pays = call_receives;
2363 const asset margin_call_fee = call_pays - limit_receives;
2365 if( filled_call && before_core_hardfork_343 )
2369 fill_call_order( call_order, call_pays, call_receives, match_price, for_new_limit_order, margin_call_fee);
2372 if( update_current_feed )
2374 update_bitasset_current_feed( bitasset,
true );
2379 if( !before_core_hardfork_1270 )
2380 call_collateral_itr = call_collateral_index.lower_bound( call_min );
2381 else if( !before_core_hardfork_343 )
2382 call_price_itr = call_price_index.lower_bound( call_min );
2384 auto next_limit_itr = std::next( limit_itr );
2386 bool really_filled = fill_limit_order( limit_order, limit_pays, limit_receives,
true,
2387 match_price, !for_new_limit_order );
2388 if( really_filled || ( filled_limit && before_core_hardfork_453 ) )
2389 limit_itr = next_limit_itr;
2393 }
catch(
const UndercollateralizationException& ) {
2403 if( skip_matching_settle_orders || !after_core_hardfork_2481 )
2404 return margin_called;
2409 auto settle_itr = settlement_index.lower_bound( bitasset.
asset_id );
2410 if( settle_itr == settlement_index.end() || settle_itr->balance.asset_id != bitasset.
asset_id )
2411 return margin_called;
2416 if( match_force_settlements( bitasset ) )
2418 margin_called =
true;
2419 call_collateral_itr = call_collateral_index.lower_bound( call_min );
2420 if( update_current_feed )
2430 check_settled_debt_order( bitasset );
2431 return margin_called;
2437 auto maint_time = get_dynamic_global_properties().next_maintenance_time;
2440 FC_ASSERT( HARDFORK_CORE_2481_PASSED( maint_time ),
"Internal error: hard fork core-2481 not passed" );
2446 auto head_time = head_block_time();
2447 bool after_core_hardfork_2582 = HARDFORK_CORE_2582_PASSED( head_time );
2449 const auto& settlement_index = get_index_type<force_settlement_index>().indices().get<
by_expiration>();
2450 auto settle_itr = settlement_index.lower_bound( bitasset.
asset_id );
2451 auto settle_end = settlement_index.upper_bound( bitasset.
asset_id );
2454 const auto& call_collateral_index = get_index_type<call_order_index>().indices().get<by_collateral>();
2457 auto call_itr = call_collateral_index.lower_bound( call_min );
2458 auto call_end = call_collateral_index.upper_bound( call_max );
2469 while( settle_itr != settle_end && call_itr != call_end )
2490 auto result = match( call_order, settle_order, call_pays_price, bitasset, max_debt_to_cover, call_match_price );
2494 if( result.amount > 0 )
2498 settle_itr = settlement_index.lower_bound( bitasset.
asset_id );
2499 call_itr = call_collateral_index.lower_bound( call_min );
2506 const auto& head_time = head_block_time();
2507 bool after_core_hardfork_2591 = HARDFORK_CORE_2591_PASSED( head_time );
2508 if( !after_core_hardfork_2591 )
2513 if( bsrm_type::individual_settlement_to_order != bsrm )
2521 const auto& limit_price_index = limit_index.
indices().get<by_price>();
2529 auto limit_itr = limit_price_index.lower_bound( max_price );
2530 auto limit_end = limit_price_index.upper_bound( min_price );
2532 bool finished =
false;
2533 while( !finished && limit_itr != limit_end )
2538 finished = ( match_settled_debt_limit( *limit_ptr, matching_limit_order, matching_limit_order.
sell_price )
2539 != match_result_type::only_maker_filled );
2540 if( !finished && old_price != limit_ptr->
sell_price )
2541 limit_end = limit_price_index.upper_bound( ~limit_ptr->
sell_price );
2547 if( pays.
asset_id == asset_id_type() )
2549 const auto& stats = receiver.
statistics(*
this);
2554 adjust_balance(receiver.
get_id(), receives);
2558 const bool& is_maker )
const
2560 assert( trade_asset.
id == trade_amount.
asset_id );
2563 return trade_asset.
amount(0);
2566 return trade_asset.
amount(0);
2570 if(!is_maker && taker_fee_percent.
valid() && *taker_fee_percent == 0)
2571 return trade_asset.
amount(0);
2573 uint16_t fee_percent;
2595 const auto market_fees = ( calculated_market_fees.
valid() ? *calculated_market_fees
2596 : calculate_market_fee( recv_asset, receives, is_maker ) );
2597 auto issuer_fees = market_fees;
2598 FC_ASSERT( issuer_fees <= receives,
"Market fee shouldn't be greater than receives");
2600 if ( issuer_fees.amount > 0 )
2603 const uint16_t network_percent = get_global_properties().parameters.get_market_fee_network_percent();
2604 if( network_percent > 0 )
2607 FC_ASSERT( network_fees_amt <= issuer_fees.amount,
2608 "Fee shared to the network shouldn't be greater than total market fee" );
2609 if( network_fees_amt > 0 )
2611 const asset network_fees = recv_asset.
amount( network_fees_amt );
2613 issuer_fees -= network_fees;
2619 if ( issuer_fees.amount > 0 )
2624 auto is_rewards_allowed = [&recv_asset, seller]() {
2628 return ( !
white_list || (*white_list).empty()
2629 || ( (*white_list).find(seller->
registrar) != (*white_list).end() ) );
2632 if ( is_rewards_allowed() )
2635 if ( reward_percent.valid() && (*reward_percent) > 0 )
2640 reward = recv_asset.
amount(reward_value);
2642 if( head_block_time() < HARDFORK_1774_TIME ){
2643 FC_ASSERT( reward < issuer_fees,
"Market reward should be less than issuer fees");
2646 FC_ASSERT( reward <= issuer_fees,
"Market reward should not be greater than issuer fees");
2649 auto registrar_reward = reward;
2655 if( head_block_time() >= HARDFORK_CORE_1800_TIME )
2663 if( referrer != registrar )
2668 if ( referrer_rewards_value > 0 &&
is_authorized_asset(*
this, referrer(*
this), recv_asset) )
2671 "Referrer reward shouldn't be greater than total reward" );
2672 const asset referrer_reward = recv_asset.
amount(referrer_rewards_value);
2673 registrar_reward -= referrer_reward;
2674 deposit_market_fee_vesting_balance(referrer, referrer_reward);
2677 if( registrar_reward.amount > 0 )
2678 deposit_market_fee_vesting_balance(registrar, registrar_reward);
2683 if( issuer_fees.amount > reward.
amount )
2708 if( !collecting_bitasset_opts.
extensions.value.force_settle_fee_percent.valid()
2709 || *collecting_bitasset_opts.
extensions.value.force_settle_fee_percent == 0 )
2713 *collecting_bitasset_opts.
extensions.value.force_settle_fee_percent);