Fetch ZEC Price for display purposes
This commit is contained in:
@@ -33,7 +33,7 @@ You need a C++14 compatible compiler like g++ or clang++
|
||||
```
|
||||
git clone https://github.com/adityapk00/zcash-qt-wallet.git
|
||||
cd zcash-qt-wallet
|
||||
/path/to/qt5/bin/qmake zcash-qt-wallet.pro CONFIG+=DEBUG
|
||||
/path/to/qt5/bin/qmake zcash-qt-wallet.pro CONFIG+=debug
|
||||
make -j$(nproc)
|
||||
|
||||
./zcash-qt-wallet
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "balancestablemodel.h"
|
||||
#include "settings.h"
|
||||
#include "utils.h"
|
||||
|
||||
BalancesTableModel::BalancesTableModel(QObject *parent)
|
||||
@@ -68,12 +69,22 @@ QVariant BalancesTableModel::data(const QModelIndex &index, int role) const
|
||||
return b;
|
||||
}
|
||||
|
||||
if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
|
||||
if (role == Qt::DisplayRole) {
|
||||
switch (index.column()) {
|
||||
case 0: return std::get<0>(modeldata->at(index.row()));
|
||||
case 1: return QVariant(std::get<1>(modeldata->at(index.row())) % " " % Utils::getTokenName());
|
||||
}
|
||||
}
|
||||
|
||||
if(role == Qt::ToolTipRole) {
|
||||
switch (index.column()) {
|
||||
case 0: return std::get<0>(modeldata->at(index.row()));
|
||||
case 1: {
|
||||
auto bal = std::get<1>(modeldata->at(index.row())).toDouble();
|
||||
return "$ " + QString::number(bal * Settings::getInstance()->getZECPrice(), 'f', 2); // Use 'f' notation to get 2 decimal places
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
@@ -58,7 +58,9 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
setupBalancesTab();
|
||||
|
||||
rpc = new RPC(new QNetworkAccessManager(this), this);
|
||||
rpc->refresh();
|
||||
rpc->refreshZECPrice();
|
||||
|
||||
rpc->refresh();
|
||||
}
|
||||
|
||||
void MainWindow::setupStatusBar() {
|
||||
|
||||
72
src/rpc.cpp
72
src/rpc.cpp
@@ -39,6 +39,13 @@ RPC::RPC(QNetworkAccessManager* client, MainWindow* main) {
|
||||
});
|
||||
// Start at every 10s. When an operation is pending, this will change to every second
|
||||
txTimer->start(Utils::updateSpeed);
|
||||
|
||||
// Set up timer to refresh Price
|
||||
priceTimer = new QTimer(main);
|
||||
QObject::connect(timer, &QTimer::timeout, [=]() {
|
||||
refreshZECPrice();
|
||||
});
|
||||
priceTimer->start(1 * 60 * 60 * 1000); // Every hour
|
||||
}
|
||||
|
||||
RPC::~RPC() {
|
||||
@@ -515,3 +522,68 @@ void RPC::refreshTxStatus(const QString& newOpid) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void RPC::refreshZECPrice() {
|
||||
QUrl cmcURL("https://api.coinmarketcap.com/v1/ticker/");
|
||||
|
||||
QNetworkRequest req;
|
||||
req.setUrl(cmcURL);
|
||||
|
||||
QNetworkReply *reply = restclient->get(req);
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
||||
reply->deleteLater();
|
||||
|
||||
try {
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
auto parsed = json::parse(reply->readAll(), nullptr, false);
|
||||
if (!parsed.is_discarded() && !parsed["error"]["message"].is_null()) {
|
||||
qDebug() << QString::fromStdString(parsed["error"]["message"]);
|
||||
} else {
|
||||
qDebug() << reply->errorString();
|
||||
}
|
||||
Settings::getInstance()->setZECPrice(0);
|
||||
return;
|
||||
}
|
||||
|
||||
auto all = reply->readAll();
|
||||
|
||||
auto parsed = json::parse(all, nullptr, false);
|
||||
if (parsed.is_discarded()) {
|
||||
Settings::getInstance()->setZECPrice(0);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const json& item : parsed.get<json::array_t>()) {
|
||||
if (item["symbol"].get<json::string_t>().compare("ZEC") == 0) {
|
||||
QString price = QString::fromStdString(item["price_usd"].get<json::string_t>());
|
||||
qDebug() << "ZEC Price=" << price;
|
||||
Settings::getInstance()->setZECPrice(price.toDouble());
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (...) {
|
||||
// If anything at all goes wrong, just set the price to 0 and move on.
|
||||
qDebug() << QString("Caught something nasty");
|
||||
}
|
||||
|
||||
// If nothing, then set the price to 0;
|
||||
Settings::getInstance()->setZECPrice(0);
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
.url("https://api.coinmarketcap.com/v1/ticker/")
|
||||
.withHeaders("Accept" -> "application/json")
|
||||
.get()
|
||||
.map {
|
||||
response => {
|
||||
val prices = response.json.as[JsArray].value
|
||||
.map(x => ((x \ "symbol").as[String], (x \ "price_usd").as[BigDecimal].setScale(2, RoundingMode.HALF_UP)))
|
||||
.toMap
|
||||
|
||||
coinCodes.map { coinCode =>
|
||||
CoinPrice(coinCode, prices.get(coinCode), Some(System.currentTimeMillis() / 1000))
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -20,6 +20,7 @@ public:
|
||||
void refresh(); // Refresh all transactions
|
||||
void refreshTxStatus(const QString& newOpid = QString()); // Refresh the status of all pending txs.
|
||||
void refreshAddresses(); // Refresh wallet Z-addrs
|
||||
void refreshZECPrice();
|
||||
|
||||
void sendZTransaction (json params, const std::function<void(json)>& cb);
|
||||
|
||||
@@ -38,8 +39,8 @@ private:
|
||||
void doSendRPC (const json& payload, const std::function<void(json)>& cb);
|
||||
|
||||
void refreshBalances();
|
||||
void refreshTransactions();
|
||||
|
||||
void refreshTransactions();
|
||||
|
||||
bool processUnspent (const json& reply);
|
||||
void updateUI (bool anyUnconfirmed);
|
||||
|
||||
@@ -52,7 +53,6 @@ private:
|
||||
void getTransactions (const std::function<void(json)>& cb);
|
||||
void getZAddresses (const std::function<void(json)>& cb);
|
||||
|
||||
|
||||
void handleConnectionError (const QString& error);
|
||||
void handleTxError (const QString& error);
|
||||
|
||||
@@ -70,6 +70,7 @@ private:
|
||||
|
||||
QTimer* timer;
|
||||
QTimer* txTimer;
|
||||
QTimer* priceTimer;
|
||||
|
||||
Ui::MainWindow* ui;
|
||||
MainWindow* main;
|
||||
|
||||
@@ -52,4 +52,5 @@ if [ -f bin/linux-zcash-qt-wallet-v$APP_VERSION.tar.gz ] ; then
|
||||
else
|
||||
echo "[ERROR]"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
@@ -31,6 +31,9 @@ public:
|
||||
|
||||
const QString& getZcashdConfLocation() { return confLocation; }
|
||||
|
||||
void setZECPrice(double p) { zecPrice = p; }
|
||||
double getZECPrice() { return zecPrice; }
|
||||
|
||||
private:
|
||||
// This class can only be accessed through Settings::getInstance()
|
||||
Settings() = default;
|
||||
@@ -48,6 +51,8 @@ private:
|
||||
|
||||
bool _isTestnet = false;
|
||||
bool _isSyncing = false;
|
||||
|
||||
double zecPrice = 0.0;
|
||||
};
|
||||
|
||||
#endif // SETTINGS_H
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "txtablemodel.h"
|
||||
#include "settings.h"
|
||||
#include "utils.h"
|
||||
|
||||
TxTableModel::TxTableModel(QObject *parent)
|
||||
@@ -53,7 +54,13 @@ void TxTableModel::setNewData(QList<TransactionItem>* data) {
|
||||
case 0: return modeldata->at(index.row()).type;
|
||||
case 1: return modeldata->at(index.row()).address;
|
||||
case 2: return modeldata->at(index.row()).datetime;
|
||||
case 3: return QVariant(QString::number(modeldata->at(index.row()).amount, 'g', 8) % " " % Utils::getTokenName());
|
||||
case 3: {
|
||||
if (role == Qt::DisplayRole)
|
||||
return QVariant(QString::number(modeldata->at(index.row()).amount, 'g', 8) % " " % Utils::getTokenName());
|
||||
else {
|
||||
return "$ " + QString::number(Settings::getInstance()->getZECPrice() * modeldata->at(index.row()).amount, 'f', 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ public:
|
||||
static double getTotalFee();
|
||||
|
||||
static const int updateSpeed = 20 * 1000; // 20 sec
|
||||
private:
|
||||
private:
|
||||
Utils() = delete;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user