From 3882d024cb0591b9c7dbdaca768a7b19e52aad57 Mon Sep 17 00:00:00 2001 From: sowle Date: Sat, 22 Jun 2024 20:25:04 +0200 Subject: [PATCH] simplewallet: display own assets with correct decimal point, get_asset_info() knows own assets, + minor fixes --- src/simplewallet/simplewallet.cpp | 31 +++++++++++-------- src/simplewallet/simplewallet.h | 2 +- src/wallet/wallet2.cpp | 49 +++++++++++++++++-------------- src/wallet/wallet2.h | 5 +++- 4 files changed, 51 insertions(+), 36 deletions(-) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index dd72a2f4..fb1c3a61 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -816,38 +816,45 @@ void simple_wallet::on_new_block(uint64_t height, const currency::block& block) //---------------------------------------------------------------------------------------------------- std::string print_money_trailing_zeros_replaced_with_spaces(uint64_t amount, size_t decimal_point = CURRENCY_DISPLAY_DECIMAL_POINT) { - std::string s = print_money(amount); + std::string s = print_money(amount, decimal_point); size_t p = s.find_last_not_of('0'); if (p != std::string::npos) { if (s[p] == '.') ++p; size_t l = s.length() - p - 1; - return s.replace(p + 1, l, l, ' '); + s.replace(p + 1, l, l, ' '); + if (decimal_point < CURRENCY_DISPLAY_DECIMAL_POINT) + s += std::string(CURRENCY_DISPLAY_DECIMAL_POINT - decimal_point, ' '); } return s; } //---------------------------------------------------------------------------------------------------- -std::string simple_wallet::get_tocken_info_string(const crypto::public_key& asset_id, uint64_t& decimal_points) +std::string simple_wallet::get_token_info_string(const crypto::public_key& asset_id, uint64_t& decimal_points) { std::string token_info = "ZANO"; decimal_points = CURRENCY_DISPLAY_DECIMAL_POINT; if (asset_id != currency::native_coin_asset_id) { currency::asset_descriptor_base adb = AUTO_VAL_INIT(adb); - bool whitelisted = false; - if (!m_wallet->get_asset_id_info(asset_id, adb, whitelisted)) + uint32_t asset_info_flags{}; + if (!m_wallet->get_asset_info(asset_id, adb, asset_info_flags)) { token_info = "!UNKNOWN!"; } - else { + else + { decimal_points = adb.decimal_point; token_info = adb.ticker; - if (whitelisted) + if (asset_info_flags & tools::wallet2::aif_whitelisted) { token_info += "[*]"; } + else if (asset_info_flags & tools::wallet2::aif_own) + { + token_info += "[$]"; + } else { token_info += std::string("[") + epee::string_tools::pod_to_hex(asset_id) + "]"; @@ -864,7 +871,7 @@ void simple_wallet::on_transfer2(const tools::wallet_public::wallet_transfer_inf { epee::log_space::console_colors color = !wti.has_outgoing_entries() ? epee::log_space::console_color_green : epee::log_space::console_color_magenta; uint64_t decimal_points = CURRENCY_DISPLAY_DECIMAL_POINT; - std::string token_info = get_tocken_info_string(wti.subtransfers[0].asset_id, decimal_points); + std::string token_info = get_token_info_string(wti.subtransfers[0].asset_id, decimal_points); message_writer(color, false) << "height " << wti.height << ", tx " << wti.tx_hash << @@ -879,10 +886,10 @@ void simple_wallet::on_transfer2(const tools::wallet_public::wallet_transfer_inf { epee::log_space::console_colors color = st.is_income ? epee::log_space::console_color_green : epee::log_space::console_color_magenta; uint64_t decimal_points = CURRENCY_DISPLAY_DECIMAL_POINT; - std::string token_info = get_tocken_info_string(st.asset_id, decimal_points); + std::string token_info = get_token_info_string(st.asset_id, decimal_points); - message_writer(epee::log_space::console_color_cyan, false) << " " - << std::right << std::setw(18) << print_money_trailing_zeros_replaced_with_spaces(st.amount, decimal_points) << (st.is_income ? " received," : " spent") << " " << token_info; + message_writer(epee::log_space::console_color_cyan, false) << " " + << std::right << std::setw(24) << print_money_trailing_zeros_replaced_with_spaces(st.amount, decimal_points) << std::left << (st.is_income ? " received," : " spent") << " " << token_info; } } @@ -1059,7 +1066,7 @@ bool simple_wallet::print_wti(const tools::wallet_public::wallet_transfer_info& { epee::log_space::console_colors cl = st.is_income ? epee::log_space::console_color_green: epee::log_space::console_color_magenta; uint64_t decimal_points = CURRENCY_DISPLAY_DECIMAL_POINT; - std::string token_info = get_tocken_info_string(st.asset_id, decimal_points); + std::string token_info = get_token_info_string(st.asset_id, decimal_points); success_msg_writer(cl) << (st.is_income ? "Received " : "Sent ") diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 20dce45a..5f260433 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -110,7 +110,7 @@ namespace currency uint64_t get_daemon_blockchain_height(std::string& err); bool try_connect_to_daemon(); - std::string get_tocken_info_string(const crypto::public_key& asset_id, uint64_t& decimal_point); + std::string get_token_info_string(const crypto::public_key& asset_id, uint64_t& decimal_point); bool print_wti(const tools::wallet_public::wallet_transfer_info& wti); bool check_password_for_operation(); crypto::hash get_hash_from_pass_and_salt(const std::string& pass, uint64_t salt); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 1907d8fa..5effc4f1 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3813,37 +3813,42 @@ bool wallet2::balance(std::list& balances, u return true; } //---------------------------------------------------------------------------------------------------- -bool wallet2::get_asset_id_info(const crypto::public_key& asset_id, currency::asset_descriptor_base& asset_info, bool& whitelist_) const +bool wallet2::get_asset_info(const crypto::public_key& asset_id, currency::asset_descriptor_base& asset_info, uint32_t& asset_flags) const { + asset_flags = aif_none; if (asset_id == currency::native_coin_asset_id) { asset_info = currency::get_native_coin_asset_descriptor(); - whitelist_ = true; + asset_flags |= aif_whitelisted; return true; } - //check if asset is whitelisted or customly added - whitelist_ = false; + + // whitelisted? auto it_white = m_whitelisted_assets.find(asset_id); - if (it_white == m_whitelisted_assets.end()) - { - //check if it custom asset - auto it_cust = m_custom_assets.find(asset_id); - if (it_cust == m_custom_assets.end()) - { - return false; - } - else - { - asset_info = it_cust->second; - } - } - else + if (it_white != m_whitelisted_assets.end()) { asset_info = it_white->second; - whitelist_ = true; + asset_flags |= aif_whitelisted; + return true; } - return true; + // custom asset? + auto it_cust = m_custom_assets.find(asset_id); + if (it_cust != m_custom_assets.end()) + { + asset_info = it_cust->second; + return true; + } + + auto it_own = m_own_asset_descriptors.find(asset_id); + if (it_own != m_own_asset_descriptors.end()) + { + asset_info = it_own->second; + asset_flags |= aif_own; + return true; + } + + return false; } //---------------------------------------------------------------------------------------------------- @@ -3965,8 +3970,8 @@ std::string wallet2::get_transfers_str(bool include_spent /*= true*/, bool inclu bool native_coin = td.is_native_coin(); asset_descriptor_base adb{}; - bool whitelisted = false; - if (get_asset_id_info(td.get_asset_id(), adb, whitelisted) == show_only_unknown) + uint32_t asset_info_flags{}; + if (get_asset_info(td.get_asset_id(), adb, asset_info_flags) == show_only_unknown) { if (!show_only_unknown) ++unknown_assets_outs_count; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index dd4718bd..448af662 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -441,7 +441,10 @@ namespace tools uint64_t balance(uint64_t& unloked) const; uint64_t unlocked_balance() const; - bool get_asset_id_info(const crypto::public_key& asset_id, currency::asset_descriptor_base& asset_info, bool& whitelist_) const; + + enum asset_info_flags_t : uint32_t { aif_none = 0, aif_whitelisted = 1 << 0, aif_own = 1 << 1 }; + bool get_asset_info(const crypto::public_key& asset_id, currency::asset_descriptor_base& asset_info, uint32_t& asset_flags) const; + void transfer(uint64_t amount, const currency::account_public_address& acc, const crypto::public_key& asset_id = currency::native_coin_asset_id); void transfer(uint64_t amount, size_t fake_outs_count, const currency::account_public_address& acc, uint64_t fee = TX_DEFAULT_FEE, const crypto::public_key& asset_id = currency::native_coin_asset_id); void transfer(uint64_t amount, const currency::account_public_address& acc, currency::transaction& result_tx, const crypto::public_key& asset_id = currency::native_coin_asset_id);