From bf3430b49b3979db4ea87bb7cb13e18f659d486d Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Fri, 18 Nov 2022 16:11:15 +0100 Subject: [PATCH] added support of multibalances on wallets_manager and wallet rpc level --- src/wallet/view_iface.h | 6 ++-- src/wallet/wallet2.cpp | 38 ++++++++++++++++++++ src/wallet/wallet2.h | 2 ++ src/wallet/wallet_helpers.h | 3 +- src/wallet/wallet_public_structs_defs.h | 4 +-- src/wallet/wallet_rpc_server.cpp | 48 ++++++++++++------------- src/wallet/wallets_manager.cpp | 1 + 7 files changed, 68 insertions(+), 34 deletions(-) diff --git a/src/wallet/view_iface.h b/src/wallet/view_iface.h index 3243242e..5e5a8452 100644 --- a/src/wallet/view_iface.h +++ b/src/wallet/view_iface.h @@ -215,8 +215,7 @@ public: struct wallet_info { - uint64_t unlocked_balance; - uint64_t balance; + std::vector 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) diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 1faa8bb2..1ea777f9 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3125,6 +3125,44 @@ bool wallet2::balance(std::unordered_map& balances, uint64_t& mined) +{ + std::unordered_map 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(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(new_item.asset_info) = *asset_ptr; + } + + return true; +} +//---------------------------------------------------------------------------------------------------- uint64_t wallet2::balance() const { uint64_t stub = 0; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index d43650e6..58e47149 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -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& balances, uint64_t& mined) const; + bool balance(std::vector& balances, uint64_t& mined) const; + uint64_t balance(uint64_t& unloked) const; uint64_t unlocked_balance() const; diff --git a/src/wallet/wallet_helpers.h b/src/wallet/wallet_helpers.h index 9ebf9137..12a75d7a 100644 --- a/src/wallet/wallet_helpers.h +++ b/src/wallet/wallet_helpers.h @@ -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(); diff --git a/src/wallet/wallet_public_structs_defs.h b/src/wallet/wallet_public_structs_defs.h index ad7c0e67..22da411d 100644 --- a/src/wallet/wallet_public_structs_defs.h +++ b/src/wallet/wallet_public_structs_defs.h @@ -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() }; diff --git a/src/wallet/wallet_rpc_server.cpp b/src/wallet/wallet_rpc_server.cpp index 9ecf1008..2b4b86a1 100644 --- a/src/wallet/wallet_rpc_server.cpp +++ b/src/wallet/wallet_rpc_server.cpp @@ -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 balances; - m_wallet.balance(balances, mined); - auto it = balances.find(currency::null_hash); - if (it != balances.end()) + // std::unordered_map 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(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) diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index f00b1d85..f238a5d6 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -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);