Split up amounts
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "utils.h"
|
||||
#include "senttxstore.h"
|
||||
|
||||
|
||||
#include "precompiled.h"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <cmath>
|
||||
|
||||
#include <QApplication>
|
||||
#include <QFontDatabase>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "precompiled.h"
|
||||
#include "turnstile.h"
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
Turnstile::Turnstile() {
|
||||
}
|
||||
@@ -9,29 +9,53 @@ Turnstile::Turnstile() {
|
||||
Turnstile::~Turnstile() {
|
||||
}
|
||||
|
||||
QList<QString> Turnstile::splitAmount(double amount, int parts) {
|
||||
QList<QString> amounts;
|
||||
QList<double> Turnstile::splitAmount(double amount, int parts) {
|
||||
QList<double> 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<QString>& amounts, double amount, int count) {
|
||||
void Turnstile::fillAmounts(QList<double>& 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);
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
#pragma once
|
||||
#ifndef TURNSTILE_H
|
||||
#define TURNSTILE_H
|
||||
|
||||
#include "precompiled.h"
|
||||
|
||||
class Turnstile
|
||||
{
|
||||
public:
|
||||
Turnstile();
|
||||
~Turnstile();
|
||||
|
||||
QList<QString> Turnstile::splitAmount(double amount, int parts);
|
||||
void fillAmounts(QList<QString>& amounts, double amount, int count);
|
||||
QList<double> splitAmount(double amount, int parts);
|
||||
void fillAmounts(QList<double>& amounts, double amount, int count);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user