Skip to content

Commit

Permalink
Check for blackswan by collateral after hf #1270
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed Sep 13, 2018
1 parent 68c702b commit 8bcf38d
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions libraries/chain/db_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,34 @@ bool database::check_for_blackswan( const asset_object& mia, bool enable_black_s
auto settle_price = bitasset.current_feed.settlement_price;
if( settle_price.is_null() ) return false; // no feed

const call_order_index& call_index = get_index_type<call_order_index>();
const auto& call_price_index = call_index.indices().get<by_price>();
const call_order_object* call_ptr = nullptr; // place holder for the call order with least collateral ratio

auto call_min = price::min( bitasset.options.short_backing_asset, mia.id );
auto call_max = price::max( bitasset.options.short_backing_asset, mia.id );
auto call_itr = call_price_index.lower_bound( call_min );
auto call_end = call_price_index.upper_bound( call_max );
asset_id_type debt_asset_id = mia.id;
auto call_min = price::min( bitasset.options.short_backing_asset, debt_asset_id );

if( call_itr == call_end ) return false; // no call orders
auto maint_time = get_dynamic_global_properties().next_maintenance_time;
bool before_core_hardfork_1270 = ( maint_time <= HARDFORK_CORE_1270_TIME ); // call price caching issue

price highest = settle_price;
if( before_core_hardfork_1270 ) // before core-1270 hard fork, check with call_price
{
const auto& call_price_index = get_index_type<call_order_index>().indices().get<by_price>();
auto call_itr = call_price_index.lower_bound( call_min );
if( call_itr == call_price_index.end() ) // no call order
return false;
call_ptr = &(*call_itr);
}
else // after core-1270 hard fork, check with collateralization
{
const auto& call_collateral_index = get_index_type<call_order_index>().indices().get<by_collateral>();
auto call_itr = call_collateral_index.lower_bound( call_min );
if( call_itr == call_collateral_index.end() ) // no call order
return false;
call_ptr = &(*call_itr);
}
if( call_ptr->debt_type() != debt_asset_id ) // no call order
return false;

const auto& dyn_prop = get_dynamic_global_properties();
auto maint_time = dyn_prop.next_maintenance_time;
price highest = settle_price;
if( maint_time > HARDFORK_CORE_1270_TIME )
// due to #338, we won't check for black swan on incoming limit order, so need to check with MSSP here
highest = bitasset.current_feed.max_short_squeeze_price();
Expand All @@ -229,10 +243,10 @@ bool database::check_for_blackswan( const asset_object& mia, bool enable_black_s
highest = std::max( limit_itr->sell_price, highest );
}

auto least_collateral = call_itr->collateralization();
auto least_collateral = call_ptr->collateralization();
if( ~least_collateral >= highest )
{
wdump( (*call_itr) );
wdump( (*call_ptr) );
elog( "Black Swan detected on asset ${symbol} (${id}) at block ${b}: \n"
" Least collateralized call: ${lc} ${~lc}\n"
// " Highest Bid: ${hb} ${~hb}\n"
Expand Down

0 comments on commit 8bcf38d

Please # to comment.