Skip to content

Commit

Permalink
refactor get_limit_orders to accept asset symbol or id
Browse files Browse the repository at this point in the history
  • Loading branch information
oxarbitrage committed Aug 23, 2018
1 parent 90bd426 commit ee0a8ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
65 changes: 39 additions & 26 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
uint64_t get_asset_count()const;

// Markets / feeds
vector<limit_order_object> get_limit_orders(asset_id_type a, asset_id_type b, uint32_t limit)const;
vector<limit_order_object> get_limit_orders(std::string a, std::string b, uint32_t limit)const;
vector<limit_order_object> get_account_limit_orders( const string& account_name_or_id,
const string &base,
const string &quote, uint32_t limit,
Expand Down Expand Up @@ -258,6 +258,35 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
});
return result;
}
vector<limit_order_object> get_limit_orders(const asset_id_type& a, const asset_id_type& b, const uint32_t& limit)const
{
const auto& limit_order_idx = _db.get_index_type<limit_order_index>();
const auto& limit_price_idx = limit_order_idx.indices().get<by_price>();

vector<limit_order_object> result;
result.reserve(limit*2);

uint32_t count = 0;
auto limit_itr = limit_price_idx.lower_bound(price::max(a,b));
auto limit_end = limit_price_idx.upper_bound(price::min(a,b));
while(limit_itr != limit_end && count < limit)
{
result.push_back(*limit_itr);
++limit_itr;
++count;
}
count = 0;
limit_itr = limit_price_idx.lower_bound(price::max(b,a));
limit_end = limit_price_idx.upper_bound(price::min(b,a));
while(limit_itr != limit_end && count < limit)
{
result.push_back(*limit_itr);
++limit_itr;
++count;
}

return result;
}

template<typename T>
const std::pair<asset_id_type,asset_id_type> get_order_market( const T& order )
Expand Down Expand Up @@ -1165,41 +1194,25 @@ vector<optional<asset_object>> database_api_impl::lookup_asset_symbols(const vec
// //
//////////////////////////////////////////////////////////////////////

vector<limit_order_object> database_api::get_limit_orders(asset_id_type a, asset_id_type b, uint32_t limit)const
vector<limit_order_object> database_api::get_limit_orders(std::string a, std::string b, uint32_t limit)const
{
return my->get_limit_orders( a, b, limit );
}

/**
* @return the limit orders for both sides of the book for the two assets specified up to limit number on each side.
*/
vector<limit_order_object> database_api_impl::get_limit_orders(asset_id_type a, asset_id_type b, uint32_t limit)const
vector<limit_order_object> database_api_impl::get_limit_orders(std::string a, std::string b, uint32_t limit)const
{
const auto& limit_order_idx = _db.get_index_type<limit_order_index>();
const auto& limit_price_idx = limit_order_idx.indices().get<by_price>();
FC_ASSERT( limit <= 100 );
vector<limit_order_object> results;
results.reserve(limit*2);

vector<limit_order_object> result;
const asset_id_type asset_a_id = get_asset_from_string(a)->id;
const asset_id_type asset_b_id = get_asset_from_string(b)->id;

uint32_t count = 0;
auto limit_itr = limit_price_idx.lower_bound(price::max(a,b));
auto limit_end = limit_price_idx.upper_bound(price::min(a,b));
while(limit_itr != limit_end && count < limit)
{
result.push_back(*limit_itr);
++limit_itr;
++count;
}
count = 0;
limit_itr = limit_price_idx.lower_bound(price::max(b,a));
limit_end = limit_price_idx.upper_bound(price::min(b,a));
while(limit_itr != limit_end && count < limit)
{
result.push_back(*limit_itr);
++limit_itr;
++count;
}

return result;
results = get_limit_orders(asset_a_id, asset_b_id, limit);
return results;
}

vector<call_order_object> database_api::get_call_orders(asset_id_type a, uint32_t limit)const
Expand Down
6 changes: 3 additions & 3 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,12 @@ class database_api

/**
* @brief Get limit orders in a given market
* @param a ID of asset being sold
* @param b ID of asset being purchased
* @param a Symbol or ID of asset being sold
* @param b Symbol or ID of asset being purchased
* @param limit Maximum number of orders to retrieve
* @return The limit orders, ordered from least price to greatest
*/
vector<limit_order_object> get_limit_orders(asset_id_type a, asset_id_type b, uint32_t limit)const;
vector<limit_order_object> get_limit_orders(std::string a, std::string b, uint32_t limit)const;

/**
* @brief Get call orders in a given asset
Expand Down
4 changes: 2 additions & 2 deletions libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3098,9 +3098,9 @@ vector<limit_order_object> wallet_api::get_account_limit_orders( const string& n
return my->_remote_db->get_account_limit_orders(name_or_id, base, quote, limit, ostart_id, ostart_price);
}

vector<limit_order_object> wallet_api::get_limit_orders(string a, string b, uint32_t limit)const
vector<limit_order_object> wallet_api::get_limit_orders(std::string a, std::string b, uint32_t limit)const
{
return my->_remote_db->get_limit_orders(get_asset(a).id, get_asset(b).id, limit);
return my->_remote_db->get_limit_orders(a, b, limit);
}

vector<call_order_object> wallet_api::get_call_orders(string a, uint32_t limit)const
Expand Down

0 comments on commit ee0a8ce

Please sign in to comment.