Skip to content

Commit

Permalink
add get_top_markets
Browse files Browse the repository at this point in the history
  • Loading branch information
oxarbitrage committed Mar 16, 2018
1 parent 91ae7c9 commit ef5fd4a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
35 changes: 35 additions & 0 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
market_ticker get_ticker( const string& base, const string& quote, bool skip_order_book = false )const;
market_volume get_24_volume( const string& base, const string& quote )const;
order_book get_order_book( const string& base, const string& quote, unsigned limit = 50 )const;
vector<market_volume> get_top_markets(uint limit)const;
vector<market_trade> get_trade_history( const string& base, const string& quote, fc::time_point_sec start, fc::time_point_sec stop, unsigned limit = 100 )const;
vector<market_trade> get_trade_history_by_sequence( const string& base, const string& quote, int64_t start, fc::time_point_sec stop, unsigned limit = 100 )const;

Expand Down Expand Up @@ -1292,6 +1293,40 @@ order_book database_api_impl::get_order_book( const string& base, const string&
return result;
}

vector<market_volume> database_api::get_top_markets(uint limit)const
{
return my->get_top_markets(limit);
}

vector<market_volume> database_api_impl::get_top_markets(uint limit)const
{
FC_ASSERT( limit <= 100 );

const auto& volume_idx = _db.get_index_type<graphene::market_history::market_ticker_index>().indices().get<by_volume>();
auto itr = volume_idx.rbegin();
vector<market_volume> result;

fc::uint128 base_volume;
fc::uint128 quote_volume;

uint c = 0;
const fc::time_point_sec now = fc::time_point::now();

while( itr != volume_idx.rend() && result.size() < limit)
{
market_volume mv;
mv.time = now;
const auto assets = get_assets( { itr->base, itr->quote } );
mv.base = assets[0]->symbol;
mv.quote = assets[1]->symbol;
mv.base_volume = uint128_amount_to_string( itr->base_volume, assets[0]->precision );
mv.quote_volume = uint128_amount_to_string( itr->quote_volume, assets[1]->precision );
result.push_back(mv);
++itr;
}
return result;
}

vector<market_trade> database_api::get_trade_history( const string& base,
const string& quote,
fc::time_point_sec start,
Expand Down
8 changes: 8 additions & 0 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,13 @@ class database_api
*/
order_book get_order_book( const string& base, const string& quote, unsigned limit = 50 )const;

/**
* @brief Returns vector of 24 hour volume markets sorted by reverse base_volume
* @param limit Max number of results
* @return Desc Sorted volume vector
*/
vector<market_volume> get_top_markets(uint limit)const;

/**
* @brief Returns recent trades for the market assetA:assetB, ordered by time, most recent first. The range is [stop, start)
* Note: Currently, timezone offsets are not supported. The time must be UTC.
Expand Down Expand Up @@ -715,6 +722,7 @@ FC_API(graphene::app::database_api,
(unsubscribe_from_market)
(get_ticker)
(get_24_volume)
(get_top_markets)
(get_trade_history)
(get_trade_history_by_sequence)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,12 @@ typedef multi_index_container<
> order_history_multi_index_type;

struct by_market;
struct by_volume;
typedef multi_index_container<
market_ticker_object,
indexed_by<
ordered_unique< tag<by_id>, member< object, object_id_type, &object::id > >,
ordered_non_unique< tag<by_volume>, member< market_ticker_object, fc::uint128, &market_ticker_object::base_volume > >,
ordered_unique<
tag<by_market>,
composite_key<
Expand Down

0 comments on commit ef5fd4a

Please sign in to comment.