2020-10-14 15:12:33 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2021-11-16 19:14:45 +01:00
|
|
|
* Copyright (C) 2020-2021 Tom Zander <tom@flowee.org>
|
2020-10-14 15:12:33 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
#ifndef PAYMENT_H
|
|
|
|
|
#define PAYMENT_H
|
|
|
|
|
|
2021-11-21 17:46:17 +01:00
|
|
|
#include "WalletCoinsModel.h"
|
|
|
|
|
|
2020-10-14 15:12:33 +02:00
|
|
|
#include <BroadcastTxData.h>
|
2021-11-02 11:09:56 +01:00
|
|
|
#include <primitives/Tx.h>
|
2020-10-14 15:12:33 +02:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
class Wallet;
|
2021-11-15 15:49:00 +01:00
|
|
|
class PaymentDetail;
|
|
|
|
|
class PaymentDetailOutput;
|
2021-11-19 11:20:01 +01:00
|
|
|
class PaymentDetailInputs;
|
2021-11-16 11:47:33 +01:00
|
|
|
class AccountInfo;
|
2020-10-14 15:12:33 +02:00
|
|
|
|
2021-11-23 17:45:38 +01:00
|
|
|
|
2020-10-14 15:12:33 +02:00
|
|
|
class Payment : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
Q_PROPERTY(int feePerByte READ feePerByte WRITE setFeePerByte NOTIFY feePerByteChanged)
|
|
|
|
|
Q_PROPERTY(double paymentAmount READ paymentAmount WRITE setPaymentAmount NOTIFY amountChanged)
|
|
|
|
|
Q_PROPERTY(QString targetAddress READ targetAddress WRITE setTargetAddress NOTIFY targetAddressChanged)
|
|
|
|
|
// cleaned up and re-formatted
|
|
|
|
|
Q_PROPERTY(QString formattedTargetAddress READ formattedTargetAddress NOTIFY targetAddressChanged)
|
2021-05-04 17:41:49 +02:00
|
|
|
Q_PROPERTY(bool preferSchnorr READ preferSchnorr WRITE setPreferSchnorr NOTIFY preferSchnorrChanged);
|
2021-11-16 19:14:45 +01:00
|
|
|
/// Input is valid, tx can be prepared
|
|
|
|
|
Q_PROPERTY(bool isValid READ validate NOTIFY validChanged);
|
2021-11-16 11:47:33 +01:00
|
|
|
Q_PROPERTY(QList<QObject*> details READ paymentDetails NOTIFY paymentDetailsChanged);
|
2021-11-16 19:14:45 +01:00
|
|
|
Q_PROPERTY(BroadcastStatus broadcastStatus READ broadcastStatus NOTIFY broadcastStatusChanged)
|
2021-11-18 14:25:47 +01:00
|
|
|
/// The price of on BCH
|
|
|
|
|
Q_PROPERTY(int fiatPrice READ fiatPrice WRITE setFiatPrice NOTIFY fiatPriceChanged)
|
|
|
|
|
Q_PROPERTY(AccountInfo *account READ currentAccount WRITE setCurrentAccount NOTIFY currentAccountChanged)
|
2021-11-29 23:16:45 +01:00
|
|
|
Q_PROPERTY(QString userComment READ userComment WRITE setUserComment NOTIFY userCommentChanged)
|
2022-05-18 20:16:34 +02:00
|
|
|
Q_PROPERTY(bool walletNeedsPin READ walletNeedsPin NOTIFY walletPinChanged);
|
2021-11-29 23:16:45 +01:00
|
|
|
|
|
|
|
|
// --- Stuff that becomes available / useful after prepare has been called:
|
|
|
|
|
/// Tx has been prepared
|
|
|
|
|
Q_PROPERTY(bool txPrepared READ txPrepared NOTIFY txPreparedChanged);
|
|
|
|
|
Q_PROPERTY(QString txid READ txid NOTIFY txCreated)
|
|
|
|
|
Q_PROPERTY(int assignedFee READ assignedFee NOTIFY txCreated)
|
|
|
|
|
Q_PROPERTY(int txSize READ txSize NOTIFY txCreated)
|
|
|
|
|
Q_PROPERTY(int effectiveFiatAmount READ effectiveFiatAmount NOTIFY txCreated)
|
|
|
|
|
Q_PROPERTY(double effectiveBchAmount READ effectiveBchAmount NOTIFY txCreated)
|
|
|
|
|
/// If Prepare failed, this is set.
|
|
|
|
|
Q_PROPERTY(QString error READ error NOTIFY errorChanged);
|
2021-11-15 15:49:00 +01:00
|
|
|
|
2021-11-16 19:14:45 +01:00
|
|
|
Q_ENUMS(DetailType BroadcastStatus)
|
2020-10-14 15:12:33 +02:00
|
|
|
public:
|
2021-11-15 15:49:00 +01:00
|
|
|
enum DetailType {
|
|
|
|
|
InputSelector,
|
|
|
|
|
PayToAddress
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-29 23:16:45 +01:00
|
|
|
/**
|
|
|
|
|
* The broadcast status is a statemachine to indicate if the transaction
|
|
|
|
|
* has been offered to the network to the final state of
|
|
|
|
|
* either TxRejected or TxBroadcastSuccess
|
|
|
|
|
*
|
|
|
|
|
* The statemachine goes like this;
|
|
|
|
|
*
|
|
|
|
|
* 0. `NotStarted`
|
|
|
|
|
* 1. After the API call 'broadcast()' we offer the transaction to all peers.
|
|
|
|
|
* `TxOffered`
|
|
|
|
|
* 2. A peer responds by downloading the actual transaction from us.
|
|
|
|
|
* `TxWaiting`
|
2021-12-04 11:35:22 +01:00
|
|
|
* 3. Optionally, a peer responds with 'rejected' if the transaction is somehow wrong.
|
2021-11-29 23:16:45 +01:00
|
|
|
* `TxRejected`
|
|
|
|
|
* Stop here.
|
2021-12-04 11:35:22 +01:00
|
|
|
* 4. We waited a little time and no rejected came in, implying 2 or more peers like our tx.
|
2021-11-29 23:16:45 +01:00
|
|
|
* `TxBroadcastSuccess`
|
|
|
|
|
*/
|
2021-11-16 19:14:45 +01:00
|
|
|
enum BroadcastStatus {
|
2021-11-29 23:16:45 +01:00
|
|
|
NotStarted, //< We have not yet seen a call to broadcast()
|
|
|
|
|
TxOffered, //< Tx has not been offered to any peers.
|
|
|
|
|
TxSent1, //< Tx has been sent to at least one peer.
|
|
|
|
|
TxWaiting, //< Tx has been downloaded by more than one peer.
|
|
|
|
|
TxBroadcastSuccess, //< Tx broadcast and accepted by multiple peers.
|
|
|
|
|
TxRejected //< Tx has been offered, downloaded and rejected by at least one peer.
|
2021-11-16 19:14:45 +01:00
|
|
|
};
|
|
|
|
|
|
2021-11-16 11:47:33 +01:00
|
|
|
Payment(QObject *parent = nullptr);
|
2020-10-14 15:12:33 +02:00
|
|
|
|
|
|
|
|
void setFeePerByte(int sats);
|
|
|
|
|
int feePerByte();
|
|
|
|
|
|
2020-10-23 22:34:34 +02:00
|
|
|
/**
|
2021-11-24 14:56:52 +01:00
|
|
|
* Sats the amount of BCH on our single-detail payment.
|
|
|
|
|
*
|
|
|
|
|
* This method can no longer be used after addExtraOutput() have been called.
|
|
|
|
|
* This method assumes a single output, which is the default for this class.
|
|
|
|
|
*
|
|
|
|
|
* This sets the amount to pay, in Satoshis, to the target address.
|
2020-10-23 22:34:34 +02:00
|
|
|
*/
|
2020-10-14 15:12:33 +02:00
|
|
|
void setPaymentAmount(double amount);
|
2021-11-24 14:56:52 +01:00
|
|
|
/**
|
|
|
|
|
* Returns the total amount of satoshis that are selected by outputs.
|
|
|
|
|
* Notice that if 'max' is requested that this is not counted.
|
|
|
|
|
*/
|
2020-10-14 15:12:33 +02:00
|
|
|
double paymentAmount();
|
|
|
|
|
|
2021-11-24 14:56:52 +01:00
|
|
|
/**
|
|
|
|
|
* Sets the address to pay to.
|
|
|
|
|
*
|
|
|
|
|
* This method can no longer be used after addExtraOutput has been called.
|
|
|
|
|
* This method assumes a single output, which is the default for this class.
|
|
|
|
|
*/
|
2020-10-14 15:12:33 +02:00
|
|
|
void setTargetAddress(const QString &address);
|
2021-11-24 14:56:52 +01:00
|
|
|
/**
|
|
|
|
|
* Returns the address to pay to, as the user typed it.
|
|
|
|
|
*
|
|
|
|
|
* This method can no longer be used after addExtraOutput has been called.
|
|
|
|
|
* This method assumes a single output, which is the default for this class.
|
|
|
|
|
*/
|
2020-10-14 15:12:33 +02:00
|
|
|
QString targetAddress();
|
2021-11-24 14:56:52 +01:00
|
|
|
/**
|
|
|
|
|
* Returns the validated and formatted address to pay to.
|
|
|
|
|
*
|
|
|
|
|
* This method can no longer be used after addExtraOutput has been called.
|
|
|
|
|
* This method assumes a single output, which is the default for this class.
|
|
|
|
|
*/
|
2020-10-14 15:12:33 +02:00
|
|
|
QString formattedTargetAddress();
|
|
|
|
|
|
2022-05-18 20:16:34 +02:00
|
|
|
bool walletNeedsPin() const;
|
|
|
|
|
|
2021-11-18 14:25:47 +01:00
|
|
|
/// return true if all fields are correctly populated and we can prepare()
|
|
|
|
|
bool validate();
|
|
|
|
|
/// A user error occured during prepare()
|
|
|
|
|
const QString &error() const;
|
|
|
|
|
|
|
|
|
|
Q_INVOKABLE void prepare();
|
2021-11-16 11:47:33 +01:00
|
|
|
Q_INVOKABLE void broadcast();
|
2021-11-16 19:14:45 +01:00
|
|
|
Q_INVOKABLE void reset();
|
2021-11-17 14:24:19 +01:00
|
|
|
Q_INVOKABLE void addExtraOutput();
|
2021-11-19 11:20:01 +01:00
|
|
|
Q_INVOKABLE void addInputSelector();
|
2021-11-17 14:24:19 +01:00
|
|
|
Q_INVOKABLE void remove(PaymentDetail *detail);
|
2022-05-18 20:16:34 +02:00
|
|
|
Q_INVOKABLE void decrypt(const QString &password);
|
2021-11-15 15:49:00 +01:00
|
|
|
|
2020-10-14 15:12:33 +02:00
|
|
|
/// return the txid, should there be a transaction (otherwise empty string)
|
|
|
|
|
QString txid() const;
|
|
|
|
|
|
2021-11-24 14:56:52 +01:00
|
|
|
/// The fee decided to be used in 'prepare()'.
|
2020-10-14 15:12:33 +02:00
|
|
|
int assignedFee() const;
|
2021-11-24 14:56:52 +01:00
|
|
|
/// The size of the transaction we prepare()d.
|
2020-10-14 15:12:33 +02:00
|
|
|
int txSize() const;
|
2021-11-24 14:56:52 +01:00
|
|
|
/// Return true if prepare() successfully completed.
|
2021-11-16 11:47:33 +01:00
|
|
|
bool txPrepared() const;
|
2020-10-14 15:12:33 +02:00
|
|
|
|
2021-11-29 23:16:45 +01:00
|
|
|
/**
|
|
|
|
|
* This returns a total fiat amount input into the prepared transaction.
|
|
|
|
|
*/
|
|
|
|
|
int effectiveFiatAmount() const;
|
|
|
|
|
/**
|
|
|
|
|
* This returns a total BCH (in sats) amount that went into the prepared transaction.
|
|
|
|
|
*/
|
|
|
|
|
double effectiveBchAmount() const;
|
|
|
|
|
|
2021-11-18 14:25:47 +01:00
|
|
|
/// Return the wallet used by the previous prepare()
|
|
|
|
|
/// \sa currentAccount
|
2020-10-14 15:12:33 +02:00
|
|
|
Wallet *wallet() const;
|
|
|
|
|
|
2021-05-04 17:41:49 +02:00
|
|
|
bool preferSchnorr() const;
|
|
|
|
|
void setPreferSchnorr(bool preferSchnorr);
|
|
|
|
|
|
2021-11-16 11:47:33 +01:00
|
|
|
QList<QObject *> paymentDetails() const;
|
|
|
|
|
|
2021-11-16 19:14:45 +01:00
|
|
|
BroadcastStatus broadcastStatus() const;
|
|
|
|
|
|
2021-11-29 23:16:45 +01:00
|
|
|
/// The exchange rate. The amount of cents for one BCH.
|
2021-11-18 14:25:47 +01:00
|
|
|
int fiatPrice() const;
|
2021-11-29 23:16:45 +01:00
|
|
|
/// The exchange rate. The amount of cents for one BCH.
|
|
|
|
|
void setFiatPrice(int pricePerCoin);
|
2021-11-18 14:25:47 +01:00
|
|
|
|
|
|
|
|
AccountInfo *currentAccount() const;
|
|
|
|
|
void setCurrentAccount(AccountInfo *account);
|
|
|
|
|
|
2021-11-29 23:16:45 +01:00
|
|
|
const QString &userComment() const;
|
|
|
|
|
void setUserComment(const QString &comment);
|
|
|
|
|
|
2020-10-14 15:12:33 +02:00
|
|
|
private slots:
|
|
|
|
|
void sentToPeer();
|
|
|
|
|
void txRejected(short reason, const QString &message);
|
2021-11-24 14:56:52 +01:00
|
|
|
void recalcAmounts();
|
2020-10-14 15:12:33 +02:00
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
void feePerByteChanged();
|
|
|
|
|
void amountChanged();
|
|
|
|
|
void targetAddressChanged();
|
2021-11-16 11:47:33 +01:00
|
|
|
void txPreparedChanged();
|
2021-05-04 17:41:49 +02:00
|
|
|
void preferSchnorrChanged();
|
2021-11-16 11:47:33 +01:00
|
|
|
void paymentDetailsChanged();
|
2021-11-16 19:14:45 +01:00
|
|
|
void broadcastStatusChanged();
|
|
|
|
|
void validChanged();
|
2021-11-18 14:25:47 +01:00
|
|
|
void errorChanged();
|
2021-11-16 19:14:45 +01:00
|
|
|
void txCreated();
|
2021-11-18 14:25:47 +01:00
|
|
|
void fiatPriceChanged();
|
|
|
|
|
void currentAccountChanged();
|
2021-11-29 23:16:45 +01:00
|
|
|
void userCommentChanged();
|
2022-05-18 20:16:34 +02:00
|
|
|
void walletPinChanged();
|
2021-11-29 23:16:45 +01:00
|
|
|
|
2020-10-14 15:12:33 +02:00
|
|
|
private:
|
2021-11-18 14:25:47 +01:00
|
|
|
friend class PaymentDetailOutput;
|
|
|
|
|
|
2021-11-15 15:49:00 +01:00
|
|
|
/// Helper method to get the output, assuming that is the only detail.
|
|
|
|
|
/// Will throw if the Payment has more than one detail.
|
|
|
|
|
PaymentDetailOutput *soleOut() const;
|
2021-11-17 14:24:19 +01:00
|
|
|
void addDetail(PaymentDetail*);
|
2021-11-15 15:49:00 +01:00
|
|
|
|
2021-11-18 14:25:47 +01:00
|
|
|
AccountInfo *m_account = nullptr;
|
|
|
|
|
|
|
|
|
|
// Payment Variable initialization in reset() please
|
2021-11-15 15:49:00 +01:00
|
|
|
QList<PaymentDetail*> m_paymentDetails;
|
2021-11-16 19:14:45 +01:00
|
|
|
bool m_txPrepared;
|
2021-11-29 23:16:45 +01:00
|
|
|
bool m_txBroadcastStarted;
|
2021-11-16 19:14:45 +01:00
|
|
|
bool m_preferSchnorr;
|
2020-10-14 15:12:33 +02:00
|
|
|
Tx m_tx;
|
2021-11-16 19:14:45 +01:00
|
|
|
int m_fee; // in sats per byte
|
|
|
|
|
int m_assignedFee;
|
2021-11-18 14:25:47 +01:00
|
|
|
int m_fiatPrice; // price for one whole BCH
|
2020-10-14 15:12:33 +02:00
|
|
|
std::shared_ptr<BroadcastTxData> m_infoObject;
|
2021-11-16 19:14:45 +01:00
|
|
|
short m_sentPeerCount;
|
|
|
|
|
short m_rejectedPeerCount;
|
2021-11-18 14:25:47 +01:00
|
|
|
Wallet *m_wallet;
|
|
|
|
|
QString m_error;
|
2021-11-29 23:16:45 +01:00
|
|
|
QString m_userComment;
|
2020-10-14 15:12:33 +02:00
|
|
|
};
|
|
|
|
|
|
2021-11-23 17:45:38 +01:00
|
|
|
|
2021-11-15 15:49:00 +01:00
|
|
|
class PaymentDetail : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
Q_PROPERTY(Payment::DetailType type READ type CONSTANT)
|
|
|
|
|
Q_PROPERTY(bool collapsable READ collapsable WRITE setCollapsable NOTIFY collapsableChanged)
|
|
|
|
|
Q_PROPERTY(bool collapsed READ collapsed WRITE setCollapsed NOTIFY collapsedChanged)
|
|
|
|
|
public:
|
2021-11-18 14:25:47 +01:00
|
|
|
PaymentDetail(Payment *parent, Payment::DetailType type);
|
2021-11-15 15:49:00 +01:00
|
|
|
|
|
|
|
|
Payment::DetailType type() const;
|
|
|
|
|
|
|
|
|
|
bool collapsable() const;
|
|
|
|
|
void setCollapsable(bool newCollapsable);
|
|
|
|
|
|
|
|
|
|
bool collapsed() const;
|
|
|
|
|
void setCollapsed(bool newCollapsed);
|
|
|
|
|
|
2021-11-17 14:24:19 +01:00
|
|
|
inline bool isOutput() const {
|
|
|
|
|
return m_type == Payment::PayToAddress;
|
|
|
|
|
}
|
2021-11-19 11:20:01 +01:00
|
|
|
inline bool isInputs() const {
|
|
|
|
|
return m_type == Payment::InputSelector;
|
|
|
|
|
}
|
2021-11-15 15:49:00 +01:00
|
|
|
PaymentDetailOutput *toOutput();
|
2021-11-19 11:20:01 +01:00
|
|
|
PaymentDetailInputs *toInputs();
|
2021-11-15 15:49:00 +01:00
|
|
|
|
2021-11-18 14:25:47 +01:00
|
|
|
bool valid() const;
|
|
|
|
|
|
2021-12-09 16:39:00 +01:00
|
|
|
/**
|
|
|
|
|
* When the user selects another wallet (via Payment::setCurrentAccount)
|
|
|
|
|
* default implementation is empty.
|
|
|
|
|
*/
|
|
|
|
|
virtual void setWallet(Wallet *wallet);
|
|
|
|
|
|
2021-11-16 19:14:45 +01:00
|
|
|
protected:
|
|
|
|
|
void setValid(bool valid);
|
|
|
|
|
|
2021-11-15 15:49:00 +01:00
|
|
|
signals:
|
|
|
|
|
void collapsableChanged();
|
|
|
|
|
void collapsedChanged();
|
2021-11-16 19:14:45 +01:00
|
|
|
void validChanged();
|
2021-11-15 15:49:00 +01:00
|
|
|
|
2021-11-17 14:24:19 +01:00
|
|
|
void maxAllowedChanged();
|
|
|
|
|
|
2021-11-15 15:49:00 +01:00
|
|
|
private:
|
|
|
|
|
const Payment::DetailType m_type;
|
|
|
|
|
bool m_collapsable = true;
|
|
|
|
|
bool m_collapsed = false;
|
2021-11-16 19:14:45 +01:00
|
|
|
bool m_valid = false; // when all user-input is valid
|
2021-11-15 15:49:00 +01:00
|
|
|
};
|
|
|
|
|
|
2020-10-14 15:12:33 +02:00
|
|
|
#endif
|