This commit is contained in:
Daniel Larimer 2015-06-19 18:49:32 -04:00
parent 127ca9f8de
commit 08bf4ec3fe
6 changed files with 29 additions and 11 deletions

View file

@ -100,6 +100,20 @@ namespace graphene { namespace chain {
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::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); } price price::min( asset_id_type base, asset_id_type quote ) { return asset( 1, base ) / asset( GRAPHENE_MAX_SHARE_SUPPLY, quote); }
/**
* The call price is defined so that the collateral is able to purchase debt * collateral ratio.
*
* Give a margin order with @ref debt and @ref collateral we can infer the market price that
* would be necessary to maintain the invariant that the collateral can purchase debt * collateral ratio
*
* (debt / collateral_ratio) / collateral == fair market price
*
* Stated another way:
*
* C * R / D
*
* This method only works if attempting to calculate the call price from a margin position.
*/
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 {
fc::uint128 tmp( collateral.amount.value ); fc::uint128 tmp( collateral.amount.value );
@ -125,7 +139,8 @@ namespace graphene { namespace chain {
FC_ASSERT( maximum_short_squeeze_ratio >= GRAPHENE_MIN_COLLATERAL_RATIO ); FC_ASSERT( maximum_short_squeeze_ratio >= GRAPHENE_MIN_COLLATERAL_RATIO );
FC_ASSERT( maximum_short_squeeze_ratio <= GRAPHENE_MAX_COLLATERAL_RATIO ); FC_ASSERT( maximum_short_squeeze_ratio <= GRAPHENE_MAX_COLLATERAL_RATIO );
FC_ASSERT( maintenance_collateral_ratio >= GRAPHENE_MIN_COLLATERAL_RATIO ); FC_ASSERT( maintenance_collateral_ratio >= GRAPHENE_MIN_COLLATERAL_RATIO );
FC_ASSERT( maintenance_collateral_ratio <= maximum_short_squeeze_ratio ); FC_ASSERT( maintenance_collateral_ratio <= GRAPHENE_MAX_COLLATERAL_RATIO );
//FC_ASSERT( maintenance_collateral_ratio >= maximum_short_squeeze_ratio );
} FC_CAPTURE_AND_RETHROW( (*this) ) } } FC_CAPTURE_AND_RETHROW( (*this) ) }
price price_feed::max_short_squeeze_price()const price price_feed::max_short_squeeze_price()const
@ -138,6 +153,7 @@ namespace graphene { namespace chain {
collateral.amount = tmp.to_uint64(); collateral.amount = tmp.to_uint64();
return settlement_price.base / collateral; return settlement_price.base / collateral;
} }
/*
price price_feed::maintenance_price()const price price_feed::maintenance_price()const
{ {
asset collateral = settlement_price.quote; asset collateral = settlement_price.quote;
@ -148,6 +164,7 @@ namespace graphene { namespace chain {
collateral.amount = tmp.to_uint64(); collateral.amount = tmp.to_uint64();
return settlement_price.base / collateral; return settlement_price.base / collateral;
} }
*/
} } // graphene::chain } } // graphene::chain

View file

