diff --git a/src/main.cpp b/src/main.cpp index f96df19..b73e119 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,7 +31,11 @@ int main(int argc, char *argv[]) // Temp Turnstile t; - qDebug() << t.splitAmount(1245.2294371, 3); + double amt = 2329127.99999999; + qDebug() << QString::number(amt, 'f', 8) << ":"; + for (auto a : t.splitAmount(amt, 3)) { + qDebug() << QString::number(a, 'f', 8); + } return QApplication::exec(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index d03c0a6..9816d6a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -10,7 +10,6 @@ #include "utils.h" #include "senttxstore.h" - #include "precompiled.h" using json = nlohmann::json; diff --git a/src/precompiled.h b/src/precompiled.h index 976fba1..552226d 100644 --- a/src/precompiled.h +++ b/src/precompiled.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include diff --git a/src/turnstile.cpp b/src/turnstile.cpp index c442d70..bb15260 100644 --- a/src/turnstile.cpp +++ b/src/turnstile.cpp @@ -1,6 +1,6 @@ -#include "precompiled.h" #include "turnstile.h" +#include "utils.h" Turnstile::Turnstile() { } @@ -9,29 +9,53 @@ Turnstile::Turnstile() { Turnstile::~Turnstile() { } -QList Turnstile::splitAmount(double amount, int parts) { - QList amounts; +QList Turnstile::splitAmount(double amount, int parts) { + QList amounts; fillAmounts(amounts, amount, parts); // Ensure they all add up! double sumofparts = 0; for (auto a : amounts) { - sumofparts += a.toDouble(); + sumofparts += a; } Q_ASSERT(sumofparts == amount); return amounts; } -void Turnstile::fillAmounts(QList& amounts, double amount, int count) { +void Turnstile::fillAmounts(QList& amounts, double amount, int count) { if (count == 1 || amount < 1) { - amounts.push_back(QString::number(amount, 'g', 8)); + // Split the chaff. + // Chaff is all amounts lesser than 0.0001 ZEC. The chaff will be added to the + // dev fee, and is done so to protect privacy. + + // Get the rounded value to 4 decimal places (approx $0.01) + double actual = std::floor(amount * 10000) / 10000; + + // Reduce the Dev Tx fee from the amount + actual = actual - 0.0001; //Utils::getDevFee(); + + // Calculate the chaff. + double chaff = amount - actual; + + amounts.push_back(actual); + if (chaff > 0.00000001) // zcash is split down to 8 decimal places + amounts.push_back(chaff); return; } - // Get a random amount off the amount and call recursively. + // Get a random amount off the amount (between half and full) and call recursively. auto curAmount = std::rand() % (int)std::floor(amount); - amounts.push_back(QString::number(curAmount, 'g', 8)); + + // Try to round it off + if (curAmount > 1000) { + curAmount = std::floor(curAmount / 100) * 100; + } else if (curAmount > 100) { + curAmount = std::floor(curAmount / 10) * 10; + } + + if (curAmount > 0) + amounts.push_back(curAmount); fillAmounts(amounts, amount - curAmount, count - 1); } \ No newline at end of file diff --git a/src/turnstile.h b/src/turnstile.h index 0da7098..7a4bac7 100644 --- a/src/turnstile.h +++ b/src/turnstile.h @@ -1,11 +1,16 @@ -#pragma once +#ifndef TURNSTILE_H +#define TURNSTILE_H + +#include "precompiled.h" + class Turnstile { public: Turnstile(); ~Turnstile(); - QList Turnstile::splitAmount(double amount, int parts); - void fillAmounts(QList& amounts, double amount, int count); + QList splitAmount(double amount, int parts); + void fillAmounts(QList& amounts, double amount, int count); }; +#endif \ No newline at end of file