From 03f1b74c161b1acd00d1570c35f98ec73f417f26 Mon Sep 17 00:00:00 2001 From: smk762 Date: Fri, 25 Feb 2022 01:41:12 +0800 Subject: [PATCH 01/13] keep active coins when resetting config --- src/core/atomicdex/pages/qt.settings.page.cpp | 95 ++++++++++++++----- 1 file changed, 73 insertions(+), 22 deletions(-) diff --git a/src/core/atomicdex/pages/qt.settings.page.cpp b/src/core/atomicdex/pages/qt.settings.page.cpp index a8fa01df32..43be690e80 100644 --- a/src/core/atomicdex/pages/qt.settings.page.cpp +++ b/src/core/atomicdex/pages/qt.settings.page.cpp @@ -560,27 +560,6 @@ namespace atomic_dex const fs::path logo_path = atomic_dex::utils::get_logo_path(); const fs::path theme_path = atomic_dex::utils::get_themes_path(); - - if (fs::exists(wallet_custom_cfg_path)) - { - nlohmann::json custom_config_json_data; - QFile fs; - fs.setFileName(std_path_to_qstring(wallet_custom_cfg_path)); - fs.open(QIODevice::ReadOnly | QIODevice::Text); - - //! Read Contents - custom_config_json_data = nlohmann::json::parse(QString(fs.readAll()).toStdString()); - fs.close(); - - //! Modify - for (auto&& [key, value]: custom_config_json_data.items()) { value["active"] = false; } - - //! Write - fs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); - fs.write(QString::fromStdString(custom_config_json_data.dump()).toUtf8()); - fs.close(); - } - const auto functor_remove = [](auto&& path_to_remove) { if (fs::exists(path_to_remove)) @@ -606,12 +585,84 @@ namespace atomic_dex } }; - functor_remove(std::move(wallet_cfg_path)); + if (fs::exists(wallet_cfg_path)) + { + nlohmann::json wallet_config_json_data; + nlohmann::json active_list = nlohmann::json::array(); + QFile fs; + fs.setFileName(std_path_to_qstring(wallet_cfg_path)); + fs.open(QIODevice::ReadOnly | QIODevice::Text); + + //! Read Contents + wallet_config_json_data = nlohmann::json::parse(QString(fs.readAll()).toStdString()); + fs.close(); + + //! Get the active coins + for (auto&& [key, value]: wallet_config_json_data.items()) + { + if (value["active"]) { active_list.push_back(key); } + } + SPDLOG_WARN("Active list created: {}", active_list); + + // remove old coins file + functor_remove(std::move(wallet_cfg_path)); + + //! Copy default coins file + const auto cfg_path = ag::core::assets_real_path() / "config"; + std::string filename = std::string(atomic_dex::get_raw_version()) + "-coins.json"; + fs::copy(cfg_path / filename, wallet_cfg_path); + + //! Open coins file + fs.setFileName(std_path_to_qstring(wallet_cfg_path)); + fs.open(QIODevice::ReadOnly | QIODevice::Text); + + //! Read Contents + wallet_config_json_data = nlohmann::json::parse(QString(fs.readAll()).toStdString()); + fs.close(); + + //! set active coins again + for (auto&& [key, value]: wallet_config_json_data.items()) + { + if (std::find(active_list.begin(), active_list.end(), key) != active_list.end()) + { + value["active"] = true; + } + } + SPDLOG_WARN("Active list resurrected"); //! Write + fs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); + fs.write(QString::fromStdString(wallet_config_json_data.dump(4)).toUtf8()); + fs.close(); + + } + + + if (fs::exists(wallet_custom_cfg_path)) + { + nlohmann::json custom_config_json_data; + QFile fs; + fs.setFileName(std_path_to_qstring(wallet_custom_cfg_path)); + fs.open(QIODevice::ReadOnly | QIODevice::Text); + + //! Read Contents + custom_config_json_data = nlohmann::json::parse(QString(fs.readAll()).toStdString()); + fs.close(); + + //! Modify + for (auto&& [key, value]: custom_config_json_data.items()) { value["active"] = false; } + + //! Write + fs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); + fs.write(QString::fromStdString(custom_config_json_data.dump()).toUtf8()); + fs.close(); + } + + functor_remove(std::move(mm2_coins_file_path)); functor_remove(std::move(ini_file_path)); functor_remove(std::move(cfg_json_file_path)); functor_remove(std::move(logo_path)); functor_remove(std::move(theme_path)); + } QStringList From 3ddfc0e6fdf41ebad3d79ffcaf2e0d778ae73b8c Mon Sep 17 00:00:00 2001 From: smk762 Date: Fri, 25 Feb 2022 02:02:36 +0800 Subject: [PATCH 02/13] remove logs --- src/core/atomicdex/pages/qt.settings.page.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/atomicdex/pages/qt.settings.page.cpp b/src/core/atomicdex/pages/qt.settings.page.cpp index 43be690e80..a2f89854c6 100644 --- a/src/core/atomicdex/pages/qt.settings.page.cpp +++ b/src/core/atomicdex/pages/qt.settings.page.cpp @@ -602,7 +602,6 @@ namespace atomic_dex { if (value["active"]) { active_list.push_back(key); } } - SPDLOG_WARN("Active list created: {}", active_list); // remove old coins file functor_remove(std::move(wallet_cfg_path)); @@ -628,7 +627,8 @@ namespace atomic_dex value["active"] = true; } } - SPDLOG_WARN("Active list resurrected"); //! Write + + //! Write fs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); fs.write(QString::fromStdString(wallet_config_json_data.dump(4)).toUtf8()); fs.close(); From 7cd39dbd179f546c757bea904d66c63dd239bcbc Mon Sep 17 00:00:00 2001 From: smk762 Date: Fri, 25 Feb 2022 03:13:24 +0800 Subject: [PATCH 03/13] use unnordered set --- src/core/atomicdex/pages/qt.settings.page.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/core/atomicdex/pages/qt.settings.page.cpp b/src/core/atomicdex/pages/qt.settings.page.cpp index a2f89854c6..2546715a36 100644 --- a/src/core/atomicdex/pages/qt.settings.page.cpp +++ b/src/core/atomicdex/pages/qt.settings.page.cpp @@ -588,7 +588,7 @@ namespace atomic_dex if (fs::exists(wallet_cfg_path)) { nlohmann::json wallet_config_json_data; - nlohmann::json active_list = nlohmann::json::array(); + std::unordered_set active_list; QFile fs; fs.setFileName(std_path_to_qstring(wallet_cfg_path)); fs.open(QIODevice::ReadOnly | QIODevice::Text); @@ -600,7 +600,7 @@ namespace atomic_dex //! Get the active coins for (auto&& [key, value]: wallet_config_json_data.items()) { - if (value["active"]) { active_list.push_back(key); } + if (value["active"]) { active_list.insert(key); } } // remove old coins file @@ -622,7 +622,7 @@ namespace atomic_dex //! set active coins again for (auto&& [key, value]: wallet_config_json_data.items()) { - if (std::find(active_list.begin(), active_list.end(), key) != active_list.end()) + if (active_list.contains(key)) { value["active"] = true; } @@ -632,7 +632,6 @@ namespace atomic_dex fs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); fs.write(QString::fromStdString(wallet_config_json_data.dump(4)).toUtf8()); fs.close(); - } From 3e2d944eba97017c0dba0b3606afc893792af62d Mon Sep 17 00:00:00 2001 From: smk762 Date: Fri, 25 Feb 2022 03:35:16 +0800 Subject: [PATCH 04/13] smaller loop --- src/core/atomicdex/pages/qt.settings.page.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/atomicdex/pages/qt.settings.page.cpp b/src/core/atomicdex/pages/qt.settings.page.cpp index 2546715a36..da78a42522 100644 --- a/src/core/atomicdex/pages/qt.settings.page.cpp +++ b/src/core/atomicdex/pages/qt.settings.page.cpp @@ -620,11 +620,11 @@ namespace atomic_dex fs.close(); //! set active coins again - for (auto&& [key, value]: wallet_config_json_data.items()) + for (auto&& key: active_list) { if (active_list.contains(key)) { - value["active"] = true; + wallet_config_json_data[key]["active"] = true; } } From fbe6347fb49a0af10b5a879d6778d9dbe642550d Mon Sep 17 00:00:00 2001 From: smk762 Date: Fri, 25 Feb 2022 03:52:25 +0800 Subject: [PATCH 05/13] rm useless if, rename set --- src/core/atomicdex/pages/qt.settings.page.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/core/atomicdex/pages/qt.settings.page.cpp b/src/core/atomicdex/pages/qt.settings.page.cpp index da78a42522..e6bf7f814c 100644 --- a/src/core/atomicdex/pages/qt.settings.page.cpp +++ b/src/core/atomicdex/pages/qt.settings.page.cpp @@ -588,7 +588,7 @@ namespace atomic_dex if (fs::exists(wallet_cfg_path)) { nlohmann::json wallet_config_json_data; - std::unordered_set active_list; + std::unordered_set active_coins_registry; QFile fs; fs.setFileName(std_path_to_qstring(wallet_cfg_path)); fs.open(QIODevice::ReadOnly | QIODevice::Text); @@ -600,7 +600,7 @@ namespace atomic_dex //! Get the active coins for (auto&& [key, value]: wallet_config_json_data.items()) { - if (value["active"]) { active_list.insert(key); } + if (value["active"]) { active_coins_registry.insert(key); } } // remove old coins file @@ -620,12 +620,9 @@ namespace atomic_dex fs.close(); //! set active coins again - for (auto&& key: active_list) + for (auto&& key: active_coins_registry) { - if (active_list.contains(key)) - { - wallet_config_json_data[key]["active"] = true; - } + wallet_config_json_data[key]["active"] = true; } //! Write From 6b3a3844f826b7aadc23fb8fbf9d70f7261a1e01 Mon Sep 17 00:00:00 2001 From: smk762 Date: Wed, 2 Mar 2022 10:00:46 +0800 Subject: [PATCH 06/13] fs -> file --- src/core/atomicdex/pages/qt.settings.page.cpp | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/core/atomicdex/pages/qt.settings.page.cpp b/src/core/atomicdex/pages/qt.settings.page.cpp index e6bf7f814c..fe01b870db 100644 --- a/src/core/atomicdex/pages/qt.settings.page.cpp +++ b/src/core/atomicdex/pages/qt.settings.page.cpp @@ -589,13 +589,13 @@ namespace atomic_dex { nlohmann::json wallet_config_json_data; std::unordered_set active_coins_registry; - QFile fs; - fs.setFileName(std_path_to_qstring(wallet_cfg_path)); - fs.open(QIODevice::ReadOnly | QIODevice::Text); + QFile file; + file.setFileName(std_path_to_qstring(wallet_cfg_path)); + file.open(QIODevice::ReadOnly | QIODevice::Text); //! Read Contents - wallet_config_json_data = nlohmann::json::parse(QString(fs.readAll()).toStdString()); - fs.close(); + wallet_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); + file.close(); //! Get the active coins for (auto&& [key, value]: wallet_config_json_data.items()) @@ -612,12 +612,12 @@ namespace atomic_dex fs::copy(cfg_path / filename, wallet_cfg_path); //! Open coins file - fs.setFileName(std_path_to_qstring(wallet_cfg_path)); - fs.open(QIODevice::ReadOnly | QIODevice::Text); + file.setFileName(std_path_to_qstring(wallet_cfg_path)); + file.open(QIODevice::ReadOnly | QIODevice::Text); //! Read Contents - wallet_config_json_data = nlohmann::json::parse(QString(fs.readAll()).toStdString()); - fs.close(); + wallet_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); + file.close(); //! set active coins again for (auto&& key: active_coins_registry) @@ -626,30 +626,30 @@ namespace atomic_dex } //! Write - fs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); - fs.write(QString::fromStdString(wallet_config_json_data.dump(4)).toUtf8()); - fs.close(); + file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); + file.write(QString::fromStdString(wallet_config_json_data.dump(4)).toUtf8()); + file.close(); } if (fs::exists(wallet_custom_cfg_path)) { nlohmann::json custom_config_json_data; - QFile fs; - fs.setFileName(std_path_to_qstring(wallet_custom_cfg_path)); - fs.open(QIODevice::ReadOnly | QIODevice::Text); + QFile file; + file.setFileName(std_path_to_qstring(wallet_custom_cfg_path)); + file.open(QIODevice::ReadOnly | QIODevice::Text); //! Read Contents - custom_config_json_data = nlohmann::json::parse(QString(fs.readAll()).toStdString()); - fs.close(); + custom_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); + file.close(); //! Modify for (auto&& [key, value]: custom_config_json_data.items()) { value["active"] = false; } //! Write - fs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); - fs.write(QString::fromStdString(custom_config_json_data.dump()).toUtf8()); - fs.close(); + file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); + file.write(QString::fromStdString(custom_config_json_data.dump()).toUtf8()); + file.close(); } From 2414c92e20a95dfb214de1eb3c038fe233a3dcf1 Mon Sep 17 00:00:00 2001 From: smk762 <35845239+smk762@users.noreply.github.com> Date: Tue, 17 May 2022 20:00:37 +0800 Subject: [PATCH 07/13] code style --- src/core/atomicdex/pages/qt.trading.page.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/core/atomicdex/pages/qt.trading.page.cpp b/src/core/atomicdex/pages/qt.trading.page.cpp index 380f0d6b11..251c3a4215 100644 --- a/src/core/atomicdex/pages/qt.trading.page.cpp +++ b/src/core/atomicdex/pages/qt.trading.page.cpp @@ -194,6 +194,7 @@ namespace atomic_dex req.selected_order_use_input_volume = true; } } + nlohmann::json batch; nlohmann::json buy_request = ::mm2::api::template_request("buy"); ::mm2::api::to_json(buy_request, req); @@ -307,6 +308,7 @@ namespace atomic_dex } auto max_taker_vol_json_obj = get_orderbook_wrapper()->get_base_max_taker_vol().toJsonObject(); + if (is_selected_order) { SPDLOG_INFO( @@ -358,6 +360,7 @@ namespace atomic_dex sell_request["userpass"] = "******"; SPDLOG_INFO("sell request: {}", sell_request.dump(4)); + //! Answer auto answer_functor = [this](web::http::http_response resp) { @@ -476,6 +479,7 @@ namespace atomic_dex { std::error_code ec; t_orderbook_answer result = mm2_system.get_orderbook(ec); + if (!ec) { auto* wrapper = get_orderbook_wrapper(); @@ -594,6 +598,7 @@ namespace atomic_dex const auto* market_selector_mdl = get_market_pairs_mdl(); set_current_orderbook(market_selector_mdl->get_left_selected_coin(), market_selector_mdl->get_right_selected_coin()); emit marketModeChanged(); + if (m_market_mode == MarketMode::Buy) { this->get_orderbook_wrapper()->get_best_orders()->get_orderbook_proxy()->sort(0, Qt::AscendingOrder); @@ -618,6 +623,7 @@ namespace atomic_dex { price = "0"; } + if (m_price != price) { m_price = std::move(price); @@ -675,6 +681,7 @@ namespace atomic_dex emit minTradeVolChanged(); this->set_volume("0"); } + this->set_total_amount("0"); this->set_trading_error(TradingError::None); this->m_preferred_order = std::nullopt; @@ -1034,6 +1041,7 @@ namespace atomic_dex SPDLOG_INFO("preferred_order: {}", preferred_order.dump(-1)); m_preferred_order = std::move(preferred_order); emit prefferedOrderChanged(); + if (!m_preferred_order->empty() && m_preferred_order->contains("price")) { m_preferred_order->operator[]("capped") = false; @@ -1042,6 +1050,7 @@ namespace atomic_dex QString min_vol = QString::fromStdString(utils::format_float(safe_float(m_preferred_order->at("base_min_volume").get()))); this->set_min_trade_vol(min_vol); auto available_quantity = m_preferred_order->at("base_max_volume").get(); + if (this->m_current_trading_mode == TradingModeGadget::Pro) { this->set_volume(QString::fromStdString(utils::extract_large_float(available_quantity))); @@ -1153,11 +1162,13 @@ namespace atomic_dex { std::string body = TO_STD_STR(resp.extract_string(true).get()); SPDLOG_INFO("preimage answer received: {}", body); + if (resp.status_code() == web::http::status_codes::OK) { auto answers = nlohmann::json::parse(body); nlohmann::json answer = answers[0]; auto trade_preimage_answer = ::mm2::api::rpc_process_answer_batch(answer, "trade_preimage"); + if (trade_preimage_answer.error.has_value()) { auto error_answer = trade_preimage_answer.error.value(); @@ -1165,6 +1176,7 @@ namespace atomic_dex fees["error"] = QString::fromStdString(error_answer); this->set_fees(fees); } + if (trade_preimage_answer.result.has_value()) { auto success_answer = trade_preimage_answer.result.value(); @@ -1186,7 +1198,6 @@ namespace atomic_dex fees["fee_to_send_taker_fee"] = QString::fromStdString(utils::adjust_precision(success_answer.fee_to_send_taker_fee.value().amount)); fees["fee_to_send_taker_fee_ticker"] = QString::fromStdString(success_answer.fee_to_send_taker_fee.value().coin); - for (auto&& cur: success_answer.total_fees) { if (!mm2.do_i_have_enough_funds(cur.at("coin").get(), safe_float(cur.at("required_balance").get()))) @@ -1226,6 +1237,7 @@ namespace atomic_dex const bool has_preferred_order = m_preferred_order.has_value(); const bool is_selected_min_max = has_preferred_order && m_preferred_order->at("base_min_volume").get() == m_preferred_order->at("base_max_volume").get(); + if (left_cfg.has_parent_fees_ticker && left_cfg.ticker != "QTUM") { const auto left_fee_cfg = mm2.get_coin_info(left_cfg.fees_ticker); @@ -1250,6 +1262,7 @@ namespace atomic_dex current_trading_error = TradingError::RightParentChainNotEnoughBalance; } } + if (current_trading_error == TradingError::None) { if (max_balance_without_dust < safe_float(regular_min_taker_vol)) //get_left_selected_coin(); const auto& rel = market_selector->get_right_selected_coin(); + if (auto cex_price = QString::fromStdString(price_service.get_cex_rates(base.toStdString(), rel.toStdString())); cex_price != m_cex_price) { m_cex_price = std::move(cex_price); @@ -1490,6 +1504,7 @@ namespace atomic_dex t_float_50 spread = settings.value("Spread", 1.0).toDouble(); t_float_50 min_volume_percent = settings.value("MinVolume", 10.0).toDouble() / 100; ///< min volume is always 10% of the order or more settings.endGroup(); + if (!is_disabled) { SPDLOG_WARN("{}/{} have trading settings - using them", left.toStdString(), right.toStdString()); From 55d49ab9b85bb7723cf41d151fd3db6f10ff7698 Mon Sep 17 00:00:00 2001 From: smk762 Date: Sat, 18 Jun 2022 08:18:44 +0800 Subject: [PATCH 08/13] avoid crash on delist --- src/core/atomicdex/pages/qt.settings.page.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/atomicdex/pages/qt.settings.page.cpp b/src/core/atomicdex/pages/qt.settings.page.cpp index 4658e11147..1c38c0e26b 100644 --- a/src/core/atomicdex/pages/qt.settings.page.cpp +++ b/src/core/atomicdex/pages/qt.settings.page.cpp @@ -614,7 +614,10 @@ namespace atomic_dex //! set active coins again for (auto&& key: active_coins_registry) { - wallet_config_json_data[key]["active"] = true; + if (wallet_config_json_data.contains(key)) + { + wallet_config_json_data[key]["active"] = true; + } } //! Write From b82107ea7db93629a749d46c0dfa51948eb72c52 Mon Sep 17 00:00:00 2001 From: smk762 Date: Sat, 18 Jun 2022 15:33:24 +0800 Subject: [PATCH 09/13] mark active false if enable fails --- src/core/atomicdex/services/mm2/mm2.service.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/core/atomicdex/services/mm2/mm2.service.cpp b/src/core/atomicdex/services/mm2/mm2.service.cpp index d3f0265a8a..a372717f97 100644 --- a/src/core/atomicdex/services/mm2/mm2.service.cpp +++ b/src/core/atomicdex/services/mm2/mm2.service.cpp @@ -709,7 +709,15 @@ namespace atomic_dex } } - for (auto&& t: to_remove) { tickers.erase(std::remove(tickers.begin(), tickers.end(), t), tickers.end()); } + if (!to_remove.empty()) + { + std::vector disable_coins; + for (auto&& t: to_remove) { + tickers.erase(std::remove(tickers.begin(), tickers.end(), t), tickers.end()); + disable_coins.push_back(t); + } + update_coin_status(this->m_current_wallet_name, disable_coins, false, m_coins_informations, m_coin_cfg_mutex); + } if (!tickers.empty()) { @@ -725,6 +733,7 @@ namespace atomic_dex fetch_single_balance(get_coin_info(tickers[0])); } // batch_balance_and_tx(false, tickers, true); + update_coin_status(this->m_current_wallet_name, tickers, true, m_coins_informations, m_coin_cfg_mutex); } } } @@ -766,7 +775,6 @@ namespace atomic_dex mm2_service::enable_multiple_coins(const std::vector& tickers) { batch_enable_coins(tickers); - update_coin_status(this->m_current_wallet_name, tickers, true, m_coins_informations, m_coin_cfg_mutex); } coin_config From 167475b7f4eb78bb09601f7cbc35d1387e7bb238 Mon Sep 17 00:00:00 2001 From: smk762 Date: Tue, 6 Sep 2022 20:14:43 +0800 Subject: [PATCH 10/13] move restart after coins reset to backend --- .../Dex/Components/RestartModal.qml | 1 - atomic_defi_design/Dex/Screens/Dashboard.qml | 2 +- .../Dex/Settings/SettingModal.qml | 2 +- atomic_defi_design/Dex/Settings/Settings.qml | 2 +- src/app/app.cpp | 120 ++++++++++++++++++ src/app/app.hpp | 1 + src/core/atomicdex/pages/qt.settings.page.cpp | 118 ----------------- src/core/atomicdex/pages/qt.settings.page.hpp | 1 - 8 files changed, 124 insertions(+), 123 deletions(-) diff --git a/atomic_defi_design/Dex/Components/RestartModal.qml b/atomic_defi_design/Dex/Components/RestartModal.qml index c2cef491be..b5907d7a55 100644 --- a/atomic_defi_design/Dex/Components/RestartModal.qml +++ b/atomic_defi_design/Dex/Components/RestartModal.qml @@ -29,7 +29,6 @@ MultipageModal console.log("Restarting the application...") _restartTimer.stop() onTimerEnded() - API.app.restart() } } diff --git a/atomic_defi_design/Dex/Screens/Dashboard.qml b/atomic_defi_design/Dex/Screens/Dashboard.qml index d59e163876..05bbebbe69 100644 --- a/atomic_defi_design/Dex/Screens/Dashboard.qml +++ b/atomic_defi_design/Dex/Screens/Dashboard.qml @@ -96,7 +96,7 @@ Item if (API.app.portfolio_pg.portfolio_mdl.length > atomic_settings2.value("MaximumNbCoinsEnabled")) { open() onTimerEnded = () => { - API.app.settings_pg.reset_coin_cfg() + API.app.reset_coin_cfg() } } } diff --git a/atomic_defi_design/Dex/Settings/SettingModal.qml b/atomic_defi_design/Dex/Settings/SettingModal.qml index 51cadd0818..459f7ec862 100644 --- a/atomic_defi_design/Dex/Settings/SettingModal.qml +++ b/atomic_defi_design/Dex/Settings/SettingModal.qml @@ -292,7 +292,7 @@ Qaterial.Dialog restart_modal.open() restart_modal.item.onTimerEnded = () => { - API.app.settings_pg.reset_coin_cfg() + API.app.reset_coin_cfg() } } }) diff --git a/atomic_defi_design/Dex/Settings/Settings.qml b/atomic_defi_design/Dex/Settings/Settings.qml index bbf6594176..e4eac7af09 100644 --- a/atomic_defi_design/Dex/Settings/Settings.qml +++ b/atomic_defi_design/Dex/Settings/Settings.qml @@ -225,7 +225,7 @@ Item { text: qsTr("Reset wallet configuration") onClicked: { restart_modal.open() - restart_modal.item.onTimerEnded = () => { API.app.settings_pg.reset_coin_cfg() } + restart_modal.item.onTimerEnded = () => { API.app.reset_coin_cfg() } } } diff --git a/src/app/app.cpp b/src/app/app.cpp index 07922b0fcc..f76598c19b 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -179,6 +179,126 @@ namespace atomic_dex return qt_wallet_manager::get_wallets().empty(); } + + void atomic_dex::application::reset_coin_cfg() + { + using namespace std::string_literals; + const std::string wallet_name = qt_wallet_manager::get_default_wallet_name().toStdString(); + const std::string wallet_cfg_file = std::string(atomic_dex::get_raw_version()) + "-coins"s + "."s + wallet_name + ".json"s; + std::string wallet_custom_cfg_filename = "custom-tokens."s + wallet_name + ".json"s; + const fs::path wallet_custom_cfg_path{utils::get_atomic_dex_config_folder() / wallet_custom_cfg_filename}; + const fs::path wallet_cfg_path{utils::get_atomic_dex_config_folder() / wallet_cfg_file}; + const fs::path mm2_coins_file_path{atomic_dex::utils::get_current_configs_path() / "coins.json"}; + const fs::path ini_file_path = atomic_dex::utils::get_current_configs_path() / "cfg.ini"; + const fs::path cfg_json_file_path = atomic_dex::utils::get_current_configs_path() / "cfg.json"; + const fs::path logo_path = atomic_dex::utils::get_logo_path(); + const fs::path theme_path = atomic_dex::utils::get_themes_path(); + + const auto functor_remove = [](auto&& path_to_remove) + { + if (fs::exists(path_to_remove)) + { + fs_error_code ec; + if (fs::is_directory(path_to_remove)) + { + fs::remove_all(path_to_remove, ec); + } + else + { + fs::remove(path_to_remove, ec); + } + if (ec) + { + LOG_PATH("error when removing {}", path_to_remove); + SPDLOG_ERROR("error: {}", ec.message()); + } + else + { + LOG_PATH("Successfully removed {}", path_to_remove); + } + } + }; + + if (fs::exists(wallet_cfg_path)) + { + nlohmann::json wallet_config_json_data; + std::unordered_set active_coins_registry; + QFile file; + file.setFileName(std_path_to_qstring(wallet_cfg_path)); + file.open(QIODevice::ReadOnly | QIODevice::Text); + + //! Read Contents + wallet_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); + file.close(); + + //! Get the active coins + for (auto&& [key, value]: wallet_config_json_data.items()) + { + if (value["active"]) { active_coins_registry.insert(key); } + } + + // remove old coins file + functor_remove(std::move(wallet_cfg_path)); + + //! Copy default coins file + const auto cfg_path = ag::core::assets_real_path() / "config"; + std::string filename = std::string(atomic_dex::get_raw_version()) + "-coins.json"; + fs::copy(cfg_path / filename, wallet_cfg_path); + + //! Open coins file + file.setFileName(std_path_to_qstring(wallet_cfg_path)); + file.open(QIODevice::ReadOnly | QIODevice::Text); + + //! Read Contents + wallet_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); + file.close(); + + //! set active coins again + int i = 0; + for (auto&& key: active_coins_registry) + { + if (wallet_config_json_data.contains(key)) + { + i = i + 1; + wallet_config_json_data[key]["active"] = true; + } + } + + //! Write + //SPDLOG_DEBUG("Data written: ", wallet_config_json_data.dump(4)); + file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); + file.write(QString::fromStdString(wallet_config_json_data.dump(4)).toUtf8()); + file.close(); + } + + if (fs::exists(wallet_custom_cfg_path)) + { + nlohmann::json custom_config_json_data; + QFile file; + file.setFileName(std_path_to_qstring(wallet_custom_cfg_path)); + file.open(QIODevice::ReadOnly | QIODevice::Text); + + //! Read Contents + custom_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); + file.close(); + + //! Modify + for (auto&& [key, value]: custom_config_json_data.items()) { value["active"] = false; } + + //! Write + file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); + file.write(QString::fromStdString(custom_config_json_data.dump()).toUtf8()); + file.close(); + } + functor_remove(std::move(mm2_coins_file_path)); + functor_remove(std::move(cfg_json_file_path)); + functor_remove(std::move(logo_path)); + functor_remove(std::move(theme_path)); + // Uncomment if you want to reset fiat/language/theme + // functor_remove(std::move(ini_file_path)); + atomic_dex::application::restart(); + } + void application::launch() { SPDLOG_INFO("Launch the application"); diff --git a/src/app/app.hpp b/src/app/app.hpp index 5ad906ff54..1be05f46d5 100644 --- a/src/app/app.hpp +++ b/src/app/app.hpp @@ -151,6 +151,7 @@ namespace atomic_dex //! Portfolio QML API Bindings Q_INVOKABLE QString recover_fund(const QString& uuid); + Q_INVOKABLE void reset_coin_cfg(); Q_INVOKABLE void refresh_orders_and_swaps(); Q_INVOKABLE static QString get_mnemonic(); Q_INVOKABLE static bool first_run(); diff --git a/src/core/atomicdex/pages/qt.settings.page.cpp b/src/core/atomicdex/pages/qt.settings.page.cpp index f75f7f2298..252fb840bf 100644 --- a/src/core/atomicdex/pages/qt.settings.page.cpp +++ b/src/core/atomicdex/pages/qt.settings.page.cpp @@ -542,124 +542,6 @@ namespace atomic_dex m_qml_engine = engine; } - void settings_page::reset_coin_cfg() - { - using namespace std::string_literals; - const std::string wallet_name = qt_wallet_manager::get_default_wallet_name().toStdString(); - const std::string wallet_cfg_file = std::string(atomic_dex::get_raw_version()) + "-coins"s + "."s + wallet_name + ".json"s; - std::string wallet_custom_cfg_filename = "custom-tokens."s + wallet_name + ".json"s; - const fs::path wallet_custom_cfg_path{utils::get_atomic_dex_config_folder() / wallet_custom_cfg_filename}; - const fs::path wallet_cfg_path{utils::get_atomic_dex_config_folder() / wallet_cfg_file}; - const fs::path mm2_coins_file_path{atomic_dex::utils::get_current_configs_path() / "coins.json"}; - const fs::path ini_file_path = atomic_dex::utils::get_current_configs_path() / "cfg.ini"; - const fs::path cfg_json_file_path = atomic_dex::utils::get_current_configs_path() / "cfg.json"; - const fs::path logo_path = atomic_dex::utils::get_logo_path(); - const fs::path theme_path = atomic_dex::utils::get_themes_path(); - - const auto functor_remove = [](auto&& path_to_remove) - { - if (fs::exists(path_to_remove)) - { - fs_error_code ec; - if (fs::is_directory(path_to_remove)) - { - fs::remove_all(path_to_remove, ec); - } - else - { - fs::remove(path_to_remove, ec); - } - if (ec) - { - LOG_PATH("error when removing {}", path_to_remove); - SPDLOG_ERROR("error: {}", ec.message()); - } - else - { - LOG_PATH("Successfully removed {}", path_to_remove); - } - } - }; - - if (fs::exists(wallet_cfg_path)) - { - nlohmann::json wallet_config_json_data; - std::unordered_set active_coins_registry; - QFile file; - file.setFileName(std_path_to_qstring(wallet_cfg_path)); - file.open(QIODevice::ReadOnly | QIODevice::Text); - - //! Read Contents - wallet_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); - file.close(); - - //! Get the active coins - for (auto&& [key, value]: wallet_config_json_data.items()) - { - if (value["active"]) { active_coins_registry.insert(key); } - } - - // remove old coins file - functor_remove(std::move(wallet_cfg_path)); - - //! Copy default coins file - const auto cfg_path = ag::core::assets_real_path() / "config"; - std::string filename = std::string(atomic_dex::get_raw_version()) + "-coins.json"; - fs::copy(cfg_path / filename, wallet_cfg_path); - - //! Open coins file - file.setFileName(std_path_to_qstring(wallet_cfg_path)); - file.open(QIODevice::ReadOnly | QIODevice::Text); - - //! Read Contents - wallet_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); - file.close(); - - //! set active coins again - for (auto&& key: active_coins_registry) - { - if (wallet_config_json_data.contains(key)) - { - wallet_config_json_data[key]["active"] = true; - } - } - - //! Write - file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); - file.write(QString::fromStdString(wallet_config_json_data.dump(4)).toUtf8()); - file.close(); - } - - - if (fs::exists(wallet_custom_cfg_path)) - { - nlohmann::json custom_config_json_data; - QFile file; - file.setFileName(std_path_to_qstring(wallet_custom_cfg_path)); - file.open(QIODevice::ReadOnly | QIODevice::Text); - - //! Read Contents - custom_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); - file.close(); - - //! Modify - for (auto&& [key, value]: custom_config_json_data.items()) { value["active"] = false; } - - //! Write - file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); - file.write(QString::fromStdString(custom_config_json_data.dump()).toUtf8()); - file.close(); - } - - - functor_remove(std::move(mm2_coins_file_path)); - functor_remove(std::move(ini_file_path)); - functor_remove(std::move(cfg_json_file_path)); - functor_remove(std::move(logo_path)); - functor_remove(std::move(theme_path)); - - } - QStringList settings_page::retrieve_seed(const QString& wallet_name, const QString& password) { QStringList out; diff --git a/src/core/atomicdex/pages/qt.settings.page.hpp b/src/core/atomicdex/pages/qt.settings.page.hpp index 444d5e9247..d1dd6712cb 100644 --- a/src/core/atomicdex/pages/qt.settings.page.hpp +++ b/src/core/atomicdex/pages/qt.settings.page.hpp @@ -108,7 +108,6 @@ namespace atomic_dex Q_INVOKABLE void process_token_add(const QString& contract_address, const QString& coingecko_id, const QString& icon_filepath, CoinType coin_type); Q_INVOKABLE void process_qrc_20_token_add(const QString& contract_address, const QString& coingecko_id, const QString& icon_filepath); Q_INVOKABLE void submit(); - Q_INVOKABLE void reset_coin_cfg(); Q_INVOKABLE QStringList retrieve_seed(const QString& wallet_name, const QString& password); Q_INVOKABLE static QString get_mm2_version(); Q_INVOKABLE static QString get_log_folder(); From a3b07288c956fb2073d928e935e096ed574017e3 Mon Sep 17 00:00:00 2001 From: smk762 Date: Wed, 7 Sep 2022 04:40:35 +0800 Subject: [PATCH 11/13] attempt edge crash fix --- src/app/app.cpp | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index f76598c19b..c1f83b4ea3 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -179,7 +179,6 @@ namespace atomic_dex return qt_wallet_manager::get_wallets().empty(); } - void atomic_dex::application::reset_coin_cfg() { using namespace std::string_literals; @@ -221,18 +220,18 @@ namespace atomic_dex if (fs::exists(wallet_cfg_path)) { - nlohmann::json wallet_config_json_data; + nlohmann::json coin_config_json_data; std::unordered_set active_coins_registry; - QFile file; - file.setFileName(std_path_to_qstring(wallet_cfg_path)); - file.open(QIODevice::ReadOnly | QIODevice::Text); + QFile coins_file; + coins_file.setFileName(std_path_to_qstring(wallet_cfg_path)); + coins_file.open(QIODevice::ReadOnly | QIODevice::Text); //! Read Contents - wallet_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); - file.close(); + coin_config_json_data = nlohmann::json::parse(QString(coins_file.readAll()).toStdString()); + coins_file.close(); //! Get the active coins - for (auto&& [key, value]: wallet_config_json_data.items()) + for (auto&& [key, value]: coin_config_json_data.items()) { if (value["active"]) { active_coins_registry.insert(key); } } @@ -244,31 +243,39 @@ namespace atomic_dex const auto cfg_path = ag::core::assets_real_path() / "config"; std::string filename = std::string(atomic_dex::get_raw_version()) + "-coins.json"; fs::copy(cfg_path / filename, wallet_cfg_path); + QFile default_coins_file; //! Open coins file - file.setFileName(std_path_to_qstring(wallet_cfg_path)); - file.open(QIODevice::ReadOnly | QIODevice::Text); + default_coins_file.setFileName(std_path_to_qstring(wallet_cfg_path)); + default_coins_file.open(QIODevice::ReadOnly | QIODevice::Text); - //! Read Contents - wallet_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); - file.close(); + //! Read default coins contents + nlohmann::json default_coin_config_json_data; + default_coin_config_json_data = nlohmann::json::parse(QString(default_coins_file.readAll()).toStdString()); + default_coins_file.close(); //! set active coins again - int i = 0; for (auto&& key: active_coins_registry) { - if (wallet_config_json_data.contains(key)) + try { - i = i + 1; - wallet_config_json_data[key]["active"] = true; + if (default_coin_config_json_data.contains(key)) + { + default_coin_config_json_data[key]["active"] = true; + } + } + catch (const std::exception& error) + { + SPDLOG_ERROR("Exception caught: {}", error.what()); } } //! Write - //SPDLOG_DEBUG("Data written: ", wallet_config_json_data.dump(4)); - file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); - file.write(QString::fromStdString(wallet_config_json_data.dump(4)).toUtf8()); - file.close(); + QFile output_coins_file; + //SPDLOG_DEBUG("Data written: ", default_coin_config_json_data.dump(4)); + output_coins_file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); + output_coins_file.write(QString::fromStdString(default_coin_config_json_data.dump(4)).toUtf8()); + output_coins_file.close(); } if (fs::exists(wallet_custom_cfg_path)) From 494befc18cdec59b80027effa59b891e4b5aa655 Mon Sep 17 00:00:00 2001 From: smk762 Date: Tue, 13 Sep 2022 19:57:12 +0800 Subject: [PATCH 12/13] rm log, fix file write --- src/app/app.cpp | 1 + src/core/atomicdex/services/mm2/mm2.service.cpp | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 68e89e175e..4398a76a17 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -287,6 +287,7 @@ namespace atomic_dex //! Write QFile output_coins_file; //SPDLOG_DEBUG("Data written: ", default_coin_config_json_data.dump(4)); + output_coins_file.setFileName(std_path_to_qstring(wallet_cfg_path)); output_coins_file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); output_coins_file.write(QString::fromStdString(default_coin_config_json_data.dump(4)).toUtf8()); output_coins_file.close(); diff --git a/src/core/atomicdex/services/mm2/mm2.service.cpp b/src/core/atomicdex/services/mm2/mm2.service.cpp index 5c0f92694a..3b0469aecd 100644 --- a/src/core/atomicdex/services/mm2/mm2.service.cpp +++ b/src/core/atomicdex/services/mm2/mm2.service.cpp @@ -682,7 +682,6 @@ namespace atomic_dex void mm2_service::process_enable_zhtlc(std::vector coins) { - SPDLOG_DEBUG("[process_enable_zhtlc] ==========================================================================================="); dispatcher_.trigger(); auto request_functor = [this](coin_config coin_info) -> std::pair> { From 41cd2706ccdfa3c6ff9c2a3a38216099226217af Mon Sep 17 00:00:00 2001 From: smk762 Date: Tue, 13 Sep 2022 22:54:01 +0800 Subject: [PATCH 13/13] detect and repair invalid json --- src/app/app.cpp | 11 +-- .../atomicdex/managers/qt.wallet.manager.cpp | 15 +++- .../atomicdex/services/mm2/mm2.service.cpp | 70 ++++--------------- .../atomicdex/utilities/global.utilities.cpp | 26 +++++++ .../atomicdex/utilities/global.utilities.hpp | 2 + 5 files changed, 59 insertions(+), 65 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 4398a76a17..f3ac6b59b5 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -295,19 +295,14 @@ namespace atomic_dex if (fs::exists(wallet_custom_cfg_path)) { - nlohmann::json custom_config_json_data; - QFile file; - file.setFileName(std_path_to_qstring(wallet_custom_cfg_path)); - file.open(QIODevice::ReadOnly | QIODevice::Text); - - //! Read Contents - custom_config_json_data = nlohmann::json::parse(QString(file.readAll()).toStdString()); - file.close(); + nlohmann::json custom_config_json_data = utils::read_json_file(wallet_custom_cfg_path); //! Modify for (auto&& [key, value]: custom_config_json_data.items()) { value["active"] = false; } //! Write + QFile file; + file.setFileName(std_path_to_qstring(wallet_custom_cfg_path)); file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); file.write(QString::fromStdString(custom_config_json_data.dump()).toUtf8()); file.close(); diff --git a/src/core/atomicdex/managers/qt.wallet.manager.cpp b/src/core/atomicdex/managers/qt.wallet.manager.cpp index c2265debe8..d5844cef06 100644 --- a/src/core/atomicdex/managers/qt.wallet.manager.cpp +++ b/src/core/atomicdex/managers/qt.wallet.manager.cpp @@ -300,12 +300,23 @@ namespace atomic_dex const std::string wallet_cfg_file = std::string(atomic_dex::get_raw_version()) + "-coins"s + "."s + wallet_name.toStdString() + ".json"s; const fs::path wallet_cfg_path = utils::get_atomic_dex_config_folder() / wallet_cfg_file; + bool valid_json = false; - if (not fs::exists(wallet_cfg_path)) + if (fs::exists(wallet_cfg_path)) + { + QFile ifs; + ifs.setFileName(std_path_to_qstring(wallet_cfg_path)); + ifs.open(QIODevice::ReadOnly | QIODevice::Text); + std::string json_data = QString(ifs.readAll()).toUtf8().constData(); + valid_json = nlohmann::json::accept(json_data); + ifs.close(); + } + + if (!valid_json) { const auto cfg_path = ag::core::assets_real_path() / "config"; std::string filename = std::string(atomic_dex::get_raw_version()) + "-coins.json"; - fs::copy(cfg_path / filename, wallet_cfg_path); + fs::copy(cfg_path / filename, wallet_cfg_path, fs::copy_options::overwrite_existing); } const fs::path seed_path = utils::get_atomic_dex_config_folder() / (wallet_name.toStdString() + ".seed"s); diff --git a/src/core/atomicdex/services/mm2/mm2.service.cpp b/src/core/atomicdex/services/mm2/mm2.service.cpp index 3b0469aecd..08acee03e8 100644 --- a/src/core/atomicdex/services/mm2/mm2.service.cpp +++ b/src/core/atomicdex/services/mm2/mm2.service.cpp @@ -148,23 +148,9 @@ namespace std::string custom_tokens_filename = "custom-tokens." + wallet_name + ".json"; fs::path custom_tokens_filepath = cfg_path / custom_tokens_filename; - QFile ifs; - ifs.setFileName(atomic_dex::std_path_to_qstring((cfg_path / filename))); - ifs.open(QIODevice::ReadOnly | QIODevice::Text); + nlohmann::json config_json_data = atomic_dex::utils::read_json_file(cfg_path / filename); + nlohmann::json custom_cfg_data = atomic_dex::utils::read_json_file(custom_tokens_filepath); - nlohmann::json config_json_data; - nlohmann::json custom_cfg_data; - - if (fs::exists(custom_tokens_filepath.c_str())) - { - QFile ifs_custom; - ifs_custom.setFileName(atomic_dex::std_path_to_qstring(custom_tokens_filepath)); - ifs_custom.open(QIODevice::ReadOnly | QIODevice::Text); - custom_cfg_data = nlohmann::json::parse(QString(ifs_custom.readAll()).toStdString()); - ifs_custom.close(); - } - - config_json_data = nlohmann::json::parse(QString(ifs.readAll()).toStdString()); { std::shared_lock lock(registry_mtx); for (auto&& ticker: tickers) @@ -189,8 +175,6 @@ namespace } } - ifs.close(); - //! Write contents QFile ofs; ofs.setFileName(atomic_dex::std_path_to_qstring((cfg_path / filename))); @@ -231,10 +215,7 @@ namespace atomic_dex { try { - QFile ifs; - ifs.setFileName(atomic_dex::std_path_to_qstring(path)); - ifs.open(QIODevice::ReadOnly | QIODevice::Text); - nlohmann::json config_json_data = nlohmann::json::parse(QString(ifs.readAll()).toStdString()); + nlohmann::json config_json_data = atomic_dex::utils::read_json_file(path); auto res = config_json_data.get>(); return res; } @@ -2005,19 +1986,9 @@ namespace atomic_dex fs::path cfg_path = utils::get_atomic_dex_config_folder(); std::string filename = "custom-tokens." + m_current_wallet_name + ".json"; fs::path file_path = cfg_path / filename; - nlohmann::json config_json_data; - if (fs::exists(file_path)) - { - SPDLOG_DEBUG("reading contents of custom tokens cfg"); - QFile ifs; - ifs.setFileName(std_path_to_qstring(file_path)); - ifs.open(QIODevice::Text | QIODevice::ReadOnly); - - //! Read Contents - config_json_data = nlohmann::json::parse(QString(ifs.readAll()).toStdString()); - ifs.close(); - } + SPDLOG_DEBUG("reading contents of custom tokens cfg"); + nlohmann::json config_json_data = atomic_dex::utils::read_json_file(file_path); //! Modify contents config_json_data[coin_cfg_json.begin().key()] = coin_cfg_json.at(coin_cfg_json.begin().key()); @@ -2031,15 +2002,13 @@ namespace atomic_dex } if (not raw_coin_cfg_json.empty() && not is_this_ticker_present_in_raw_cfg(raw_coin_cfg_json.at("coin").get())) { - const fs::path mm2_cfg_path{atomic_dex::utils::get_current_configs_path() / "coins.json"}; - SPDLOG_DEBUG("Adding entry : {} to mm2 coins file {}", raw_coin_cfg_json.dump(4), mm2_cfg_path.string()); + const fs::path coins_json_path{atomic_dex::utils::get_current_configs_path() / "coins.json"}; + SPDLOG_DEBUG("Adding entry : {} to mm2 coins file {}", raw_coin_cfg_json.dump(4), coins_json_path.string()); QFile ifs; - ifs.setFileName(std_path_to_qstring(mm2_cfg_path)); + ifs.setFileName(std_path_to_qstring(coins_json_path)); ifs.open(QIODevice::ReadOnly | QIODevice::Text); - nlohmann::json config_json_data; - //! Read Contents - config_json_data = nlohmann::json::parse(QString(ifs.readAll()).toStdString()); + nlohmann::json config_json_data = atomic_dex::utils::read_json_file(coins_json_path); //! Modify contents config_json_data.push_back(raw_coin_cfg_json); @@ -2049,7 +2018,7 @@ namespace atomic_dex //! Write contents QFile ofs; - ofs.setFileName(std_path_to_qstring(mm2_cfg_path)); + ofs.setFileName(std_path_to_qstring(coins_json_path)); ofs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); ofs.write(QString::fromStdString(config_json_data.dump()).toUtf8()); ofs.close(); @@ -2082,25 +2051,16 @@ namespace atomic_dex SPDLOG_DEBUG("remove it from custom cfg: {}", ticker); fs::path cfg_path = utils::get_atomic_dex_config_folder(); std::string filename = "custom-tokens." + m_current_wallet_name + ".json"; - QFile ifs; - ifs.setFileName(std_path_to_qstring((cfg_path / filename))); - ifs.open(QIODevice::ReadOnly | QIODevice::Text); - nlohmann::json config_json_data; - - //! Read Contents - config_json_data = nlohmann::json::parse(QString(ifs.readAll()).toStdString()); + SPDLOG_DEBUG("reading contents of custom tokens cfg"); + nlohmann::json config_json_data = atomic_dex::utils::read_json_file(cfg_path / filename); { std::unique_lock lock(m_coin_cfg_mutex); this->m_coins_informations.erase(ticker); } - config_json_data.erase(config_json_data.find(ticker)); - //! Close - ifs.close(); - //! Write contents QFile ofs; ofs.setFileName(std_path_to_qstring((cfg_path / filename))); @@ -2112,9 +2072,9 @@ namespace atomic_dex if (is_this_ticker_present_in_raw_cfg(ticker)) { SPDLOG_DEBUG("remove it from mm2 cfg: {}", ticker); - fs::path mm2_cfg_path{atomic_dex::utils::get_current_configs_path() / "coins.json"}; + fs::path coins_json_path{atomic_dex::utils::get_current_configs_path() / "coins.json"}; QFile ifs; - ifs.setFileName(std_path_to_qstring(mm2_cfg_path)); + ifs.setFileName(std_path_to_qstring(coins_json_path)); ifs.open(QIODevice::ReadOnly | QIODevice::Text); nlohmann::json config_json_data; @@ -2130,7 +2090,7 @@ namespace atomic_dex //! Write contents QFile ofs; - ofs.setFileName(std_path_to_qstring(mm2_cfg_path)); + ofs.setFileName(std_path_to_qstring(coins_json_path)); ofs.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate); ofs.write(QString::fromStdString(config_json_data.dump()).toUtf8()); ofs.close(); diff --git a/src/core/atomicdex/utilities/global.utilities.cpp b/src/core/atomicdex/utilities/global.utilities.cpp index 85d078a04b..6be2ed91b2 100644 --- a/src/core/atomicdex/utilities/global.utilities.cpp +++ b/src/core/atomicdex/utilities/global.utilities.cpp @@ -10,6 +10,8 @@ //! Qt Headers #include #include +#include + //! Project Headers #include "atomicdex/utilities/global.utilities.hpp" @@ -306,4 +308,28 @@ namespace atomic_dex::utils } return out; } + + nlohmann::json + read_json_file(fs::path filepath) + { + nlohmann::json valid_json_data; + + if (fs::exists(filepath)) + { + QFile ifs; +#if defined(_WIN32) || defined(WIN32) + ifs.setFileName(QString::fromStdWString(filepath.wstring())); +#else + ifs.setFileName(QString::fromStdString(filepath.string())); +#endif + ifs.open(QIODevice::ReadOnly | QIODevice::Text); + std::string json_str = QString(ifs.readAll()).toUtf8().constData(); + if (nlohmann::json::accept(json_str)) + { + valid_json_data = nlohmann::json::parse(json_str); + } + ifs.close(); + } + return valid_json_data; + } } // namespace atomic_dex::utils diff --git a/src/core/atomicdex/utilities/global.utilities.hpp b/src/core/atomicdex/utilities/global.utilities.hpp index 185fcfea12..bfdce6af17 100644 --- a/src/core/atomicdex/utilities/global.utilities.hpp +++ b/src/core/atomicdex/utilities/global.utilities.hpp @@ -84,6 +84,8 @@ namespace atomic_dex::utils fs::path get_atomic_dex_config_folder(); + nlohmann::json read_json_file(fs::path filepath); + //std::string minimal_trade_amount_str(); //const t_float_50 minimal_trade_amount();