Skip to content

Commit

Permalink
get_ticker API now use precalculated data. bitshares#509
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed Nov 30, 2017
1 parent ed11ede commit 43a8d04
Showing 1 changed file with 25 additions and 40 deletions.
65 changes: 25 additions & 40 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
vector<collateral_bid_object> get_collateral_bids(const asset_id_type asset, uint32_t limit, uint32_t start)const;
void subscribe_to_market(std::function<void(const variant&)> callback, asset_id_type a, asset_id_type b);
void unsubscribe_from_market(asset_id_type a, asset_id_type b);
market_ticker get_ticker( const string& base, const string& quote )const;
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_trade> get_trade_history( const string& base, const string& quote, fc::time_point_sec start, fc::time_point_sec stop, unsigned limit = 100 )const;
Expand Down Expand Up @@ -1154,7 +1154,7 @@ market_ticker database_api::get_ticker( const string& base, const string& quote
return my->get_ticker( base, quote );
}

market_ticker database_api_impl::get_ticker( const string& base, const string& quote )const
market_ticker database_api_impl::get_ticker( const string& base, const string& quote, bool skip_order_book )const
{
const auto assets = lookup_asset_symbols( {base, quote} );
FC_ASSERT( assets[0], "Invalid base asset symbol: ${s}", ("s",base) );
Expand All @@ -1178,11 +1178,6 @@ market_ticker database_api_impl::get_ticker( const string& base, const string& q
auto quote_id = assets[1]->id;
if( base_id > quote_id ) std::swap( base_id, quote_id );

history_key hkey;
hkey.base = base_id;
hkey.quote = quote_id;
hkey.sequence = std::numeric_limits<int64_t>::min();

// TODO: move following duplicate code out
// TODO: using pow is a bit inefficient here, optimization is possible
auto asset_to_real = [&]( const asset& a, int p ) { return double(a.amount.value)/pow( 10, p ); };
Expand All @@ -1194,44 +1189,31 @@ market_ticker database_api_impl::get_ticker( const string& base, const string& q
return asset_to_real( p.quote, assets[0]->precision ) / asset_to_real( p.base, assets[1]->precision );
};

const auto& history_idx = _db.get_index_type<graphene::market_history::history_index>().indices().get<by_key>();
auto itr = history_idx.lower_bound( hkey );

bool is_latest = true;
price latest_price;
fc::uint128 base_volume;
fc::uint128 quote_volume;
while( itr != history_idx.end() && itr->key.base == base_id && itr->key.quote == quote_id )

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() )
{
if( is_latest )
price latest_price = asset( itr->latest_base, itr->base ) / asset( itr->latest_quote, itr->quote );
result.latest = price_to_real( latest_price );
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
{
is_latest = false;
latest_price = itr->op.fill_price;
result.latest = price_to_real( latest_price );
price last_day_price = asset( itr->last_day_base, itr->base ) / asset( itr->last_day_quote, itr->quote );
result.percent_change = ( result.latest / price_to_real( last_day_price ) - 1 ) * 100;
}

if( itr->time < yesterday )
if( assets[0]->id == itr->base )
{
if( itr->op.fill_price != latest_price )
result.percent_change = ( result.latest / price_to_real( itr->op.fill_price ) - 1 ) * 100;
break;
base_volume = itr->base_volume;
quote_volume = itr->quote_volume;
}

if( itr->op.is_maker )
else
{
if( assets[0]->id == itr->op.receives.asset_id )
{
base_volume += itr->op.receives.amount.value;
quote_volume += itr->op.pays.amount.value;
}
else
{
base_volume += itr->op.pays.amount.value;
quote_volume += itr->op.receives.amount.value;
}
base_volume = itr->quote_volume;
quote_volume = itr->base_volume;
}

++itr;
}

auto uint128_to_double = []( const fc::uint128& n )
Expand All @@ -1242,9 +1224,12 @@ market_ticker database_api_impl::get_ticker( const string& base, const string& q
result.base_volume = uint128_to_double( base_volume ) / pow( 10, assets[0]->precision );
result.quote_volume = uint128_to_double( quote_volume ) / pow( 10, assets[1]->precision );

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;
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;
}
Expand All @@ -1256,7 +1241,7 @@ 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 );
const auto& ticker = get_ticker( base, quote, true );

market_volume result;
result.time = ticker.time;
Expand Down

0 comments on commit 43a8d04

Please sign in to comment.