Skip to content

Commit

Permalink
Merge pull request #2638 from bitshares/pr-2637-orderbook-api
Browse files Browse the repository at this point in the history
Extend get_order_book API to return more info
  • Loading branch information
abitmore authored Sep 12, 2022
2 parents 43ee8fa + 4ab7acf commit b5eeae8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
24 changes: 24 additions & 0 deletions libraries/app/api_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@

namespace graphene { namespace app {

order::order( const string& _price,
const string& _quote,
const string& _base,
const limit_order_id_type& _id,
const account_id_type& _oid,
const string& _oname,
const time_point_sec& _exp )
: price( _price ),
quote( _quote ),
base( _base ),
id( _id ),
owner_id( _oid ),
owner_name( _oname ),
expiration( _exp )
{
// Nothing to do
}

order_book::order_book( const string& _base, const string& _quote )
: base( _base ), quote( _quote )
{
// Do nothing else
}

market_ticker::market_ticker(const market_ticker_object& mto,
const fc::time_point_sec& now,
const asset_object& asset_base,
Expand Down
23 changes: 10 additions & 13 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1417,9 +1417,7 @@ order_book database_api_impl::get_order_book( const string& base, const string&
"limit can not be greater than ${configured_limit}",
("configured_limit", configured_limit) );

order_book result;
result.base = base;
result.quote = quote;
order_book result( base, quote );

auto assets = lookup_asset_symbols( {base, quote} );
FC_ASSERT( assets[0], "Invalid base asset symbol: ${s}", ("s",base) );
Expand All @@ -1431,25 +1429,24 @@ order_book database_api_impl::get_order_book( const string& base, const string&

for( const auto& o : orders )
{
auto order_price = price_to_string( o.sell_price, *assets[0], *assets[1] );
if( o.sell_price.base.asset_id == base_id )
{
order ord;
ord.price = price_to_string( o.sell_price, *assets[0], *assets[1] );
ord.quote = assets[1]->amount_to_string( share_type( fc::uint128_t( o.for_sale.value )
auto quote_amt = assets[1]->amount_to_string( share_type( fc::uint128_t( o.for_sale.value )
* o.sell_price.quote.amount.value
/ o.sell_price.base.amount.value ) );
ord.base = assets[0]->amount_to_string( o.for_sale );
result.bids.push_back( ord );
auto base_amt = assets[0]->amount_to_string( o.for_sale );
result.bids.emplace_back( order_price, quote_amt, base_amt, o.id,
o.seller, o.seller(_db).name, o.expiration );
}
else
{
order ord;
ord.price = price_to_string( o.sell_price, *assets[0], *assets[1] );
ord.quote = assets[1]->amount_to_string( o.for_sale );
ord.base = assets[0]->amount_to_string( share_type( fc::uint128_t( o.for_sale.value )
auto quote_amt = assets[1]->amount_to_string( o.for_sale );
auto base_amt = assets[0]->amount_to_string( share_type( fc::uint128_t( o.for_sale.value )
* o.sell_price.quote.amount.value
/ o.sell_price.base.amount.value ) );
result.asks.push_back( ord );
result.asks.emplace_back( order_price, quote_amt, base_amt, o.id,
o.seller, o.seller(_db).name, o.expiration );
}
}

Expand Down
17 changes: 16 additions & 1 deletion libraries/app/include/graphene/app/api_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ namespace graphene { namespace app {
string price;
string quote;
string base;
limit_order_id_type id;
account_id_type owner_id;
string owner_name;
time_point_sec expiration;

order() = default;
order( const string& _price,
const string& _quote,
const string& _base,
const limit_order_id_type& _id,
const account_id_type& _oid,
const string& _oname,
const time_point_sec& _exp );
};

struct order_book
Expand All @@ -92,6 +105,8 @@ namespace graphene { namespace app {
string quote;
vector< order > bids;
vector< order > asks;
order_book() = default;
order_book( const string& _base, const string& _quote );
};

struct market_ticker
Expand Down Expand Up @@ -191,7 +206,7 @@ FC_REFLECT( graphene::app::full_account,
(more_data_available)
)

FC_REFLECT( graphene::app::order, (price)(quote)(base) )
FC_REFLECT( graphene::app::order, (price)(quote)(base)(id)(owner_id)(owner_name)(expiration) )
FC_REFLECT( graphene::app::order_book, (base)(quote)(bids)(asks) )
FC_REFLECT( graphene::app::market_ticker,
(time)(base)(quote)(latest)(lowest_ask)(lowest_ask_base_size)(lowest_ask_quote_size)
Expand Down
21 changes: 13 additions & 8 deletions tests/tests/api_limit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,29 +215,34 @@ BOOST_AUTO_TEST_CASE( api_limit_get_settle_orders ){
}
}
BOOST_AUTO_TEST_CASE( api_limit_get_order_book ){
try{
try {
graphene::app::database_api db_api( db, &( app.get_options() ));
auto nathan_private_key = generate_private_key("nathan");
auto dan_private_key = generate_private_key("dan");
account_id_type nathan_id = create_account("nathan", nathan_private_key.get_public_key()).id;
account_id_type dan_id = create_account("dan", dan_private_key.get_public_key()).id;
transfer(account_id_type(), nathan_id, asset(100));
transfer(account_id_type(), dan_id, asset(100));
asset_id_type bitusd_id = create_bitasset(
"USDBIT", nathan_id, 100, disable_force_settle).id;
asset_id_type bitdan_id = create_bitasset(
"DANBIT", dan_id, 100, disable_force_settle).id;
asset_id_type bitusd_id = create_user_issued_asset( "USDBIT", nathan_id(db), charge_market_fee).id;
asset_id_type bitdan_id = create_user_issued_asset( "DANBIT", dan_id(db), charge_market_fee).id;
issue_uia( nathan_id, asset(100, bitusd_id) );
issue_uia( dan_id, asset(100, bitdan_id) );
create_sell_order( nathan_id, asset(100, bitusd_id), asset(10000, bitdan_id) );
create_sell_order( dan_id, asset(100, bitdan_id), asset(10000, bitusd_id) );
generate_block();
fc::usleep(fc::milliseconds(100));
GRAPHENE_CHECK_THROW(db_api.get_order_book(std::string(static_cast<object_id_type>(bitusd_id)),
std::string(static_cast<object_id_type>(bitdan_id)),89), fc::exception);
graphene::app::order_book result =db_api.get_order_book(std::string(
static_cast<object_id_type>(bitusd_id)), std::string(static_cast<object_id_type>(bitdan_id)),78);
BOOST_REQUIRE_EQUAL( result.bids.size(), 0u);
}catch (fc::exception& e) {
BOOST_REQUIRE_EQUAL( result.bids.size(), 1u );
BOOST_CHECK( result.bids.front().owner_name == "nathan" );
BOOST_REQUIRE_EQUAL( result.asks.size(), 1u );
BOOST_CHECK( result.asks.front().owner_name == "dan" );
} catch (fc::exception& e) {
edump((e.to_detail_string()));
throw;
}
}
}

BOOST_AUTO_TEST_CASE( api_limit_lookup_accounts ) {
Expand Down

0 comments on commit b5eeae8

Please sign in to comment.