Skip to content

Commit

Permalink
Merge pull request #2115 from KomodoPlatform/trade_error_refactor
Browse files Browse the repository at this point in the history
Tweak trade errors
  • Loading branch information
smk762 authored Dec 2, 2022
2 parents 06f1c20 + b841c10 commit 530b4cf
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 66 deletions.
36 changes: 28 additions & 8 deletions atomic_defi_design/Dex/Constants/General.qml
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ QtObject {
return coin_info.is_zhtlc_family
}

function isZhtlcReady(ticker, progress=100)
function isZhtlcReady(ticker)
{
if (!isZhtlc(ticker)) return true
let activation_status = API.app.get_zhtlc_status(ticker)
let progress = zhtlcActivationProgress(activation_status, ticker)
if (progress == 100) return true
const coin_info = API.app.portfolio_pg.global_cfg_mdl.get_coin_info(ticker)
if (!coin_info.is_zhtlc_family) return true
console.log("Progress: " + progress)
return false
}

Expand Down Expand Up @@ -386,6 +386,26 @@ QtObject {
)
}

function getSimpleFromPlaceholder(selectedTicker, selectedOrder, sell_ticker_balance) {
if (sell_ticker_balance == 0)
{
return qsTr("%1 balance is zero").arg(selectedTicker)
}
if (!isZhtlcReady(selectedTicker))
{
return qsTr("Activating %1 (%2%)").arg(atomic_qt_utilities.retrieve_main_ticker(selectedTicker)).arg(progress)
}
if (API.app.trading_pg.max_volume == 0)
{
return qsTr("Loading wallet...")
}
if (typeof selectedOrder !== 'undefined')
{
return qsTr("Min: %1").arg(API.app.trading_pg.min_trade_vol)
}
return qsTr("Enter an amount")
}

