Fix #130 Black Swans Settle at Price Feed

This commit is contained in:
Daniel Larimer 2015-07-03 17:07:24 -04:00
parent cc1348bf91
commit c0c6022338
3 changed files with 13 additions and 7 deletions

View file

@ -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); }

View file

@ -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<call_order_index>();
const auto& call_price_index = call_index.indices().get<by_price>();
// 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;
}

View file

@ -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()
}