mirror of
https://git.btclock.dev/btclock/btclock_v3
synced 2025-06-06 01:02:05 +00:00
475 lines
16 KiB
C++
475 lines
16 KiB
C++
#include "data_handler.hpp"
|
|
#ifdef __EMSCRIPTEN__
|
|
#include <emscripten.h>
|
|
#include <emscripten/bind.h>
|
|
#endif
|
|
|
|
|
|
char getCurrencySymbol(char input)
|
|
{
|
|
switch (input)
|
|
{
|
|
case CURRENCY_EUR:
|
|
return '[';
|
|
case CURRENCY_GBP:
|
|
return ']';
|
|
case CURRENCY_JPY:
|
|
return '^';
|
|
// Dollar symbol currencies
|
|
case CURRENCY_AUD:
|
|
case CURRENCY_CAD:
|
|
case CURRENCY_USD:
|
|
return '$';
|
|
default:
|
|
return input;
|
|
}
|
|
}
|
|
|
|
std::string getCurrencyCode(char input)
|
|
{
|
|
switch (input)
|
|
{
|
|
case CURRENCY_EUR:
|
|
return CURRENCY_CODE_EUR;
|
|
case CURRENCY_GBP:
|
|
return CURRENCY_CODE_GBP;
|
|
case CURRENCY_JPY:
|
|
return CURRENCY_CODE_JPY;
|
|
case CURRENCY_AUD:
|
|
return CURRENCY_CODE_AUD;
|
|
case CURRENCY_CAD:
|
|
return CURRENCY_CODE_CAD;
|
|
case CURRENCY_USD:
|
|
return CURRENCY_CODE_USD;
|
|
default:
|
|
return CURRENCY_CODE_UNKNOWN;
|
|
}
|
|
}
|
|
|
|
char getCurrencyChar(const std::string& input)
|
|
{
|
|
if (input == "EUR")
|
|
return CURRENCY_EUR;
|
|
else if (input == "GBP")
|
|
return CURRENCY_GBP;
|
|
else if (input == "JPY")
|
|
return CURRENCY_JPY;
|
|
else if (input == "AUD")
|
|
return CURRENCY_AUD;
|
|
else if (input == "CAD")
|
|
return CURRENCY_CAD;
|
|
else if (input == "USD")
|
|
return CURRENCY_USD;
|
|
else if (input == "ALL")
|
|
return CURRENCY_ALL;
|
|
else if (input == "BWP")
|
|
return CURRENCY_BWP;
|
|
else if (input == "HNL")
|
|
return CURRENCY_HNL;
|
|
else if (input == "HTG")
|
|
return CURRENCY_HTG;
|
|
else if (input == "MWK")
|
|
return CURRENCY_MWK;
|
|
else if (input == "MZN")
|
|
return CURRENCY_MZN;
|
|
else if (input == "NAD")
|
|
return CURRENCY_NAD;
|
|
else if (input == "PYG")
|
|
return CURRENCY_PYG;
|
|
else if (input == "RON")
|
|
return CURRENCY_RON;
|
|
else if (input == "SZL")
|
|
return CURRENCY_SZL;
|
|
else if (input == "ZAR")
|
|
return CURRENCY_ZAR;
|
|
else if (input == "ZMW")
|
|
return CURRENCY_ZMW;
|
|
// Map dollar-symbol currencies to USD character code
|
|
else if (input == "NZD" || input == "SGD" || input == "HKD" ||
|
|
input == "BND" || input == "FJD" || input == "SBD" ||
|
|
input == "TTD" || input == "XCD" || input == "KYD" ||
|
|
input == "GYD" || input == "BBD" || input == "BSD" ||
|
|
input == "BMD" || input == "LRD" || input == "ZWL" ||
|
|
input == "ARS" || input == "CLP" || input == "COP" ||
|
|
input == "MXN" || input == "UYU" || input == "DOP" ||
|
|
input == "SVC" || input == "SRD")
|
|
return CURRENCY_USD;
|
|
else
|
|
return ' ';
|
|
}
|
|
|
|
// Private helper methods
|
|
namespace {
|
|
std::array<std::string, NUM_SCREENS> formatPriceData(std::uint32_t price, char currencySymbol, const std::string& currencyCode, bool useSuffixFormat, bool mowMode, bool shareDot, bool useSymbol)
|
|
{
|
|
std::array<std::string, NUM_SCREENS> ret;
|
|
std::string priceString;
|
|
// If useSymbol is true, we're using the char version - use symbol as requested
|
|
// If useSymbol is false, we're using the string version - only use symbol if it's a recognized currency
|
|
bool shouldUseSymbol = useSymbol || (!useSymbol && currencySymbol != ' ');
|
|
|
|
if (std::to_string(price).length() >= NUM_SCREENS || useSuffixFormat)
|
|
{
|
|
int numScreens = shareDot || mowMode ? NUM_SCREENS - 1 : NUM_SCREENS - 2;
|
|
if (shouldUseSymbol) {
|
|
priceString = getCurrencySymbol(currencySymbol) + formatNumberWithSuffix(price, numScreens, mowMode);
|
|
} else {
|
|
priceString = formatNumberWithSuffix(price, numScreens, mowMode);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (shouldUseSymbol) {
|
|
priceString = getCurrencySymbol(currencySymbol) + std::to_string(price);
|
|
} else {
|
|
priceString = std::to_string(price);
|
|
}
|
|
}
|
|
std::uint32_t firstIndex = 0;
|
|
if ((shareDot && priceString.length() <= (NUM_SCREENS)) || priceString.length() < (NUM_SCREENS))
|
|
{
|
|
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
|
|
|
if (mowMode)
|
|
{
|
|
ret[0] = "MOW/UNITS";
|
|
}
|
|
else
|
|
{
|
|
ret[0] = "BTC/" + currencyCode;
|
|
}
|
|
|
|
|
|
firstIndex = 1;
|
|
}
|
|
|
|
size_t dotPosition = priceString.find('.');
|
|
|
|
if (shareDot && dotPosition != std::string::npos && dotPosition > 0)
|
|
{
|
|
std::vector<std::string> tempArray;
|
|
if (dotPosition != std::string::npos && dotPosition > 0)
|
|
{
|
|
for (size_t i = 0; i < priceString.length(); ++i)
|
|
{
|
|
if (i == dotPosition - 1)
|
|
{
|
|
tempArray.push_back(std::string(1, priceString[i]) + ".");
|
|
++i; // Skip the dot in the next iteration
|
|
}
|
|
else
|
|
{
|
|
tempArray.push_back(std::string(1, priceString[i]));
|
|
}
|
|
}
|
|
|
|
// Copy from tempArray to ret
|
|
for (std::uint32_t i = firstIndex; i < NUM_SCREENS && i - firstIndex < tempArray.size(); ++i)
|
|
{
|
|
ret[i] = tempArray[i - firstIndex];
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
|
|
{
|
|
ret[i] = std::string(1, priceString[i]);
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
std::array<std::string, NUM_SCREENS> formatSatsPerCurrency(std::uint32_t price, char currencySymbol, const std::string& currencyCode, bool withSatsSymbol, bool alwaysShowSats)
|
|
{
|
|
std::array<std::string, NUM_SCREENS> ret;
|
|
double satsPerCurrency = (1.0 / static_cast<double>(price)) * 1e8;
|
|
std::string priceString;
|
|
|
|
// Handle values below 1 sat per currency with 3 decimal places
|
|
if (satsPerCurrency < 1.0) {
|
|
std::ostringstream oss;
|
|
oss << std::fixed << std::setprecision(3) << satsPerCurrency;
|
|
priceString = oss.str();
|
|
}
|
|
// // Check if price is greater than 1 billion (for displaying with 3 decimal places)
|
|
// else if (price >= 100000000) {
|
|
// std::ostringstream oss;
|
|
// oss << std::fixed << std::setprecision(3) << satsPerCurrency;
|
|
// priceString = oss.str();
|
|
// }
|
|
else {
|
|
// Default formatting for integer values
|
|
priceString = std::to_string(static_cast<int>(round(satsPerCurrency)));
|
|
}
|
|
|
|
std::uint32_t firstIndex = 0;
|
|
std::uint8_t insertSatSymbol = NUM_SCREENS - priceString.length() - 1;
|
|
|
|
if (priceString.length() < (NUM_SCREENS))
|
|
{
|
|
// Pad the string with spaces if necessary
|
|
if (priceString.length() < NUM_SCREENS)
|
|
{
|
|
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
|
}
|
|
|
|
if (alwaysShowSats || currencySymbol != CURRENCY_USD || price >= 100000000) // no time anymore when earlier than 1
|
|
ret[0] = "SATS/" + currencyCode;
|
|
else
|
|
ret[0] = "MSCW/TIME";
|
|
|
|
firstIndex = 1;
|
|
|
|
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
|
|
{
|
|
ret[i] = priceString[i];
|
|
}
|
|
|
|
if (withSatsSymbol)
|
|
{
|
|
ret[insertSatSymbol] = "STS";
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
// Updated public methods to use the helper methods
|
|
std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, char currencySymbol, bool useSuffixFormat, bool mowMode, bool shareDot)
|
|
{
|
|
// For char version, always use the currency symbol
|
|
return formatPriceData(price, currencySymbol, getCurrencyCode(currencySymbol), useSuffixFormat, mowMode, shareDot, true);
|
|
}
|
|
|
|
std::array<std::string, NUM_SCREENS> parsePriceData(std::uint32_t price, const std::string& currencyCode, bool useSuffixFormat, bool mowMode, bool shareDot)
|
|
{
|
|
// For string version, let formatPriceData decide whether to use symbol based on if it's a recognized currency
|
|
char currencyChar = getCurrencyChar(currencyCode);
|
|
return formatPriceData(price, currencyChar, currencyCode, useSuffixFormat, mowMode, shareDot, false);
|
|
}
|
|
|
|
std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price, char currencySymbol, bool withSatsSymbol)
|
|
{
|
|
return formatSatsPerCurrency(price, currencySymbol, getCurrencyCode(currencySymbol), withSatsSymbol, false);
|
|
}
|
|
|
|
std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price, const std::string& currencyCode, bool withSatsSymbol)
|
|
{
|
|
return formatSatsPerCurrency(price, getCurrencyChar(currencyCode), currencyCode, withSatsSymbol, true);
|
|
}
|
|
|
|
std::array<std::string, NUM_SCREENS> parseBlockHeight(std::uint32_t blockHeight)
|
|
{
|
|
std::array<std::string, NUM_SCREENS> ret;
|
|
std::string blockNrString = std::to_string(blockHeight);
|
|
std::uint32_t firstIndex = 0;
|
|
|
|
if (blockNrString.length() < NUM_SCREENS)
|
|
{
|
|
blockNrString.insert(blockNrString.begin(), NUM_SCREENS - blockNrString.length(), ' ');
|
|
ret[0] = "BLOCK/HEIGHT";
|
|
firstIndex = 1;
|
|
}
|
|
|
|
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
|
|
{
|
|
ret[i] = blockNrString[i];
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
std::array<std::string, NUM_SCREENS> parseBlockFees(std::uint16_t blockFees)
|
|
{
|
|
std::array<std::string, NUM_SCREENS> ret;
|
|
std::string blockFeesString = std::to_string(blockFees);
|
|
std::uint32_t firstIndex = 0;
|
|
|
|
if (blockFeesString.length() < NUM_SCREENS)
|
|
{
|
|
blockFeesString.insert(blockFeesString.begin(), NUM_SCREENS - blockFeesString.length() - 1, ' ');
|
|
ret[0] = "FEE/RATE";
|
|
firstIndex = 1;
|
|
}
|
|
|
|
for (std::uint8_t i = firstIndex; i < NUM_SCREENS - 1; i++)
|
|
{
|
|
ret[i] = blockFeesString[i];
|
|
}
|
|
|
|
ret[NUM_SCREENS - 1] = "sat/vB";
|
|
|
|
return ret;
|
|
}
|
|
|
|
std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHeight, bool asBlocks)
|
|
{
|
|
std::array<std::string, NUM_SCREENS> ret;
|
|
const std::uint32_t nextHalvingBlock = 210000 - (blockHeight % 210000);
|
|
const std::uint32_t minutesToHalving = nextHalvingBlock * 10;
|
|
|
|
if (asBlocks)
|
|
{
|
|
std::string blockNrString = std::to_string(nextHalvingBlock);
|
|
std::uint32_t firstIndex = 0;
|
|
|
|
if (blockNrString.length() < NUM_SCREENS)
|
|
{
|
|
blockNrString.insert(blockNrString.begin(), NUM_SCREENS - blockNrString.length(), ' ');
|
|
ret[0] = "HAL/VING";
|
|
firstIndex = 1;
|
|
}
|
|
|
|
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
|
|
{
|
|
ret[i] = blockNrString[i];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
const int years = floor(minutesToHalving / 525600);
|
|
const int days = floor((minutesToHalving - (years * 525600)) / (24 * 60));
|
|
const int hours = floor((minutesToHalving - (years * 525600) - (days * (24 * 60))) / 60);
|
|
const int mins = floor(minutesToHalving - (years * 525600) - (days * (24 * 60)) - (hours * 60));
|
|
ret[0] = "BIT/COIN";
|
|
ret[1] = "HAL/VING";
|
|
ret[(NUM_SCREENS - 5)] = std::to_string(years) + "/YRS";
|
|
ret[(NUM_SCREENS - 4)] = std::to_string(days) + "/DAYS";
|
|
ret[(NUM_SCREENS - 3)] = std::to_string(hours) + "/HRS";
|
|
ret[(NUM_SCREENS - 2)] = std::to_string(mins) + "/MINS";
|
|
ret[(NUM_SCREENS - 1)] = "TO/GO";
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
std::array<std::string, NUM_SCREENS> parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars)
|
|
{
|
|
std::array<std::string, NUM_SCREENS> ret;
|
|
std::uint32_t firstIndex = 0;
|
|
double supply = getSupplyAtBlock(blockHeight);
|
|
uint64_t marketCap = static_cast<std::uint64_t>(supply * double(price));
|
|
|
|
ret[0] = getCurrencyCode(currencySymbol) + "/MCAP";
|
|
|
|
if (bigChars)
|
|
{
|
|
firstIndex = 1;
|
|
// Serial.print("Market cap: ");
|
|
// Serial.println(marketCap);
|
|
std::string priceString = currencySymbol + formatNumberWithSuffix(marketCap, (NUM_SCREENS - 2));
|
|
priceString.insert(priceString.begin(), NUM_SCREENS - priceString.length(), ' ');
|
|
|
|
for (std::uint32_t i = firstIndex; i < NUM_SCREENS; i++)
|
|
{
|
|
ret[i] = priceString[i];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
std::string stringValue = std::to_string(marketCap);
|
|
size_t mcLength = stringValue.length();
|
|
size_t leadingSpaces = (3 - mcLength % 3) % 3;
|
|
stringValue = std::string(leadingSpaces, ' ') + stringValue;
|
|
|
|
std::uint32_t groups = (mcLength + leadingSpaces) / 3;
|
|
|
|
if (groups < NUM_SCREENS)
|
|
{
|
|
firstIndex = 1;
|
|
}
|
|
|
|
for (int i = firstIndex; i < NUM_SCREENS - groups - 1; i++)
|
|
{
|
|
ret[i] = "";
|
|
}
|
|
|
|
ret[NUM_SCREENS - groups - 1] = std::string(" ") + currencySymbol + " ";
|
|
for (std::uint32_t i = 0; i < groups; i++)
|
|
{
|
|
ret[(NUM_SCREENS - groups + i)] = stringValue.substr(i * 3, 3).c_str();
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
emscripten::val arrayToStringArray(const std::array<std::string, NUM_SCREENS> &arr)
|
|
{
|
|
emscripten::val jsArray = emscripten::val::array();
|
|
for (const auto &str : arr)
|
|
{
|
|
jsArray.call<void>("push", str);
|
|
}
|
|
return jsArray;
|
|
}
|
|
|
|
emscripten::val vectorToStringArray(const std::vector<std::string> &vec)
|
|
{
|
|
emscripten::val jsArray = emscripten::val::array();
|
|
for (size_t i = 0; i < vec.size(); ++i)
|
|
{
|
|
jsArray.set(i, vec[i]);
|
|
}
|
|
return jsArray;
|
|
}
|
|
|
|
emscripten::val parseBlockHeightArray(std::uint32_t blockHeight)
|
|
{
|
|
return arrayToStringArray(parseBlockHeight(blockHeight));
|
|
}
|
|
|
|
emscripten::val parsePriceDataArray(std::uint32_t price, const std::string ¤cySymbol, bool useSuffixFormat = false, bool mowMode = false, bool shareDot = false)
|
|
{
|
|
// Handle both single character and three-character currency codes
|
|
if (currencySymbol.length() == 1) {
|
|
return arrayToStringArray(parsePriceData(price, currencySymbol[0], useSuffixFormat, mowMode, shareDot));
|
|
} else {
|
|
return arrayToStringArray(parsePriceData(price, currencySymbol, useSuffixFormat, mowMode, shareDot));
|
|
}
|
|
}
|
|
|
|
emscripten::val parseHalvingCountdownArray(std::uint32_t blockHeight, bool asBlocks)
|
|
{
|
|
return arrayToStringArray(parseHalvingCountdown(blockHeight, asBlocks));
|
|
}
|
|
|
|
emscripten::val parseMarketCapArray(std::uint32_t blockHeight, std::uint32_t price, const std::string ¤cySymbol, bool bigChars)
|
|
{
|
|
return arrayToStringArray(parseMarketCap(blockHeight, price, currencySymbol[0], bigChars));
|
|
}
|
|
|
|
emscripten::val parseBlockFeesArray(std::uint16_t blockFees)
|
|
{
|
|
return arrayToStringArray(parseBlockFees(blockFees));
|
|
}
|
|
|
|
emscripten::val parseSatsPerCurrencyArray(std::uint32_t price, const std::string ¤cySymbol, bool withSatsSymbol)
|
|
{
|
|
// Handle both single character and three-character currency codes
|
|
if (currencySymbol.length() == 1) {
|
|
return arrayToStringArray(parseSatsPerCurrency(price, currencySymbol[0], withSatsSymbol));
|
|
} else {
|
|
return arrayToStringArray(parseSatsPerCurrency(price, currencySymbol, withSatsSymbol));
|
|
}
|
|
}
|
|
|
|
EMSCRIPTEN_BINDINGS(my_module)
|
|
{
|
|
// emscripten::register_vector<std::string>("StringList");
|
|
|
|
emscripten::function("parseBlockHeight", &parseBlockHeightArray);
|
|
emscripten::function("parseHalvingCountdown", &parseHalvingCountdownArray);
|
|
emscripten::function("parseMarketCap", &parseMarketCapArray);
|
|
emscripten::function("parseBlockFees", &parseBlockFeesArray);
|
|
emscripten::function("parseSatsPerCurrency", &parseSatsPerCurrencyArray);
|
|
emscripten::function("parsePriceData", &parsePriceDataArray);
|
|
|
|
emscripten::function("arrayToStringArray", &arrayToStringArray);
|
|
emscripten::function("vectorToStringArray", &vectorToStringArray);
|
|
}
|
|
#endif |