Skip to content

Commit

Permalink
Merge pull request #2181 from bitshares/pr-2163-to-hardfork
Browse files Browse the repository at this point in the history
Add a `type` field to trade history APIs
  • Loading branch information
abitmore authored May 28, 2020
2 parents 6e532a8 + 81625ec commit 86c2ba8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
16 changes: 16 additions & 0 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,10 @@ vector<market_trade> database_api_impl::get_trade_history( const string& base,
{
trade.sequence = -itr->key.sequence;
trade.side1_account_id = itr->op.account_id;
if(itr->op.receives.asset_id == assets[0]->id)
trade.type = "sell";
else
trade.type = "buy";
}
else
trade.side2_account_id = itr->op.account_id;
Expand All @@ -1567,6 +1571,10 @@ vector<market_trade> database_api_impl::get_trade_history( const string& base,
{
trade.sequence = -next_itr->key.sequence;
trade.side1_account_id = next_itr->op.account_id;
if(next_itr->op.receives.asset_id == assets[0]->id)
trade.type = "sell";
else
trade.type = "buy";
}
else
trade.side2_account_id = next_itr->op.account_id;
Expand Down Expand Up @@ -1664,6 +1672,10 @@ vector<market_trade> database_api_impl::get_trade_history_by_sequence(
{
trade.sequence = -itr->key.sequence;
trade.side1_account_id = itr->op.account_id;
if(itr->op.receives.asset_id == assets[0]->id)
trade.type = "sell";
else
trade.type = "buy";
}
else
trade.side2_account_id = itr->op.account_id;
Expand All @@ -1677,6 +1689,10 @@ vector<market_trade> database_api_impl::get_trade_history_by_sequence(
{
trade.sequence = -next_itr->key.sequence;
trade.side1_account_id = next_itr->op.account_id;
if(next_itr->op.receives.asset_id == assets[0]->id)
trade.type = "sell";
else
trade.type = "buy";
}
else
trade.side2_account_id = next_itr->op.account_id;
Expand Down
4 changes: 3 additions & 1 deletion libraries/app/include/graphene/app/api_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ namespace graphene { namespace app {
string price;
string amount;
string value;
string type;
account_id_type side1_account_id = GRAPHENE_NULL_ACCOUNT;
account_id_type side2_account_id = GRAPHENE_NULL_ACCOUNT;
};
Expand Down Expand Up @@ -185,7 +186,8 @@ FC_REFLECT( graphene::app::market_ticker,
(time)(base)(quote)(latest)(lowest_ask)(lowest_ask_base_size)(lowest_ask_quote_size)
(highest_bid)(highest_bid_base_size)(highest_bid_quote_size)(percent_change)(base_volume)(quote_volume) );
FC_REFLECT( graphene::app::market_volume, (time)(base)(quote)(base_volume)(quote_volume) );
FC_REFLECT( graphene::app::market_trade, (sequence)(date)(price)(amount)(value)(side1_account_id)(side2_account_id) );
FC_REFLECT( graphene::app::market_trade, (sequence)(date)(price)(amount)(value)(type)
(side1_account_id)(side2_account_id));

FC_REFLECT_DERIVED( graphene::app::extended_asset_object, (graphene::chain::asset_object),
(total_in_collateral)(total_backing_collateral) );
69 changes: 69 additions & 0 deletions tests/tests/database_api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1842,4 +1842,73 @@ BOOST_AUTO_TEST_CASE( asset_in_collateral )

} FC_LOG_AND_RETHROW() }


BOOST_AUTO_TEST_CASE( get_trade_history )
{ try {

app.enable_plugin("market_history");
graphene::app::application_options opt=app.get_options();
opt.has_market_history_plugin = true;
graphene::app::database_api db_api( db, &opt);

ACTORS((bob)(alice));

const auto& eur = create_user_issued_asset("EUR");
asset_id_type eur_id = eur.id;
const auto& usd = create_user_issued_asset("USD");
asset_id_type usd_id = usd.id;

issue_uia( bob_id, usd.amount(1000000) );
issue_uia( alice_id, eur.amount(1000000) );

// maker create an order
create_sell_order(bob, usd.amount(200), eur.amount(210));

// taker match it
create_sell_order(alice, eur.amount(210), usd.amount(200));

generate_block();

// taker is selling
auto history = db_api.get_trade_history( "EUR", "USD", db.head_block_time(), db.head_block_time() - fc::days(1) );
BOOST_REQUIRE_EQUAL( 1, history.size() );
BOOST_CHECK_EQUAL( "1.05", history[0].price );
BOOST_CHECK_EQUAL( "2", history[0].amount );
BOOST_CHECK_EQUAL( "2.10", history[0].value );
BOOST_CHECK_EQUAL( "sell", history[0].type );
BOOST_CHECK_EQUAL( bob_id.instance.value, history[0].side1_account_id.instance.value );
BOOST_CHECK_EQUAL( alice_id.instance.value, history[0].side2_account_id.instance.value );

// opposite side, taker is buying
history = db_api.get_trade_history( "USD", "EUR", db.head_block_time(), db.head_block_time() - fc::days(1) );
BOOST_REQUIRE_EQUAL( 1, history.size() );
BOOST_CHECK_EQUAL( "0.9523809523809523809", history[0].price );
BOOST_CHECK_EQUAL( "2.10", history[0].amount );
BOOST_CHECK_EQUAL( "2", history[0].value );
BOOST_CHECK_EQUAL( "buy", history[0].type );
BOOST_CHECK_EQUAL( bob_id.instance.value, history[0].side1_account_id.instance.value );
BOOST_CHECK_EQUAL( alice_id.instance.value, history[0].side2_account_id.instance.value );

// by sequence
history = db_api.get_trade_history_by_sequence( "EUR", "USD", 2, db.head_block_time() - fc::days(1) );
BOOST_CHECK_EQUAL( "1.05", history[0].price );
BOOST_CHECK_EQUAL( "2", history[0].amount );
BOOST_CHECK_EQUAL( "2.10", history[0].value );
BOOST_CHECK_EQUAL( "sell", history[0].type );
BOOST_CHECK_EQUAL( bob_id.instance.value, history[0].side1_account_id.instance.value );
BOOST_CHECK_EQUAL( alice_id.instance.value, history[0].side2_account_id.instance.value );

// opposite side
history = db_api.get_trade_history_by_sequence( "USD", "EUR", 2, db.head_block_time() - fc::days(1) );
BOOST_REQUIRE_EQUAL( 1, history.size() );
BOOST_CHECK_EQUAL( "0.9523809523809523809", history[0].price );
BOOST_CHECK_EQUAL( "2.10", history[0].amount );
BOOST_CHECK_EQUAL( "2", history[0].value );
BOOST_CHECK_EQUAL( "buy", history[0].type );
BOOST_CHECK_EQUAL( bob_id.instance.value, history[0].side1_account_id.instance.value );
BOOST_CHECK_EQUAL( alice_id.instance.value, history[0].side2_account_id.instance.value );

} FC_LOG_AND_RETHROW() }


BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 86c2ba8

Please sign in to comment.