Files
pay/src/PriceDataProvider.cpp
T

370 lines
13 KiB
C++
Raw Permalink Normal View History

2021-05-07 19:47:11 +02:00
/*
* This file is part of the Flowee project
2024-01-28 20:02:36 +01:00
* Copyright (C) 2021-2024 Tom Zander <tom@flowee.org>
2021-05-07 19:47:11 +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/>.
*/
#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>
2023-04-05 19:41:40 +02:00
#include <cmath>
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
2023-08-27 23:01:23 +02:00
// Yadio (conversion from USD to various currencies)
static const char * YadioURL = "https://api.yadio.io/exrates/USD";
static const char * YadioURLJSONRoot = "USD";
// Yadio end
2021-05-07 19:47:11 +02:00
static constexpr int ReloadTimeout = 7 * 60 * 1000;
PriceDataProvider::PriceDataProvider(const QString &countryCode, QObject *parent) : QObject(parent)
2021-05-07 19:47:11 +02:00
{
if (countryCode.isEmpty())
setCurrency(QLocale::system());
else
setCountry(countryCode);
QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(fetch()));
}
void PriceDataProvider::start()
{
if (m_priceHistory.get())
m_priceHistory->initialPopulate();
m_timer.start(ReloadTimeout);
fetch();
}
void PriceDataProvider::setCurrency(const QLocale &countryLocale)
{
auto newCurrency = countryLocale.currencySymbol(QLocale::CurrencyIsoCode);
if (m_currency == newCurrency)
return;
m_currency = newCurrency;
m_currencySymbolPrefix = countryLocale.currencySymbol(QLocale::CurrencySymbol);
m_currencySymbolPost.clear();
2021-05-08 12:22:44 +02:00
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")
2023-02-08 15:06:26 +01:00
|| m_currency == QLatin1String("NOK")
2021-05-08 12:22:44 +02:00
|| m_currency == QLatin1String("PYG")
|| m_currency == QLatin1String("RUB")
|| m_currency == QLatin1String("VND")) {
2023-02-08 15:06:26 +01:00
// these currencies format the name after the numbers part.
m_currencySymbolPost = QString(" ") + m_currencySymbolPrefix;
m_currencySymbolPrefix.clear();
2021-05-08 12:22:44 +02:00
}
2021-06-07 11:35:54 +02:00
// drop the '.00' behind the prices as this country doesn't traditionlly do that
2022-04-30 15:01:45 +02:00
m_displayCents = !(m_currency == QLatin1String("JPY")
|| m_currency == QLatin1String("NOK")
|| m_currency == QLatin1String("ARS"));
2023-06-19 22:47:15 +02:00
if (m_currency == QLatin1String("CHF")) {
m_currencySymbolPrefix += QLatin1String(" ");
}
2023-08-27 23:01:23 +02:00
// some currencies need conversion from USD
if (m_currency == QLatin1String("ARS")
|| m_currency == QLatin1String("VES")) {
m_yadioViaUSD = true;
}
emit currencySymbolChanged();
if (!m_basedir.isEmpty()) {
assert(m_priceHistory.get());
// we need to replace the history of our coin to the new history of the new coin.
loadPriceHistory(m_basedir);
if (m_timer.isActive()) // implies we are start()-ed
start();
}
2023-02-08 14:29:02 +01:00
emit priceChanged(m_currentPrice.price);
2021-05-07 19:47:11 +02:00
}
void PriceDataProvider::setCountry(const QString &countrycode)
2021-05-07 19:47:11 +02:00
{
setCurrency(QLocale(countrycode));
2021-11-23 17:16:45 +01:00
}
2021-05-07 19:47:11 +02:00
2023-06-21 19:40:15 +02:00
QString PriceDataProvider::formattedPrice(double amountSats, int64_t price) const
2021-05-08 00:03:07 +02:00
{
if (price == 0)
return QString();
return formattedPrice(priceFor(amountSats, price));
}
2023-06-21 19:40:15 +02:00
int64_t PriceDataProvider::priceFor(double amountSats, int64_t price) const
{
2023-04-05 19:41:40 +02:00
if (std::isnan(amountSats))
return 0;
2021-05-08 00:03:07 +02:00
qint64 fiatValue = amountSats * price;
2023-06-21 19:40:15 +02:00
return (fiatValue + (amountSats > 0 ? 50000000: -50000000)) / qint64(100000000);
}
2021-05-08 00:03:07 +02:00
2023-06-21 19:40:15 +02:00
QString PriceDataProvider::formattedPrice(int64_t fiatValue) const
{
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));
2022-08-20 18:23:51 +02:00
const int offset = std::min<int>(2, centsPrice.length() - 3); // number of those zeros to cut off again.
2021-07-30 15:20:18 +02:00
const int length = centsPrice.length() - 2 - offset;
2022-08-20 18:23:51 +02:00
QString actualPrice = centsPrice.mid(offset, length);
2021-07-30 15:20:18 +02:00
// group size is 3
if (length > 3 && length <= 15) {
// lets insert some thousands separators.
char buf[21];
memset(buf, 0, sizeof(buf));
int add = 0;
for (int i = 0; i < actualPrice.length(); ++i) {
// insert thousands separators.
if (i > 0 && actualPrice.length() % 3 == i % 3)
2022-08-20 18:23:51 +02:00
buf[i + add++] = QLocale::system().groupSeparator().at(0).toLatin1();
2021-07-30 15:20:18 +02:00
assert(actualPrice.at(i).unicode() < 127); // only digits
buf[i + add] = actualPrice.at(i).unicode();
}
2023-03-12 20:37:39 +01:00
actualPrice = QString::fromLatin1(buf);
2021-07-30 15:20:18 +02:00
}
2021-05-08 00:03:07 +02:00
2022-04-30 15:01:45 +02:00
if (m_displayCents) {
2023-02-08 15:06:26 +01:00
return m_currencySymbolPrefix
% (fiatValue < 0 ? QLatin1String("-") : QLatin1String(""))
2021-07-30 15:20:18 +02:00
% actualPrice
2021-06-07 11:35:54 +02:00
% QLocale::system().decimalPoint()
% centsPrice.right(2)
% m_currencySymbolPost;
}
else {
2023-11-03 14:48:32 +01:00
return (fiatValue < 0 ? QLatin1String("-") : QLatin1String(""))
% m_currencySymbolPrefix
2021-07-30 15:20:18 +02:00
% actualPrice
2021-06-07 11:35:54 +02:00
% m_currencySymbolPost;
}
2021-05-08 00:03:07 +02:00
}
2023-02-03 19:59:02 +01:00
int PriceDataProvider::historicalPrice(const QDateTime &timestamp) const
{
if (m_priceHistory.get() == nullptr)
return m_currentPrice.price;
2023-03-09 22:44:17 +01:00
return m_priceHistory->historicalPrice(timestamp.toSecsSinceEpoch(),
2023-03-09 22:53:56 +01:00
PriceHistoryDataProvider::Nearest);
}
int PriceDataProvider::historicalPriceAccurate(const QDateTime &timestamp) const
{
if (m_priceHistory.get() == nullptr)
return 0;
return m_priceHistory->historicalPrice(timestamp.toSecsSinceEpoch(),
PriceHistoryDataProvider::Accurate);
2023-02-03 19:59:02 +01:00
}
2023-03-09 22:44:17 +01:00
int PriceDataProvider::historicalPriceAccurate(int days) const
2023-02-19 18:42:33 +01:00
{
if (days < 0 || days > 3000)
throw std::runtime_error("Invalid number of days ago");
QDateTime now = QDateTime::currentDateTimeUtc();
2023-03-09 22:44:17 +01:00
if (m_priceHistory.get() == nullptr)
return 0;
return m_priceHistory->historicalPrice(now.addDays(days * -1).toSecsSinceEpoch(),
PriceHistoryDataProvider::Accurate);
2023-02-19 18:42:33 +01:00
}
2023-06-21 19:40:15 +02:00
QString PriceDataProvider::priceToStringSimple(int64_t cents) const
{
auto value = QString::number(cents);
if (!m_displayCents)
return value.left(value.size() - 2);
const QChar comma = QLocale::system().decimalPoint().at(0);
if (cents < 10)
return "0" % comma % "0" % value;
if (cents < 100)
return "0" % comma % value;
return value.left(value.size() - 2) % comma % value.right(2);
}
2021-05-07 19:47:11 +02:00
void PriceDataProvider::fetch()
{
2024-01-28 20:02:36 +01:00
if (m_reply)
return;
2023-08-27 23:01:23 +02:00
if (m_yadioViaUSD && m_currencyConversions.isEmpty()) {
m_reply = m_network.get(QNetworkRequest(QUrl(YadioURL)));
logInfo() << "fetching yadio.io USD-conversions";
connect(m_reply, SIGNAL(finished()), this, SLOT(finishedYadioDownload()));
return; // one feed at a time.
}
2021-05-07 19:47:11 +02:00
QString url(CoinGeckoURL);
2023-08-27 23:01:23 +02:00
url = url.arg(m_yadioViaUSD ? "usd" : m_currency.toLower());
logInfo() << "fetch" << url;
m_reply = m_network.get(QNetworkRequest(QUrl(url)));
2021-05-07 19:47:11 +02:00
connect(m_reply, SIGNAL(finished()), this, SLOT(finishedDownload()));
}
void PriceDataProvider::finishedDownload()
{
2023-02-08 14:29:02 +01:00
logInfo() << "finishDownload";
2021-11-02 14:08:50 +01:00
if (m_reply == nullptr)
return;
2022-04-29 15:18:01 +02:00
const auto data = m_reply->readAll();
const bool failed = m_reply->error() != QNetworkReply::NoError || data.isEmpty();
2023-02-08 14:29:02 +01:00
if (failed)
logCritical() << " failed";
2021-05-08 14:04:00 +02:00
m_reply->deleteLater();
m_reply = nullptr;
2022-04-29 15:18:01 +02:00
if (failed) {
m_timer.stop();
if (m_failedCount++ < 5) {
// things like a DNS caching server or a flaky wifi can add a lot
// of delays between a fetch() and a failed reply coming back.
// So we take a middle road that works for all cases, the first 5
// times we instantly check again. In the case of networking delays, those
// will be our waiting time.
// otherwise, should it return instantly, we just cycle through those 5 in
// a second and then we poll every 20 seconds below.
fetch();
} else {
m_timer.start(20 * 1000);
}
return;
}
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();
2023-08-27 23:01:23 +02:00
auto price = section.value(m_yadioViaUSD ? QLatin1String("usd") : m_currency.toLower());
2021-05-08 14:04:00 +02:00
if (price.isUndefined()) { // our provider does not support this coin.
2023-02-08 14:29:02 +01:00
logCritical() << " provider does not support this coin" << m_currency;
2023-08-27 23:01:23 +02:00
assert(!m_yadioViaUSD);
2023-02-08 14:29:02 +01:00
setCountry("en_US");
2021-05-08 14:04:00 +02:00
return;
}
2023-08-27 23:01:23 +02:00
if (m_yadioViaUSD) {
auto multiplier = m_currencyConversions.value(m_currency);
if (multiplier == 0) {
logCritical() << "Currency conversion not available in the yadio feed";
setCountry("en_US");
return;
}
m_currentPrice.price = std::round(price.toDouble() * 100 * multiplier);
} else {
m_currentPrice.price = price.toDouble() * 100;
}
2021-05-07 19:47:11 +02:00
m_currentPrice.timestamp = time(nullptr);
2022-04-30 15:01:45 +02:00
emit priceChanged(m_currentPrice.price);
2021-05-07 19:47:11 +02:00
} catch (const std::runtime_error &error) {
logWarning() << "PriceDataProvider failed." << error.what();
if (!data.isEmpty())
logInfo() << QString::fromUtf8(data);
m_timer.start(20 * 1000);
return;
2021-05-07 19:47:11 +02:00
}
2023-03-29 16:06:12 +02:00
logCritical().nospace() << "Current fiat price: " << m_currencySymbolPrefix << m_currentPrice.price << m_currencySymbolPost;
m_timer.start(ReloadTimeout);
2021-05-07 19:47:11 +02:00
}
2021-11-03 18:30:24 +01:00
2023-08-27 23:01:23 +02:00
void PriceDataProvider::finishedYadioDownload()
{
logInfo();
assert(m_reply);
const auto data = m_reply->readAll();
const bool failed = m_reply->error() != QNetworkReply::NoError || data.isEmpty();
if (failed)
logCritical() << " failed";
m_reply->deleteLater();
m_reply = nullptr;
if (failed) {
m_timer.stop();
if (m_failedCount++ < 5)
fetch();
else
m_timer.start(20 * 1000);
return;
}
try {
QJsonDocument doc = QJsonDocument::fromJson(data);
if (doc.isEmpty())
throw std::runtime_error("Failed parsing the yadio data");
m_currencyConversions.clear();
auto root = doc.object();
auto prices = root.value(YadioURLJSONRoot).toObject();
for (auto i = prices.begin(); i != prices.end(); ++i) {
if (i.key().size() != 3)
continue;
auto value = i.value().toDouble(-1);
if (value > 0)
m_currencyConversions.insert(i.key(), value);
}
logInfo() << "got" << m_currencyConversions.size() << "values";
logInfo() << m_currencyConversions.value("ARS");
fetch(); // fetch the CoinGecko one next.
} catch (const std::runtime_error &error) {
logWarning() << "PriceDataProvider Yadio feed failed:" << error.what();
m_timer.start(200 * 1000);
}
}
QString PriceDataProvider::currencySymbolPost() const
{
return m_currencySymbolPost;
}
2023-02-03 19:59:02 +01:00
void PriceDataProvider::loadPriceHistory(const QString &basedir)
{
m_basedir = basedir;
m_priceHistory.reset(new PriceHistoryDataProvider(basedir, m_currency));
2023-02-03 19:59:02 +01:00
// take the last known price from our historical module to have something
// mostly useful until we manage to fetch the data from the life feeds.
auto lastKnownPrice = historicalPrice(QDateTime::currentDateTimeUtc());
if (lastKnownPrice == 0)
lastKnownPrice = 10000; // if we never fetched, set to 100,-
m_currentPrice.price = lastKnownPrice;
2023-02-03 19:59:02 +01:00
connect (this, &PriceDataProvider::priceChanged,
m_priceHistory.get(), [=](int price) {
2023-05-06 12:45:33 +02:00
if (price != 10000) // ignore the magic starting price
m_priceHistory->addPrice(currencyName(), QDateTime::currentSecsSinceEpoch(), price);
2023-02-03 19:59:02 +01:00
});
}
QString PriceDataProvider::currencySymbolPrefix() const
{
return m_currencySymbolPrefix;
}
2022-04-30 15:01:45 +02:00
bool PriceDataProvider::displayCents() const
2021-11-03 18:30:24 +01:00
{
2022-04-30 15:01:45 +02:00
return m_displayCents;
2021-11-03 18:30:24 +01:00
}