Skip to content

Commit

Permalink
Add db API to find liquidity pools by one asset
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed Feb 26, 2021
1 parent f637d32 commit 5b4cb7d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
61 changes: 61 additions & 0 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ database_api_impl::database_api_impl( graphene::chain::database& db, const appli
{
amount_in_collateral_index = nullptr;
}

try
{
asset_in_liquidity_pools_index = &_db.get_index_type< primary_index< liquidity_pool_index > >()
.get_secondary_index<graphene::api_helper_indexes::asset_in_liquidity_pools_index>();
}
catch( fc::assert_exception& e )
{
asset_in_liquidity_pools_index = nullptr;
}
}

database_api_impl::~database_api_impl()
Expand Down Expand Up @@ -1816,6 +1826,57 @@ vector<extended_liquidity_pool_object> database_api_impl::get_liquidity_pools_by
with_statistics );
}

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

vector<extended_liquidity_pool_object> database_api_impl::get_liquidity_pools_by_one_asset(
std::string asset_symbol_or_id,
optional<uint32_t> olimit,
optional<liquidity_pool_id_type> ostart_id,
optional<bool> with_statistics )const
{
// api_helper_indexes plugin is required for accessing the secondary index
FC_ASSERT( _app_options && _app_options->has_api_helper_indexes_plugin,
"api_helper_indexes plugin is not enabled on this server." );

uint32_t limit = olimit.valid() ? *olimit : 101;
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) );

asset_id_type aid = get_asset_from_string(asset_symbol_or_id)->id;

FC_ASSERT( asset_in_liquidity_pools_index, "Internal error" );
const auto& pools = asset_in_liquidity_pools_index->get_liquidity_pools_by_asset( aid );

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

auto itr = pools.lower_bound( start_id );

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

vector<extended_liquidity_pool_object> results;

results.reserve( limit );
for ( ; itr != pools.end() && results.size() < limit; ++itr )
{
results.emplace_back( extend_liquidity_pool( (*itr)(_db), with_stats ) );
}

return results;
}

vector<extended_liquidity_pool_object> database_api::get_liquidity_pools_by_both_assets(
std::string asset_symbol_or_id_a,
std::string asset_symbol_or_id_b,
Expand Down
6 changes: 6 additions & 0 deletions libraries/app/database_api_impl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
optional<uint32_t> limit = 101,
optional<liquidity_pool_id_type> start_id = optional<liquidity_pool_id_type>(),
optional<bool> with_statistics = false )const;
vector<extended_liquidity_pool_object> get_liquidity_pools_by_one_asset(
std::string asset_symbol_or_id,
optional<uint32_t> limit = 101,
optional<liquidity_pool_id_type> start_id = optional<liquidity_pool_id_type>(),
optional<bool> with_statistics = false )const;
vector<extended_liquidity_pool_object> get_liquidity_pools_by_both_assets(
std::string asset_symbol_or_id_a,
std::string asset_symbol_or_id_b,
Expand Down Expand Up @@ -465,6 +470,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
const application_options* _app_options = nullptr;

const graphene::api_helper_indexes::amount_in_collateral_index* amount_in_collateral_index;
const graphene::api_helper_indexes::asset_in_liquidity_pools_index* asset_in_liquidity_pools_index;
};

} } // graphene::app
21 changes: 21 additions & 0 deletions libraries/app/include/graphene/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,26 @@ class database_api
optional<liquidity_pool_id_type> start_id = optional<liquidity_pool_id_type>(),
optional<bool> with_statistics = false )const;

/**
* @brief Get a list of liquidity pools by the symbol or ID of one asset in the pool
* @param asset_symbol_or_id symbol name or ID of the asset
* @param limit The limitation of items each query can fetch, not greater than a configured value
* @param start_id Start liquidity pool id, fetch pools whose IDs are greater than or equal to this ID
* @param with_statistics Whether to return statistics
* @return The liquidity pools
*
* @note
* 1. if @p asset_symbol_or_id cannot be tied to an asset, an error will be returned
* 2. @p limit can be omitted or be null, if so the default value 101 will be used
* 3. @p start_id can be omitted or be null, if so the api will return the "first page" of pools
* 4. can only omit one or more arguments in the end of the list, but not one or more in the middle
*/
vector<extended_liquidity_pool_object> get_liquidity_pools_by_one_asset(
std::string asset_symbol_or_id,
optional<uint32_t> limit = 101,
optional<liquidity_pool_id_type> start_id = optional<liquidity_pool_id_type>(),
optional<bool> with_statistics = false )const;

/**
* @brief Get a list of liquidity pools by the symbols or IDs of the two assets in the pool
* @param asset_symbol_or_id_a symbol name or ID of one asset
Expand Down Expand Up @@ -1179,6 +1199,7 @@ FC_API(graphene::app::database_api,
(list_liquidity_pools)
(get_liquidity_pools_by_asset_a)
(get_liquidity_pools_by_asset_b)
(get_liquidity_pools_by_one_asset)
(get_liquidity_pools_by_both_assets)
(get_liquidity_pools)
(get_liquidity_pools_by_share_asset)
Expand Down

0 comments on commit 5b4cb7d

Please sign in to comment.