diff --git a/libraries/chain/asset.cpp b/libraries/chain/asset.cpp index c03cd11d..f9e8bace 100644 --- a/libraries/chain/asset.cpp +++ b/libraries/chain/asset.cpp @@ -94,10 +94,11 @@ namespace graphene { namespace chain { } price operator / ( const asset& base, const asset& quote ) - { + { try { FC_ASSERT( base.asset_id != quote.asset_id ); return price{base,quote}; - } + } FC_CAPTURE_AND_RETHROW( (base)(quote) ) } + price price::max( asset_id_type base, asset_id_type quote ) { return asset( share_type(GRAPHENE_MAX_SHARE_SUPPLY), base ) / asset( share_type(1), quote); } price price::min( asset_id_type base, asset_id_type quote ) { return asset( 1, base ) / asset( GRAPHENE_MAX_SHARE_SUPPLY, quote); } diff --git a/libraries/chain/db_market.cpp b/libraries/chain/db_market.cpp index 370aea97..ff54af38 100644 --- a/libraries/chain/db_market.cpp +++ b/libraries/chain/db_market.cpp @@ -55,13 +55,16 @@ void database::globally_settle_asset( const asset_object& mia, const price& sett const call_order_index& call_index = get_index_type(); const auto& call_price_index = call_index.indices().get(); - // cancel all call orders and accumulate it into collateral_gathered auto call_itr = call_price_index.lower_bound( price::min( bitasset.options.short_backing_asset, mia.id ) ); auto call_end = call_price_index.upper_bound( price::max( bitasset.options.short_backing_asset, mia.id ) ); while( call_itr != call_end ) { auto pays = call_itr->get_debt() * settlement_price; + + if( pays > call_itr->get_collateral() ) + pays = call_itr->get_collateral(); + collateral_gathered += pays; const auto& order = *call_itr; ++call_itr; @@ -69,7 +72,8 @@ void database::globally_settle_asset( const asset_object& mia, const price& sett } modify( bitasset, [&]( asset_bitasset_data_object& obj ){ - obj.settlement_price = settlement_price; + assert( collateral_gathered.asset_id == settlement_price.quote.asset_id ); + obj.settlement_price = mia.amount(original_mia_supply) / collateral_gathered; //settlement_price; obj.settlement_fund = collateral_gathered.amount; }); @@ -418,7 +422,8 @@ bool database::check_call_orders(const asset_object& mia, bool enable_black_swan if( usd_to_buy * match_price > call_itr->get_collateral() ) { FC_ASSERT( enable_black_swan ); - globally_settle_asset(mia, call_itr->get_debt() / call_itr->get_collateral()); + //globally_settle_asset(mia, call_itr->get_debt() / call_itr->get_collateral()); + globally_settle_asset(mia, bitasset.current_feed.settlement_price );// call_itr->get_debt() / call_itr->get_collateral()); return true; } diff --git a/tests/tests/operation_tests2.cpp b/tests/tests/operation_tests2.cpp index cbee71f0..9e48dff7 100644 --- a/tests/tests/operation_tests2.cpp +++ b/tests/tests/operation_tests2.cpp @@ -546,11 +546,11 @@ BOOST_AUTO_TEST_CASE( global_settle_test ) force_settle(dan_id(db), asset(495, bit_usd_id)); BOOST_CHECK_EQUAL(get_balance(valentine_id, bit_usd_id), 0); - BOOST_CHECK_EQUAL(get_balance(valentine_id, asset_id_type()), 10046); + BOOST_CHECK_EQUAL(get_balance(valentine_id, asset_id_type()), 10045); BOOST_CHECK_EQUAL(get_balance(ben_id, bit_usd_id), 0); BOOST_CHECK_EQUAL(get_balance(ben_id, asset_id_type()), 10091); BOOST_CHECK_EQUAL(get_balance(dan_id, bit_usd_id), 0); - BOOST_CHECK_EQUAL(get_balance(dan_id, asset_id_type()), 9850); + BOOST_CHECK_EQUAL(get_balance(dan_id, asset_id_type()), 9849); } FC_LOG_AND_RETHROW() }