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

Extend get_top_markets API #1911

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
Next Next commit
Store two ticker objects for each market
For get_top_markets API
abitmore committed Aug 15, 2019
commit 0d6def39bf36dae403139b2b465e260ffe82873d
39 changes: 36 additions & 3 deletions libraries/plugins/market_history/market_history_plugin.cpp
Original file line number Diff line number Diff line change
@@ -181,7 +181,7 @@ struct operation_process_fill_order
auto ticker_itr = ticker_idx.find( std::make_tuple( key.base, key.quote ) );
if( ticker_itr == ticker_idx.end() )
{
db.create<market_ticker_object>( [&]( market_ticker_object& mt ) {
db.create<market_ticker_object>( [&key,&fill_price,&trade_price]( market_ticker_object& mt ) {
mt.base = key.base;
mt.quote = key.quote;
mt.last_day_base = 0;
@@ -191,15 +191,37 @@ struct operation_process_fill_order
mt.base_volume = trade_price.base.amount.value;
mt.quote_volume = trade_price.quote.amount.value;
});
// save an object for "flipped" market for get_top_markets API
db.create<market_ticker_object>( [&key,&fill_price,&trade_price]( market_ticker_object& mt ) {
mt.base = key.quote;
mt.quote = key.base;
mt.last_day_base = 0;
mt.last_day_quote = 0;
mt.latest_base = fill_price.quote.amount;
mt.latest_quote = fill_price.base.amount;
mt.base_volume = trade_price.quote.amount.value;
mt.quote_volume = trade_price.base.amount.value;
});
}
else
{
db.modify( *ticker_itr, [&]( market_ticker_object& mt ) {
db.modify( *ticker_itr, [&fill_price,&trade_price]( market_ticker_object& mt ) {
mt.latest_base = fill_price.base.amount;
mt.latest_quote = fill_price.quote.amount;
mt.base_volume += trade_price.base.amount.value; // ignore overflow
mt.quote_volume += trade_price.quote.amount.value; // ignore overflow
});
// update data of flipped market
auto flipped_ticker_itr = ticker_idx.find( std::make_tuple( key.quote, key.base ) );
if( flipped_ticker_itr != ticker_idx.end() ) // should always be true
{
db.modify( *flipped_ticker_itr, [&fill_price,&trade_price]( market_ticker_object& mt ) {
mt.latest_base = fill_price.quote.amount;
mt.latest_quote = fill_price.base.amount;
mt.base_volume += trade_price.quote.amount.value; // ignore overflow
mt.quote_volume += trade_price.base.amount.value; // ignore overflow
});
}
}

// To update buckets data
@@ -347,13 +369,24 @@ void market_history_plugin_impl::update_market_histories( const signed_block& b
auto ticker_itr = ticker_idx.find( std::make_tuple( key.base, key.quote ) );
if( ticker_itr != ticker_idx.end() ) // should always be true
{
db.modify( *ticker_itr, [&]( market_ticker_object& mt ) {
db.modify( *ticker_itr, [&fill_price,&trade_price]( market_ticker_object& mt ) {
mt.last_day_base = fill_price.base.amount;
mt.last_day_quote = fill_price.quote.amount;
mt.base_volume -= trade_price.base.amount.value; // ignore underflow
mt.quote_volume -= trade_price.quote.amount.value; // ignore underflow
});
}
// update data of flipped market
ticker_itr = ticker_idx.find( std::make_tuple( key.quote, key.base ) );
if( ticker_itr != ticker_idx.end() ) // should always be true
{
db.modify( *ticker_itr, [&fill_price,&trade_price]( market_ticker_object& mt ) {
mt.last_day_base = fill_price.quote.amount;
mt.last_day_quote = fill_price.base.amount;
mt.base_volume -= trade_price.quote.amount.value; // ignore underflow
mt.quote_volume -= trade_price.base.amount.value; // ignore underflow
});
}
}
last_min_his_id = history_itr->id;
++history_itr;