@ -106,7 +106,7 @@ void_result call_order_update_evaluator::do_apply(const call_order_update_operat
call.borrower = o.funding_account; call.borrower = o.funding_account;
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(o.delta_debt, o.delta_collateral, call.call_price = ~price::call_price(o.delta_debt, o.delta_collateral,
_bitasset_data->current_feed.maintenance_collateral_ratio); _bitasset_data->current_feed.maintenance_collateral_ratio);
}); });
} }
@ -117,7 +117,7 @@ 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(), call.call_price = ~price::call_price(call.get_debt(), call.get_collateral(),
_bitasset_data->current_feed.maintenance_collateral_ratio); _bitasset_data->current_feed.maintenance_collateral_ratio);
}); });
} }
@ -136,9 +136,9 @@ void_result call_order_update_evaluator::do_apply(const call_order_update_operat
if( !_bitasset_data->is_prediction_market ) if( !_bitasset_data->is_prediction_market )
{ {
// Check that the order's debt per collateral is less than the system's minimum debt per collateral. // Check that the order's debt per collateral is less than the system's minimum debt per collateral.
FC_ASSERT( ~call_obj->call_price < _bitasset_data->current_feed.maintenance_price(), FC_ASSERT( ~call_obj->call_price <= _bitasset_data->current_feed.settlement_price,
"Insufficient collateral for debt.", "Insufficient collateral for debt.",
("a", call_obj->call_price)("b", _bitasset_data->current_feed.maintenance_price())); ("a", ~call_obj->call_price)("b", _bitasset_data->current_feed.settlement_price));
auto call_order_id = call_obj->id; auto call_order_id = call_obj->id;

View file

@ -334,9 +334,9 @@ bool database::check_call_orders( const asset_object& mia, bool enable_black_swa
// stop when limit orders are selling too little USD for too much CORE // stop when limit orders are selling too little USD for too much CORE
auto min_price = bitasset.current_feed.max_short_squeeze_price(); auto min_price = bitasset.current_feed.max_short_squeeze_price();
// edump((bitasset.current_feed)); // edump((bitasset.current_feed));
// edump((min_price.to_real())(min_price)); edump((min_price.to_real())(min_price));
edump((max_price.to_real())(max_price));
//auto min_price = price::min( mia.id, bitasset.options.short_backing_asset ); //auto min_price = price::min( mia.id, bitasset.options.short_backing_asset );
/*
idump((bitasset.current_feed.settlement_price)(bitasset.current_feed.settlement_price.to_real())); idump((bitasset.current_feed.settlement_price)(bitasset.current_feed.settlement_price.to_real()));
{ {
for( const auto& order : limit_price_index ) for( const auto& order : limit_price_index )
@ -350,10 +350,9 @@ bool database::check_call_orders( const asset_object& mia, bool enable_black_swa
wdump((max_price)(max_price.to_real())); wdump((max_price)(max_price.to_real()));
wdump((min_price)(min_price.to_real())); wdump((min_price)(min_price.to_real()));
} }
*/
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 );

View file

@ -157,8 +157,8 @@ namespace graphene { namespace chain {
* debt * maintenance_price() < collateral * debt * maintenance_price() < collateral
* debt * settlement_price < debt * maintenance * debt * settlement_price < debt * maintenance
* debt * maintenance_price() < debt * max_short_squeeze_price() * debt * maintenance_price() < debt * max_short_squeeze_price()
*/
price maintenance_price()const; price maintenance_price()const;
*/
/** When selling collateral to pay off debt, the least amount of debt to receive should be /** When selling collateral to pay off debt, the least amount of debt to receive should be
* min_usd = max_short_squeeze_price() * collateral * min_usd = max_short_squeeze_price() * collateral

View file

@ -48,7 +48,7 @@ namespace graphene { namespace chain {
account_id_type borrower; account_id_type borrower;
share_type collateral; ///< call_price.base.asset_id, access via get_collateral share_type collateral; ///< call_price.base.asset_id, access via get_collateral
share_type debt; ///< call_price.quote.asset_id, access via get_collateral share_type debt; ///< call_price.quote.asset_id, access via get_collateral
price call_price; price call_price; ///< Debt / Collateral
}; };
/** /**

View file

@ -47,12 +47,14 @@ BOOST_AUTO_TEST_CASE( feed_limit_logic_test )
price_feed feed; price_feed feed;
feed.settlement_price = usd / core; feed.settlement_price = usd / core;
/*
wdump((feed.settlement_price.to_real())); wdump((feed.settlement_price.to_real()));
wdump((feed.maintenance_price().to_real())); wdump((feed.maintenance_price().to_real()));
wdump((feed.max_short_squeeze_price().to_real())); wdump((feed.max_short_squeeze_price().to_real()));
BOOST_CHECK( usd * feed.settlement_price < usd * feed.maintenance_price() ); BOOST_CHECK( usd * feed.settlement_price < usd * feed.maintenance_price() );
BOOST_CHECK( usd * feed.maintenance_price() < usd * feed.max_short_squeeze_price() ); BOOST_CHECK( usd * feed.maintenance_price() < usd * feed.max_short_squeeze_price() );
*/
} catch (fc::exception& e) { } catch (fc::exception& e) {
edump((e.to_detail_string())); edump((e.to_detail_string()));