-
Notifications
You must be signed in to change notification settings - Fork 649
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
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a83cbd5
expand get_top_markets
oxarbitrage 2635cab
create initial get_ticker_data()
oxarbitrage c09bd0c
move get_ticker_data to constructor
oxarbitrage ee53019
move some code outside if
oxarbitrage 444c970
change order_book to optional
oxarbitrage 46ffa19
add more optionals, fix empty volume
oxarbitrage be0e0c5
reverse optional in production calls
oxarbitrage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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 // | ||
|
@@ -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; | ||
} | ||
|
||
market_volume database_api::get_24_volume( const string& base, const string& quote )const | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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 } ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest that we use a small local cache in this |
||
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
tooptional
, actually it was a typo, I meant to use a local variable which isoptional
type or is a pointer, to store*itr
(skip if it'send()
), then constructmt
with it.