mirror of
https://github.com/wownero/wowlet.git
synced 2026-11-27 10:34:11 +01:00
Merge pull request 'Exchanges: Remove XMR.to' (#316) from tobtoht/feather:rip_to into master
Reviewed-on: https://git.wownero.com/feather/feather/pulls/316
This commit is contained in:
@@ -1,110 +0,0 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// Copyright (c) 2020-2021, The Monero Project.
|
||||
|
||||
#include "xmrto.h"
|
||||
|
||||
#include "appcontext.h"
|
||||
|
||||
QMap<OrderState, QString> XmrTo::stateMap;
|
||||
|
||||
XmrTo::XmrTo(AppContext *ctx, QObject *parent) :
|
||||
QObject(parent),
|
||||
m_ctx(ctx) {
|
||||
|
||||
m_baseUrl = m_ctx->networkType == NetworkType::Type::MAINNET ? "https://xmr.to" : "https://test.xmr.to";
|
||||
|
||||
m_netTor = new UtilsNetworking(this->m_ctx->network);
|
||||
m_netClear = new UtilsNetworking(this->m_ctx->networkClearnet);
|
||||
|
||||
m_apiTor = new XmrToApi(this, m_netTor, m_baseUrl);
|
||||
m_apiClear = new XmrToApi(this, m_netClear, m_baseUrl);
|
||||
|
||||
connect(m_apiTor, &XmrToApi::ApiResponse, this, &XmrTo::onApiResponse);
|
||||
connect(m_apiClear, &XmrToApi::ApiResponse, this, &XmrTo::onApiResponse);
|
||||
|
||||
connect(this, &XmrTo::orderPaymentRequired, this->m_ctx, QOverload<XmrToOrder*>::of(&AppContext::onCreateTransaction));
|
||||
|
||||
XmrTo::stateMap[OrderState::Status_Idle] = "IDLE";
|
||||
XmrTo::stateMap[OrderState::Status_OrderCreating] = "CREATING";
|
||||
XmrTo::stateMap[OrderState::Status_OrderUnpaid] = "UNPAID";
|
||||
XmrTo::stateMap[OrderState::Status_OrderToBeCreated] = "TO_BE_CREATED";
|
||||
XmrTo::stateMap[OrderState::Status_OrderUnderPaid] = "UNDERPAID";
|
||||
XmrTo::stateMap[OrderState::Status_OrderPaidUnconfirmed] = "PAID_UNCONFIRMED";
|
||||
XmrTo::stateMap[OrderState::Status_OrderPaid] = "PAID";
|
||||
XmrTo::stateMap[OrderState::Status_OrderBTCSent] = "BTC_SENT";
|
||||
XmrTo::stateMap[OrderState::Status_OrderTimedOut] = "TIMED_OUT";
|
||||
XmrTo::stateMap[OrderState::Status_OrderFailed] = "FAILED";
|
||||
XmrTo::stateMap[OrderState::Status_OrderXMRSent] = "XMR_SENT";
|
||||
|
||||
this->tableModel = new XmrToModel(&this->orders, this);
|
||||
}
|
||||
|
||||
void XmrTo::createOrder(double amount, const QString ¤cy, const QString &btcAddress) {
|
||||
// ^[13][a-km-zA-HJ-NP-Z0-9]{26,33}$
|
||||
|
||||
XmrToOrder *order;
|
||||
order = new XmrToOrder(this->m_ctx, m_netTor, m_baseUrl, false, &this->rates, this);
|
||||
|
||||
connect(order, &XmrToOrder::orderFailed, this, &XmrTo::orderFailed);
|
||||
connect(order, &XmrToOrder::orderPaid, this, &XmrTo::orderPaid);
|
||||
connect(order, &XmrToOrder::orderPaidUnconfirmed, this, &XmrTo::orderPaidUnconfirmed);
|
||||
connect(order, &XmrToOrder::orderPaymentRequired, this, &XmrTo::orderPaymentRequired);
|
||||
connect(order, &XmrToOrder::orderChanged, this->tableModel, &XmrToModel::update);
|
||||
|
||||
order->create(amount, currency, btcAddress);
|
||||
this->orders.append(order);
|
||||
tableModel->update();
|
||||
}
|
||||
|
||||
|
||||
void XmrTo::onApiResponse(const XmrToResponse &resp) {
|
||||
if (!resp.ok) {
|
||||
this->onApiFailure(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
emit connectionSuccess();
|
||||
if (resp.endpoint == Endpoint::RATES) {
|
||||
onRatesReceived(resp.obj);
|
||||
}
|
||||
}
|
||||
|
||||
void XmrTo::onApiFailure(const XmrToResponse &resp) {
|
||||
emit connectionError(resp.message);
|
||||
}
|
||||
|
||||
void XmrTo::onGetRates() {
|
||||
m_apiTor->getRates();
|
||||
}
|
||||
|
||||
void XmrTo::onRatesReceived(const QJsonObject &doc) {
|
||||
this->rates.price = doc.value("price").toString().toDouble();
|
||||
this->rates.ln_lower_limit = doc.value("ln_lower_limit").toString().toDouble();
|
||||
this->rates.ln_upper_limit = doc.value("ln_upper_limit").toString().toDouble();
|
||||
this->rates.lower_limit = doc.value("lower_limit").toString().toDouble();
|
||||
this->rates.upper_limit = doc.value("upper_limit").toString().toDouble();
|
||||
this->rates.zero_conf_enabled = doc.value("zero_conf_enabled").toBool();
|
||||
this->rates.zero_conf_max_amount = doc.value("zero_conf_enabled").toString().toDouble();
|
||||
emit ratesUpdated(rates);
|
||||
}
|
||||
|
||||
void XmrTo::onNetworkChanged(bool clearnet) {
|
||||
m_api = clearnet ? m_apiClear : m_apiTor;
|
||||
}
|
||||
|
||||
void XmrTo::onWalletClosed() {
|
||||
// @TODO: cleanup
|
||||
for(const auto &order: this->orders)
|
||||
order->deleteLater();
|
||||
|
||||
this->tableModel->update();
|
||||
}
|
||||
|
||||
void XmrTo::onWalletOpened() {
|
||||
// @TODO: read past XMR.To orders, start pending ones
|
||||
}
|
||||
|
||||
void XmrTo::onViewOrder(const QString &orderId) {
|
||||
QString url = QString("%1/nojs/status/%2").arg(this->m_baseUrl).arg(orderId);
|
||||
emit openURL(url);
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// Copyright (c) 2020-2021, The Monero Project.
|
||||
|
||||
#ifndef FEATHER_XMRTOCONVERT_H
|
||||
#define FEATHER_XMRTOCONVERT_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "model/XmrToModel.h"
|
||||
#include "utils/xmrtoorder.h"
|
||||
#include "utils/xmrtoapi.h"
|
||||
|
||||
class AppContext;
|
||||
class XmrTo: public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit XmrTo(AppContext *ctx, QObject *parent = nullptr);
|
||||
Q_ENUM(OrderState);
|
||||
|
||||
XmrToModel *tableModel;
|
||||
static QMap<OrderState, QString> stateMap;
|
||||
XmrToRates rates;
|
||||
QList<XmrToOrder*> orders;
|
||||
|
||||
public slots:
|
||||
void createOrder(double amount, const QString ¤cy, const QString &btcAddress);
|
||||
void onGetRates();
|
||||
void onRatesReceived(const QJsonObject &doc);
|
||||
void onViewOrder(const QString &orderId);
|
||||
void onNetworkChanged(bool clearnet);
|
||||
void onWalletOpened();
|
||||
void onWalletClosed();
|
||||
|
||||
private slots:
|
||||
void onApiResponse(const XmrToResponse &doc);
|
||||
|
||||
signals:
|
||||
void orderPaymentRequired(XmrToOrder *order);
|
||||
void orderPaidUnconfirmed(XmrToOrder *order);
|
||||
void orderPaid(XmrToOrder *order);
|
||||
void orderFailed(XmrToOrder *order);
|
||||
void ratesUpdated(XmrToRates rates);
|
||||
void openURL(const QString &url);
|
||||
void connectionError(const QString &err);
|
||||
void connectionSuccess();
|
||||
|
||||
private:
|
||||
void onApiFailure(const XmrToResponse &doc);
|
||||
|
||||
QString m_baseUrl;
|
||||
AppContext *m_ctx;
|
||||
int m_orderTimeout = 900; // https://xmrto-api.readthedocs.io/en/latest/introduction.html#various-parameters
|
||||
|
||||
UtilsNetworking *m_netTor;
|
||||
UtilsNetworking *m_netClear;
|
||||
|
||||
XmrToApi *m_api;
|
||||
XmrToApi *m_apiTor;
|
||||
XmrToApi *m_apiClear;
|
||||
};
|
||||
|
||||
#endif //FEATHER_XMRTOCONVERT_H
|
||||
@@ -1,76 +0,0 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// Copyright (c) 2020-2021, The Monero Project.
|
||||
|
||||
#include "xmrtoapi.h"
|
||||
|
||||
XmrToApi::XmrToApi(QObject *parent, UtilsNetworking *network, QString baseUrl)
|
||||
: QObject(parent)
|
||||
, m_network(network)
|
||||
, m_baseUrl(std::move(baseUrl))
|
||||
{
|
||||
}
|
||||
|
||||
void XmrToApi::getRates() {
|
||||
QString url = QString("%1/api/v3/xmr2btc/order_parameter_query/").arg(this->m_baseUrl);
|
||||
QNetworkReply *reply = m_network->getJson(url);
|
||||
connect(reply, &QNetworkReply::finished, std::bind(&XmrToApi::onResponse, this, reply, Endpoint::RATES));
|
||||
}
|
||||
|
||||
void XmrToApi::createOrder(double amount, const QString &amount_currency, const QString &dest_address) {
|
||||
QJsonObject order;
|
||||
order["amount"] = amount;
|
||||
order["amount_currency"] = amount_currency;
|
||||
order["btc_dest_address"] = dest_address;
|
||||
|
||||
QString url = QString("%1/api/v3/xmr2btc/order_create/").arg(m_baseUrl);
|
||||
QNetworkReply *reply = m_network->postJson(url, order);
|
||||
connect(reply, &QNetworkReply::finished, std::bind(&XmrToApi::onResponse, this, reply, Endpoint::ORDER_CREATE));
|
||||
}
|
||||
|
||||
void XmrToApi::getOrderStatus(const QString &uuid) {
|
||||
QJsonObject order;
|
||||
order["uuid"] = uuid;
|
||||
|
||||
QString url = QString("%1/api/v3/xmr2btc/order_status_query/").arg(m_baseUrl);
|
||||
QNetworkReply *reply = m_network->postJson(url, order);
|
||||
connect(reply, &QNetworkReply::finished, std::bind(&XmrToApi::onResponse, this, reply, Endpoint::ORDER_STATUS));
|
||||
}
|
||||
|
||||
void XmrToApi::onResponse(QNetworkReply *reply, Endpoint endpoint) {
|
||||
const auto ok = reply->error() == QNetworkReply::NoError;
|
||||
const auto err = reply->errorString();
|
||||
|
||||
QByteArray data = reply->readAll();
|
||||
QJsonObject obj;
|
||||
if (!data.isEmpty() && Utils::validateJSON(data)) {
|
||||
auto doc = QJsonDocument::fromJson(data);
|
||||
obj = doc.object();
|
||||
}
|
||||
else if (!ok) {
|
||||
emit ApiResponse(XmrToResponse(false, endpoint, err));
|
||||
return;
|
||||
}
|
||||
else {
|
||||
emit ApiResponse(XmrToResponse(false, endpoint, "Invalid response from XMR.to"));
|
||||
return;
|
||||
}
|
||||
|
||||
XmrToError xmrto_err = XmrToApi::getApiError(obj);
|
||||
if (!xmrto_err.code.isEmpty()) {
|
||||
emit ApiResponse(XmrToResponse(false, endpoint, m_errorMap.contains(xmrto_err.code) ? m_errorMap[xmrto_err.code] : "", xmrto_err));
|
||||
return;
|
||||
}
|
||||
|
||||
reply->deleteLater();
|
||||
emit ApiResponse(XmrToResponse(true, endpoint, "", obj));
|
||||
}
|
||||
|
||||
XmrToError XmrToApi::getApiError(const QJsonObject &obj) {
|
||||
if (!obj.contains("error"))
|
||||
return XmrToError();
|
||||
|
||||
QString code = obj.value("error").toString();
|
||||
QString msg = obj.value("error_msg").toString();
|
||||
|
||||
return XmrToError(code, msg);
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// Copyright (c) 2020-2021, The Monero Project.
|
||||
|
||||
#ifndef FEATHER_XMRTOAPI_H
|
||||
#define FEATHER_XMRTOAPI_H
|
||||
|
||||
#include <QObject>
|
||||
#include <utility>
|
||||
|
||||
#include "utils/networking.h"
|
||||
|
||||
enum Endpoint {
|
||||
RATES = 0,
|
||||
ORDER_CREATE,
|
||||
ORDER_STATUS
|
||||
};
|
||||
|
||||
struct XmrToError {
|
||||
explicit XmrToError(QString code = "", QString msg = "")
|
||||
: code(std::move(code)), msg(std::move(msg)) {};
|
||||
|
||||
QString code;
|
||||
QString msg;
|
||||
};
|
||||
|
||||
struct XmrToResponse {
|
||||
explicit XmrToResponse(bool ok, Endpoint endpoint, QString message, XmrToError error = XmrToError(), QJsonObject obj = {})
|
||||
: ok(ok), endpoint(endpoint), message(std::move(message)), error(std::move(error)), obj(std::move(obj)) {};
|
||||
|
||||
explicit XmrToResponse(bool ok, Endpoint endpoint, QString message, QJsonObject obj)
|
||||
: ok(ok), endpoint(endpoint), message(std::move(message)), obj(std::move(obj)) {};
|
||||
|
||||
bool ok;
|
||||
Endpoint endpoint;
|
||||
QString message;
|
||||
XmrToError error = XmrToError();
|
||||
QJsonObject obj;
|
||||
};
|
||||
|
||||
class XmrToApi : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit XmrToApi(QObject *parent, UtilsNetworking *network, QString baseUrl = "https://xmr.to");
|
||||
|
||||
void getRates();
|
||||
void createOrder(double amount, const QString &amount_currency, const QString &dest_address);
|
||||
void getOrderStatus(const QString &uuid);
|
||||
|
||||
signals:
|
||||
void ApiResponse(XmrToResponse resp);
|
||||
|
||||
private slots:
|
||||
void onResponse(QNetworkReply *reply, Endpoint endpoint);
|
||||
|
||||
private:
|
||||
static XmrToError getApiError(const QJsonObject &obj);
|
||||
|
||||
QString m_baseUrl;
|
||||
UtilsNetworking *m_network;
|
||||
|
||||
// https://xmrto-api.readthedocs.io/en/latest/introduction.html#list-of-all-error-codes
|
||||
const QMap<QString, QString> m_errorMap = {
|
||||
{"XMRTO-ERROR-001", "internal services not available, try again later."},
|
||||
{"XMRTO-ERROR-002", "malformed bitcoin address, check address validity."},
|
||||
{"XMRTO-ERROR-003", "invalid bitcoin amount, check amount data type."},
|
||||
{"XMRTO-ERROR-004", "bitcoin amount out of bounds, check min and max amount."},
|
||||
{"XMRTO-ERROR-005", "unexpected validation error, contact support."},
|
||||
{"XMRTO-ERROR-006", "requested order not found, check order UUID."},
|
||||
{"XMRTO-ERROR-007", "third party service not available, try again later."},
|
||||
{"XMRTO-ERROR-008", "insufficient funds available, try again later."},
|
||||
{"XMRTO-ERROR-009", "invalid request, check request parameters."},
|
||||
{"XMRTO-ERROR-010", "payment protocol failed, invalid or outdated data served by URL."},
|
||||
{"XMRTO-ERROR-011", "malformed payment protocol url, URL is malformed or cannot be contacted."},
|
||||
{"XMRTO-ERROR-012", "too many requests, try less often."},
|
||||
{"XMRTO-ERROR-013", "access forbidden."},
|
||||
{"XMRTO-ERROR-014", "service is not available in your region."},
|
||||
{"XMRTO-ERROR-015", "invalid monero amount, check amount data type."},
|
||||
{"XMRTO-ERROR-016", "invalid currency, check available currency options."},
|
||||
{"XMRTO-ERROR-017", "malformed lightning network invoice, provide a correct invoice for the main network."},
|
||||
{"XMRTO-ERROR-018", "lightning payment unlikely to succeed, check first if xmr.to has routes available."},
|
||||
{"XMRTO-ERROR-019", "lightning invoice preimage already known, don’t use the same invoice more than once."}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
#endif //FEATHER_XMRTOAPI_H
|
||||
@@ -1,274 +0,0 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// Copyright (c) 2020-2021, The Monero Project.
|
||||
|
||||
#include "xmrtoorder.h"
|
||||
|
||||
#include "libwalletqt/Wallet.h"
|
||||
#include "appcontext.h"
|
||||
#include "globals.h"
|
||||
|
||||
XmrToOrder::XmrToOrder(AppContext *ctx, UtilsNetworking *network, QString baseUrl, bool clearnet, XmrToRates *rates, QObject *parent) :
|
||||
QObject(parent),
|
||||
m_ctx(ctx),
|
||||
m_network(network),
|
||||
m_baseUrl(std::move(baseUrl)),
|
||||
m_rates(rates),
|
||||
m_clearnet(clearnet) {
|
||||
this->state = OrderState::Status_Idle;
|
||||
|
||||
m_baseUrl = m_ctx->networkType == NetworkType::Type::MAINNET ? "https://xmr.to" : "https://test.xmr.to";
|
||||
m_api = new XmrToApi(this, network, m_baseUrl);
|
||||
|
||||
connect(m_api, &XmrToApi::ApiResponse, this, &XmrToOrder::onApiResponse);
|
||||
connect(m_ctx, &AppContext::transactionCommitted, this, &XmrToOrder::onTransactionCommitted);
|
||||
connect(m_ctx, &AppContext::createTransactionCancelled, this, &XmrToOrder::onTransactionCancelled);
|
||||
}
|
||||
|
||||
void XmrToOrder::onTransactionCancelled(const QVector<QString> &address, double amount) {
|
||||
// listener for all cancelled transactions - will try to match the exact amount to this order.
|
||||
if(this->incoming_amount_total != amount || this->receiving_subaddress != address[0]) return;
|
||||
|
||||
this->errorMsg = "TX cancelled by user";
|
||||
this->changeState(OrderState::Status_OrderFailed);
|
||||
this->stop();
|
||||
}
|
||||
|
||||
void XmrToOrder::onTransactionCommitted(bool status, PendingTransaction *tx, const QStringList& txid) {
|
||||
// listener for all outgoing transactions - will try to match the exact amount to this order.
|
||||
if(this->state == OrderState::Status_OrderUnpaid){
|
||||
if(tx->amount() / globals::cdiv == this->incoming_amount_total) {
|
||||
if(!status) {
|
||||
this->errorMsg = "TX failed to commit";
|
||||
this->changeState(OrderState::Status_OrderFailed);
|
||||
this->stop();
|
||||
return;
|
||||
}
|
||||
|
||||
this->xmr_txid = txid.at(0);
|
||||
this->m_paymentSent = true;
|
||||
this->changeState(OrderState::Status_OrderXMRSent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void XmrToOrder::onApiFailure(const XmrToResponse &resp) {
|
||||
this->errorCode = resp.error.code;
|
||||
this->errorMsg = resp.message;
|
||||
|
||||
switch (resp.endpoint) {
|
||||
case ORDER_CREATE:
|
||||
this->onCreatedError();
|
||||
break;
|
||||
case ORDER_STATUS:
|
||||
this->onCheckedError(resp.error);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void XmrToOrder::onApiResponse(const XmrToResponse& resp) {
|
||||
if (!resp.ok) {
|
||||
this->onApiFailure(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (resp.endpoint) {
|
||||
case ORDER_CREATE:
|
||||
this->onCreated(resp.obj);
|
||||
break;
|
||||
case ORDER_STATUS:
|
||||
this->onChecked(resp.obj);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void XmrToOrder::create(double amount, const QString ¤cy, const QString &btcAddress) {
|
||||
if(this->m_ctx->currentWallet == nullptr) {
|
||||
this->errorMsg = "No wallet opened";
|
||||
this->changeState(OrderState::Status_OrderFailed);
|
||||
return;
|
||||
}
|
||||
|
||||
m_api->createOrder(amount, currency, btcAddress);
|
||||
this->changeState(OrderState::Status_OrderCreating);
|
||||
}
|
||||
|
||||
void XmrToOrder::onCreatedError() {
|
||||
this->changeState(OrderState::Status_OrderFailed);
|
||||
}
|
||||
|
||||
void XmrToOrder::onCreated(const QJsonObject &object) {
|
||||
if(!object.contains("state"))
|
||||
this->errorMsg = "Could not parse 'state' from JSON response";
|
||||
if(object.value("state").toString() != "TO_BE_CREATED")
|
||||
this->errorMsg = "unknown state from response, should be \"TO_BE_CREATED\"";
|
||||
|
||||
if(!this->errorMsg.isEmpty()) {
|
||||
this->changeState(OrderState::Status_OrderFailed);
|
||||
return;
|
||||
}
|
||||
|
||||
if(m_created) return;
|
||||
m_created = true;
|
||||
this->btc_amount = object.value("btc_amount").toDouble();
|
||||
this->btc_dest_address = object.value("btc_dest_address").toString();
|
||||
this->uses_lightning = object.value("uses_lightning").toBool();
|
||||
this->uuid = object.value("uuid").toString();
|
||||
m_checkTimer.start(1000*5);
|
||||
m_countdownTimer.start(1000);
|
||||
connect(&m_checkTimer, &QTimer::timeout, this, &XmrToOrder::check);
|
||||
connect(&m_countdownTimer, &QTimer::timeout, this, &XmrToOrder::onCountdown);
|
||||
|
||||
this->changeState(OrderState::Status_OrderToBeCreated);
|
||||
this->check();
|
||||
}
|
||||
|
||||
void XmrToOrder::check() {
|
||||
if(this->m_ctx->currentWallet == nullptr)
|
||||
return;
|
||||
|
||||
m_api->getOrderStatus(this->uuid);
|
||||
}
|
||||
|
||||
void XmrToOrder::onCheckedError(const XmrToError& err) {
|
||||
if (!err.code.isEmpty())
|
||||
this->changeState(OrderState::Status_OrderFailed);
|
||||
|
||||
m_checkFailures += 1;
|
||||
if(m_checkFailures > 15){
|
||||
this->errorMsg = "Too many failed attempts";
|
||||
this->changeState(OrderState::Status_OrderFailed);
|
||||
}
|
||||
}
|
||||
|
||||
void XmrToOrder::onChecked(const QJsonObject &object) {
|
||||
if(object.contains("btc_amount"))
|
||||
this->btc_amount = object.value("btc_amount").toString().toDouble();
|
||||
if(object.contains("btc_dest_address"))
|
||||
this->btc_dest_address = object.value("btc_dest_address").toString();
|
||||
if(object.contains("seconds_till_timeout")) {
|
||||
this->seconds_till_timeout = object.value("seconds_till_timeout").toInt();
|
||||
this->countdown = this->seconds_till_timeout;
|
||||
}
|
||||
if(object.contains("created_at"))
|
||||
this->created_at = object.value("created_at").toString();
|
||||
if(object.contains("expires_at"))
|
||||
this->expires_at = object.value("expires_at").toString();
|
||||
if(object.contains("incoming_amount_total"))
|
||||
this->incoming_amount_total = object.value("incoming_amount_total").toString().toDouble();
|
||||
if(object.contains("remaining_amount_incoming"))
|
||||
this->remaining_amount_incoming = object.value("remaining_amount_incoming").toString().toDouble();
|
||||
if(object.contains("incoming_price_btc"))
|
||||
{
|
||||
qDebug() << object.value("incoming_price_btc").toString();
|
||||
this->incoming_price_btc = object.value("incoming_price_btc").toString().toDouble();
|
||||
}
|
||||
if(object.contains("receiving_subaddress"))
|
||||
this->receiving_subaddress = object.value("receiving_subaddress").toString();
|
||||
|
||||
if(object.contains("payments")) {
|
||||
// detect btc txid, xmr.to api can output several - we'll just grab the first #yolo
|
||||
auto payments = object.value("payments").toArray();
|
||||
for(const auto &payment: payments){
|
||||
auto obj = payment.toObject();
|
||||
if(obj.contains("tx_id")) {
|
||||
this->btc_txid = obj.value("tx_id").toString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this->changeState(object.value("state").toString());
|
||||
}
|
||||
|
||||
void XmrToOrder::changeState(const QString &_state) {
|
||||
for(const auto &key: XmrTo::stateMap.keys()) {
|
||||
const auto &val = XmrTo::stateMap[key];
|
||||
if(_state == val){
|
||||
this->changeState(key);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void XmrToOrder::changeState(OrderState _state) {
|
||||
if(this->m_ctx->currentWallet == nullptr)
|
||||
return;
|
||||
|
||||
if(_state == OrderState::Status_OrderUnderPaid && m_paymentSent) {
|
||||
this->state = OrderState::Status_OrderXMRSent;
|
||||
emit orderChanged();
|
||||
return;
|
||||
}
|
||||
|
||||
if(_state == this->state) return;
|
||||
switch(_state){
|
||||
case OrderState::Status_Idle:
|
||||
break;
|
||||
case OrderState::Status_OrderCreating:
|
||||
break;
|
||||
case OrderState::Status_OrderToBeCreated:
|
||||
break;
|
||||
case OrderState::Status_OrderUnderPaid:
|
||||
emit orderFailed(this);
|
||||
this->stop();
|
||||
break;
|
||||
case OrderState::Status_OrderUnpaid:
|
||||
// need to send Monero
|
||||
if(!m_paymentRequested) {
|
||||
auto unlocked_balance = m_ctx->currentWallet->unlockedBalance() / globals::cdiv;
|
||||
if (this->incoming_amount_total >= unlocked_balance) {
|
||||
this->state = OrderState::Status_OrderFailed;
|
||||
emit orderFailed(this);
|
||||
this->stop();
|
||||
break;
|
||||
}
|
||||
m_paymentRequested = true;
|
||||
emit orderPaymentRequired(this);
|
||||
}
|
||||
break;
|
||||
case OrderState::Status_OrderFailed:
|
||||
emit orderFailed(this);
|
||||
this->stop();
|
||||
break;
|
||||
case OrderState::Status_OrderPaidUnconfirmed:
|
||||
emit orderPaidUnconfirmed(this);
|
||||
break;
|
||||
case OrderState::Status_OrderPaid:
|
||||
emit orderPaid(this);
|
||||
break;
|
||||
case OrderState::Status_OrderTimedOut:
|
||||
emit orderFailed(this);
|
||||
this->stop();
|
||||
break;
|
||||
case OrderState::Status_OrderBTCSent:
|
||||
emit orderPaid(this);
|
||||
this->stop();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
this->state = _state;
|
||||
emit orderChanged();
|
||||
}
|
||||
|
||||
void XmrToOrder::onCountdown() {
|
||||
if(this->countdown <= 0) return;
|
||||
this->countdown -= 1;
|
||||
emit orderChanged();
|
||||
}
|
||||
|
||||
void XmrToOrder::stop(){
|
||||
this->m_checkTimer.stop();
|
||||
this->m_countdownTimer.stop();
|
||||
}
|
||||
|
||||
XmrToOrder::~XmrToOrder(){
|
||||
this->stop();
|
||||
this->disconnect();
|
||||
this->m_network->deleteLater();
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
// Copyright (c) 2020-2021, The Monero Project.
|
||||
|
||||
#ifndef FEATHER_XMRTOORDER_H
|
||||
#define FEATHER_XMRTOORDER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "utils/networking.h"
|
||||
#include "PendingTransaction.h"
|
||||
#include "utils/xmrtoapi.h"
|
||||
|
||||
enum OrderState {
|
||||
Status_Idle,
|
||||
Status_OrderCreating,
|
||||
Status_OrderToBeCreated,
|
||||
Status_OrderUnpaid,
|
||||
Status_OrderXMRSent,
|
||||
Status_OrderUnderPaid,
|
||||
Status_OrderPaidUnconfirmed,
|
||||
Status_OrderPaid,
|
||||
Status_OrderBTCSent,
|
||||
Status_OrderTimedOut,
|
||||
Status_OrderFailed
|
||||
};
|
||||
|
||||
struct XmrToRates {
|
||||
double price;
|
||||
double upper_limit;
|
||||
double lower_limit;
|
||||
double ln_upper_limit;
|
||||
double ln_lower_limit;
|
||||
double zero_conf_max_amount;
|
||||
bool zero_conf_enabled;
|
||||
};
|
||||
|
||||
class XmrToOrder : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit XmrToOrder(AppContext *ctx, UtilsNetworking *network, QString baseUrl, bool clearnet, XmrToRates *rates, QObject *parent = nullptr);
|
||||
void create(double btcAmount, const QString ¤cy, const QString &btcAddress);
|
||||
void changeState(OrderState state);
|
||||
void changeState(const QString &state);
|
||||
void stop();
|
||||
|
||||
int state;
|
||||
int countdown = -1; // seconds remaining calculated from `seconds_till_timeout`
|
||||
QString uuid;
|
||||
QString errorMsg;
|
||||
QString errorCode;
|
||||
|
||||
double btc_amount = 0;
|
||||
QString btc_dest_address;
|
||||
QString btc_txid;
|
||||
QString xmr_txid;
|
||||
bool uses_lightning = false;
|
||||
|
||||
QString receiving_subaddress;
|
||||
QString created_at;
|
||||
QString expires_at;
|
||||
int seconds_till_timeout = -1;
|
||||
double incoming_amount_total = 0; // amount_in_incoming_currency_for_this_order_as_string
|
||||
double remaining_amount_incoming; // amount_in_incoming_currency_that_the_user_must_still_send_as_string
|
||||
double incoming_price_btc = 0; // price_of_1_incoming_in_btc_currency_as_offered_by_service
|
||||
|
||||
public slots:
|
||||
void onCountdown();
|
||||
void onTransactionCancelled(const QVector<QString> &address, double amount);
|
||||
void onTransactionCommitted(bool status, PendingTransaction *tx, const QStringList& txid);
|
||||
|
||||
void onCreatedError();
|
||||
void onChecked(const QJsonObject &object);
|
||||
void onCheckedError(const XmrToError& err);
|
||||
void check();
|
||||
|
||||
private:
|
||||
bool m_created = false;
|
||||
QString m_baseUrl;
|
||||
QTimer m_checkTimer;
|
||||
QTimer m_countdownTimer;
|
||||
int m_checkFailures = 0;
|
||||
bool m_clearnet;
|
||||
bool m_paymentSent = false;
|
||||
bool m_paymentRequested = false;
|
||||
UtilsNetworking *m_network;
|
||||
AppContext *m_ctx;
|
||||
XmrToRates *m_rates;
|
||||
XmrToApi *m_api;
|
||||
|
||||
~XmrToOrder();
|
||||
|
||||
signals:
|
||||
void orderChanged();
|
||||
void orderPaymentRequired(XmrToOrder *order);
|
||||
void orderPaid(XmrToOrder *order);
|
||||
void orderPaidUnconfirmed(XmrToOrder *order);
|
||||
void orderFailed(XmrToOrder *order);
|
||||
|
||||
private slots:
|
||||
void onCreated(const QJsonObject &object);
|
||||
void onApiFailure(const XmrToResponse &resp);
|
||||
void onApiResponse(const XmrToResponse &resp);
|
||||
};
|
||||
|
||||
#endif //FEATHER_XMRTOORDER_H
|
||||
Reference in New Issue
Block a user