Skip to content

Commit

Permalink
Simplify code for LP-related database APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed Jul 23, 2022
1 parent 3857d03 commit 604cb29
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 138 deletions.
125 changes: 17 additions & 108 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1720,68 +1720,24 @@ vector<extended_liquidity_pool_object> database_api::list_liquidity_pools(
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const
{
return my->list_liquidity_pools(
return my->get_liquidity_pools_by_asset_x<by_id>(
limit,
start_id,
with_statistics );
}

vector<extended_liquidity_pool_object> database_api_impl::list_liquidity_pools(
const optional<uint32_t>& olimit,
const optional<liquidity_pool_id_type>& ostart_id,
const optional<bool>& with_statistics )const
{
uint32_t limit = olimit.valid() ? *olimit : application_options::get_default().api_limit_get_liquidity_pools;

FC_ASSERT( _app_options, "Internal error" );
const auto configured_limit = _app_options->api_limit_get_liquidity_pools;
FC_ASSERT( limit <= configured_limit,
"limit can not be greater than ${configured_limit}",
("configured_limit", configured_limit) );

bool with_stats = ( with_statistics.valid() && *with_statistics );

vector<extended_liquidity_pool_object> results;

liquidity_pool_id_type start_id = ostart_id.valid() ? *ostart_id : liquidity_pool_id_type();

const auto& idx = _db.get_index_type<liquidity_pool_index>().indices().get<by_id>();
auto lower_itr = idx.lower_bound( start_id );
auto upper_itr = idx.end();

results.reserve( limit );
for ( ; lower_itr != upper_itr && results.size() < limit; ++lower_itr )
{
results.emplace_back( extend_liquidity_pool( *lower_itr, with_stats ) );
}

return results;
}

vector<extended_liquidity_pool_object> database_api::get_liquidity_pools_by_asset_a(
const std::string& asset_symbol_or_id,
const optional<uint32_t>& limit,
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const
{
return my->get_liquidity_pools_by_asset_a(
asset_symbol_or_id,
limit,
start_id,
with_statistics );
}

vector<extended_liquidity_pool_object> database_api_impl::get_liquidity_pools_by_asset_a(
const std::string& asset_symbol_or_id,
const optional<uint32_t>& limit,
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const
{
return get_liquidity_pools_by_asset_x<by_asset_a>(
asset_symbol_or_id,
asset_id_type asset_id = my->get_asset_from_string(asset_symbol_or_id)->id;
return my->get_liquidity_pools_by_asset_x<by_asset_a>(
limit,
start_id,
with_statistics );
with_statistics,
asset_id );
}

vector<extended_liquidity_pool_object> database_api::get_liquidity_pools_by_asset_b(
Expand All @@ -1790,24 +1746,12 @@ vector<extended_liquidity_pool_object> database_api::get_liquidity_pools_by_asse
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const
{
return my->get_liquidity_pools_by_asset_b(
asset_symbol_or_id,
limit,
start_id,
with_statistics );
}

vector<extended_liquidity_pool_object> database_api_impl::get_liquidity_pools_by_asset_b(
const std::string& asset_symbol_or_id,
const optional<uint32_t>& limit,
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const
{
return get_liquidity_pools_by_asset_x<by_asset_b>(
asset_symbol_or_id,
asset_id_type asset_id = my->get_asset_from_string(asset_symbol_or_id)->id;
return my->get_liquidity_pools_by_asset_x<by_asset_b>(
limit,
start_id,
with_statistics );
with_statistics,
asset_id );
}

vector<extended_liquidity_pool_object> database_api::get_liquidity_pools_by_one_asset(
Expand Down Expand Up @@ -1869,51 +1813,16 @@ vector<extended_liquidity_pool_object> database_api::get_liquidity_pools_by_both
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const
{
return my->get_liquidity_pools_by_both_assets(
asset_symbol_or_id_a,
asset_symbol_or_id_b,
limit,
start_id,
with_statistics );
}

vector<extended_liquidity_pool_object> database_api_impl::get_liquidity_pools_by_both_assets(
const std::string& asset_symbol_or_id_a,
const std::string& asset_symbol_or_id_b,
const optional<uint32_t>& olimit,
const optional<liquidity_pool_id_type>& ostart_id,
const optional<bool>& with_statistics )const
{
uint32_t limit = olimit.valid() ? *olimit : application_options::get_default().api_limit_get_liquidity_pools;

FC_ASSERT( _app_options, "Internal error" );
const auto configured_limit = _app_options->api_limit_get_liquidity_pools;
FC_ASSERT( limit <= configured_limit,
"limit can not be greater than ${configured_limit}",
("configured_limit", configured_limit) );

bool with_stats = ( with_statistics.valid() && *with_statistics );

vector<extended_liquidity_pool_object> results;

asset_id_type asset_id_a = get_asset_from_string(asset_symbol_or_id_a)->id;
asset_id_type asset_id_b = get_asset_from_string(asset_symbol_or_id_b)->id;
asset_id_type asset_id_a = my->get_asset_from_string(asset_symbol_or_id_a)->id;
asset_id_type asset_id_b = my->get_asset_from_string(asset_symbol_or_id_b)->id;
if( asset_id_a > asset_id_b )
std::swap( asset_id_a, asset_id_b );

liquidity_pool_id_type start_id = ostart_id.valid() ? *ostart_id : liquidity_pool_id_type();

const auto& idx = _db.get_index_type<liquidity_pool_index>().indices().get<by_asset_ab>();
auto lower_itr = idx.lower_bound( std::make_tuple( asset_id_a, asset_id_b, start_id ) );
auto upper_itr = idx.upper_bound( std::make_tuple( asset_id_a, asset_id_b ) );

results.reserve( limit );
for ( ; lower_itr != upper_itr && results.size() < limit; ++lower_itr )
{
results.emplace_back( extend_liquidity_pool( *lower_itr, with_stats ) );
}

return results;
return my->get_liquidity_pools_by_asset_x<by_asset_ab>(
limit,
start_id,
with_statistics,
asset_id_a,
asset_id_b );
}

vector<optional<extended_liquidity_pool_object>> database_api::get_liquidity_pools(
Expand Down
39 changes: 9 additions & 30 deletions libraries/app/database_api_impl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -136,31 +136,11 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
uint32_t limit )const;

// Liquidity pools
vector<extended_liquidity_pool_object> list_liquidity_pools(
const optional<uint32_t>& limit,
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const;
vector<extended_liquidity_pool_object> get_liquidity_pools_by_asset_a(
const std::string& asset_symbol_or_id,
const optional<uint32_t>& limit,
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const;
vector<extended_liquidity_pool_object> get_liquidity_pools_by_asset_b(
const std::string& asset_symbol_or_id,
const optional<uint32_t>& limit,
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const;
vector<extended_liquidity_pool_object> get_liquidity_pools_by_one_asset(
const std::string& asset_symbol_or_id,
const optional<uint32_t>& limit,
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const;
vector<extended_liquidity_pool_object> get_liquidity_pools_by_both_assets(
const std::string& asset_symbol_or_id_a,
const std::string& asset_symbol_or_id_b,
const optional<uint32_t>& limit,
const optional<liquidity_pool_id_type>& start_id,
const optional<bool>& with_statistics )const;
vector<optional<extended_liquidity_pool_object>> get_liquidity_pools(
const vector<liquidity_pool_id_type>& ids,
const optional<bool>& subscribe,
Expand Down Expand Up @@ -291,12 +271,12 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
}

// template function to reduce duplicate code
template <typename X>
template <typename INDEX_TAG, typename... X>
vector<extended_liquidity_pool_object> get_liquidity_pools_by_asset_x(
std::string asset_symbol_or_id,
optional<uint32_t> olimit,
optional<liquidity_pool_id_type> ostart_id,
optional<bool> with_statistics )const
const optional<uint32_t>& olimit,
const optional<liquidity_pool_id_type>& ostart_id,
const optional<bool>& with_statistics,
X... x )const
{
uint32_t limit = olimit.valid() ? *olimit : application_options::get_default().api_limit_get_liquidity_pools;

Expand All @@ -310,13 +290,12 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>

vector<extended_liquidity_pool_object> results;

const asset_id_type asset_id = get_asset_from_string(asset_symbol_or_id)->id;

liquidity_pool_id_type start_id = ostart_id.valid() ? *ostart_id : liquidity_pool_id_type();

const auto& idx = _db.get_index_type<liquidity_pool_index>().indices().get<X>();
auto lower_itr = idx.lower_bound( std::make_tuple( asset_id, start_id ) );
auto upper_itr = idx.upper_bound( asset_id );
const auto& idx = _db.get_index_type<liquidity_pool_index>().indices().get<INDEX_TAG>();

auto lower_itr = idx.lower_bound( make_tuple_if_multiple( x..., start_id ) );
auto upper_itr = call_end_or_upper_bound( idx, x... );

results.reserve( limit );
for ( ; lower_itr != upper_itr && results.size() < limit; ++lower_itr )
Expand Down

0 comments on commit 604cb29

Please sign in to comment.