Skip to content

Commit

Permalink
Move code around
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed Aug 9, 2019
1 parent a04f49c commit c7d657a
Showing 1 changed file with 88 additions and 88 deletions.
176 changes: 88 additions & 88 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,94 +445,6 @@ vector<optional<account_object>> database_api_impl::get_accounts( const vector<s
return result;
}

vector<limit_order_object> database_api::get_account_limit_orders(
const string& account_name_or_id, const string &base, const string &quote,
uint32_t limit, optional<limit_order_id_type> ostart_id, optional<price> ostart_price )
{
return my->get_account_limit_orders( account_name_or_id, base, quote, limit, ostart_id, ostart_price );
}

vector<limit_order_object> database_api_impl::get_account_limit_orders(
const string& account_name_or_id, const string &base, const string &quote,
uint32_t limit, optional<limit_order_id_type> ostart_id, optional<price> ostart_price )
{
FC_ASSERT( limit <= 101 );

vector<limit_order_object> results;
uint32_t count = 0;

const account_object* account = get_account_from_string(account_name_or_id);
if (account == nullptr)
return results;

auto assets = lookup_asset_symbols( {base, quote} );
FC_ASSERT( assets[0], "Invalid base asset symbol: ${s}", ("s",base) );
FC_ASSERT( assets[1], "Invalid quote asset symbol: ${s}", ("s",quote) );

auto base_id = assets[0]->id;
auto quote_id = assets[1]->id;

if (ostart_price.valid()) {
FC_ASSERT(ostart_price->base.asset_id == base_id, "Base asset inconsistent with start price");
FC_ASSERT(ostart_price->quote.asset_id == quote_id, "Quote asset inconsistent with start price");
}

const auto& index_by_account = _db.get_index_type<limit_order_index>().indices().get<by_account>();
limit_order_multi_index_type::index<by_account>::type::const_iterator lower_itr;
limit_order_multi_index_type::index<by_account>::type::const_iterator upper_itr;

// if both order_id and price are invalid, query the first page
if ( !ostart_id.valid() && !ostart_price.valid() )
{
lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, price::max(base_id, quote_id)));
}
else if ( ostart_id.valid() )
{
// in case of the order been deleted during page querying
const limit_order_object *p_loo = _db.find(*ostart_id);

if ( !p_loo )
{
if ( ostart_price.valid() )
{
lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, *ostart_price, *ostart_id));
}
else
{
// start order id been deleted, yet not provided price either
FC_THROW("Order id invalid (maybe just been canceled?), and start price not provided");
}
}
else
{
const limit_order_object &loo = *p_loo;

// in case of the order not belongs to specified account or market
FC_ASSERT(loo.sell_price.base.asset_id == base_id, "Order base asset inconsistent");
FC_ASSERT(loo.sell_price.quote.asset_id == quote_id, "Order quote asset inconsistent with order");
FC_ASSERT(loo.seller == account->get_id(), "Order not owned by specified account");

lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, loo.sell_price, *ostart_id));
}
}
else
{
// if reach here start_price must be valid
lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, *ostart_price));
}

upper_itr = index_by_account.upper_bound(std::make_tuple(account->id, price::min(base_id, quote_id)));

// Add the account's orders
for ( ; lower_itr != upper_itr && count < limit; ++lower_itr, ++count)
{
const limit_order_object &order = *lower_itr;
results.emplace_back(order);
}

return results;
}

std::map<string,full_account> database_api::get_full_accounts( const vector<string>& names_or_ids,
optional<bool> subscribe )
{
Expand Down Expand Up @@ -1057,6 +969,94 @@ vector<limit_order_object> database_api_impl::get_limit_orders( const std::strin
return get_limit_orders(asset_a_id, asset_b_id, limit);
}

vector<limit_order_object> database_api::get_account_limit_orders(
const string& account_name_or_id, const string &base, const string &quote,
uint32_t limit, optional<limit_order_id_type> ostart_id, optional<price> ostart_price )
{
return my->get_account_limit_orders( account_name_or_id, base, quote, limit, ostart_id, ostart_price );
}

vector<limit_order_object> database_api_impl::get_account_limit_orders(
const string& account_name_or_id, const string &base, const string &quote,
uint32_t limit, optional<limit_order_id_type> ostart_id, optional<price> ostart_price )
{
FC_ASSERT( limit <= 101 );

vector<limit_order_object> results;
uint32_t count = 0;

const account_object* account = get_account_from_string(account_name_or_id);
if (account == nullptr)
return results;

auto assets = lookup_asset_symbols( {base, quote} );
FC_ASSERT( assets[0], "Invalid base asset symbol: ${s}", ("s",base) );
FC_ASSERT( assets[1], "Invalid quote asset symbol: ${s}", ("s",quote) );

auto base_id = assets[0]->id;
auto quote_id = assets[1]->id;

if (ostart_price.valid()) {
FC_ASSERT(ostart_price->base.asset_id == base_id, "Base asset inconsistent with start price");
FC_ASSERT(ostart_price->quote.asset_id == quote_id, "Quote asset inconsistent with start price");
}

const auto& index_by_account = _db.get_index_type<limit_order_index>().indices().get<by_account>();
limit_order_multi_index_type::index<by_account>::type::const_iterator lower_itr;
limit_order_multi_index_type::index<by_account>::type::const_iterator upper_itr;

// if both order_id and price are invalid, query the first page
if ( !ostart_id.valid() && !ostart_price.valid() )
{
lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, price::max(base_id, quote_id)));
}
else if ( ostart_id.valid() )
{
// in case of the order been deleted during page querying
const limit_order_object *p_loo = _db.find(*ostart_id);

if ( !p_loo )
{
if ( ostart_price.valid() )
{
lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, *ostart_price, *ostart_id));
}
else
{
// start order id been deleted, yet not provided price either
FC_THROW("Order id invalid (maybe just been canceled?), and start price not provided");
}
}
else
{
const limit_order_object &loo = *p_loo;

// in case of the order not belongs to specified account or market
FC_ASSERT(loo.sell_price.base.asset_id == base_id, "Order base asset inconsistent");
FC_ASSERT(loo.sell_price.quote.asset_id == quote_id, "Order quote asset inconsistent with order");
FC_ASSERT(loo.seller == account->get_id(), "Order not owned by specified account");

lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, loo.sell_price, *ostart_id));
}
}
else
{
// if reach here start_price must be valid
lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, *ostart_price));
}

upper_itr = index_by_account.upper_bound(std::make_tuple(account->id, price::min(base_id, quote_id)));

// Add the account's orders
for ( ; lower_itr != upper_itr && count < limit; ++lower_itr, ++count)
{
const limit_order_object &order = *lower_itr;
results.emplace_back(order);
}

return results;
}

vector<call_order_object> database_api::get_call_orders(const std::string& a, uint32_t limit)const
{
return my->get_call_orders( a, limit );
Expand Down

0 comments on commit c7d657a

Please sign in to comment.