diff --git a/libraries/app/database_api.cpp b/libraries/app/database_api.cpp index 8ebaeaa309..ff03ce2e2a 100644 --- a/libraries/app/database_api.cpp +++ b/libraries/app/database_api.cpp @@ -1554,6 +1554,10 @@ vector 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; @@ -1567,6 +1571,10 @@ vector 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; @@ -1664,6 +1672,10 @@ vector 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; @@ -1677,6 +1689,10 @@ vector 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; diff --git a/libraries/app/include/graphene/app/api_objects.hpp b/libraries/app/include/graphene/app/api_objects.hpp index ff7f027f09..3582baaa1d 100644 --- a/libraries/app/include/graphene/app/api_objects.hpp +++ b/libraries/app/include/graphene/app/api_objects.hpp @@ -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; }; @@ -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) ); diff --git a/tests/tests/database_api_tests.cpp b/tests/tests/database_api_tests.cpp index 36c160c271..41f17216f0 100644 --- a/tests/tests/database_api_tests.cpp +++ b/tests/tests/database_api_tests.cpp @@ -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()