From 6f79abcc14e714ec48951230416bdfdbdadc5b8f Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Thu, 18 Oct 2018 10:11:12 -0700 Subject: [PATCH] Add dev fee for testnet --- src/confirm.ui | 41 ++++++++++++++++++++++++++++++++------- src/sendtab.cpp | 32 ++++++++++++++++++++++++++----- src/settings.h | 1 - src/ui_confirm.h | 37 ++++++++++++++++++++++++++++------- src/utils.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++- src/utils.h | 7 +++++++ 6 files changed, 147 insertions(+), 21 deletions(-) diff --git a/src/confirm.ui b/src/confirm.ui index ea6cce7..6e4f00b 100644 --- a/src/confirm.ui +++ b/src/confirm.ui @@ -39,6 +39,16 @@ To + + + + Miner Amount + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + @@ -66,6 +76,30 @@ + + + + Miner Fee + + + + + + + Dev Fee + + + + + + + Dev Fee Amount + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + @@ -89,13 +123,6 @@ - - - - - - - diff --git a/src/sendtab.cpp b/src/sendtab.cpp index abe10d7..31e180b 100644 --- a/src/sendtab.cpp +++ b/src/sendtab.cpp @@ -42,7 +42,8 @@ void MainWindow::setupSendTab() { QObject::connect(ui->tabWidget, &QTabWidget::currentChanged, [=] (int pos) { if (pos == 1) { // Set the fees - ui->sendTxFees->setText("0.0001 " + Utils::getTokenName()); + ui->sendTxFees->setText(QString::number(Utils::getTotalFee(), 'g', 8) % + " " % Utils::getTokenName()); // Set focus to the first address box ui->Address1->setFocus(); @@ -221,7 +222,7 @@ void MainWindow::maxAmountChecked(int checked) { auto amt = ui->sendToWidgets->findChild(QString("Amount") % QString::number(i+1)); sumAllAmounts += amt->text().toDouble(); } - sumAllAmounts += settings->fees(); + sumAllAmounts += Utils::getTotalFee(); auto addr = ui->inputsCombo->currentText().split("(")[0]; @@ -276,6 +277,8 @@ void MainWindow::sendButton() { return; } + auto devAddress = Utils::getDevAddr(fromAddr, toAddrs); + // Get all the addresses and amounts json allRecepients = json::array(); @@ -336,6 +339,28 @@ void MainWindow::sendButton() { } } + // Add the dev fee to the transaction + if (!devAddress.isEmpty() && Utils::getDevFee() > 0) { + json devFee = json::object(); + devFee["address"] = devAddress.toStdString(); + devFee["amount"] = Utils::getDevFee(); + allRecepients.push_back(devFee); + } + + // Add two rows for fees + { + confirm.labelMinerFee->setText("Miner Fee"); + confirm.minerFee->setText(QString::number(Utils::getMinerFee(), 'g', 8) % " " % Utils::getTokenName()); + + if (!devAddress.isEmpty() && Utils::getDevFee() > 0) { + confirm.labelDevFee->setText("Dev Fee"); + confirm.devFee->setText(QString::number(Utils::getMinerFee(), 'g', 8) % " " % Utils::getTokenName()); + } else { + confirm.labelDevFee->setText(""); + confirm.devFee->setText(""); + } + } + // Add sender json params = json::array(); params.push_back(fromAddr.toStdString()); @@ -344,9 +369,6 @@ void MainWindow::sendButton() { // And show it in the confirm dialog confirm.sendFrom->setText(fnSplitAddressForWrap(fromAddr)); - // Fees in the confirm dialog - confirm.feesLabel->setText("Fee 0.0001 " % Utils::getTokenName()); - // Show the dialog and submit it if the user confirms if (d.exec() == QDialog::Accepted) { rpc->sendZTransaction(params, [=](const json& reply) { diff --git a/src/settings.h b/src/settings.h index 07ce65a..977908b 100644 --- a/src/settings.h +++ b/src/settings.h @@ -20,7 +20,6 @@ public: QString getHost(); QString getPort(); - double fees() { return 0.0001; } bool loadFromSettings(); bool loadFromFile(); diff --git a/src/ui_confirm.h b/src/ui_confirm.h index 8c4ff29..05f0771 100644 --- a/src/ui_confirm.h +++ b/src/ui_confirm.h @@ -31,12 +31,15 @@ public: QLabel *sendFrom; QGroupBox *sendToAddrs; QGridLayout *gridLayout; + QLabel *minerFee; QLabel *Addr1; QLabel *Amt1; QLabel *Memo1; + QLabel *labelMinerFee; + QLabel *labelDevFee; + QLabel *devFee; QSpacerItem *verticalSpacer; QFrame *line; - QLabel *feesLabel; QDialogButtonBox *buttonBox; void setupUi(QDialog *confirm) @@ -63,6 +66,12 @@ public: sendToAddrs->setObjectName(QStringLiteral("sendToAddrs")); gridLayout = new QGridLayout(sendToAddrs); gridLayout->setObjectName(QStringLiteral("gridLayout")); + minerFee = new QLabel(sendToAddrs); + minerFee->setObjectName(QStringLiteral("minerFee")); + minerFee->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); + + gridLayout->addWidget(minerFee, 2, 1, 1, 1); + Addr1 = new QLabel(sendToAddrs); Addr1->setObjectName(QStringLiteral("Addr1")); Addr1->setWordWrap(true); @@ -80,6 +89,22 @@ public: gridLayout->addWidget(Memo1, 1, 0, 1, 2); + labelMinerFee = new QLabel(sendToAddrs); + labelMinerFee->setObjectName(QStringLiteral("labelMinerFee")); + + gridLayout->addWidget(labelMinerFee, 2, 0, 1, 1); + + labelDevFee = new QLabel(sendToAddrs); + labelDevFee->setObjectName(QStringLiteral("labelDevFee")); + + gridLayout->addWidget(labelDevFee, 3, 0, 1, 1); + + devFee = new QLabel(sendToAddrs); + devFee->setObjectName(QStringLiteral("devFee")); + devFee->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); + + gridLayout->addWidget(devFee, 3, 1, 1, 1); + verticalLayout->addWidget(sendToAddrs); @@ -94,11 +119,6 @@ public: verticalLayout->addWidget(line); - feesLabel = new QLabel(confirm); - feesLabel->setObjectName(QStringLiteral("feesLabel")); - - verticalLayout->addWidget(feesLabel); - buttonBox = new QDialogButtonBox(confirm); buttonBox->setObjectName(QStringLiteral("buttonBox")); buttonBox->setOrientation(Qt::Horizontal); @@ -120,10 +140,13 @@ public: groupBox->setTitle(QApplication::translate("confirm", "From", nullptr)); sendFrom->setText(QString()); sendToAddrs->setTitle(QApplication::translate("confirm", "To", nullptr)); + minerFee->setText(QApplication::translate("confirm", "Miner Amount", nullptr)); Addr1->setText(QApplication::translate("confirm", "TextLabel", nullptr)); Amt1->setText(QApplication::translate("confirm", "TextLabel", nullptr)); Memo1->setText(QApplication::translate("confirm", "TextLabel", nullptr)); - feesLabel->setText(QString()); + labelMinerFee->setText(QApplication::translate("confirm", "Miner Fee", nullptr)); + labelDevFee->setText(QApplication::translate("confirm", "Dev Fee", nullptr)); + devFee->setText(QApplication::translate("confirm", "Dev Fee Amount", nullptr)); } // retranslateUi }; diff --git a/src/utils.cpp b/src/utils.cpp index a0c5b1f..ed1bb40 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,5 +1,6 @@ #include "utils.h" #include "settings.h" +#include "mainwindow.h" const QString Utils::txidStatusMessage = QString("Tx submitted (right click to copy) txid:"); @@ -9,4 +10,51 @@ const QString Utils::getTokenName() { } else { return "ZEC"; } -} \ No newline at end of file +} + +// Get the dev fee address based on the transaction +const QString Utils::getDevAddr(const QString& fromAddr, const QList& toAddrs) { + auto testnetAddrLookup = [=] (const QString& addr) -> QString { + if (addr.startsWith("ztestsapling")) { + return "ztestsapling1kdp74adyfsmm9838jaupgfyx3npgw8ut63stjjx757pc248cuc0ymzphqeux60c64qe5qt68ygh"; + } else if (addr.startsWith("zt")) { + return "ztbGDqgkmXQjheivgeirwEvJLD4SUNqsWCGwxwVg4YpGz1ARR8P2rXaptkT14z3NDKamcxNmQuvmvktyokMs7HkutRNSx1D"; + } else { + return QString(); + } + }; + + if (Settings::getInstance()->isTestnet()) { + auto devAddr = testnetAddrLookup(fromAddr); + if (!devAddr.isEmpty()) { + return devAddr; + } + + // t-Addr, find if it is going to a sprout or sapling address + for (const ToFields& to : toAddrs) { + devAddr = testnetAddrLookup(to.addr); + if (!devAddr.isEmpty()) { + return devAddr; + } + } + + // If this is a t-Addr -> t-Addr transaction, use the sapling address by default + return testnetAddrLookup("ztestsapling"); + } else { + // Mainnet doesn't have a fee yet! + return QString(); + } +} + + +double Utils::getMinerFee() { + return 0.0001; +} +double Utils::getDevFee() { + if (Settings::getInstance()->isTestnet()) { + return 0.0001; + } else { + return 0; + } +} +double Utils::getTotalFee() { return getMinerFee() + getDevFee(); } \ No newline at end of file diff --git a/src/utils.h b/src/utils.h index 5408ae1..e3121b0 100644 --- a/src/utils.h +++ b/src/utils.h @@ -3,12 +3,19 @@ #include "precompiled.h" +struct ToFields; + class Utils { public: static const QString txidStatusMessage; static const QString getTokenName(); + static const QString getDevAddr(const QString& fromAddr, const QList& toAddrs); + + static double getMinerFee(); + static double getDevFee(); + static double getTotalFee(); static const int updateSpeed = 20 * 1000; // 20 sec private: