add execute method to allow autonomous Txns
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,6 +4,7 @@ release/
|
|||||||
x64/
|
x64/
|
||||||
artifacts/
|
artifacts/
|
||||||
.vscode/
|
.vscode/
|
||||||
|
res/libsodium/libsodium*
|
||||||
src/ui_*.h
|
src/ui_*.h
|
||||||
*.autosave
|
*.autosave
|
||||||
src/precompiled.h.cpp
|
src/precompiled.h.cpp
|
||||||
|
|||||||
@@ -616,17 +616,20 @@ void MainWindow::postToZBoard() {
|
|||||||
tx.toAddrs.push_back(ToFields{ toAddr, Settings::getZboardAmount(), memo, memo.toUtf8().toHex() });
|
tx.toAddrs.push_back(ToFields{ toAddr, Settings::getZboardAmount(), memo, memo.toUtf8().toHex() });
|
||||||
tx.fee = Settings::getMinerFee();
|
tx.fee = Settings::getMinerFee();
|
||||||
|
|
||||||
json params = json::array();
|
|
||||||
rpc->fillTxJsonParams(params, tx);
|
|
||||||
std::cout << std::setw(2) << params << std::endl;
|
|
||||||
|
|
||||||
// And send the Tx
|
// And send the Tx
|
||||||
rpc->sendZTransaction(params, [=](const json& reply) {
|
rpc->executeTransaction(tx, [=] (QString opid) {
|
||||||
QString opid = QString::fromStdString(reply.get<json::string_t>());
|
|
||||||
ui->statusBar->showMessage(tr("Computing Tx: ") % opid);
|
ui->statusBar->showMessage(tr("Computing Tx: ") % opid);
|
||||||
|
},
|
||||||
|
[=] (QString opid, QString txid) {
|
||||||
|
ui->statusBar->showMessage(Settings::txidStatusMessage + " " + txid);
|
||||||
|
},
|
||||||
|
[=] (QString opid, QString errStr) {
|
||||||
|
ui->statusBar->showMessage(QObject::tr(" Tx ") % opid % QObject::tr(" failed"), 15 * 1000);
|
||||||
|
|
||||||
// And then start monitoring the transaction
|
if (!opid.isEmpty())
|
||||||
rpc->addNewTxToWatch(tx, opid);
|
errStr = QObject::tr("The transaction with id ") % opid % QObject::tr(" failed. The error was") + ":\n\n" + errStr;
|
||||||
|
|
||||||
|
QMessageBox::critical(this, QObject::tr("Transaction Error"), errStr, QMessageBox::Ok);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
62
src/rpc.cpp
62
src/rpc.cpp
@@ -213,7 +213,8 @@ void RPC::getTransactions(const std::function<void(json)>& cb) {
|
|||||||
conn->doRPCWithDefaultErrorHandling(payload, cb);
|
conn->doRPCWithDefaultErrorHandling(payload, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RPC::sendZTransaction(json params, const std::function<void(json)>& cb) {
|
void RPC::sendZTransaction(json params, const std::function<void(json)>& cb,
|
||||||
|
const std::function<void(QString)>& err) {
|
||||||
json payload = {
|
json payload = {
|
||||||
{"jsonrpc", "1.0"},
|
{"jsonrpc", "1.0"},
|
||||||
{"id", "someid"},
|
{"id", "someid"},
|
||||||
@@ -221,7 +222,13 @@ void RPC::sendZTransaction(json params, const std::function<void(json)>& cb) {
|
|||||||
{"params", params}
|
{"params", params}
|
||||||
};
|
};
|
||||||
|
|
||||||
conn->doRPCWithDefaultErrorHandling(payload, cb);
|
conn->doRPC(payload, cb, [=] (auto reply, auto parsed) {
|
||||||
|
if (!parsed.is_discarded() && !parsed["error"]["message"].is_null()) {
|
||||||
|
err(QString::fromStdString(parsed["error"]["message"]));
|
||||||
|
} else {
|
||||||
|
err(reply->errorString());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -831,12 +838,36 @@ void RPC::refreshSentZTrans() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RPC::addNewTxToWatch(Tx tx, const QString& newOpid) {
|
void RPC::addNewTxToWatch(const QString& newOpid, WatchedTx wtx) {
|
||||||
watchingOps.insert(newOpid, tx);
|
watchingOps.insert(newOpid, wtx);
|
||||||
|
|
||||||
watchTxStatus();
|
watchTxStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Execute a transaction!
|
||||||
|
void RPC::executeTransaction(Tx tx,
|
||||||
|
const std::function<void(QString opid)> submitted,
|
||||||
|
const std::function<void(QString opid, QString txid)> computed,
|
||||||
|
const std::function<void(QString opid, QString errStr)> error) {
|
||||||
|
// First, create the json params
|
||||||
|
json params = json::array();
|
||||||
|
fillTxJsonParams(params, tx);
|
||||||
|
std::cout << std::setw(2) << params << std::endl;
|
||||||
|
|
||||||
|
sendZTransaction(params, [=](const json& reply) {
|
||||||
|
QString opid = QString::fromStdString(reply.get<json::string_t>());
|
||||||
|
|
||||||
|
// And then start monitoring the transaction
|
||||||
|
addNewTxToWatch( opid, WatchedTx { opid, tx, computed, error} );
|
||||||
|
submitted(opid);
|
||||||
|
},
|
||||||
|
[=](QString errStr) {
|
||||||
|
error("", errStr);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void RPC::watchTxStatus() {
|
void RPC::watchTxStatus() {
|
||||||
if (conn == nullptr)
|
if (conn == nullptr)
|
||||||
return noConnection();
|
return noConnection();
|
||||||
@@ -856,35 +887,26 @@ void RPC::watchTxStatus() {
|
|||||||
if (watchingOps.contains(id)) {
|
if (watchingOps.contains(id)) {
|
||||||
// And if it ended up successful
|
// And if it ended up successful
|
||||||
QString status = QString::fromStdString(it["status"]);
|
QString status = QString::fromStdString(it["status"]);
|
||||||
|
main->loadingLabel->setVisible(false);
|
||||||
|
|
||||||
if (status == "success") {
|
if (status == "success") {
|
||||||
auto txid = QString::fromStdString(it["result"]["txid"]);
|
auto txid = QString::fromStdString(it["result"]["txid"]);
|
||||||
|
|
||||||
SentTxStore::addToSentTx(watchingOps.value(id), txid);
|
SentTxStore::addToSentTx(watchingOps[id].tx, txid);
|
||||||
|
|
||||||
main->ui->statusBar->showMessage(Settings::txidStatusMessage + " " + txid);
|
|
||||||
main->loadingLabel->setVisible(false);
|
|
||||||
|
|
||||||
|
auto wtx = watchingOps[id];
|
||||||
watchingOps.remove(id);
|
watchingOps.remove(id);
|
||||||
|
wtx.completed(id, txid);
|
||||||
|
|
||||||
// Refresh balances to show unconfirmed balances
|
// Refresh balances to show unconfirmed balances
|
||||||
refresh(true);
|
refresh(true);
|
||||||
} else if (status == "failed") {
|
} else if (status == "failed") {
|
||||||
// If it failed, then we'll actually show a warning.
|
// If it failed, then we'll actually show a warning.
|
||||||
auto errorMsg = QString::fromStdString(it["error"]["message"]);
|
auto errorMsg = QString::fromStdString(it["error"]["message"]);
|
||||||
QMessageBox msg(
|
|
||||||
QMessageBox::Critical,
|
|
||||||
QObject::tr("Transaction Error"),
|
|
||||||
QObject::tr("The transaction with id ") % id % QObject::tr(" failed. The error was") + ":\n\n" + errorMsg,
|
|
||||||
QMessageBox::Ok,
|
|
||||||
main
|
|
||||||
);
|
|
||||||
|
|
||||||
|
auto wtx = watchingOps[id];
|
||||||
watchingOps.remove(id);
|
watchingOps.remove(id);
|
||||||
|
wtx.error(id, errorMsg);
|
||||||
main->ui->statusBar->showMessage(QObject::tr(" Tx ") % id % QObject::tr(" failed"), 15 * 1000);
|
|
||||||
main->loadingLabel->setVisible(false);
|
|
||||||
|
|
||||||
msg.exec();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
18
src/rpc.h
18
src/rpc.h
@@ -24,6 +24,13 @@ struct TransactionItem {
|
|||||||
QString memo;
|
QString memo;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct WatchedTx {
|
||||||
|
QString opid;
|
||||||
|
Tx tx;
|
||||||
|
std::function<void(QString, QString)> completed;
|
||||||
|
std::function<void(QString, QString)> error;
|
||||||
|
};
|
||||||
|
|
||||||
class RPC
|
class RPC
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -42,10 +49,15 @@ public:
|
|||||||
void refreshZECPrice();
|
void refreshZECPrice();
|
||||||
void getZboardTopics(std::function<void(QMap<QString, QString>)> cb);
|
void getZboardTopics(std::function<void(QMap<QString, QString>)> cb);
|
||||||
|
|
||||||
|
void executeTransaction(Tx tx,
|
||||||
|
const std::function<void(QString opid)> submitted,
|
||||||
|
const std::function<void(QString opid, QString txid)> computed,
|
||||||
|
const std::function<void(QString opid, QString errStr)> error);
|
||||||
|
|
||||||
void fillTxJsonParams(json& params, Tx tx);
|
void fillTxJsonParams(json& params, Tx tx);
|
||||||
void sendZTransaction (json params, const std::function<void(json)>& cb);
|
void sendZTransaction(json params, const std::function<void(json)>& cb, const std::function<void(QString)>& err);
|
||||||
void watchTxStatus();
|
void watchTxStatus();
|
||||||
void addNewTxToWatch(Tx tx, const QString& newOpid);
|
void addNewTxToWatch(const QString& newOpid, WatchedTx wtx);
|
||||||
|
|
||||||
const TxTableModel* getTransactionsModel() { return transactionsTableModel; }
|
const TxTableModel* getTransactionsModel() { return transactionsTableModel; }
|
||||||
const QList<QString>* getAllZAddresses() { return zaddresses; }
|
const QList<QString>* getAllZAddresses() { return zaddresses; }
|
||||||
@@ -98,7 +110,7 @@ private:
|
|||||||
QMap<QString, bool>* usedAddresses = nullptr;
|
QMap<QString, bool>* usedAddresses = nullptr;
|
||||||
QList<QString>* zaddresses = nullptr;
|
QList<QString>* zaddresses = nullptr;
|
||||||
|
|
||||||
QMap<QString, Tx> watchingOps;
|
QMap<QString, WatchedTx> watchingOps;
|
||||||
|
|
||||||
TxTableModel* transactionsTableModel = nullptr;
|
TxTableModel* transactionsTableModel = nullptr;
|
||||||
BalancesTableModel* balancesTableModel = nullptr;
|
BalancesTableModel* balancesTableModel = nullptr;
|
||||||
|
|||||||
@@ -620,18 +620,23 @@ void MainWindow::sendButton() {
|
|||||||
|
|
||||||
// Show a dialog to confirm the Tx
|
// Show a dialog to confirm the Tx
|
||||||
if (confirmTx(tx)) {
|
if (confirmTx(tx)) {
|
||||||
json params = json::array();
|
|
||||||
rpc->fillTxJsonParams(params, tx);
|
|
||||||
std::cout << std::setw(2) << params << std::endl;
|
|
||||||
|
|
||||||
// And send the Tx
|
// And send the Tx
|
||||||
rpc->sendZTransaction(params, [=](const json& reply) {
|
rpc->executeTransaction(tx,
|
||||||
QString opid = QString::fromStdString(reply.get<json::string_t>());
|
[=] (QString opid) {
|
||||||
ui->statusBar->showMessage(tr("Computing Tx: ") % opid);
|
ui->statusBar->showMessage(tr("Computing Tx: ") % opid);
|
||||||
|
},
|
||||||
|
[=] (QString opid, QString txid) {
|
||||||
|
ui->statusBar->showMessage(Settings::txidStatusMessage + " " + txid);
|
||||||
|
},
|
||||||
|
[=] (QString opid, QString errStr) {
|
||||||
|
ui->statusBar->showMessage(QObject::tr(" Tx ") % opid % QObject::tr(" failed"), 15 * 1000);
|
||||||
|
|
||||||
// And then start monitoring the transaction
|
if (!opid.isEmpty())
|
||||||
rpc->addNewTxToWatch(tx, opid);
|
errStr = QObject::tr("The transaction with id ") % opid % QObject::tr(" failed. The error was") + ":\n\n" + errStr;
|
||||||
});
|
|
||||||
|
QMessageBox::critical(this, QObject::tr("Transaction Error"), errStr, QMessageBox::Ok);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -351,17 +351,19 @@ void Turnstile::executeMigrationStep() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Turnstile::doSendTx(Tx tx, std::function<void(void)> cb) {
|
void Turnstile::doSendTx(Tx tx, std::function<void(void)> cb) {
|
||||||
json params = json::array();
|
rpc->executeTransaction(tx, [=] (QString opid) {
|
||||||
rpc->fillTxJsonParams(params, tx);
|
mainwindow->ui->statusBar->showMessage(QObject::tr("Computing Tx: ") % opid);
|
||||||
std::cout << std::setw(2) << params << std::endl;
|
},
|
||||||
rpc->sendZTransaction(params, [=] (const json& reply) {
|
[=] (QString opid, QString txid) {
|
||||||
QString opid = QString::fromStdString(reply.get<json::string_t>());
|
mainwindow->ui->statusBar->showMessage(Settings::txidStatusMessage + " " + txid);
|
||||||
//qDebug() << opid;
|
},
|
||||||
mainwindow->ui->statusBar->showMessage(QObject::tr("Computing Tx: ") % opid);
|
[=] (QString opid, QString errStr) {
|
||||||
|
mainwindow->ui->statusBar->showMessage(QObject::tr(" Tx ") % opid % QObject::tr(" failed"), 15 * 1000);
|
||||||
|
|
||||||
// And then start monitoring the transaction
|
if (!opid.isEmpty())
|
||||||
rpc->addNewTxToWatch(tx, opid);
|
errStr = QObject::tr("The transaction with id ") % opid % QObject::tr(" failed. The error was") + ":\n\n" + errStr;
|
||||||
|
|
||||||
|
QMessageBox::critical(mainwindow, QObject::tr("Transaction Error"), errStr, QMessageBox::Ok);
|
||||||
|
});
|
||||||
|
|
||||||
cb();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user