Split up amounts

This commit is contained in:
Aditya Kulkarni
2018-10-24 22:28:55 -07:00
parent ed85a4c1e9
commit 5f124a4271
5 changed files with 46 additions and 13 deletions

View File

@@ -31,7 +31,11 @@ int main(int argc, char *argv[])
// Temp // Temp
Turnstile t; 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(); return QApplication::exec();
} }

View File

@@ -10,7 +10,6 @@
#include "utils.h" #include "utils.h"
#include "senttxstore.h" #include "senttxstore.h"
#include "precompiled.h" #include "precompiled.h"
using json = nlohmann::json; using json = nlohmann::json;

View File

@@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <cmath>
#include <QApplication> #include <QApplication>
#include <QFontDatabase> #include <QFontDatabase>

View File

@@ -1,6 +1,6 @@
#include "precompiled.h"
#include "turnstile.h" #include "turnstile.h"
#include "utils.h"
Turnstile::Turnstile() { Turnstile::Turnstile() {
} }
@@ -9,29 +9,53 @@ Turnstile::Turnstile() {
Turnstile::~Turnstile() { Turnstile::~Turnstile() {
} }
QList<QString> Turnstile::splitAmount(double amount, int parts) { QList<double> Turnstile::splitAmount(double amount, int parts) {
QList<QString> amounts; QList<double> amounts;
fillAmounts(amounts, amount, parts); fillAmounts(amounts, amount, parts);
// Ensure they all add up! // Ensure they all add up!
double sumofparts = 0; double sumofparts = 0;
for (auto a : amounts) { for (auto a : amounts) {
sumofparts += a.toDouble(); sumofparts += a;
} }
Q_ASSERT(sumofparts == amount); Q_ASSERT(sumofparts == amount);
return amounts; return amounts;
} }
void Turnstile::fillAmounts(QList<QString>& amounts, double amount, int count) { void Turnstile::fillAmounts(QList<double>& amounts, double amount, int count) {
if (count == 1 || amount < 1) { 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; 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); 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); fillAmounts(amounts, amount - curAmount, count - 1);
} }

View File

@@ -1,11 +1,16 @@
#pragma once #ifndef TURNSTILE_H
#define TURNSTILE_H
#include "precompiled.h"
class Turnstile class Turnstile
{ {
public: public:
Turnstile(); Turnstile();
~Turnstile(); ~Turnstile();
QList<QString> Turnstile::splitAmount(double amount, int parts); QList<double> splitAmount(double amount, int parts);
void fillAmounts(QList<QString>& amounts, double amount, int count); void fillAmounts(QList<double>& amounts, double amount, int count);
}; };
#endif