Skip to content

Commit

Permalink
Resolve bitshares#1781
Browse files Browse the repository at this point in the history
  • Loading branch information
pmconrad committed Jun 29, 2019
1 parent d40f70a commit 6bb8213
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
4 changes: 4 additions & 0 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
asset_id_type id = a.id;
extended_asset_object result = extended_asset_object( std::forward<ASSET>( a ) );
if( helper )
{
result.total_in_collateral = helper->get_amount_in_collateral( id );
if( result.bitasset_data_id.valid() )
result.total_backing_collateral = helper->get_backing_collateral( id );
}
return result;
}

Expand Down
4 changes: 3 additions & 1 deletion libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct extended_asset_object : asset_object
explicit extended_asset_object( ASSET&& a ) : asset_object( std::forward<ASSET>(a) ) {}

optional<share_type> total_in_collateral;
optional<share_type> total_backing_collateral;
};

/**
Expand Down Expand Up @@ -846,7 +847,8 @@ FC_REFLECT( graphene::app::market_ticker,
FC_REFLECT( graphene::app::market_volume, (time)(base)(quote)(base_volume)(quote_volume) );
FC_REFLECT( graphene::app::market_trade, (sequence)(date)(price)(amount)(value)(side1_account_id)(side2_account_id) );

FC_REFLECT_DERIVED( graphene::app::extended_asset_object, (graphene::chain::asset_object), (total_in_collateral) );
FC_REFLECT_DERIVED( graphene::app::extended_asset_object, (graphene::chain::asset_object),
(total_in_collateral)(total_backing_collateral) );

FC_API(graphene::app::database_api,
// Objects
Expand Down
49 changes: 39 additions & 10 deletions libraries/plugins/api_helper_indexes/api_helper_indexes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,41 @@ namespace graphene { namespace api_helper_indexes {
void amount_in_collateral_index::object_inserted( const object& objct )
{ try {
const call_order_object& o = static_cast<const call_order_object&>( objct );
auto itr = amounts.find( o.collateral_type() );
if( itr == amounts.end() )
amounts[o.collateral_type()] = o.collateral;
else
itr->second += o.collateral;

{
auto itr = in_collateral.find( o.collateral_type() );
if( itr == in_collateral.end() )
in_collateral[o.collateral_type()] = o.collateral;
else
itr->second += o.collateral;
}

{
auto itr = backing_collateral.find( o.debt_type() );
if( itr == backing_collateral.end() )
backing_collateral[o.debt_type()] = o.collateral;
else
itr->second += o.collateral;
}

} FC_CAPTURE_AND_RETHROW( (objct) ); }

void amount_in_collateral_index::object_removed( const object& objct )
{ try {
const call_order_object& o = static_cast<const call_order_object&>( objct );
auto itr = amounts.find( o.collateral_type() );
if( itr == amounts.end() ) return; // should never happen
itr->second -= o.collateral;

{
auto itr = in_collateral.find( o.collateral_type() );
if( itr != in_collateral.end() ) // should always be true
itr->second -= o.collateral;
}

{
auto itr = backing_collateral.find( o.debt_type() );
if( itr != backing_collateral.end() ) // should always be true
itr->second -= o.collateral;
}

} FC_CAPTURE_AND_RETHROW( (objct) ); }

void amount_in_collateral_index::about_to_modify( const object& objct )
Expand All @@ -57,8 +79,15 @@ void amount_in_collateral_index::object_modified( const object& objct )

share_type amount_in_collateral_index::get_amount_in_collateral( const asset_id_type& asset )const
{ try {
auto itr = amounts.find( asset );
if( itr == amounts.end() ) return 0;
auto itr = in_collateral.find( asset );
if( itr == in_collateral.end() ) return 0;
return itr->second;
} FC_CAPTURE_AND_RETHROW( (asset) ); }

share_type amount_in_collateral_index::get_backing_collateral( const asset_id_type& asset )const
{ try {
auto itr = backing_collateral.find( asset );
if( itr == backing_collateral.end() ) return 0;
return itr->second;
} FC_CAPTURE_AND_RETHROW( (asset) ); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace graphene { namespace api_helper_indexes {
using namespace chain;

/**
* @brief This secondary index tracks how much of each asset is locked up as collateral for MPAs.
* @brief This secondary index tracks how much of each asset is locked up as collateral for MPAs, and how much
* collateral is backing an MPA in total.
*/
class amount_in_collateral_index : public secondary_index
{
Expand All @@ -41,9 +42,11 @@ class amount_in_collateral_index : public secondary_index
virtual void object_modified( const object& after ) override;

share_type get_amount_in_collateral( const asset_id_type& asset )const;
share_type get_backing_collateral( const asset_id_type& asset )const;

private:
flat_map<asset_id_type, share_type> amounts;
flat_map<asset_id_type, share_type> in_collateral;
flat_map<asset_id_type, share_type> backing_collateral;
};

namespace detail
Expand Down

0 comments on commit 6bb8213

Please sign in to comment.