Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep active on restart #1632

Merged
merged 24 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion atomic_defi_design/Dex/Components/RestartModal.qml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ MultipageModal
console.log("Restarting the application...")
_restartTimer.stop()
onTimerEnded()
API.app.restart()
}
}

Expand Down
2 changes: 1 addition & 1 deletion atomic_defi_design/Dex/Screens/Dashboard.qml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,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()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion atomic_defi_design/Dex/Settings/SettingModal.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
})
Expand Down
123 changes: 123 additions & 0 deletions src/app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,129 @@ 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))
{
std::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 coin_config_json_data;
std::unordered_set<std::string> active_coins_registry;
QFile coins_file;
coins_file.setFileName(std_path_to_qstring(wallet_cfg_path));
coins_file.open(QIODevice::ReadOnly | QIODevice::Text);

//! Read Contents
coin_config_json_data = nlohmann::json::parse(QString(coins_file.readAll()).toStdString());
coins_file.close();

//! Get the active coins
for (auto&& [key, value]: coin_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);
QFile default_coins_file;

//! Open coins file
default_coins_file.setFileName(std_path_to_qstring(wallet_cfg_path));
default_coins_file.open(QIODevice::ReadOnly | QIODevice::Text);

//! 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
for (auto&& key: active_coins_registry)
{
try
{
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
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();
}

if (fs::exists(wallet_custom_cfg_path))
{
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();
}
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");
Expand Down
1 change: 1 addition & 0 deletions src/app/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,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();
Expand Down
1 change: 0 additions & 1 deletion src/core/atomicdex/managers/qt.wallet.manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ namespace atomic_dex
ifs.open(QIODevice::ReadOnly | QIODevice::Text);
std::string json_data = QString(ifs.readAll()).toUtf8().constData();
valid_json = nlohmann::json::accept(json_data);

ifs.close();
}

Expand Down
68 changes: 0 additions & 68 deletions src/core/atomicdex/pages/qt.settings.page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,74 +547,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 std::filesystem::path wallet_custom_cfg_path{utils::get_atomic_dex_config_folder() / wallet_custom_cfg_filename};
const std::filesystem::path wallet_cfg_path{utils::get_atomic_dex_config_folder() / wallet_cfg_file};
const std::filesystem::path mm2_coins_file_path{atomic_dex::utils::get_current_configs_path() / "coins.json"};
const std::filesystem::path ini_file_path = atomic_dex::utils::get_current_configs_path() / "cfg.ini";
const std::filesystem::path cfg_json_file_path = atomic_dex::utils::get_current_configs_path() / "cfg.json";
const std::filesystem::path logo_path = atomic_dex::utils::get_logo_path();
const std::filesystem::path theme_path = atomic_dex::utils::get_themes_path();


if (std::filesystem::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 (std::filesystem::exists(path_to_remove))
{
std::error_code ec;
if (std::filesystem::is_directory(path_to_remove))
{
std::filesystem::remove_all(path_to_remove, ec);
}
else
{
std::filesystem::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);
}
}
};

functor_remove(std::move(wallet_cfg_path));
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;
Expand Down
1 change: 0 additions & 1 deletion src/core/atomicdex/pages/qt.settings.page.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 15 additions & 10 deletions src/core/atomicdex/pages/qt.trading.page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ namespace atomic_dex
req.selected_order_use_input_volume = true;
}
}

nlohmann::json batch;
nlohmann::json buy_request = mm2::template_request("buy");
mm2::to_json(buy_request, req);
Expand Down Expand Up @@ -313,6 +314,7 @@ namespace atomic_dex
}

auto max_taker_vol_json_obj = get_orderbook_wrapper()->get_base_max_taker_vol().toJsonObject();

if (is_selected_order)
{
SPDLOG_DEBUG(
Expand Down Expand Up @@ -364,6 +366,7 @@ namespace atomic_dex

sell_request["userpass"] = "******";
SPDLOG_DEBUG("sell request: {}", sell_request.dump(4));

//! Answer
auto answer_functor = [this](web::http::http_response resp)
{
Expand Down Expand Up @@ -482,6 +485,7 @@ namespace atomic_dex
{
std::error_code ec;
t_orderbook_answer result = mm2_system.get_orderbook(ec);

if (!ec)
{
auto* wrapper = get_orderbook_wrapper();
Expand Down Expand Up @@ -601,6 +605,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);
Expand All @@ -625,6 +630,7 @@ namespace atomic_dex
{
price = "0";
}

if (m_price != price)
{
m_price = std::move(price);
Expand Down Expand Up @@ -682,6 +688,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;
Expand Down Expand Up @@ -1057,17 +1064,12 @@ namespace atomic_dex
this->determine_max_volume();
QString min_vol = QString::fromStdString(utils::format_float(safe_float(m_preferred_order->at("base_min_volume").get<std::string>())));
this->set_min_trade_vol(min_vol);
auto available_quantity = m_preferred_order->at("base_max_volume").get<std::string>();

if (this->m_current_trading_mode == TradingModeGadget::Pro)
{
m_preferred_order->operator[]("capped") = false;
this->set_price(QString::fromStdString(utils::format_float(safe_float(m_preferred_order->at("price").get<std::string>()))));
this->determine_max_volume();
QString min_vol = QString::fromStdString(utils::format_float(safe_float(m_preferred_order->at("base_min_volume").get<std::string>())));
this->set_min_trade_vol(min_vol);
auto available_quantity = m_preferred_order->at("base_max_volume").get<std::string>();
{
if (this->m_current_trading_mode == TradingModeGadget::Pro)
{
auto available_quantity = m_preferred_order->at("base_max_volume").get<std::string>();
this->set_volume(QString::fromStdString(utils::extract_large_float(available_quantity)));
}
this->get_orderbook_wrapper()->refresh_best_orders();
Expand Down Expand Up @@ -1199,7 +1201,7 @@ namespace atomic_dex
auto answer_functor = [this, &mm2](web::http::http_response resp)
{
std::string body = TO_STD_STR(resp.extract_string(true).get());
SPDLOG_DEBUG("trade_preimage answer received: {}", body);
SPDLOG_INFO("[determine_fees] trade_preimage answer received: {}", body);
if (resp.status_code() == web::http::status_codes::OK)
{
auto answers = nlohmann::json::parse(body);
Expand All @@ -1212,6 +1214,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();
Expand All @@ -1233,7 +1236,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<std::string>(), safe_float(cur.at("required_balance").get<std::string>())))
Expand Down Expand Up @@ -1273,6 +1275,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<std::string>() == m_preferred_order->at("base_max_volume").get<std::string>();

if (left_cfg.has_parent_fees_ticker && left_cfg.ticker != "QTUM")
{
const auto left_fee_cfg = mm2.get_coin_info(left_cfg.fees_ticker);
Expand Down Expand Up @@ -1348,6 +1351,7 @@ namespace atomic_dex
const auto* market_selector = get_market_pairs_mdl();
const auto& base = market_selector->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);
Expand Down Expand Up @@ -1558,6 +1562,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());
Expand Down
Loading