Files
pay/PriceDataProvider.cpp
T

122 lines
4.4 KiB
C++
Raw Permalink Normal View History

2021-05-07 19:47:11 +02:00
/*
* This file is part of the Flowee project
* Copyright (C) 2021 Tom Zander <tom@flowee.org>
*
* 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/>.
*/
#include "PriceDataProvider.h"
2021-05-08 00:03:07 +02:00
#include "Logger.h"
2021-05-07 19:47:11 +02:00
#include <QLocale>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
2021-05-08 14:04:24 +02:00
#include <QStringBuilder>
2021-05-07 19:47:11 +02:00
// CoinGecko
static const char *CoinGeckoURL = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin-cash&vs_currencies=%1";
static const char *CoinGeckoJSONRoot = "bitcoin-cash";
// CoinGecko end
PriceDataProvider::PriceDataProvider(QObject *parent) : QObject(parent)
{
2021-05-08 12:22:44 +02:00
QLocale here(QLocale::system());
m_currency = here.currencySymbol(QLocale::CurrencyIsoCode);
m_currencySymbolPrefix = here.currencySymbol(QLocale::CurrencySymbol);
if (m_currency == QLatin1String("AZN")
|| m_currency == QLatin1String("ILS")
|| m_currency == QLatin1String("IRR")
|| m_currency == QLatin1String("KHR")
|| m_currency == QLatin1String("KZT")
|| m_currency == QLatin1String("PLN")
|| m_currency == QLatin1String("PYG")
|| m_currency == QLatin1String("RUB")
|| m_currency == QLatin1String("VND")) {
// these currencies format the symbol after the numbers part.
std::swap(m_currencySymbolPost, m_currencySymbolPrefix);
}
2021-05-07 19:47:11 +02:00
m_timer.setInterval(5 * 60 * 1000);
QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(fetch()));
}
void PriceDataProvider::start()
{
2021-05-08 12:22:44 +02:00
m_timer.start();
fetch();
2021-05-07 19:47:11 +02:00
}
2021-05-08 00:03:07 +02:00
QString PriceDataProvider::formattedPrice(double amountSats, int price) const
{
if (price == 0)
return QString();
qint64 fiatValue = amountSats * price;
2021-05-08 11:11:03 +02:00
fiatValue = (fiatValue + 50000000) / qint64(100000000);
2021-05-08 00:03:07 +02:00
// convert cheaply (low number of mallocs) to a price.
// since our fiat is in cents, we assume we may add up to two leading zeros.
static const QString priceTemplate("00%1");
QString centsPrice = priceTemplate.arg(std::abs(fiatValue));
const int offset = std::min(2, centsPrice.length() - 3); // number of those zeros to cut off again.
2021-05-08 12:22:44 +02:00
return m_currencySymbolPrefix
2021-05-08 00:03:07 +02:00
% (fiatValue < 0 ? QLatin1String("-") : QLatin1String(""))
% centsPrice.mid(offset, centsPrice.length() - 2 - offset)
% QLocale::system().decimalPoint()
2021-05-08 12:22:44 +02:00
% centsPrice.right(2)
% m_currencySymbolPost;
2021-05-08 00:03:07 +02:00
}
2021-05-07 19:47:11 +02:00
void PriceDataProvider::fetch()
{
QString url(CoinGeckoURL);
QNetworkRequest req(QUrl(url.arg(m_currency.toLower())));
m_reply = m_network.get(req);
connect(m_reply, SIGNAL(finished()), this, SLOT(finishedDownload()));
}
void PriceDataProvider::finishedDownload()
{
assert(m_reply);
auto data = m_reply->readAll();
2021-05-08 14:04:00 +02:00
m_reply->deleteLater();
m_reply = nullptr;
2021-05-07 19:47:11 +02:00
try {
QJsonDocument doc = QJsonDocument::fromJson(data);
if (doc.isEmpty())
throw std::runtime_error("Failed parsing the data");
auto root = doc.object();
// coingecko
auto section = root.value(CoinGeckoJSONRoot).toObject();
auto price = section.value(m_currency.toLower());
2021-05-08 14:04:00 +02:00
if (price.isUndefined()) { // our provider does not support this coin.
if (m_currency != "USD") {
m_currency = "USD";
m_currencySymbolPost.clear();
m_currencySymbolPrefix = "$";
fetch();
}
return;
}
2021-05-07 19:47:11 +02:00
m_currentPrice.price = price.toDouble() * 100;
m_currentPrice.timestamp = time(nullptr);
emit priceChanged();
} catch (const std::runtime_error &error) {
qWarning() << error.what();
qWarning() << QString::fromUtf8(data);
}
2021-05-08 12:22:44 +02:00
logInfo() << "Current fiat price: " << m_currencySymbolPrefix << m_currentPrice.price << m_currencySymbolPost;
2021-05-07 19:47:11 +02:00
}