Skip to content

Commit

Permalink
Merge pull request cryptonomex#226 from oxarbitrage/new-functions-ass…
Browse files Browse the repository at this point in the history
…et-api

Asset API Added 2 functions
  • Loading branch information
vikramrajkumar committed Feb 1, 2017
2 parents fa9f7a7 + f2c0492 commit ec52052
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
37 changes: 37 additions & 0 deletions libraries/app/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,5 +611,42 @@ namespace graphene { namespace app {

return result;
}
// get number of asset holders.
int asset_api::get_asset_holders_count( asset_id_type asset_id ) const {

const auto& bal_idx = _db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
auto range = bal_idx.equal_range( boost::make_tuple( asset_id ) );

int count = boost::distance(range) - 1;

return count;
}
// function to get vector of system assets with holders count.
vector<asset_holders> asset_api::get_all_asset_holders() const {

vector<asset_holders> result;

vector<asset_id_type> total_assets;
for( const asset_object& asset_obj : _db.get_index_type<asset_index>().indices() )
{
const auto& dasset_obj = asset_obj.dynamic_asset_data_id(_db);

asset_id_type asset_id;
asset_id = dasset_obj.id;

const auto& bal_idx = _db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
auto range = bal_idx.equal_range( boost::make_tuple( asset_id ) );

int count = boost::distance(range) - 1;

asset_holders ah;
ah.asset_id = asset_id;
ah.count = count;

result.push_back(ah);
}

return result;
}

} } // graphene::app
12 changes: 11 additions & 1 deletion libraries/app/include/graphene/app/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ namespace graphene { namespace app {
account_id_type account_id;
share_type amount;
};

struct asset_holders
{
asset_id_type asset_id;
int count;
};

/**
* @brief The history_api class implements the RPC API for account history
*
Expand Down Expand Up @@ -285,6 +290,8 @@ namespace graphene { namespace app {
~asset_api();

vector<account_asset_balance> get_asset_holders( asset_id_type asset_id )const;
int get_asset_holders_count( asset_id_type asset_id )const;
vector<asset_holders> get_all_asset_holders() const;

private:
graphene::chain::database& _db;
Expand Down Expand Up @@ -355,6 +362,7 @@ FC_REFLECT( graphene::app::verify_range_proof_rewind_result,
//FC_REFLECT_TYPENAME( fc::ecc::commitment_type );

FC_REFLECT( graphene::app::account_asset_balance, (name)(account_id)(amount) );
FC_REFLECT( graphene::app::asset_holders, (asset_id)(count) );

FC_API(graphene::app::history_api,
(get_account_history)
Expand Down Expand Up @@ -392,6 +400,8 @@ FC_API(graphene::app::crypto_api,
)
FC_API(graphene::app::asset_api,
(get_asset_holders)
(get_asset_holders_count)
(get_all_asset_holders)
)
FC_API(graphene::app::login_api,
(login)
Expand Down

0 comments on commit ec52052

Please sign in to comment.