From 20a10a074b9d85892541b5b9aded8276c2ef17a5 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Tue, 23 Jun 2015 18:23:41 -0400 Subject: [PATCH] Issue #47 market history added to api --- libraries/app/CMakeLists.txt | 2 +- libraries/app/api.cpp | 23 +++++++++++++++++++ libraries/app/include/graphene/app/api.hpp | 7 +++++- .../market_history/market_history_plugin.hpp | 4 ++++ programs/witness_node/CMakeLists.txt | 2 +- programs/witness_node/main.cpp | 2 ++ 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/libraries/app/CMakeLists.txt b/libraries/app/CMakeLists.txt index 308ee736..07d1d43b 100644 --- a/libraries/app/CMakeLists.txt +++ b/libraries/app/CMakeLists.txt @@ -6,7 +6,7 @@ add_library( graphene_app plugin.cpp ) -target_link_libraries( graphene_app graphene_chain fc graphene_db graphene_net graphene_time graphene_utilities ) +target_link_libraries( graphene_app graphene_market_history graphene_chain fc graphene_db graphene_net graphene_time graphene_utilities ) target_include_directories( graphene_app PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ) diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index 47ac4625..6a7a2d15 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -445,6 +445,28 @@ namespace graphene { namespace app { } return result; } + vector history_api::get_market_history( asset_id_type a, asset_id_type b, uint32_t bucket_seconds, fc::time_point_sec start, fc::time_point_sec end )const + { try { + FC_ASSERT(_app.chain_database()); + const auto& db = *_app.chain_database(); + vector result; + result.reserve(100); + + if( a > b ) std::swap(a,b); + + const auto& bidx = db.get_index_type(); + const auto& by_key_idx = bidx.indices().get(); + + auto itr = by_key_idx.lower_bound( bucket_key( a, b, bucket_seconds, start ) ); + while( itr != by_key_idx.end() && itr->key.open <= end && result.size() < 100 ) + { + if( !(itr->key.base == a && itr->key.quote == b && itr->key.seconds == bucket_seconds) ) + return result; + result.push_back(*itr); + ++itr; + } + return result; + } FC_CAPTURE_AND_RETHROW( (a)(b)(bucket_seconds)(start)(end) ) } /** TODO: add secondary index that will accelerate this process */ vector database_api::get_proposed_transactions( account_id_type id )const @@ -464,4 +486,5 @@ namespace graphene { namespace app { return result; } + } } // graphene::app diff --git a/libraries/app/include/graphene/app/api.hpp b/libraries/app/include/graphene/app/api.hpp index b0513b45..b6a7efbe 100644 --- a/libraries/app/include/graphene/app/api.hpp +++ b/libraries/app/include/graphene/app/api.hpp @@ -29,10 +29,14 @@ #include #include + +#include + #include namespace graphene { namespace app { using namespace graphene::chain; + using namespace graphene::market_history; class application; @@ -270,6 +274,7 @@ namespace graphene { namespace app { int limit = 100, operation_history_id_type start = operation_history_id_type())const; + vector get_market_history( asset_id_type a, asset_id_type b, uint32_t bucket_seconds, fc::time_point_sec start, fc::time_point_sec end )const; private: application& _app; }; @@ -372,7 +377,7 @@ FC_API(graphene::app::database_api, (get_transaction_hex) (get_proposed_transactions) ) -FC_API(graphene::app::history_api, (get_account_history)) +FC_API(graphene::app::history_api, (get_account_history)(get_market_history)) FC_API(graphene::app::network_api, (broadcast_transaction)(add_node)(get_connected_peers)) FC_API(graphene::app::login_api, (login) diff --git a/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp b/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp index 3c146444..6c0bdca1 100644 --- a/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp +++ b/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp @@ -42,6 +42,10 @@ namespace bpo = boost::program_options; struct bucket_key { + bucket_key( asset_id_type a, asset_id_type b, uint32_t s, fc::time_point_sec o ) + :base(a),quote(b),seconds(s),open(o){} + bucket_key(){} + asset_id_type base; asset_id_type quote; uint32_t seconds = 0; diff --git a/programs/witness_node/CMakeLists.txt b/programs/witness_node/CMakeLists.txt index 0c75e94a..d4d276ae 100644 --- a/programs/witness_node/CMakeLists.txt +++ b/programs/witness_node/CMakeLists.txt @@ -9,4 +9,4 @@ endif() #endif() target_link_libraries( witness_node - PRIVATE graphene_app graphene_account_history graphene_witness graphene_chain fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} ) + PRIVATE graphene_app graphene_account_history graphene_market_history graphene_witness graphene_chain fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} ) diff --git a/programs/witness_node/main.cpp b/programs/witness_node/main.cpp index 541bb00c..bb7604ef 100644 --- a/programs/witness_node/main.cpp +++ b/programs/witness_node/main.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -49,6 +50,7 @@ int main(int argc, char** argv) { auto witness_plug = node.register_plugin(); auto history_plug = node.register_plugin(); + auto market_history_plug = node.register_plugin(); { bpo::options_description cli, cfg;