Skip to content

Commit

Permalink
Market related minor code refactory and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed Sep 1, 2018
1 parent a0225fb commit 0a403a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
41 changes: 21 additions & 20 deletions libraries/chain/db_market.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ void database::_cancel_bids_and_revive_mpa( const asset_object& bitasset, const

// cancel remaining bids
const auto& bid_idx = get_index_type< collateral_bid_index >().indices().get<by_price>();
auto itr = bid_idx.lower_bound( boost::make_tuple( bitasset.id, price::max( bad.options.short_backing_asset, bitasset.id ), collateral_bid_id_type() ) );
auto itr = bid_idx.lower_bound( boost::make_tuple( bitasset.id,
price::max( bad.options.short_backing_asset, bitasset.id ),
collateral_bid_id_type() ) );
while( itr != bid_idx.end() && itr->inv_swan_price.quote.asset_id == bitasset.id )
{
const collateral_bid_object& bid = *itr;
Expand Down Expand Up @@ -159,7 +161,8 @@ void database::cancel_bid(const collateral_bid_object& bid, bool create_virtual_
remove(bid);
}

void database::execute_bid( const collateral_bid_object& bid, share_type debt_covered, share_type collateral_from_fund, const price_feed& current_feed )
void database::execute_bid( const collateral_bid_object& bid, share_type debt_covered, share_type collateral_from_fund,
const price_feed& current_feed )
{
const call_order_object& call_obj = create<call_order_object>( [&](call_order_object& call ){
call.borrower = bid.bidder;
Expand All @@ -170,8 +173,9 @@ void database::execute_bid( const collateral_bid_object& bid, share_type debt_co
current_feed.maintenance_collateral_ratio);
});

// Note: CORE asset in collateral_bid_object is not counted in account_stats.total_core_in_orders
if( bid.inv_swan_price.base.asset_id == asset_id_type() )
modify(bid.bidder(*this).statistics(*this), [&](account_statistics_object& stats) {
modify( get_account_stats_by_owner(bid.bidder), [&](account_statistics_object& stats) {
stats.total_core_in_orders += call_obj.collateral;
});

Expand Down Expand Up @@ -845,11 +849,11 @@ bool database::fill_limit_order( const limit_order_object& order, const asset& p
bool database::fill_call_order( const call_order_object& order, const asset& pays, const asset& receives,
const price& fill_price, const bool is_maker )
{ try {
//idump((pays)(receives)(order));
FC_ASSERT( order.get_debt().asset_id == receives.asset_id );
FC_ASSERT( order.get_collateral().asset_id == pays.asset_id );
FC_ASSERT( order.get_collateral() >= pays );
FC_ASSERT( order.debt_type() == receives.asset_id );
FC_ASSERT( order.collateral_type() == pays.asset_id );
FC_ASSERT( order.collateral >= pays.amount );

// TODO pass in mia and bitasset_data for better performance
const asset_object& mia = receives.asset_id(*this);
FC_ASSERT( mia.is_market_issued() );

Expand All @@ -865,33 +869,30 @@ bool database::fill_call_order( const call_order_object& order, const asset& pay
else if( get_dynamic_global_properties().next_maintenance_time > HARDFORK_CORE_343_TIME )
o.call_price = price::call_price( o.get_debt(), o.get_collateral(),
mia.bitasset_data(*this).current_feed.maintenance_collateral_ratio );
});
});

// update current supply
const asset_dynamic_data_object& mia_ddo = mia.dynamic_asset_data_id(*this);

modify( mia_ddo, [&]( asset_dynamic_data_object& ao ){
//idump((receives));
ao.current_supply -= receives.amount;
modify( mia_ddo, [&receives]( asset_dynamic_data_object& ao ){
ao.current_supply -= receives.amount;
});

const account_object& borrower = order.borrower(*this);
// adjust balance and/or account statistics
if( collateral_freed.valid() || pays.asset_id == asset_id_type() )
{
const account_statistics_object& borrower_statistics = borrower.statistics(*this);
if( collateral_freed.valid() )
adjust_balance(borrower.get_id(), *collateral_freed);
adjust_balance( order.borrower, *collateral_freed );

modify( borrower_statistics, [&]( account_statistics_object& b ){
if( collateral_freed.valid() && collateral_freed->amount > 0 && collateral_freed->asset_id == asset_id_type() )
b.total_core_in_orders -= collateral_freed->amount;
modify( get_account_stats_by_owner(order.borrower), [&collateral_freed,&pays]( account_statistics_object& b ){
if( collateral_freed.valid() && collateral_freed->asset_id == asset_id_type() )
b.total_core_in_orders -= collateral_freed->amount;
if( pays.asset_id == asset_id_type() )
b.total_core_in_orders -= pays.amount;
b.total_core_in_orders -= pays.amount;

assert( b.total_core_in_orders >= 0 );
});
}

assert( pays.asset_id != receives.asset_id );
push_applied_operation( fill_order_operation( order.id, order.borrower, pays, receives,
asset(0, pays.asset_id), fill_price, is_maker ) );

Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/market_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ void_result bid_collateral_evaluator::do_apply(const bid_collateral_operation& o
bid.inv_swan_price = o.additional_collateral / o.debt_covered;
});

// Note: CORE asset in collateral_bid_object is not counted in account_stats.total_core_in_orders

return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }

Expand Down

0 comments on commit 0a403a7

Please sign in to comment.