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 all commits
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
127 changes: 68 additions & 59 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 @@ -282,6 +282,58 @@ database_api_impl::~database_api_impl()
elog("freeing database api ${x}", ("x",int64_t(this)) );
}

//////////////////////////////////////////////////////////////////////
// //
// Market ticker constructor //
// //
//////////////////////////////////////////////////////////////////////

market_ticker::market_ticker(const market_ticker_object& mto,
const fc::time_point_sec& now,
const asset_object& asset_base,
const asset_object& asset_quote,
const optional<order_book>& orders)
{
time = now;
base = asset_base.symbol;
quote = asset_quote.symbol;

fc::uint128 bv;
fc::uint128 qv;

price latest_price = asset( mto.latest_base, mto.base ) / asset( mto.latest_quote, mto.quote );
if( mto.base != asset_base.id )
latest_price = ~latest_price;
latest = database_api_impl::price_to_string( latest_price, asset_base, asset_quote );
if( mto.last_day_base != 0 && mto.last_day_quote != 0 // has trade data before 24 hours
&& ( mto.last_day_base != mto.latest_base || mto.last_day_quote != mto.latest_quote ) ) // price changed
{
price last_day_price = asset( mto.last_day_base, mto.base ) / asset( mto.last_day_quote, mto.quote );
if( mto.base != asset_base.id )
last_day_price = ~last_day_price;
percent_change = price_diff_percent_string( last_day_price, latest_price );
}

if( asset_base.id == mto.base )
{
bv = mto.base_volume;
qv = mto.quote_volume;
}
else
{
bv = mto.quote_volume;
qv = mto.base_volume;
}
base_volume = uint128_amount_to_string( bv, asset_base.precision );
quote_volume = uint128_amount_to_string( qv, asset_quote.precision );

if( orders.valid())
{
if( !orders->asks.empty() ) lowest_ask = orders->asks[0].price;
if( !orders->bids.empty() ) highest_bid = orders->bids[0].price;
}
}

//////////////////////////////////////////////////////////////////////
// //
// Objects //
Expand Down Expand Up @@ -1186,62 +1238,24 @@ market_ticker database_api_impl::get_ticker( const string& base, const string& q
FC_ASSERT( assets[1], "Invalid quote asset symbol: ${s}", ("s",quote) );

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

market_ticker result;
result.time = now;
result.base = base;
result.quote = quote;
result.latest = "0";
result.lowest_ask = "0";
result.highest_bid = "0";
result.percent_change = "0";
market_ticker mt;

auto base_id = assets[0]->id;
auto quote_id = assets[1]->id;
if( base_id > quote_id ) std::swap( base_id, quote_id );

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

const auto& ticker_idx = _db.get_index_type<graphene::market_history::market_ticker_index>().indices().get<by_market>();
auto itr = ticker_idx.find( std::make_tuple( base_id, quote_id ) );
if( itr != ticker_idx.end() )
{
price latest_price = asset( itr->latest_base, itr->base ) / asset( itr->latest_quote, itr->quote );
if( itr->base != assets[0]->id )
latest_price = ~latest_price;
result.latest = price_to_string( latest_price, *assets[0], *assets[1] );
if( itr->last_day_base != 0 && itr->last_day_quote != 0 // has trade data before 24 hours
&& ( itr->last_day_base != itr->latest_base || itr->last_day_quote != itr->latest_quote ) ) // price changed
{
price last_day_price = asset( itr->last_day_base, itr->base ) / asset( itr->last_day_quote, itr->quote );
if( itr->base != assets[0]->id )
last_day_price = ~last_day_price;
result.percent_change = price_diff_percent_string( last_day_price, latest_price );
}
if( assets[0]->id == itr->base )
{
base_volume = itr->base_volume;
quote_volume = itr->quote_volume;
}
else
{
base_volume = itr->quote_volume;
quote_volume = itr->base_volume;
optional<order_book> orders;
if (!skip_order_book) {
orders = get_order_book(assets[0]->symbol, assets[1]->symbol, 1);
}
mt = market_ticker(*itr, now, *assets[0], *assets[1], orders);
return mt;
}

result.base_volume = uint128_amount_to_string( base_volume, assets[0]->precision );
result.quote_volume = uint128_amount_to_string( quote_volume, assets[1]->precision );

if( !skip_order_book )
{
const auto orders = get_order_book( base, quote, 1 );
if( !orders.asks.empty() ) result.lowest_ask = orders.asks[0].price;
if( !orders.bids.empty() ) result.highest_bid = orders.bids[0].price;
}

return result;
return mt;
Copy link
Member

@abitmore abitmore May 28, 2018

Choose a reason for hiding this comment

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

Need to include order data if skip_order_book is false, even when no ticker data.

That's why I said to change mt to optional, actually it was a typo, I meant to use a local variable which is optional type or is a pointer, to store *itr (skip if it's end()), then construct mt with it.

}

market_volume database_api::get_24_volume( const string& base, const string& quote )const
Expand All @@ -1252,14 +1266,12 @@ market_volume database_api::get_24_volume( const string& base, const string& quo
market_volume database_api_impl::get_24_volume( const string& base, const string& quote )const
{
const auto& ticker = get_ticker( base, quote, true );

market_volume result;
result.time = ticker.time;
result.base = ticker.base;
result.quote = ticker.quote;
result.base_volume = ticker.base_volume;
result.quote_volume = ticker.quote_volume;

return result;
}

Expand Down Expand Up @@ -1308,34 +1320,31 @@ 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) );
optional<order_book> orders = get_order_book(assets[0]->symbol, assets[1]->symbol, 1);
market_ticker mt(*itr, now, *assets[0], *assets[1], orders);
result.emplace_back( std::move(mt) );
++itr;
}
return result;
Expand Down
11 changes: 9 additions & 2 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ struct market_ticker
string percent_change;
string base_volume;
string quote_volume;

market_ticker() {}
market_ticker(const market_ticker_object& mto,
const fc::time_point_sec& now,
const asset_object& asset_base,
const asset_object& asset_quote,
const optional<order_book>& orders);
};

struct market_volume
Expand Down Expand Up @@ -450,12 +457,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