feat: Add LNBIts multi currency support to mcap

This commit is contained in:
Djuri Baars 2025-04-16 00:12:18 +02:00
parent 7cd4c8dd37
commit a87374fc67
No known key found for this signature in database
GPG Key ID: 61B9B2DDE5AA3AC1
4 changed files with 75 additions and 48 deletions

View File

@ -234,6 +234,55 @@ namespace {
} }
return ret; return ret;
} }
std::array<std::string, NUM_SCREENS> formatMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, const std::string& currencyCode, 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] = 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 // Updated public methods to use the helper methods
@ -348,53 +397,13 @@ std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHe
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> parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars)
{ {
std::array<std::string, NUM_SCREENS> ret; return formatMarketCap(blockHeight, price, currencySymbol, getCurrencyCode(currencySymbol), bigChars);
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"; std::array<std::string, NUM_SCREENS> parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, const std::string& currencyCode, bool bigChars)
{
if (bigChars) char currencyChar = getCurrencyChar(currencyCode);
{ return formatMarketCap(blockHeight, price, currencyChar, currencyCode, 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__ #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 &currencySymbol, bool bigChars) emscripten::val parseMarketCapArray(std::uint32_t blockHeight, std::uint32_t price, const std::string &currencySymbol, 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) emscripten::val parseBlockFeesArray(std::uint16_t blockFees)

View File

@ -101,6 +101,7 @@ std::array<std::string, NUM_SCREENS> parseSatsPerCurrency(std::uint32_t price, c
std::array<std::string, NUM_SCREENS> parseBlockHeight(std::uint32_t blockHeight); std::array<std::string, NUM_SCREENS> parseBlockHeight(std::uint32_t blockHeight);
std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHeight, bool asBlocks); std::array<std::string, NUM_SCREENS> parseHalvingCountdown(std::uint32_t blockHeight, bool asBlocks);
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> parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, bool bigChars);
std::array<std::string, NUM_SCREENS> parseMarketCap(std::uint32_t blockHeight, std::uint32_t price, const std::string& currencyCode, bool bigChars);
std::array<std::string, NUM_SCREENS> parseBlockFees(std::uint16_t blockFees); std::array<std::string, NUM_SCREENS> parseBlockFees(std::uint16_t blockFees);
char getCurrencySymbol(char input); char getCurrencySymbol(char input);
@ -111,4 +112,5 @@ char getCurrencyChar(const std::string& input);
namespace { 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 = true); 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 = true);
std::array<std::string, NUM_SCREENS> formatSatsPerCurrency(std::uint32_t price, char currencySymbol, const std::string& currencyCode, bool withSatsSymbol, bool alwaysShowSats = false); std::array<std::string, NUM_SCREENS> formatSatsPerCurrency(std::uint32_t price, char currencySymbol, const std::string& currencyCode, bool withSatsSymbol, bool alwaysShowSats = false);
std::array<std::string, NUM_SCREENS> formatMarketCap(std::uint32_t blockHeight, std::uint32_t price, char currencySymbol, const std::string& currencyCode, bool bigChars);
} }

View File

@ -292,7 +292,11 @@ void workerTask(void *pvParameters) {
} }
} else { } else {
auto& blockNotify = BlockNotify::getInstance(); 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); EPDManager::getInstance().setContent(taskEpdContent);

View File

@ -287,6 +287,12 @@ void test_SatsPerCurrencyWithCurrencyCode(void)
TEST_ASSERT_EQUAL_STRING("SATS/ZAR", output[0].c_str()); TEST_ASSERT_EQUAL_STRING("SATS/ZAR", output[0].c_str());
} }
void test_McapWithCurrencyCode(void)
{
std::array<std::string, NUM_SCREENS> output = parseMarketCap(100000, 10000, "PYG", true);
TEST_ASSERT_EQUAL_STRING("PYG/MCAP", output[0].c_str());
}
// not needed when using generate_test_runner.rb // not needed when using generate_test_runner.rb
int runUnityTests(void) int runUnityTests(void)
{ {
@ -312,6 +318,7 @@ int runUnityTests(void)
RUN_TEST(test_PriceSuffixModeMowCompact); RUN_TEST(test_PriceSuffixModeMowCompact);
RUN_TEST(test_PriceDataWithCurrencyCode); RUN_TEST(test_PriceDataWithCurrencyCode);
RUN_TEST(test_SatsPerCurrencyWithCurrencyCode); RUN_TEST(test_SatsPerCurrencyWithCurrencyCode);
RUN_TEST(test_McapWithCurrencyCode);
return UNITY_END(); return UNITY_END();
} }