diff --git a/src/api/exchanges/include/binancepublicapi.hpp b/src/api/exchanges/include/binancepublicapi.hpp index 605190be..2c4060be 100644 --- a/src/api/exchanges/include/binancepublicapi.hpp +++ b/src/api/exchanges/include/binancepublicapi.hpp @@ -93,11 +93,8 @@ class BinancePublic : public ExchangePublic { }; struct GlobalInfosFunc { - static constexpr std::string_view kCryptoFeeBaseUrl = "https://www.binance.com/en/fee/cryptoFee"; - GlobalInfosFunc(AbstractMetricGateway* pMetricGateway, const PermanentCurlOptions& permanentCurlOptions, - settings::RunMode runMode) - : _curlHandle(kCryptoFeeBaseUrl, pMetricGateway, permanentCurlOptions, runMode) {} + settings::RunMode runMode); json operator()(); diff --git a/src/api/exchanges/src/binancepublicapi.cpp b/src/api/exchanges/src/binancepublicapi.cpp index 9d3da3f7..96b33bbf 100644 --- a/src/api/exchanges/src/binancepublicapi.cpp +++ b/src/api/exchanges/src/binancepublicapi.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -128,8 +127,8 @@ BinancePublic::CommonInfo::CommonInfo(const CoincenterInfo& coincenterInfo, cons CurrencyExchangeFlatSet BinancePublic::queryTradableCurrencies(const json& data) const { CurrencyExchangeVector currencies; const CurrencyCodeSet& excludedCurrencies = _commonInfo._exchangeInfo.excludedCurrenciesAll(); - for (const json& el : data) { - std::string_view coin = el["coin"].get(); + for (const json& coinJson : data) { + std::string_view coin = coinJson["coin"].get(); if (coin.size() > CurrencyCode::kMaxLen) { continue; } @@ -138,8 +137,8 @@ CurrencyExchangeFlatSet BinancePublic::queryTradableCurrencies(const json& data) log::trace("Discard {} excluded by config", cur.str()); continue; } - bool isFiat = el["isLegalMoney"]; - const auto& networkList = el["networkList"]; + bool isFiat = coinJson["isLegalMoney"]; + const auto& networkList = coinJson["networkList"]; if (networkList.size() > 1) { log::debug("Several networks found for {}, considering only default network", cur.str()); } @@ -210,46 +209,17 @@ BinancePublic::ExchangeInfoFunc::ExchangeInfoDataByMarket BinancePublic::Exchang return ret; } -json BinancePublic::GlobalInfosFunc::operator()() { - string dataStr = _curlHandle.queryRelease("", CurlOptions(HttpRequestType::kGet)); - // This json is HUGE and contains numerous amounts of information - static constexpr std::string_view appBegJson = "application/json\">"; - std::size_t beg = dataStr.find(appBegJson); - if (beg == string::npos) { - throw exception("Unexpected answer from {}", _curlHandle.getNextBaseUrl()); - } - string::const_iterator first = dataStr.begin() + beg + appBegJson.size(); - std::string_view sv(first, dataStr.end()); - std::size_t reduxPos = sv.find("redux\":"); - std::size_t ssrStorePos = sv.find("ssrStore\":", reduxPos); - static constexpr std::string_view kCryptoFeeStart = "cryptoFee\":"; - std::size_t cryptoFeePos = sv.find(kCryptoFeeStart, ssrStorePos); - - std::size_t startPos = cryptoFeePos + kCryptoFeeStart.size(); - - sv = std::string_view(sv.begin() + startPos, sv.end()); +namespace { +constexpr std::string_view kCryptoFeeBaseUrl = "https://www.binance.com"; +} - const std::size_t svSize = sv.size(); +BinancePublic::GlobalInfosFunc::GlobalInfosFunc(AbstractMetricGateway* pMetricGateway, + const PermanentCurlOptions& permanentCurlOptions, + settings::RunMode runMode) + : _curlHandle(kCryptoFeeBaseUrl, pMetricGateway, permanentCurlOptions, runMode) {} - std::size_t endPos = 1; - int squareBracketCount = 1; - for (; endPos < svSize && squareBracketCount != 0; ++endPos) { - switch (sv[endPos]) { - case '[': - ++squareBracketCount; - break; - case ']': - --squareBracketCount; - break; - default: - break; - } - } - if (squareBracketCount != 0) { - throw exception("JSON parsing error from Binance cryptoFee scraper"); - } - - return json::parse(std::string_view(sv.data(), endPos)); +json BinancePublic::GlobalInfosFunc::operator()() { + return PublicQuery(_curlHandle, "/bapi/capital/v1/public/capital/getNetworkCoinAll").at("data"); } namespace { @@ -270,13 +240,13 @@ MonetaryAmount ComputeWithdrawalFeesFromNetworkList(CurrencyCode cur, const json WithdrawalFeeMap BinancePublic::queryWithdrawalFees() { WithdrawalFeeMap ret; - for (const json& el : _globalInfosCache.get()) { - std::string_view coinStr = el["coin"].get(); + for (const json& coinJson : _globalInfosCache.get()) { + std::string_view coinStr = coinJson["coin"].get(); if (coinStr.size() > CurrencyCode::kMaxLen) { continue; } CurrencyCode cur(coinStr); - MonetaryAmount withdrawFee = ComputeWithdrawalFeesFromNetworkList(cur, el["networkList"]); + MonetaryAmount withdrawFee = ComputeWithdrawalFeesFromNetworkList(cur, coinJson["networkList"]); log::trace("Retrieved {} withdrawal fee {}", _name, withdrawFee); ret.insert_or_assign(cur, withdrawFee); }