adding API call for market fill history

This commit is contained in:
Daniel Larimer 2015-10-23 18:13:33 -04:00
parent 6e554260e5
commit 7378fb80bc
5 changed files with 95 additions and 0 deletions

View file

@ -310,6 +310,29 @@ namespace graphene { namespace app {
return result;
} // end get_relevant_accounts( obj )
vector<fill_order_operation> history_api::get_fill_order_history( asset_id_type a, asset_id_type b )const
{
FC_ASSERT(_app.chain_database());
const auto& db = *_app.chain_database();
if( a > b ) std::swap(a,b);
const auto& history_idx = db.get_index_type<graphene::market_history::history_index>().indices().get<by_key>();
history_key hkey;
hkey.base = a;
hkey.quote = b;
hkey.sequence = 0;
auto itr = history_idx.lower_bound( hkey );
vector<fill_order_operation> result;
while( itr != history_idx.end() )
{
if( itr->key.base != a || itr->key.quote != b ) break;
result.push_back( itr->op );
++itr;
}
return result;
}
vector<operation_history_object> history_api::get_account_history(account_id_type account, operation_history_id_type stop, unsigned limit, operation_history_id_type start) const
{
FC_ASSERT(_app.chain_database());

View file

@ -70,6 +70,7 @@ namespace graphene { namespace app {
unsigned limit = 100,
operation_history_id_type start = operation_history_id_type())const;
vector<fill_order_operation> get_fill_order_history( asset_id_type a, asset_id_type b )const;
vector<bucket_object> 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;
flat_set<uint32_t> get_market_history_buckets()const;
@ -203,6 +204,7 @@ FC_REFLECT( graphene::app::network_broadcast_api::transaction_confirmation,
FC_API(graphene::app::history_api,
(get_account_history)
(get_fill_order_history)
(get_market_history)
(get_market_history_buckets)
)

View file

@ -21,6 +21,9 @@
#include <graphene/chain/market_evaluator.hpp>
#include <graphene/chain/account_object.hpp>
#include <graphene/chain/exceptions.hpp>
#include <graphene/chain/hardfork.hpp>
#include <fc/smart_ref_impl.hpp>
#include <graphene/chain/protocol/fee_schedule.hpp>
#include <fc/uint128.hpp>
namespace graphene { namespace chain {

View file

@ -84,6 +84,24 @@ struct bucket_object : public abstract_object<bucket_object>
share_type quote_volume;
};
struct history_key {
asset_id_type base;
asset_id_type quote;
uint64_t sequence = 0;
friend bool operator < ( const history_key& a, const history_key& b ) {
return std::tie( a.base, a.quote, a.sequence ) < std::tie( b.base, b.quote, b.sequence );
}
friend bool operator == ( const history_key& a, const history_key& b ) {
return std::tie( a.base, a.quote, a.sequence ) == std::tie( b.base, b.quote, b.sequence );
}
};
struct order_history_object : public abstract_object<order_history_object>
{
history_key key;
fill_order_operation op;
};
struct by_key;
typedef multi_index_container<
bucket_object,
@ -93,7 +111,17 @@ typedef multi_index_container<
>
> bucket_object_multi_index_type;
typedef multi_index_container<
order_history_object,
indexed_by<
hashed_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
ordered_unique< tag<by_key>, member< order_history_object, history_key, &order_history_object::key > >
>
> order_history_multi_index_type;
typedef generic_index<bucket_object, bucket_object_multi_index_type> bucket_index;
typedef generic_index<order_history_object, order_history_multi_index_type> history_index;
namespace detail
@ -130,6 +158,8 @@ class market_history_plugin : public graphene::app::plugin
} } //graphene::market_history
FC_REFLECT( graphene::market_history::history_key, (base)(quote)(sequence) )
FC_REFLECT_DERIVED( graphene::market_history::order_history_object, (graphene::db::object), (key)(op) )
FC_REFLECT( graphene::market_history::bucket_key, (base)(quote)(seconds)(open) )
FC_REFLECT_DERIVED( graphene::market_history::bucket_object, (graphene::db::object),
(key)

View file

@ -81,6 +81,41 @@ struct operation_process_fill_order
const auto& buckets = _plugin.tracked_buckets();
auto& db = _plugin.database();
const auto& bucket_idx = db.get_index_type<bucket_index>();
const auto& history_idx = db.get_index_type<history_index>().indices().get<by_key>();
history_key hkey;
hkey.base = o.pays.asset_id;
hkey.quote = o.receives.asset_id;
if( hkey.base > hkey.quote )
std::swap( hkey.base, hkey.quote );
hkey.sequence = uint64_t(-1);
auto itr = history_idx.upper_bound( hkey );
if( itr != history_idx.begin() )
--itr;
if( itr->key.base == hkey.base && itr->key.quote == hkey.quote )
hkey.sequence = itr->key.sequence + 1;
else
hkey.sequence = 0;
db.create<order_history_object>( [&]( order_history_object& ho ) {
ho.key = hkey;
ho.op = o;
});
if( hkey.sequence > 200 )
{
auto end = hkey.sequence - 200;
itr = history_idx.lower_bound( hkey );
while( itr != history_idx.end() && itr->key.sequence < end )
{
auto delete_me = itr;
++itr;
db.remove( *delete_me );
}
}
auto max_history = _plugin.max_history();
for( auto bucket : buckets )
@ -91,6 +126,7 @@ struct operation_process_fill_order
key.base = o.pays.asset_id;
key.quote = o.receives.asset_id;
/** for every matched order there are two fill order operations created, one for
* each side. We can filter the duplicates by only considering the fill operations where
* the base > quote
@ -222,6 +258,7 @@ void market_history_plugin::plugin_initialize(const boost::program_options::vari
{ try {
database().applied_block.connect( [&]( const signed_block& b){ my->update_market_histories(b); } );
database().add_index< primary_index< bucket_index > >();
database().add_index< primary_index< history_index > >();
if( options.count( "bucket-size" ) )
{