Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expand get_top_markets #924

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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(uint32_t limit)const;
vector<market_ticker> get_top_markets(uint32_t 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 @@ -1308,34 +1308,30 @@ order_book database_api_impl::get_order_book( const string& base, const string&
return result;
}

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

vector<market_volume> database_api_impl::get_top_markets(uint32_t limit)const
vector<market_ticker> database_api_impl::get_top_markets(uint32_t limit)const
{
FC_ASSERT( _app_options && _app_options->has_market_history_plugin, "Market history plugin is not enabled." );

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;
vector<market_ticker> result;
result.reserve(limit);

const fc::time_point_sec now = fc::time_point::now();
const fc::time_point_sec now = _db.head_block_time();

while( itr != volume_idx.rend() && result.size() < limit)
{
market_volume mv;
mv.time = now;
const auto assets = get_assets( { itr->base, itr->quote } );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest that we use a small local cache in this get_top_markets function to reduce quantity of queries to the database.

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.emplace_back( std::move(mv) );
market_ticker mt = get_ticker( assets[0]->symbol, assets[1]->symbol, true );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling get_ticker directly is inefficient, since it will query the database again for same data. We already have itr here so just need to get the data. I recommend that we move the code in get_ticket to a new static function and call it in both get_ticker and here.


result.emplace_back( std::move(mt) );
++itr;
}
return result;
Expand Down
4 changes: 2 additions & 2 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,12 @@ 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
* @brief Returns vector of markets sorted by reverse base_volume
* Note: this API is experimental and subject to change in next releases
* @param limit Max number of results
* @return Desc Sorted volume vector
*/
vector<market_volume> get_top_markets(uint32_t limit)const;
vector<market_ticker> get_top_markets(uint32_t limit)const;

/**
* @brief Returns recent trades for the market assetA:assetB, ordered by time, most recent first. The range is [stop, start)
Expand Down