From a87374fc673e4686f6c7fb152c1f8acee38ac9a9 Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Wed, 16 Apr 2025 00:12:18 +0200 Subject: [PATCH] feat: Add LNBIts multi currency support to mcap --- lib/btclock/data_handler.cpp | 108 ++++++++++++++++------------ lib/btclock/data_handler.hpp | 2 + src/lib/screen_handler.cpp | 6 +- test/test_datahandler/test_main.cpp | 7 ++ 4 files changed, 75 insertions(+), 48 deletions(-) diff --git a/lib/btclock/data_handler.cpp b/lib/btclock/data_handler.cpp index efcce70..bcb14d1 100644 --- a/lib/btclock/data_handler.cpp +++ b/lib/btclock/data_handler.cpp @@ -234,6 +234,55 @@ namespace { } return ret; } + + std::array formatMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, const std::string& currencyCode, bool bigChars) + { + std::array ret; + std::uint32_t firstIndex = 0; + double supply = getSupplyAtBlock(blockHeight); + uint64_t marketCap = static_cast(supply * double(price)); + + ret[0] = currencyCode + "/MCAP"; + + if (bigChars) + { + firstIndex = 1; + std::string priceString = getCurrencySymbol(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(" ") + getCurrencySymbol(currencySymbol) + " "; + for (std::uint32_t i = 0; i < groups; i++) + { + ret[(NUM_SCREENS - groups + i)] = stringValue.substr(i * 3, 3).c_str(); + } + } + + return ret; + } } // Updated public methods to use the helper methods @@ -348,53 +397,13 @@ std::array parseHalvingCountdown(std::uint32_t blockHe std::array parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars) { - std::array ret; - std::uint32_t firstIndex = 0; - double supply = getSupplyAtBlock(blockHeight); - uint64_t marketCap = static_cast(supply * double(price)); + return formatMarketCap(blockHeight, price, currencySymbol, getCurrencyCode(currencySymbol), bigChars); +} - 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; +std::array parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, const std::string& currencyCode, bool bigChars) +{ + char currencyChar = getCurrencyChar(currencyCode); + return formatMarketCap(blockHeight, price, currencyChar, currencyCode, bigChars); } #ifdef __EMSCRIPTEN__ @@ -440,7 +449,12 @@ emscripten::val parseHalvingCountdownArray(std::uint32_t blockHeight, bool asBlo 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)); + // Handle both single character and three-character currency codes + if (currencySymbol.length() == 1) { + return arrayToStringArray(parseMarketCap(blockHeight, price, currencySymbol[0], bigChars)); + } else { + return arrayToStringArray(parseMarketCap(blockHeight, price, currencySymbol, bigChars)); + } } emscripten::val parseBlockFeesArray(std::uint16_t blockFees) diff --git a/lib/btclock/data_handler.hpp b/lib/btclock/data_handler.hpp index 2cb2574..dc68e17 100644 --- a/lib/btclock/data_handler.hpp +++ b/lib/btclock/data_handler.hpp @@ -101,6 +101,7 @@ std::array parseSatsPerCurrency(std::uint32_t price, c std::array parseBlockHeight(std::uint32_t blockHeight); std::array parseHalvingCountdown(std::uint32_t blockHeight, bool asBlocks); std::array parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars); +std::array parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, const std::string& currencyCode, bool bigChars); std::array parseBlockFees(std::uint16_t blockFees); char getCurrencySymbol(char input); @@ -111,4 +112,5 @@ char getCurrencyChar(const std::string& input); namespace { std::array formatPriceData(std::uint32_t price, char currencySymbol, const std::string& currencyCode, bool useSuffixFormat, bool mowMode, bool shareDot, bool useSymbol = true); std::array formatSatsPerCurrency(std::uint32_t price, char currencySymbol, const std::string& currencyCode, bool withSatsSymbol, bool alwaysShowSats = false); + std::array formatMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, const std::string& currencyCode, bool bigChars); } \ No newline at end of file diff --git a/src/lib/screen_handler.cpp b/src/lib/screen_handler.cpp index 2372aa5..160dbb2 100644 --- a/src/lib/screen_handler.cpp +++ b/src/lib/screen_handler.cpp @@ -292,7 +292,11 @@ void workerTask(void *pvParameters) { } } else { auto& blockNotify = BlockNotify::getInstance(); - taskEpdContent = parseMarketCap(blockNotify.getBlockHeight(), price, currency, preferences.getBool("mcapBigChar", DEFAULT_MCAP_BIG_CHAR)); + if (preferences.getBool("lnbitsEnabled", DEFAULT_LNBITS_ENABLED)) { + taskEpdContent = parseMarketCap(blockNotify.getBlockHeight(), price, LNBitsFetch::getInstance().getCurrentCurrency(), preferences.getBool("mcapBigChar", DEFAULT_MCAP_BIG_CHAR)); + } else { + taskEpdContent = parseMarketCap(blockNotify.getBlockHeight(), price, currency, preferences.getBool("mcapBigChar", DEFAULT_MCAP_BIG_CHAR)); + } } EPDManager::getInstance().setContent(taskEpdContent); diff --git a/test/test_datahandler/test_main.cpp b/test/test_datahandler/test_main.cpp index 80b2ea5..1a9757c 100644 --- a/test/test_datahandler/test_main.cpp +++ b/test/test_datahandler/test_main.cpp @@ -287,6 +287,12 @@ void test_SatsPerCurrencyWithCurrencyCode(void) TEST_ASSERT_EQUAL_STRING("SATS/ZAR", output[0].c_str()); } +void test_McapWithCurrencyCode(void) +{ + std::array output = parseMarketCap(100000, 10000, "PYG", true); + TEST_ASSERT_EQUAL_STRING("PYG/MCAP", output[0].c_str()); +} + // not needed when using generate_test_runner.rb int runUnityTests(void) { @@ -312,6 +318,7 @@ int runUnityTests(void) RUN_TEST(test_PriceSuffixModeMowCompact); RUN_TEST(test_PriceDataWithCurrencyCode); RUN_TEST(test_SatsPerCurrencyWithCurrencyCode); + RUN_TEST(test_McapWithCurrencyCode); return UNITY_END(); }