2023-11-08 11:18:59 +00:00
|
|
|
#include "utils.hpp"
|
|
|
|
|
|
|
|
int modulo(int x, int N)
|
|
|
|
{
|
|
|
|
return (x % N + N) % N;
|
|
|
|
}
|
2023-11-08 14:27:22 +00:00
|
|
|
|
2024-03-10 11:35:20 +00:00
|
|
|
double getSupplyAtBlock(std::uint32_t blockNr)
|
|
|
|
{
|
|
|
|
if (blockNr >= 33 * 210000)
|
|
|
|
{
|
2023-11-10 18:52:06 +00:00
|
|
|
return 20999999.9769;
|
2024-03-10 11:35:20 +00:00
|
|
|
}
|
2023-11-10 18:52:06 +00:00
|
|
|
|
|
|
|
const int initialBlockReward = 50; // Initial block reward
|
2024-03-10 11:35:20 +00:00
|
|
|
const int halvingInterval = 210000; // Number of blocks before halving
|
2023-11-10 18:52:06 +00:00
|
|
|
|
|
|
|
int halvingCount = blockNr / halvingInterval;
|
|
|
|
double totalBitcoinInCirculation = 0;
|
|
|
|
|
2024-03-10 11:35:20 +00:00
|
|
|
for (int i = 0; i < halvingCount; ++i)
|
|
|
|
{
|
2023-11-10 18:52:06 +00:00
|
|
|
totalBitcoinInCirculation += halvingInterval * initialBlockReward * std::pow(0.5, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
totalBitcoinInCirculation += (blockNr % halvingInterval) * initialBlockReward * std::pow(0.5, halvingCount);
|
|
|
|
|
|
|
|
return totalBitcoinInCirculation;
|
2023-11-10 19:59:08 +00:00
|
|
|
}
|
|
|
|
|
2024-11-27 10:33:12 +00:00
|
|
|
std::string formatNumberWithSuffix(std::uint64_t num, int numCharacters)
|
|
|
|
{
|
|
|
|
return formatNumberWithSuffix(num, numCharacters, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string formatNumberWithSuffix(std::uint64_t num, int numCharacters, bool mowMode)
|
2024-03-10 11:35:20 +00:00
|
|
|
{
|
|
|
|
static char result[20]; // Adjust size as needed
|
2023-11-10 19:59:08 +00:00
|
|
|
const long long quadrillion = 1000000000000000LL;
|
|
|
|
const long long trillion = 1000000000000LL;
|
|
|
|
const long long billion = 1000000000;
|
|
|
|
const long long million = 1000000;
|
|
|
|
const long long thousand = 1000;
|
|
|
|
|
2024-03-10 11:35:20 +00:00
|
|
|
double numDouble = (double)num;
|
|
|
|
int numDigits = (int)log10(num) + 1;
|
|
|
|
char suffix;
|
|
|
|
|
|
|
|
if (num >= quadrillion || numDigits > 15)
|
|
|
|
{
|
|
|
|
numDouble /= quadrillion;
|
|
|
|
suffix = 'Q';
|
2023-11-10 19:59:08 +00:00
|
|
|
}
|
2024-03-10 11:35:20 +00:00
|
|
|
else if (num >= trillion || numDigits > 12)
|
|
|
|
{
|
|
|
|
numDouble /= trillion;
|
|
|
|
suffix = 'T';
|
|
|
|
}
|
|
|
|
else if (num >= billion || numDigits > 9)
|
|
|
|
{
|
|
|
|
numDouble /= billion;
|
|
|
|
suffix = 'B';
|
|
|
|
}
|
2024-11-27 10:33:12 +00:00
|
|
|
else if (num >= million || numDigits > 6 || (mowMode && num >= thousand))
|
2024-03-10 11:35:20 +00:00
|
|
|
{
|
|
|
|
numDouble /= million;
|
|
|
|
suffix = 'M';
|
|
|
|
}
|
2024-11-27 10:33:12 +00:00
|
|
|
else if (!mowMode && (num >= thousand || numDigits > 3))
|
2024-03-10 11:35:20 +00:00
|
|
|
{
|
|
|
|
numDouble /= thousand;
|
|
|
|
suffix = 'K';
|
|
|
|
}
|
2024-11-27 10:33:12 +00:00
|
|
|
else if (!mowMode)
|
2024-03-10 11:35:20 +00:00
|
|
|
{
|
2024-09-27 22:03:18 +00:00
|
|
|
snprintf(result, sizeof(result), "%llu", (unsigned long long)num);
|
2024-03-10 11:35:20 +00:00
|
|
|
return result;
|
|
|
|
}
|
2024-11-27 10:33:12 +00:00
|
|
|
else // mowMode is true and num < 1000
|
|
|
|
{
|
|
|
|
numDouble /= million;
|
|
|
|
suffix = 'M';
|
|
|
|
}
|
2024-03-10 11:35:20 +00:00
|
|
|
|
|
|
|
// Add suffix
|
2024-12-09 21:58:39 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
// Mow Mode always uses string truncation to avoid rounding
|
|
|
|
std::string mowAsString = std::to_string(numDouble);
|
|
|
|
if (mowMode) {
|
|
|
|
// Default to one decimal place
|
|
|
|
len = snprintf(result, sizeof(result), "%s%c", mowAsString.substr(0, mowAsString.find(".") + 2).c_str(), suffix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = snprintf(result, sizeof(result), "%.0f%c", numDouble, suffix);
|
|
|
|
}
|
2024-03-10 11:35:20 +00:00
|
|
|
|
2024-11-27 10:33:12 +00:00
|
|
|
// If there's room, add more decimal places
|
2024-03-10 11:35:20 +00:00
|
|
|
if (len < numCharacters)
|
|
|
|
{
|
2024-11-27 10:33:12 +00:00
|
|
|
int restLen = mowMode ? numCharacters - len : numCharacters - len - 1;
|
2024-12-09 21:58:39 +00:00
|
|
|
|
|
|
|
if (mowMode) {
|
|
|
|
snprintf(result, sizeof(result), "%s%c", mowAsString.substr(0, mowAsString.find(".") + 2 + restLen).c_str(), suffix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(result, sizeof(result), "%.*f%c", restLen, numDouble, suffix);
|
|
|
|
}
|
2024-03-10 11:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2024-08-24 13:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get sat amount from a bolt11 invoice
|
|
|
|
*
|
|
|
|
* Based on https://github.com/lnbits/nostr-zap-lamp/blob/main/nostrZapLamp/nostrZapLamp.ino
|
|
|
|
*/
|
|
|
|
int64_t getAmountInSatoshis(std::string bolt11) {
|
|
|
|
int64_t number = -1;
|
|
|
|
char multiplier = ' ';
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < bolt11.length(); ++i) {
|
|
|
|
if (isdigit(bolt11[i])) {
|
|
|
|
number = 0;
|
|
|
|
while (isdigit(bolt11[i])) {
|
|
|
|
number = number * 10 + (bolt11[i] - '0');
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
for (unsigned int j = i; j < bolt11.length(); ++j) {
|
|
|
|
if (isalpha(bolt11[j])) {
|
|
|
|
multiplier = bolt11[j];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (number == -1 || multiplier == ' ') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t satoshis = number;
|
|
|
|
|
|
|
|
switch (multiplier) {
|
|
|
|
case 'm':
|
|
|
|
satoshis *= 100000; // 0.001 * 100,000,000
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
satoshis *= 100; // 0.000001 * 100,000,000
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
satoshis /= 10; // 0.000000001 * 100,000,000
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
satoshis /= 10000; // 0.000000000001 * 100,000,000
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return satoshis;
|
|
|
|
}
|
2024-12-20 13:11:26 +00:00
|
|
|
|
|
|
|
void parseHashrateString(const std::string& hashrate, std::string& label, std::string& output, unsigned int maxCharacters) {
|
|
|
|
// Handle empty string or "0" cases
|
|
|
|
if (hashrate.empty() || hashrate == "0") {
|
|
|
|
label = "H/S";
|
|
|
|
output = "0";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t suffixLength = 0;
|
|
|
|
if (hashrate.length() > 21) {
|
|
|
|
label = "ZH/S";
|
|
|
|
suffixLength = 21;
|
|
|
|
} else if (hashrate.length() > 18) {
|
|
|
|
label = "EH/S";
|
|
|
|
suffixLength = 18;
|
|
|
|
} else if (hashrate.length() > 15) {
|
|
|
|
label = "PH/S";
|
|
|
|
suffixLength = 15;
|
|
|
|
} else if (hashrate.length() > 12) {
|
|
|
|
label = "TH/S";
|
|
|
|
suffixLength = 12;
|
|
|
|
} else if (hashrate.length() > 9) {
|
|
|
|
label = "GH/S";
|
|
|
|
suffixLength = 9;
|
|
|
|
} else if (hashrate.length() > 6) {
|
|
|
|
label = "MH/S";
|
|
|
|
suffixLength = 6;
|
|
|
|
} else if (hashrate.length() > 3) {
|
|
|
|
label = "KH/S";
|
|
|
|
suffixLength = 3;
|
|
|
|
} else {
|
|
|
|
label = "H/S";
|
|
|
|
suffixLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double value = std::stod(hashrate) / std::pow(10, suffixLength);
|
|
|
|
|
|
|
|
// Calculate integer part length
|
|
|
|
int integerPartLength = std::to_string(static_cast<int>(value)).length();
|
|
|
|
|
|
|
|
// Calculate remaining space for decimals
|
|
|
|
int remainingSpace = maxCharacters - integerPartLength;
|
|
|
|
|
|
|
|
char buffer[32];
|
|
|
|
if (remainingSpace <= 0)
|
|
|
|
{
|
|
|
|
// No space for decimals, just round to integer
|
|
|
|
snprintf(buffer, sizeof(buffer), "%.0f", value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Space for decimal point and some decimals
|
|
|
|
snprintf(buffer, sizeof(buffer), "%.*f", remainingSpace - 1, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove trailing zeros and decimal point if necessary
|
|
|
|
output = buffer;
|
|
|
|
if (output.find('.') != std::string::npos)
|
|
|
|
{
|
|
|
|
output = output.substr(0, output.find_last_not_of('0') + 1);
|
|
|
|
if (output.back() == '.')
|
|
|
|
{
|
|
|
|
output.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int getHashrateMultiplier(char unit) {
|
|
|
|
if (unit == '0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
static const std::unordered_map<char, int> multipliers = {
|
|
|
|
{'Z', 21}, {'E', 18}, {'P', 15}, {'T', 12},
|
|
|
|
{'G', 9}, {'M', 6}, {'K', 3}
|
|
|
|
};
|
|
|
|
return multipliers.at(unit);
|
|
|
|
}
|