function arrayExclude(arr, excl) {
let i = arr.indexOf(excl)
if (i > -1) arr.splice(i, 1);
Expand Down Expand Up @@ -786,6 +806,10 @@ QtObject {
switch(error) {
case TradingError.None:
return ""
case TradingError.LeftZhtlcChainNotEnabled:
return qsTr("Please wait for %1 to fully activate").arg(left_ticker)
case TradingError.RightZhtlcChainNotEnabled:
return qsTr("Please wait for %1 to fully activate").arg(right_ticker)
case TradingError.TotalFeesNotEnoughFunds:
return qsTr("%1 balance is lower than the fees amount: %2 %3").arg(fee_info.error_fees.coin).arg(fee_info.error_fees.required_balance).arg(fee_info.error_fees.coin)
case TradingError.BalanceIsLessThanTheMinimalTradingAmount:
Expand All @@ -794,10 +818,6 @@ QtObject {
return qsTr("Please fill the price field")
case TradingError.VolumeFieldNotFilled:
return qsTr("Please fill the volume field")
case TradingError.LeftZhtlcChainNotEnabled:
return qsTr("Please wait for %1 to fully activate").arg(left_ticker)
case TradingError.RightZhtlcChainNotEnabled:
return qsTr("Please wait for %1 to fully activate").arg(right_ticker)
case TradingError.VolumeIsLowerThanTheMinimum:
return qsTr("%1 volume is lower than minimum trade amount").arg(API.app.trading_pg.market_pairs_mdl.left_selected_coin) + " : " + General.getMinTradeAmount()
case TradingError.ReceiveVolumeIsLowerThanTheMinimum:
Expand Down
64 changes: 24 additions & 40 deletions atomic_defi_design/Dex/Exchange/Trade/SimpleView/Trade.qml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ ClipRRect // Trade Card
{
id: _tradeCard

readonly property var fees: API.app.trading_pg.fees
property string selectedTicker: API.app.get_balance(left_ticker) > 0 ? left_ticker : ""
readonly property var fees: Constants.API.app.trading_pg.fees
readonly property var max_trade_volume: Constants.API.app.trading_pg.max_volume
readonly property var min_trade_volume: Constants.API.app.trading_pg.min_trade_vol
readonly property var sell_ticker_balance: parseFloat(API.app.get_balance(left_ticker))
readonly property bool coin_tradable: selectedTicker !== "" && sell_ticker_balance > 0
readonly property bool waiting_for_sell_coin_info: (max_trade_volume == 0 || !Constants.General.isZhtlcReady(left_ticker)) && sell_ticker_balance != 0

property string selectedTicker: sell_ticker_balance > 0 ? left_ticker : ""
property var selectedOrder: undefined
property bool best: false
property bool coinSelection: false
Expand Down Expand Up @@ -63,8 +69,8 @@ ClipRRect // Trade Card
{
if (typeof selectedOrder === 'undefined')
return
if (parseFloat(_fromValue.text) > Constants.API.app.trading_pg.max_volume)
_fromValue.text = Constants.API.app.trading_pg.max_volume
if (parseFloat(_fromValue.text) > max_trade_volume)
_fromValue.text = max_trade_volume
Constants.API.app.trading_pg.determine_fees()
}

Expand Down Expand Up @@ -246,7 +252,7 @@ ClipRRect // Trade Card
anchors.verticalCenter: _fromTitle.verticalCenter
anchors.right: parent.right
anchors.rightMargin: 17
text: qsTr("%1").arg(Constants.API.app.trading_pg.max_volume)
text: qsTr("%1").arg(max_trade_volume) // This is slow to appear
font.pixelSize: Constants.Style.textSizeSmall2
elide: Text.ElideRight
color: DexTheme.foregroundColorLightColor1
Expand Down Expand Up @@ -289,16 +295,13 @@ ClipRRect // Trade Card
AmountField // Amount
{
id: _fromValue
enabled: selectedTicker !== ""
visible: enabled
enabled: sell_ticker_balance == 0 ? false : waiting_for_sell_coin_info ? false : true
anchors.bottom: parent.bottom
anchors.bottomMargin: 19
anchors.left: parent.left
anchors.leftMargin: 2
placeholderText: Constants.API.app.trading_pg.max_volume == 0 ?
"Loading wallet..." : typeof selectedOrder !== 'undefined' ?
qsTr("Min: %1").arg(Constants.API.app.trading_pg.min_trade_vol) : qsTr("Enter an amount")
font.pixelSize: Constants.Style.textSizeSmall5
placeholderText: Constants.General.getSimpleFromPlaceholder(selectedTicker, selectedOrder, sell_ticker_balance)
font.pixelSize: Constants.Style.textSizeSmall3
background: Rectangle { color: swap_from_card.color}

onTextChanged:
Expand All @@ -313,9 +316,9 @@ ClipRRect // Trade Card

onFocusChanged:
{
if (!focus && parseFloat(text) < parseFloat(Constants.API.app.trading_pg.min_trade_vol))
if (!focus && parseFloat(text) < parseFloat(min_trade_volume))
{
text = Constants.API.app.trading_pg.min_trade_vol
text = min_trade_volume
}
}
Component.onCompleted: text = ""
Expand Down Expand Up @@ -434,13 +437,13 @@ ClipRRect // Trade Card
text: qsTr("MAX")
color: Dex.CurrentTheme.foregroundColor2

onClicked: _fromValue.text = Constants.API.app.trading_pg.max_volume
onClicked: _fromValue.text = max_trade_volume
}

DefaultBusyIndicator
{
anchors.centerIn: parent
visible: selectedTicker !== "" && Constants.API.app.trading_pg.max_volume == 0
visible: waiting_for_sell_coin_info
}
}

Expand Down Expand Up @@ -706,35 +709,16 @@ ClipRRect // Trade Card

function getAlert()
{
var left_ticker = Constants.API.app.trading_pg.market_pairs_mdl.left_selected_coin
console.log("_fromValue.text: " + _fromValue.text)
var right_ticker = Constants.API.app.trading_pg.market_pairs_mdl.right_selected_coin
var right_parent = Constants.API.app.portfolio_pg.global_cfg_mdl.get_parent_coin(right_ticker)
var left_parent = Constants.API.app.portfolio_pg.global_cfg_mdl.get_parent_coin(left_ticker)
var last_trading_error = Constants.API.app.trading_pg.last_trading_error
var fees_error = Constants.API.app.trading_pg.fees.error
var base_ticker = Constants.API.app.trading_pg.market_pairs_mdl.base_selected_coin
var rel_ticker = Constants.API.app.trading_pg.market_pairs_mdl.rel_selected_coin

if (_fromValue.text === "" || parseFloat(_fromValue.text) === 0)
return qsTr("Entered amount must be superior than 0.")
return qsTr("Entered amount must be higher than 0.")
if (typeof selectedOrder === 'undefined')
return qsTr("You must select an order.")
if (last_trading_error == TradingError.VolumeIsLowerThanTheMinimum)
return qsTr("Entered amount is below the minimum required by this order: %1").arg(selectedOrder.base_min_volume)
if (last_trading_error == TradingError.LeftParentChainNotEnabled)
return qsTr("%1 needs to be enabled in order to use %2").arg(Constants.API.app.portfolio_pg.global_cfg_mdl.get_parent_coin(left_ticker)).arg(left_ticker)
if (last_trading_error == TradingError.LeftParentChainNotEnoughBalance)
return qsTr("%1 balance needs to be funded, a non-zero balance is required to pay the gas of %2 transactions").arg(left_parent).arg(left_ticker)
if (last_trading_error == TradingError.RightParentChainNotEnabled)
return qsTr("%1 needs to be enabled in order to use %2").arg(Constants.API.app.portfolio_pg.global_cfg_mdl.get_parent_coin(right_ticker)).arg(right_ticker)
if (last_trading_error == TradingError.RightParentChainNotEnoughBalance)
return qsTr("%1 balance needs to be funded, a non-zero balance is required to pay the gas of %2 transactions").arg(right_parent).arg(right_ticker)
if (fees_error) {
let coin = right_ticker
if (fees_error.search(Constants.API.app.portfolio_pg.global_cfg_mdl.get_parent_coin(left_ticker)) > -1) {
coin = left_ticker
}
return qsTr("%1 balance does not have enough funds to pay the gas of %2 transactions").arg(Constants.API.app.portfolio_pg.global_cfg_mdl.get_parent_coin(coin)).arg(coin)
}
return ""
return qsTr("Select an order.")
return Constants.General.getTradingError(last_trading_error, fees, base_ticker, rel_ticker, left_ticker, right_ticker)
}

tooltipText: _swapAlert.getAlert()
Expand Down
10 changes: 4 additions & 6 deletions atomic_defi_design/Dex/Exchange/Trade/Trade.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Item

Component.onCompleted:
{
API.app.trading_pg.on_gui_enter_dex()
if (dashboard.current_ticker!==undefined)
{
onOpened(dashboard.current_ticker)
Expand All @@ -36,8 +35,6 @@ Item
dashboard.current_ticker = undefined
}

Component.onDestruction: API.app.trading_pg.on_gui_leave_dex()

readonly property bool block_everything: swap_cooldown.running
|| fetching_multi_ticker_fees_busy

Expand Down Expand Up @@ -101,13 +98,14 @@ Item
General.default_rel)
}
setPair(true, ticker)
// triggers chart reload
app.pairChanged(base_ticker, rel_ticker)
}

function setPair(is_left_side, changed_ticker) {
function setPair(is_left_side, changed_ticker, is_swap=false) {
swap_cooldown.restart()

if (API.app.trading_pg.set_pair(is_left_side, changed_ticker))
if (API.app.trading_pg.set_pair(is_left_side, changed_ticker, is_swap))
// triggers chart reload
app.pairChanged(base_ticker, rel_ticker)
}

Expand Down
4 changes: 2 additions & 2 deletions atomic_defi_design/Dex/Wallet/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ Item
DefaultButton
{
// Address wont display until activated
enabled: General.isZhtlcReady(api_wallet_page.ticker, activation_progress)
enabled: General.isZhtlcReady(api_wallet_page.ticker)
anchors.fill: parent
radius: 18

Expand All @@ -556,7 +556,7 @@ Item
// Receive button error icon
DefaultAlertIcon
{
visible: !General.isZhtlcReady(api_wallet_page.ticker, activation_progress)
visible: !General.isZhtlcReady(api_wallet_page.ticker)
tooltipText: api_wallet_page.ticker + qsTr(" Activation: " + activation_progress + "%")
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! QT
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QProcess>
#include <QSettings>
#include <QTimer>
Expand Down Expand Up @@ -427,6 +428,12 @@ namespace atomic_dex
return this->system_manager_.get_system<mm2_service>();
}

QJsonObject application::get_zhtlc_status(const QString& coin)
{
QJsonObject res = nlohmann_json_object_to_qt_json_object(get_mm2().get_zhtlc_status(coin.toStdString()));
return res;
}

QString application::get_balance(const QString& coin)
{
std::error_code ec;
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "atomicdex/services/price/global.provider.hpp"
#include "atomicdex/services/update/update.checker.service.hpp"
#include "atomicdex/services/update/zcash.params.service.hpp"
#include "atomicdex/utilities/qt.utilities.hpp"

namespace ag = antara::gaming;

Expand Down Expand Up @@ -161,6 +162,7 @@ namespace atomic_dex
Q_INVOKABLE bool enable_coins(const QStringList& coins);
Q_INVOKABLE bool enable_coin(const QString& coin);
Q_INVOKABLE QString get_balance(const QString& coin);
Q_INVOKABLE QJsonObject get_zhtlc_status(const QString& coin);
Q_INVOKABLE [[nodiscard]] bool do_i_have_enough_funds(const QString& ticker, const QString& amount) const;
Q_INVOKABLE bool disable_coins(const QStringList& coins);
Q_INVOKABLE bool disable_no_balance_coins();
Expand Down
1 change: 0 additions & 1 deletion src/core/atomicdex/models/qt.orderbook.model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ namespace atomic_dex
{
taker_vol_std = "0";
}
// t_float_50 mm2_min_trade_vol = safe_float(trading_pg.get_mm2_min_trade_vol().toStdString());
t_float_50 taker_vol = safe_float(taker_vol_std);
i_have_enough_funds = min_volume_f > 0 && taker_vol > min_volume_f;
return i_have_enough_funds;
Expand Down
11 changes: 10 additions & 1 deletion src/core/atomicdex/pages/qt.trading.page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ namespace atomic_dex
{
this->set_price("0");
this->set_max_volume("0");
m_minimal_trading_amount = "0";
m_minimal_trading_amount = "0.00777";
emit minTradeVolChanged();
this->set_volume("0");
}
Expand Down Expand Up @@ -1307,6 +1307,10 @@ namespace atomic_dex
{
current_trading_error = TradingError::VolumeIsLowerThanTheMinimum;
}
else if (safe_float(m_total_amount.toStdString()) < 0.00777)
{
current_trading_error = TradingError::ReceiveVolumeIsLowerThanTheMinimum;
}
else
{
if (!get_fees().empty())
Expand Down Expand Up @@ -1467,6 +1471,11 @@ namespace atomic_dex
// If not associated bugs appear as a result, we can delete this.
// min_trade_vol = get_orderbook_wrapper()->get_current_min_taker_vol();
}

if (safe_float(min_taker_vol) < 0.00777)
{
min_trade_vol = "0.00777";
}

if (min_trade_vol != m_minimal_trading_amount)
{
Expand Down
5 changes: 2 additions & 3 deletions src/core/atomicdex/pages/qt.trading.page.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ namespace atomic_dex
QString m_price{"0"};
QString m_volume{"0"};
QString m_max_volume{"0"};
QString m_total_amount{"0"};
QString m_total_amount{"0.00777"};
QString m_cex_price{"0"};
QString m_minimal_trading_amount{"0"};
QString m_minimal_trading_amount{"0.00777"};
std::optional<nlohmann::json> m_preferred_order;
boost::synchronized_value<QVariantMap> m_fees;
bool m_skip_taker{false};
Expand Down Expand Up @@ -184,7 +184,6 @@ namespace atomic_dex
[[nodiscard]] QString get_price_reversed() const;
[[nodiscard]] QString get_price() const;
void set_price(QString price);
//[[nodiscard]] QString get_mm2_min_trade_vol() const;
[[nodiscard]] QString get_min_trade_vol() const;
void set_min_trade_vol(QString min_trade_vol);
[[nodiscard]] QString get_volume() const;
Expand Down
11 changes: 11 additions & 0 deletions src/core/atomicdex/services/mm2/mm2.service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1431,13 +1431,24 @@ namespace atomic_dex
}
}

nlohmann::json mm2_service::get_zhtlc_status(const std::string coin) const
{
const auto coin_info = get_coin_info(coin);
if (coin_info.is_zhtlc_family)
{
return coin_info.activation_status;
}
return {};
}

bool mm2_service::is_zhtlc_coin_ready(const std::string coin) const
{
const auto coin_info = get_coin_info(coin);
if (coin_info.is_zhtlc_family)
{
if (coin_info.activation_status.contains("result"))
{
SPDLOG_DEBUG("coin_info.activation_status {} {} :", coin, coin_info.activation_status.dump(4));
if (coin_info.activation_status.at("result").contains("status"))
{
if (coin_info.activation_status.at("result").at("status") == "Ready")
Expand Down
12 changes: 7 additions & 5 deletions src/core/atomicdex/services/mm2/mm2.service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,13 @@ namespace atomic_dex

public:
//! Add a new coin in the coin_info cfg add_new_coin(normal_cfg, mm2_cfg)
void add_new_coin(const nlohmann::json& coin_cfg_json, const nlohmann::json& raw_coin_cfg_json);
void remove_custom_coin(const std::string& ticker);
[[nodiscard]] bool is_this_ticker_present_in_raw_cfg(const std::string& ticker) const;
[[nodiscard]] bool is_this_ticker_present_in_normal_cfg(const std::string& ticker) const;
[[nodiscard]] bool is_zhtlc_coin_ready(const std::string coin) const;
void add_new_coin(const nlohmann::json& coin_cfg_json, const nlohmann::json& raw_coin_cfg_json);
void remove_custom_coin(const std::string& ticker);
[[nodiscard]] bool is_this_ticker_present_in_raw_cfg(const std::string& ticker) const;
[[nodiscard]] bool is_this_ticker_present_in_normal_cfg(const std::string& ticker) const;
[[nodiscard]] bool is_zhtlc_coin_ready(const std::string coin) const;
[[nodiscard]] nlohmann::json get_zhtlc_status(const std::string coin) const;


//! Disable a single coin
bool disable_coin(const std::string& ticker, std::error_code& ec);
Expand Down

0 comments on commit 530b4cf

Please sign in to comment.