switch to rationals

This commit is contained in:
Daniel Larimer 2015-06-22 15:04:19 -04:00
parent 49b2ce8094
commit 758d588aa1
5 changed files with 36 additions and 8 deletions

View file

@ -17,6 +17,7 @@
*/ */
#include <graphene/chain/asset.hpp> #include <graphene/chain/asset.hpp>
#include <fc/uint128.hpp> #include <fc/uint128.hpp>
#include <boost/rational.hpp>
namespace graphene { namespace chain { namespace graphene { namespace chain {
bool operator < ( const asset& a, const asset& b ) 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 * This method divides the collateral by the maintenance collateral ratio to derive
* a call price for the given black swan ratio. * 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 { { try {
//wdump((debt)(collateral)(collateral_ratio));
boost::rational<uint64_t> swan(debt.amount.value,collateral.amount.value);
boost::rational<uint64_t> 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 ); fc::uint128 tmp( collateral.amount.value );
tmp *= 1000; tmp *= 1000;
tmp /= collateral_ratio; tmp /= collateral_ratio;
FC_ASSERT( tmp <= GRAPHENE_MAX_SHARE_SUPPLY ); 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) ) } } FC_CAPTURE_AND_RETHROW( (debt)(collateral)(collateral_ratio) ) }
bool price::is_null() const { return *this == price(); } bool price::is_null() const { return *this == price(); }

View file

@ -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 ){ d.modify( *call_obj, [&]( call_order_object& call ){
call.collateral += o.delta_collateral.amount; call.collateral += o.delta_collateral.amount;
call.debt += o.delta_debt.amount; call.debt += o.delta_debt.amount;
call.call_price = price::call_price(call.get_debt(), call.get_collateral(), if( call.debt > 0 )
_bitasset_data->current_feed.maintenance_collateral_ratio); {
call.call_price = price::call_price(call.get_debt(), call.get_collateral(),
_bitasset_data->current_feed.maintenance_collateral_ratio);
}
}); });
} }

View file

@ -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 ); 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 // NOTE limit_price_index is sorted from greatest to least
auto limit_itr = limit_price_index.lower_bound( max_price ); auto limit_itr = limit_price_index.lower_bound( max_price );
auto limit_end = limit_price_index.upper_bound( min_price ); auto limit_end = limit_price_index.upper_bound( min_price );

@ -1 +1 @@
Subproject commit dd1c77b327c6eba807168856c3c12e90173468c4 Subproject commit dde8ed9d7ab49807f2556488c0815f3741b11e00

View file

@ -51,8 +51,8 @@ BOOST_AUTO_TEST_CASE( feed_limit_logic_test )
auto swanp = usd / core; auto swanp = usd / core;
auto callp = ~price::call_price( usd, core, 1750 ); auto callp = ~price::call_price( usd, core, 1750 );
// 1:1 collateral // 1:1 collateral
wdump((callp.to_real())(callp)); // wdump((callp.to_real())(callp));
wdump((swanp.to_real())(swanp)); // wdump((swanp.to_real())(swanp));
FC_ASSERT( callp.to_real() > swanp.to_real() ); FC_ASSERT( callp.to_real() > swanp.to_real() );
/* /*