diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index e2d5d605..e3d3019a 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -226,6 +226,8 @@ simple_wallet::simple_wallet() m_cmd_binder.set_handler("tor_enable", boost::bind(&simple_wallet::tor_enable, this, _1), "Enable relaying transactions over TOR network(enabled by default)"); m_cmd_binder.set_handler("tor_disable", boost::bind(&simple_wallet::tor_disable, this, _1), "Enable relaying transactions over TOR network(enabled by default)"); m_cmd_binder.set_handler("deploy_new_asset", boost::bind(&simple_wallet::deploy_new_asset, this, _1), "Deploys new asset in the network, with current wallet as a maintainer"); + m_cmd_binder.set_handler("add_custom_asset_id", boost::bind(&simple_wallet::add_custom_asset_id, this, _1), "Approve asset id to be recognized in the wallet and returned in balances"); + m_cmd_binder.set_handler("remove_custom_asset_id", boost::bind(&simple_wallet::remove_custom_asset_id, this, _1), "Cancel previously made approval for asset id"); } //---------------------------------------------------------------------------------------------------- @@ -1790,7 +1792,7 @@ bool simple_wallet::deploy_new_asset(const std::vector &args) asset_descriptor_base adb = AUTO_VAL_INIT(adb); if (!args.size() || args.size() > 1) { - fail_msg_writer() << "invalid agruments count: " << args.size() << ", expected 1"; + fail_msg_writer() << "invalid arguments count: " << args.size() << ", expected 1"; } bool r = epee::serialization::load_t_from_json_file(adb, args[0]); if (!r) @@ -1818,6 +1820,63 @@ bool simple_wallet::deploy_new_asset(const std::vector &args) return true; } //---------------------------------------------------------------------------------------------------- +bool simple_wallet::add_custom_asset_id(const std::vector &args) +{ + if (!args.size() || args.size() > 1) + { + fail_msg_writer() << "invalid arguments count: " << args.size() << ", expected 1"; + } + crypto::hash asset_id = currency::null_hash; + if (!epee::string_tools::parse_tpod_from_hex_string(args[0], asset_id)) + { + fail_msg_writer() << "expected valid asset_id"; + return true; + } + asset_descriptor_base asset_descriptor = AUTO_VAL_INIT(asset_descriptor); + bool r = m_wallet->add_custom_asset_id(asset_id, asset_descriptor); + if(!r) + { + fail_msg_writer() << "Asset id " << asset_id << " not found as registered asset"; + return true; + } + else + { + success_msg_writer() << "Added custom asset:" << ENDL + << " Id: " << asset_id << ENDL + << " Title: " << asset_descriptor.full_name << ENDL + << " Ticker: " << asset_descriptor.ticker << ENDL + << " Ticker: " << print_fixed_decimal_point(asset_descriptor.current_supply, asset_descriptor.decimal_point) << ENDL + ; + } + return true; +} +//---------------------------------------------------------------------------------------------------- +bool simple_wallet::remove_custom_asset_id(const std::vector &args) +{ + if (!args.size() || args.size() > 1) + { + fail_msg_writer() << "invalid arguments count: " << args.size() << ", expected 1"; + } + crypto::hash asset_id = currency::null_hash; + if (!epee::string_tools::parse_tpod_from_hex_string(args[0], asset_id)) + { + fail_msg_writer() << "expected valid asset_id"; + return true; + } + + bool r = m_wallet->delete_custom_asset_id(asset_id); + if (!r) + { + fail_msg_writer() << "Asset id " << asset_id << " not present in this wallet"; + return true; + } + else + { + success_msg_writer() << "Asset id " << asset_id << " removed from wallet custom list" << ENDL; + } + return true; +} +//---------------------------------------------------------------------------------------------------- bool simple_wallet::sweep_below(const std::vector &args) { bool r = false; diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 6c92c634..47cc4bfd 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -89,6 +89,9 @@ namespace currency bool tor_enable(const std::vector &args); bool tor_disable(const std::vector &args); bool deploy_new_asset(const std::vector &args); + bool add_custom_asset_id(const std::vector &args); + bool remove_custom_asset_id(const std::vector &args); + bool validate_wrap_status(uint64_t amount); bool get_alias_from_daemon(const std::string& alias_name, currency::extra_alias_entry_base& ai); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 51cfb030..7c844b97 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3148,17 +3148,25 @@ bool wallet2::balance(std::list& balances, u balances.clear(); std::unordered_map balances_map; this->balance(balances_map, mined); + std::unordered_map custom_assets_local = m_custom_assets; + + for (auto& own_asset : m_own_asset_descriptors) + { + custom_assets_local[own_asset.first] = own_asset.second.asset_descriptor; + } + + asset_descriptor_base native_asset_info = AUTO_VAL_INIT(native_asset_info); + native_asset_info.full_name = CURRENCY_NAME_SHORT_BASE; + native_asset_info.ticker = CURRENCY_NAME_ABR; + native_asset_info.decimal_point = CURRENCY_DISPLAY_DECIMAL_POINT; + for (const auto& item : balances_map) { - asset_descriptor_base native_asset_info = AUTO_VAL_INIT(native_asset_info); - native_asset_info.full_name = CURRENCY_NAME_SHORT_BASE; - native_asset_info.ticker = CURRENCY_NAME_ABR; - native_asset_info.decimal_point = CURRENCY_DISPLAY_DECIMAL_POINT; - const asset_descriptor_base* asset_ptr = nullptr; + asset_descriptor_base asset_info = AUTO_VAL_INIT(asset_info); //check if asset is whitelisted or customly added if (item.first == currency::null_hash) { - asset_ptr = &native_asset_info; + asset_info = native_asset_info; } else { @@ -3166,27 +3174,35 @@ bool wallet2::balance(std::list& balances, u 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()) + auto it_cust = custom_assets_local.find(item.first); + if (it_cust == custom_assets_local.end()) { continue; } else { - asset_ptr = &it_cust->second; + asset_info = it_cust->second; + custom_assets_local.erase(it_cust); } } else { - asset_ptr = &it->second; + asset_info = 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; + static_cast(new_item.asset_info) = asset_info; + } + //manually added assets should be always present, at least as zero balanced items + for (auto& asset : custom_assets_local) + { + balances.push_back(wallet_public::asset_balance_entry()); + wallet_public::asset_balance_entry& new_item = balances.back(); + new_item.asset_info.asset_id = asset.first; + static_cast(new_item.asset_info) = asset.second; } return true; @@ -3198,7 +3214,7 @@ uint64_t wallet2::balance() const return balance(stub, stub, stub, stub); } //---------------------------------------------------------------------------------------------------- -bool wallet2::add_custom_asset_id(const crypto::hash& asset_id) +bool wallet2::add_custom_asset_id(const crypto::hash& asset_id, asset_descriptor_base& asset_descriptor) { currency::COMMAND_RPC_GET_ASSET_INFO::request req = AUTO_VAL_INIT(req); currency::COMMAND_RPC_GET_ASSET_INFO::response resp = AUTO_VAL_INIT(resp); @@ -3207,6 +3223,7 @@ bool wallet2::add_custom_asset_id(const crypto::hash& asset_id) if (resp.status == API_RETURN_CODE_OK) { m_custom_assets[asset_id] = resp.asset_descriptor; + asset_descriptor = resp.asset_descriptor; return true; } return false; diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 8a458de4..529b78a1 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -899,7 +899,7 @@ namespace tools uint64_t get_default_fee() {return TX_DEFAULT_FEE;} void export_transaction_history(std::ostream& ss, const std::string& format, bool include_pos_transactions = true); - bool add_custom_asset_id(const crypto::hash& asset_id); + bool add_custom_asset_id(const crypto::hash& asset_id, currency::asset_descriptor_base& asset_descriptor); bool delete_custom_asset_id(const crypto::hash& asset_id); bool load_whitelisted_tokens_if_not_loaded() const; bool load_whitelisted_tokens()const;