|
| 1 | +// Copyright (c) 2024 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <qml/bitcoinamount.h> |
| 6 | + |
| 7 | +#include <QRegExp> |
| 8 | +#include <QStringList> |
| 9 | + |
| 10 | + |
| 11 | +BitcoinAmount::BitcoinAmount(QObject *parent) : QObject(parent) |
| 12 | +{ |
| 13 | + m_unit = Unit::BTC; |
| 14 | +} |
| 15 | + |
| 16 | +int BitcoinAmount::decimals(Unit unit) |
| 17 | +{ |
| 18 | + switch (unit) { |
| 19 | + case Unit::BTC: return 8; |
| 20 | + case Unit::SAT: return 0; |
| 21 | + } // no default case, so the compiler can warn about missing cases |
| 22 | + assert(false); |
| 23 | +} |
| 24 | + |
| 25 | +QString BitcoinAmount::sanitize(const QString &text) |
| 26 | +{ |
| 27 | + QString result = text; |
| 28 | + |
| 29 | + // Remove any invalid characters |
| 30 | + result.remove(QRegExp("[^0-9.]")); |
| 31 | + |
| 32 | + // Ensure only one decimal point |
| 33 | + QStringList parts = result.split('.'); |
| 34 | + if (parts.size() > 2) { |
| 35 | + result = parts[0] + "." + parts[1]; |
| 36 | + } |
| 37 | + |
| 38 | + // Limit decimal places to 8 |
| 39 | + if (parts.size() == 2 && parts[1].length() > 8) { |
| 40 | + result = parts[0] + "." + parts[1].left(8); |
| 41 | + } |
| 42 | + |
| 43 | + return result; |
| 44 | +} |
| 45 | + |
| 46 | +BitcoinAmount::Unit BitcoinAmount::unit() const |
| 47 | +{ |
| 48 | + return m_unit; |
| 49 | +} |
| 50 | + |
| 51 | +void BitcoinAmount::setUnit(const Unit unit) |
| 52 | +{ |
| 53 | + m_unit = unit; |
| 54 | + Q_EMIT unitChanged(); |
| 55 | +} |
| 56 | + |
| 57 | +QString BitcoinAmount::unitLabel() const |
| 58 | +{ |
| 59 | + switch (m_unit) { |
| 60 | + case Unit::BTC: return "₿"; |
| 61 | + case Unit::SAT: return "Sat"; |
| 62 | + } |
| 63 | + assert(false); |
| 64 | +} |
| 65 | + |
| 66 | +QString BitcoinAmount::amount() const |
| 67 | +{ |
| 68 | + return m_amount; |
| 69 | +} |
| 70 | + |
| 71 | +void BitcoinAmount::setAmount(const QString& new_amount) |
| 72 | +{ |
| 73 | + m_amount = sanitize(new_amount); |
| 74 | + Q_EMIT amountChanged(); |
| 75 | +} |
| 76 | + |
| 77 | +long long BitcoinAmount::toSatoshis(QString& amount, const Unit unit) |
| 78 | +{ |
| 79 | + |
| 80 | + int num_decimals = decimals(unit); |
| 81 | + |
| 82 | + QStringList parts = amount.remove(' ').split("."); |
| 83 | + |
| 84 | + QString whole = parts[0]; |
| 85 | + QString decimals; |
| 86 | + |
| 87 | + if(parts.size() > 1) |
| 88 | + { |
| 89 | + decimals = parts[1]; |
| 90 | + } |
| 91 | + QString str = whole + decimals.leftJustified(num_decimals, '0', true); |
| 92 | + |
| 93 | + return str.toLongLong(); |
| 94 | +} |
| 95 | + |
| 96 | +QString BitcoinAmount::convert(const QString &amount, Unit unit) |
| 97 | +{ |
| 98 | + if (amount == "") { |
| 99 | + return amount; |
| 100 | + } |
| 101 | + |
| 102 | + QString result = amount; |
| 103 | + int decimalPosition = result.indexOf("."); |
| 104 | + |
| 105 | + if (decimalPosition == -1) { |
| 106 | + decimalPosition = result.length(); |
| 107 | + result.append("."); |
| 108 | + } |
| 109 | + |
| 110 | + if (unit == Unit::BTC) { |
| 111 | + int numDigitsAfterDecimal = result.length() - decimalPosition - 1; |
| 112 | + if (numDigitsAfterDecimal < 8) { |
| 113 | + result.append(QString(8 - numDigitsAfterDecimal, '0')); |
| 114 | + } |
| 115 | + result.remove(decimalPosition, 1); |
| 116 | + } else if (unit == Unit::SAT) { |
| 117 | + result.remove(decimalPosition, 1); |
| 118 | + int newDecimalPosition = decimalPosition - 8; |
| 119 | + if (newDecimalPosition < 1) { |
| 120 | + result = QString("0").repeated(-newDecimalPosition) + result; |
| 121 | + newDecimalPosition = 0; |
| 122 | + } |
| 123 | + result.insert(newDecimalPosition, "."); |
| 124 | + } |
| 125 | + |
| 126 | + return result; |
| 127 | +} |
0 commit comments