Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor get_assets to accept id or name #1272

Merged
merged 18 commits into from Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 35 additions & 8 deletions libraries/app/database_api.cpp
Expand Up @@ -98,7 +98,7 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
vector<vesting_balance_object> get_vesting_balances( const std::string account_id_or_name )const;

// Assets
vector<optional<asset_object>> get_assets(const vector<asset_id_type>& asset_ids)const;
vector<optional<asset_object>> get_assets(const vector<std::string>& asset_names_or_ids)const;
abitmore marked this conversation as resolved.
Show resolved Hide resolved
vector<asset_object> list_assets(const string& lower_bound_symbol, uint32_t limit)const;
vector<optional<asset_object>> lookup_asset_symbols(const vector<string>& symbols_or_ids)const;
uint64_t get_asset_count()const;
Expand Down Expand Up @@ -227,6 +227,31 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
return account;
}

const asset_object* get_asset_from_string( const std::string& name_or_id ) const
{
// TODO cache the result to avoid repeatly fetching from db
FC_ASSERT( name_or_id.size() > 0);
const asset_object* asset = nullptr;
if (std::isdigit(name_or_id[0]))
asset = _db.find(fc::variant(name_or_id, 1).as<asset_id_type>(1));
else
{
const auto& idx = _db.get_index_type<asset_index>().indices().get<by_symbol>();
auto itr = idx.find(name_or_id);
if (itr != idx.end())
asset = &*itr;
}
FC_ASSERT( asset, "no such asset" );
return asset;
}
std::string asset_id_to_string(asset_id_type id) const
{
std::string asset_id = fc::to_string(id.space_id) +
"." + fc::to_string(id.type_id) +
"." + fc::to_string(id.instance.value);
return asset_id;
}

template<typename T>
const std::pair<asset_id_type,asset_id_type> get_order_market( const T& order )
{
Expand Down Expand Up @@ -1049,16 +1074,18 @@ vector<vesting_balance_object> database_api_impl::get_vesting_balances( const st
// //
//////////////////////////////////////////////////////////////////////

vector<optional<asset_object>> database_api::get_assets(const vector<asset_id_type>& asset_ids)const
vector<optional<asset_object>> database_api::get_assets(const vector<std::string>& asset_names_or_ids)const
{
return my->get_assets( asset_ids );
return my->get_assets( asset_names_or_ids );
}

vector<optional<asset_object>> database_api_impl::get_assets(const vector<asset_id_type>& asset_ids)const
vector<optional<asset_object>> database_api_impl::get_assets(const vector<std::string>& asset_names_or_ids)const
{
vector<optional<asset_object>> result; result.reserve(asset_ids.size());
std::transform(asset_ids.begin(), asset_ids.end(), std::back_inserter(result),
[this](asset_id_type id) -> optional<asset_object> {
vector<optional<asset_object>> result; result.reserve(asset_names_or_ids.size());
std::transform(asset_names_or_ids.begin(), asset_names_or_ids.end(), std::back_inserter(result),
[this](std::string id_or_name) -> optional<asset_object> {
const asset_object* asset = get_asset_from_string(id_or_name);
asset_id_type id = asset->id;
if(auto o = _db.find(id))
{
subscribe_to_item( id );
Expand Down Expand Up @@ -1453,7 +1480,7 @@ vector<market_volume> database_api_impl::get_top_markets(uint32_t limit)const
{
market_volume mv;
mv.time = now;
const auto assets = get_assets( { itr->base, itr->quote } );
const auto assets = get_assets( { asset_id_to_string(itr->base), asset_id_to_string(itr->quote) } );
abitmore marked this conversation as resolved.
Show resolved Hide resolved
mv.base = assets[0]->symbol;
mv.quote = assets[1]->symbol;
mv.base_volume = uint128_amount_to_string( itr->base_volume, assets[0]->precision );
Expand Down
4 changes: 2 additions & 2 deletions libraries/app/include/graphene/app/database_api.hpp
Expand Up @@ -375,12 +375,12 @@ class database_api

/**
* @brief Get a list of assets by ID
* @param asset_ids IDs of the assets to retrieve
* @param asset_names_or_ids Names or IDs of the assets to retrieve
* @return The assets corresponding to the provided IDs
*
* This function has semantics identical to @ref get_objects
*/
vector<optional<asset_object>> get_assets(const vector<asset_id_type>& asset_ids)const;
vector<optional<asset_object>> get_assets(const vector<std::string>& asset_names_or_ids)const;

/**
* @brief Get assets alphabetically by symbol name
Expand Down
9 changes: 8 additions & 1 deletion libraries/wallet/wallet.cpp
Expand Up @@ -608,9 +608,16 @@ class wallet_api_impl
{
return get_account(account_name_or_id).get_id();
}
std::string asset_id_to_string(asset_id_type id) const
{
std::string asset_id = fc::to_string(id.space_id) +
"." + fc::to_string(id.type_id) +
"." + fc::to_string(id.instance.value);
return asset_id;
}
optional<asset_object> find_asset(asset_id_type id)const
{
auto rec = _remote_db->get_assets({id}).front();
auto rec = _remote_db->get_assets({asset_id_to_string(id)}).front();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ugly as well.

Perhaps better to add another get_assets_by_id() API?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't change find_asset(asset_id_type id) to find(string)? Is it an function that only used internally?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I think cli_wallet is less important, it's fine to have some ugly code and slightly reduced performance.

return rec;
}
optional<asset_object> find_asset(string asset_symbol_or_id)const
Expand Down