From f2c049265ed5078538f4bc84c7b0498258e59ac2 Mon Sep 17 00:00:00 2001 From: alfredo Date: Tue, 31 Jan 2017 18:01:57 -0300 Subject: [PATCH] Asset API Added 2 functions --- libraries/app/api.cpp | 37 ++++++++++++++++++++++ libraries/app/include/graphene/app/api.hpp | 12 ++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index 47d07289..40aa7b97 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -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_api::get_all_asset_holders() const { + + vector result; + + vector total_assets; + for( const asset_object& asset_obj : _db.get_index_type().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 diff --git a/libraries/app/include/graphene/app/api.hpp b/libraries/app/include/graphene/app/api.hpp index bc629ea2..be0e0a23 100644 --- a/libraries/app/include/graphene/app/api.hpp +++ b/libraries/app/include/graphene/app/api.hpp @@ -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 * @@ -285,6 +290,8 @@ namespace graphene { namespace app { ~asset_api(); vector get_asset_holders( asset_id_type asset_id )const; + int get_asset_holders_count( asset_id_type asset_id )const; + vector get_all_asset_holders() const; private: graphene::chain::database& _db; @@ -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) @@ -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)