diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d3e32e..c4b1e83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,8 +106,7 @@ target_link_libraries( PRIVATE cpr::cpr oxen::logging - gmp - gmpxx + gmp::gmp ) if(${PROJECT_NAME}_ENABLE_SIGNER) target_link_libraries(${PROJECT_NAME} PUBLIC secp256k1) diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 219471a..a0a30e3 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -62,7 +62,8 @@ endif() # GMP # -pkg_check_modules(GMP gmp IMPORTED_TARGET REQUIRED) -add_library(gmp INTERFACE) -target_link_libraries(gmp INTERFACE PkgConfig::GMP) -message(STATUS "Found gmp ${GMP_VERSION}") +if(NOT TARGET gmp::gmp) + pkg_check_modules(GMP gmp IMPORTED_TARGET REQUIRED GLOBAL) + add_library(gmp::gmp ALIAS PkgConfig::GMP) + message(STATUS "Found gmp ${GMP_VERSION}") +endif() diff --git a/src/provider.cpp b/src/provider.cpp index b0c5a24..f715359 100644 --- a/src/provider.cpp +++ b/src/provider.cpp @@ -15,7 +15,7 @@ #include "ethyl/provider.hpp" #include "ethyl/utils.hpp" -#include +#include namespace { @@ -704,22 +704,23 @@ void Provider::getBalanceAsync(std::string_view address, optional_callbackget(); + std::string balanceHex = r->get(); - // Convert balance from hex to GMP multi-precision integer - mpz_class balance; - balance.set_str(balanceHex, 0); // 0 as base to automatically pick up hex from the prepended 0x of our balanceHex string + // Convert balance from hex to GMP multi-precision integer - user_cb(balance.get_str()); - return; - } - catch (const std::exception& e) - { - log::warning(logcat, "eth_getBalance response, failed to parse bigint: {}", r->get()); - user_cb(std::nullopt); + std::optional bal10; + mpz_t balance; + // 0 as base to automatically pick up hex from the prepended 0x of our balanceHex string + if (int rc = mpz_init_set_str(balance, balanceHex.c_str(), 0); rc == 0) { + bal10.emplace(); + bal10->resize(mpz_sizeinbase(balance, 10) + 1); + mpz_get_str(bal10->data(), 10, balance); + bal10->resize(std::strlen(bal10->c_str())); + } else { + log::warning(logcat, "eth_getBalance response, failed to parse bigint: {}", balanceHex); } + mpz_clear(balance); + user_cb(std::move(bal10)); }; makeJsonRpcRequest("eth_getBalance", params, std::move(cb)); }