From 758d588aa111caa9d6b7a55558ed6a809f3273bf Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Mon, 22 Jun 2015 15:04:19 -0400 Subject: [PATCH] switch to rationals --- libraries/chain/asset.cpp | 29 ++++++++++++++++++++++-- libraries/chain/call_order_evaluator.cpp | 7 ++++-- libraries/chain/db_market.cpp | 2 +- libraries/fc | 2 +- tests/tests/operation_tests.cpp | 4 ++-- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/libraries/chain/asset.cpp b/libraries/chain/asset.cpp index 22a2dd6a..81b761b5 100644 --- a/libraries/chain/asset.cpp +++ b/libraries/chain/asset.cpp @@ -17,6 +17,7 @@ */ #include #include +#include namespace graphene { namespace chain { bool operator < ( const asset& a, const asset& b ) @@ -108,14 +109,38 @@ namespace graphene { namespace chain { * * This method divides the collateral by the maintenance collateral ratio to derive * a call price for the given black swan ratio. + * + * There exists some cases where the debt and collateral values are so small that + * dividing by the collateral ratio will result in a 0 price or really poor + * rounding errors. No matter what the collateral part of the price ratio can + * never go to 0 and the debt can never go more than GRAPHENE_MAX_SHARE_SUPPLY + * + * CR * DEBT/COLLAT or DEBT/(COLLAT/CR) */ - price price::call_price(const asset& debt, const asset& collateral, uint16_t collateral_ratio) + price price::call_price( const asset& debt, const asset& collateral, uint16_t collateral_ratio) { try { + //wdump((debt)(collateral)(collateral_ratio)); + boost::rational swan(debt.amount.value,collateral.amount.value); + boost::rational ratio( collateral_ratio, 1000 ); + auto cp = swan * ratio; + return ~(asset( cp.numerator(), debt.asset_id ) / asset( cp.denominator(), collateral.asset_id )); + + /* + while( collateral.amount < 100000 && debt.amount < GRAPHENE_MAX_SHARE_SUPPLY/100 ) + { + collateral.amount *= 1000; + debt.amount *= 1000; + } + fc::uint128 tmp( collateral.amount.value ); tmp *= 1000; tmp /= collateral_ratio; FC_ASSERT( tmp <= GRAPHENE_MAX_SHARE_SUPPLY ); - return asset( tmp.to_uint64(), collateral.asset_id) / debt; + asset col( tmp.to_uint64(), collateral.asset_id); + + if( col.amount == 0 ) col.amount = 1; + return col / debt; + */ } FC_CAPTURE_AND_RETHROW( (debt)(collateral)(collateral_ratio) ) } bool price::is_null() const { return *this == price(); } diff --git a/libraries/chain/call_order_evaluator.cpp b/libraries/chain/call_order_evaluator.cpp index b508d49f..379e2287 100644 --- a/libraries/chain/call_order_evaluator.cpp +++ b/libraries/chain/call_order_evaluator.cpp @@ -117,8 +117,11 @@ void_result call_order_update_evaluator::do_apply(const call_order_update_operat d.modify( *call_obj, [&]( call_order_object& call ){ call.collateral += o.delta_collateral.amount; call.debt += o.delta_debt.amount; - call.call_price = price::call_price(call.get_debt(), call.get_collateral(), - _bitasset_data->current_feed.maintenance_collateral_ratio); + if( call.debt > 0 ) + { + call.call_price = price::call_price(call.get_debt(), call.get_collateral(), + _bitasset_data->current_feed.maintenance_collateral_ratio); + } }); } diff --git a/libraries/chain/db_market.cpp b/libraries/chain/db_market.cpp index da0013d7..59b35430 100644 --- a/libraries/chain/db_market.cpp +++ b/libraries/chain/db_market.cpp @@ -354,7 +354,7 @@ bool database::check_call_orders( const asset_object& mia, bool enable_black_swa */ assert( max_price.base.asset_id == min_price.base.asset_id ); - wlog( "from ${a} Debt/Col to ${b} Debt/Col ", ("a", max_price.to_real())("b",min_price.to_real()) ); + // wlog( "from ${a} Debt/Col to ${b} Debt/Col ", ("a", max_price.to_real())("b",min_price.to_real()) ); // NOTE limit_price_index is sorted from greatest to least auto limit_itr = limit_price_index.lower_bound( max_price ); auto limit_end = limit_price_index.upper_bound( min_price ); diff --git a/libraries/fc b/libraries/fc index dd1c77b3..dde8ed9d 160000 --- a/libraries/fc +++ b/libraries/fc @@ -1 +1 @@ -Subproject commit dd1c77b327c6eba807168856c3c12e90173468c4 +Subproject commit dde8ed9d7ab49807f2556488c0815f3741b11e00 diff --git a/tests/tests/operation_tests.cpp b/tests/tests/operation_tests.cpp index 800fe906..b96088d6 100644 --- a/tests/tests/operation_tests.cpp +++ b/tests/tests/operation_tests.cpp @@ -51,8 +51,8 @@ BOOST_AUTO_TEST_CASE( feed_limit_logic_test ) auto swanp = usd / core; auto callp = ~price::call_price( usd, core, 1750 ); // 1:1 collateral - wdump((callp.to_real())(callp)); - wdump((swanp.to_real())(swanp)); +// wdump((callp.to_real())(callp)); +// wdump((swanp.to_real())(swanp)); FC_ASSERT( callp.to_real() > swanp.to_real() ); /*