added support of multibalances on wallets_manager and wallet rpc level

This commit is contained in:
cryptozoidberg 2022-11-18 16:11:15 +01:00
parent 8c7aa8f578
commit bf3430b49b
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC
7 changed files with 68 additions and 34 deletions

View file

@ -215,8 +215,7 @@ public:
struct wallet_info
{
uint64_t unlocked_balance;
uint64_t balance;
std::vector<tools::wallet_public::asset_balance_entry> balances;
uint64_t mined_total;
std::string address;
std::string view_sec_key;
@ -225,8 +224,7 @@ public:
bool is_watch_only;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(unlocked_balance)
KV_SERIALIZE(balance)
KV_SERIALIZE(balances)
KV_SERIALIZE(mined_total)
KV_SERIALIZE(address)
KV_SERIALIZE(view_sec_key)

View file

@ -3125,6 +3125,44 @@ bool wallet2::balance(std::unordered_map<crypto::hash, wallet_public::asset_bala
return true;
}
//----------------------------------------------------------------------------------------------------
bool wallet2::balance(std::vector<wallet_public::asset_balance_entry>& balances, uint64_t& mined)
{
std::unordered_map<crypto::hash, wallet_public::asset_balance_entry_base> balances_map;
this->balance(balances_map, mined);
for (const auto& item : balances_map)
{
asset_descriptor_base* asset_ptr = nullptr;
//check if asset is whitelisted or customly added
auto it = m_whitelisted_assets.find(item.first);
if (it == m_whitelisted_assets.end())
{
//check if it custom asset
auto it_cust = m_custom_assets.find(item.first);
if (it_cust == m_custom_assets.end())
{
continue;
}
else
{
asset_ptr = &it_cust->second;
}
}
else
{
asset_ptr = &it->second;
}
balances.push_back(wallet_public::asset_balance_entry());
wallet_public::asset_balance_entry& new_item = balances.back();
static_cast<asset_balance_entry_base&>(new_item) = item.second;
new_item.asset_info.asset_id = item.first;
CHECK_AND_ASSERT_THROW_MES(asset_ptr, "Internal error: asset_ptr i nullptr");
static_cast<asset_descriptor_base&>(new_item.asset_info) = *asset_ptr;
}
return true;
}
//----------------------------------------------------------------------------------------------------
uint64_t wallet2::balance() const
{
uint64_t stub = 0;

View file

@ -566,6 +566,8 @@ namespace tools
uint64_t balance() const;
uint64_t balance(uint64_t& unloked, uint64_t& awaiting_in, uint64_t& awaiting_out, uint64_t& mined) const;
bool balance(std::unordered_map<crypto::hash, wallet_public::asset_balance_entry_base>& balances, uint64_t& mined) const;
bool balance(std::vector<wallet_public::asset_balance_entry>& balances, uint64_t& mined) const;
uint64_t balance(uint64_t& unloked) const;
uint64_t unlocked_balance() const;

View file

@ -16,8 +16,7 @@ namespace tools
wi = AUTO_VAL_INIT_T(view::wallet_info);
wi.address = w.get_account().get_public_address_str();
wi.view_sec_key = epee::string_tools::pod_to_hex(w.get_account().get_keys().view_secret_key);
uint64_t fake = 0;
wi.balance = w.balance(wi.unlocked_balance, fake, fake, wi.mined_total);
w.balance(wi.balances, wi.mined_total);
wi.path = epee::string_encoding::wstring_to_utf8(w.get_wallet_path());
wi.is_auditable = w.is_auditable();
wi.is_watch_only = w.is_watch_only();

View file

@ -166,10 +166,10 @@ namespace wallet_public
struct asset_balance_entry : public asset_balance_entry_base
{
crypto::hash asset_id = currency::null_hash;
currency::asset_descriptor_with_id asset_info;
//v2
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(asset_id)
KV_SERIALIZE(asset_info)
KV_CHAIN_BASE(asset_balance_entry_base)
END_KV_SERIALIZE_MAP()
};

View file

@ -112,11 +112,11 @@ namespace tools
{
LOG_PRINT_RED("no connection to the daemon", LOG_LEVEL_0);
}
catch(std::exception& e)
catch (std::exception& e)
{
LOG_ERROR("exeption caught in wallet_rpc_server::idle_handler: " << e.what());
}
catch(...)
catch (...)
{
LOG_ERROR("unknown exeption caught in wallet_rpc_server::idle_handler");
}
@ -157,26 +157,26 @@ namespace tools
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::handle_http_request(const epee::net_utils::http::http_request_info& query_info, epee::net_utils::http::http_response_info& response, connection_context& m_conn_context)
{
response.m_response_code = 200;
response.m_response_comment = "Ok";
std::string reference_stub;
bool call_found = false;
response.m_response_code = 200;
response.m_response_comment = "Ok";
std::string reference_stub;
bool call_found = false;
if (m_deaf)
{
response.m_response_code = 500;
response.m_response_comment = "Internal Server Error";
response.m_response_code = 500;
response.m_response_comment = "Internal Server Error";
return true;
}
if (!handle_http_request_map(query_info, response, m_conn_context, call_found, reference_stub) && response.m_response_code == 200)
{
response.m_response_code = 500;
response.m_response_comment = "Internal Server Error";
response.m_response_code = 500;
response.m_response_comment = "Internal Server Error";
return true;
}
if (!call_found)
{
response.m_response_code = 404;
response.m_response_comment = "Not Found";
response.m_response_code = 404;
response.m_response_comment = "Not Found";
return true;
}
return true;
@ -186,22 +186,18 @@ namespace tools
{
try
{
res.balance = m_wallet.balance();
res.unlocked_balance = m_wallet.unlocked_balance();
// res.balance = m_wallet.balance();
// res.unlocked_balance = m_wallet.unlocked_balance();
uint64_t mined = 0;
std::unordered_map<crypto::hash, wallet_public::asset_balance_entry_base> balances;
m_wallet.balance(balances, mined);
auto it = balances.find(currency::null_hash);
if (it != balances.end())
// std::unordered_map<crypto::hash, wallet_public::asset_balance_entry_base> balances;
m_wallet.balance(res.balances, mined);
for (auto it = res.balances.begin(); it != res.balances.end(); it++)
{
res.balance = it->second.total;
res.unlocked_balance = it->second.unlocked;
}
for (auto el : balances)
{
res.balances.push_back(wallet_public::asset_balance_entry());
static_cast<wallet_public::asset_balance_entry_base&>(res.balances.back()) = el.second;
res.balances.back().asset_id = el.first;
if (it->asset_info.asset_id == currency::null_hash)
{
res.balance = it->total;
res.unlocked_balance = it->unlocked;
}
}
}
catch (std::exception& e)

View file

@ -943,6 +943,7 @@ std::string wallets_manager::open_wallet(const std::wstring& path, const std::st
w->load(path, password);
if (w->is_watch_only() && !w->is_auditable())
return API_RETURN_CODE_WALLET_WATCH_ONLY_NOT_SUPPORTED;
w->load_whitelisted_tokens_list();
w->get_recent_transfers_history(owr.recent_history.history, 0, txs_to_return, owr.recent_history.total_history_items, owr.recent_history.last_item_index, exclude_mining_txs);
//w->get_unconfirmed_transfers(owr.recent_history.unconfirmed);