Skip to content

Commit

Permalink
Migrate generalconfig to glaze::json
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Oct 19, 2024
1 parent 19d5cc0 commit 144b9f5
Show file tree
Hide file tree
Showing 115 changed files with 2,116 additions and 1,547 deletions.
4 changes: 2 additions & 2 deletions src/api-objects/src/apikeysprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "accountowner.hpp"
#include "apikey.hpp"
#include "cct_exception.hpp"
#include "cct_json.hpp"
#include "cct_json-container.hpp"
#include "cct_log.hpp"
#include "cct_string.hpp"
#include "exchangename.hpp"
Expand Down Expand Up @@ -81,7 +81,7 @@ APIKeysProvider::APIKeysMap APIKeysProvider::ParseAPIKeys(std::string_view dataD
std::string_view secretFileName = GetSecretFileName(runMode);
File secretsFile(dataDir, File::Type::kSecret, secretFileName,
settings::AreTestKeysRequested(runMode) ? File::IfError::kThrow : File::IfError::kNoThrow);
json jsonData = secretsFile.readAllJson();
json::container jsonData = secretsFile.readAllJson();
for (auto& [publicExchangeName, keyObj] : jsonData.items()) {
const auto& exchangesWithoutSecrets = exchangeSecretsInfo.exchangesWithoutSecrets();
if (std::ranges::find(exchangesWithoutSecrets, ExchangeName(publicExchangeName)) !=
Expand Down
6 changes: 3 additions & 3 deletions src/api/common/include/binance-common-api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <mutex>

#include "cachedresult.hpp"
#include "cct_json.hpp"
#include "cct_json-container.hpp"
#include "curlhandle.hpp"
#include "currencycode.hpp"
#include "currencycodeset.hpp"
Expand All @@ -25,7 +25,7 @@ class BinanceGlobalInfosFunc {
BinanceGlobalInfosFunc(AbstractMetricGateway* pMetricGateway, const PermanentCurlOptions& permanentCurlOptions,
settings::RunMode runMode);

json operator()();
json::container operator()();

private:
CurlHandle _curlHandle;
Expand All @@ -45,7 +45,7 @@ class BinanceGlobalInfos {
private:
friend class BinancePrivate;

static CurrencyExchangeFlatSet ExtractTradableCurrencies(const json& allCoins,
static CurrencyExchangeFlatSet ExtractTradableCurrencies(const json::container& allCoins,
const CurrencyCodeSet& excludedCurrencies);

std::mutex _mutex;
Expand Down
34 changes: 17 additions & 17 deletions src/api/common/src/binance-common-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "abstractmetricgateway.hpp"
#include "cachedresult.hpp"
#include "cct_json.hpp"
#include "cct_json-container.hpp"
#include "cct_log.hpp"
#include "curlhandle.hpp"
#include "currencycode.hpp"
Expand All @@ -27,15 +27,15 @@ namespace cct::api {

namespace {

json PublicQuery(CurlHandle& curlHandle, std::string_view method) {
json::container PublicQuery(CurlHandle& curlHandle, std::string_view method) {
RequestRetry requestRetry(curlHandle, CurlOptions(HttpRequestType::kGet));

return requestRetry.queryJson(method, [](const json& jsonResponse) {
return requestRetry.queryJson(method, [](const json::container& jsonResponse) {
const auto foundErrorIt = jsonResponse.find("code");
const auto foundMsgIt = jsonResponse.find("msg");
if (foundErrorIt != jsonResponse.end() && foundMsgIt != jsonResponse.end()) {
const int statusCode = foundErrorIt->get<int>(); // "1100" for instance
log::warn("Binance error ({}), full json: '{}'", statusCode, jsonResponse.dump());
log::warn("Binance error ({}), full: '{}'", statusCode, jsonResponse.dump());
return RequestRetry::Status::kResponseError;
}
return RequestRetry::Status::kResponseOK;
Expand All @@ -50,18 +50,18 @@ BinanceGlobalInfosFunc::BinanceGlobalInfosFunc(AbstractMetricGateway* pMetricGat
settings::RunMode runMode)
: _curlHandle(kCryptoFeeBaseUrl, pMetricGateway, permanentCurlOptions, runMode) {}

json BinanceGlobalInfosFunc::operator()() {
json ret = PublicQuery(_curlHandle, "/bapi/capital/v1/public/capital/getNetworkCoinAll");
json::container BinanceGlobalInfosFunc::operator()() {
json::container ret = PublicQuery(_curlHandle, "/bapi/capital/v1/public/capital/getNetworkCoinAll");
auto dataIt = ret.find("data");
json dataRet;
json::container dataRet;
if (dataIt == ret.end() || !dataIt->is_array()) {
log::error("Unexpected reply from binance getNetworkCoinAll, no data array");
dataRet = json::array_t();
dataRet = json::container::array_t();
} else {
dataRet = std::move(*dataIt);
}

const auto endIt = std::remove_if(dataRet.begin(), dataRet.end(), [](const json& el) {
const auto endIt = std::remove_if(dataRet.begin(), dataRet.end(), [](const json::container& el) {
return el["coin"].get<std::string_view>().size() > CurrencyCode::kMaxLen;
});

Expand All @@ -70,21 +70,21 @@ json BinanceGlobalInfosFunc::operator()() {
dataRet.erase(endIt, dataRet.end());
}

std::sort(dataRet.begin(), dataRet.end(), [](const json& lhs, const json& rhs) {
std::sort(dataRet.begin(), dataRet.end(), [](const json::container& lhs, const json::container& rhs) {
return lhs["coin"].get<std::string_view>() < rhs["coin"].get<std::string_view>();
});
return dataRet;
}

namespace {
MonetaryAmount ComputeWithdrawalFeesFromNetworkList(CurrencyCode cur, const json& coinElem) {
MonetaryAmount ComputeWithdrawalFeesFromNetworkList(CurrencyCode cur, const json::container& coinElem) {
MonetaryAmount withdrawFee(0, cur);
auto networkListIt = coinElem.find("networkList");
if (networkListIt == coinElem.end()) {
log::error("Unexpected Binance public coin data format, returning 0 monetary amount");
return withdrawFee;
}
for (const json& networkListPart : *networkListIt) {
for (const json::container& networkListPart : *networkListIt) {
MonetaryAmount fee(networkListPart["withdrawFee"].get<std::string_view>(), cur);
auto isDefaultIt = networkListPart.find("isDefault");
if (isDefaultIt != networkListPart.end() && isDefaultIt->get<bool>()) {
Expand All @@ -109,7 +109,7 @@ MonetaryAmountByCurrencySet BinanceGlobalInfos::queryWithdrawalFees() {

fees.reserve(allCoins.size());

std::transform(allCoins.begin(), allCoins.end(), std::back_inserter(fees), [](const json& el) {
std::transform(allCoins.begin(), allCoins.end(), std::back_inserter(fees), [](const json::container& el) {
CurrencyCode cur(el["coin"].get<std::string_view>());
return ComputeWithdrawalFeesFromNetworkList(cur, el);
});
Expand All @@ -123,7 +123,7 @@ MonetaryAmount BinanceGlobalInfos::queryWithdrawalFee(CurrencyCode currencyCode)
const auto& allCoins = _globalInfosCache.get();
const auto curStr = currencyCode.str();

const auto it = std::partition_point(allCoins.begin(), allCoins.end(), [&curStr](const json& el) {
const auto it = std::partition_point(allCoins.begin(), allCoins.end(), [&curStr](const json::container& el) {
return el["coin"].get<std::string_view>() < curStr;
});
if (it != allCoins.end() && (*it)["coin"].get<std::string_view>() == curStr) {
Expand All @@ -137,10 +137,10 @@ CurrencyExchangeFlatSet BinanceGlobalInfos::queryTradableCurrencies(const Curren
return ExtractTradableCurrencies(_globalInfosCache.get(), excludedCurrencies);
}

CurrencyExchangeFlatSet BinanceGlobalInfos::ExtractTradableCurrencies(const json& allCoins,
CurrencyExchangeFlatSet BinanceGlobalInfos::ExtractTradableCurrencies(const json::container& allCoins,
const CurrencyCodeSet& excludedCurrencies) {
CurrencyExchangeVector currencies;
for (const json& coinJson : allCoins) {
for (const json::container& coinJson : allCoins) {
CurrencyCode cur(coinJson["coin"].get<std::string_view>());
if (excludedCurrencies.contains(cur)) {
log::trace("Discard {} excluded by config", cur.str());
Expand All @@ -152,7 +152,7 @@ CurrencyExchangeFlatSet BinanceGlobalInfos::ExtractTradableCurrencies(const json
log::debug("Several networks found for {}, considering only default network", cur.str());
}
const auto it = std::find_if(networkList.begin(), networkList.end(),
[](const json& el) { return el["isDefault"].get<bool>(); });
[](const json::container& el) { return el["isDefault"].get<bool>(); });
if (it != networkList.end()) {
auto deposit = (*it)["depositEnable"].get<bool>() ? CurrencyExchange::Deposit::kAvailable
: CurrencyExchange::Deposit::kUnavailable;
Expand Down
12 changes: 6 additions & 6 deletions src/api/common/src/commonapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <utility>

#include "cachedresult.hpp"
#include "cct_json.hpp"
#include "cct_json-container.hpp"
#include "cct_log.hpp"
#include "cct_string.hpp"
#include "coincenterinfo.hpp"
Expand Down Expand Up @@ -44,13 +44,13 @@ CommonAPI::CommonAPI(const CoincenterInfo& coincenterInfo, Duration fiatsUpdateF
coincenterInfo.getRunMode()),
_withdrawalFeesCrawler(coincenterInfo, withdrawalFeesUpdateFrequency, _cachedResultVault) {
if (atInit == AtInit::kLoadFromFileCache) {
json data = GetFiatCacheFile(_coincenterInfo.dataDir()).readAllJson();
json::container data = GetFiatCacheFile(_coincenterInfo.dataDir()).readAllJson();
if (!data.empty()) {
int64_t timeEpoch = data["timeepoch"].get<int64_t>();
auto& fiatsFile = data["fiats"];
CurrencyCodeSet fiats;
fiats.reserve(static_cast<CurrencyCodeSet::size_type>(fiatsFile.size()));
for (json& val : fiatsFile) {
for (json::container& val : fiatsFile) {
log::trace("Reading fiat {} from cache file", val.get<std::string_view>());
fiats.emplace_hint(fiats.end(), std::move(val.get_ref<string&>()));
}
Expand Down Expand Up @@ -138,12 +138,12 @@ CurrencyCodeVector CommonAPI::FiatsFunc::retrieveFiatsSource1() {
return fiatsVec;
}
static constexpr bool kAllowExceptions = false;
json dataCSV = json::parse(data, nullptr, kAllowExceptions);
json::container dataCSV = json::container::parse(data, nullptr, kAllowExceptions);
if (dataCSV.is_discarded()) {
log::warn("Error parsing json data of currency codes from source 1");
return fiatsVec;
}
for (const json& fiatData : dataCSV) {
for (const json::container& fiatData : dataCSV) {
static constexpr std::string_view kCodeKey = "AlphabeticCode";
static constexpr std::string_view kWithdrawalDateKey = "WithdrawalDate";

Expand Down Expand Up @@ -216,7 +216,7 @@ void CommonAPI::updateCacheFile() const {
data["fiats"].emplace_back(fiatCode.str());
}
data["timeepoch"] = TimestampToSecondsSinceEpoch(fiatsPtrLastUpdatedTimePair.second);
fiatsCacheFile.write(data);
fiatsCacheFile.writeJson(data);
}

_withdrawalFeesCrawler.updateCacheFile();
Expand Down
14 changes: 7 additions & 7 deletions src/api/common/src/fiatconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <string_view>
#include <utility>

#include "cct_json.hpp"
#include "cct_json-container.hpp"
#include "cct_log.hpp"
#include "cct_string.hpp"
#include "coincenterinfo.hpp"
Expand All @@ -33,7 +33,7 @@ string LoadCurrencyConverterAPIKey(std::string_view dataDir) {
static constexpr std::string_view kThirdPartySecretFileName = "thirdparty_secret.json";

const File thirdPartySecret(dataDir, File::Type::kSecret, kThirdPartySecretFileName, File::IfError::kNoThrow);
json data = thirdPartySecret.readAllJson();
json::container data = thirdPartySecret.readAllJson();
auto freeConverterIt = data.find("freecurrencyconverter");
if (freeConverterIt == data.end() || freeConverterIt->get<std::string_view>() == kDefaultCommunityKey) {
log::warn("Unable to find custom Free Currency Converter key in {}", kThirdPartySecretFileName);
Expand Down Expand Up @@ -61,7 +61,7 @@ FiatConverter::FiatConverter(const CoincenterInfo& coincenterInfo, Duration rate
_ratesUpdateFrequency(ratesUpdateFrequency),
_apiKey(LoadCurrencyConverterAPIKey(coincenterInfo.dataDir())),
_dataDir(coincenterInfo.dataDir()) {
const json data = fiatsCacheReader.readAllJson();
const json::container data = fiatsCacheReader.readAllJson();

_pricesMap.reserve(data.size());
for (const auto& [marketStr, rateAndTimeData] : data.items()) {
Expand All @@ -79,14 +79,14 @@ File FiatConverter::GetRatesCacheFile(std::string_view dataDir) {
}

void FiatConverter::updateCacheFile() const {
json data;
json::container data;
for (const auto& [market, priceTimeValue] : _pricesMap) {
const string marketPairStr = market.assetsPairStrUpper('-');

data[marketPairStr]["rate"] = priceTimeValue.rate;
data[marketPairStr]["timeepoch"] = TimestampToSecondsSinceEpoch(priceTimeValue.lastUpdatedTime);
}
GetRatesCacheFile(_dataDir).write(data);
GetRatesCacheFile(_dataDir).writeJson(data);
}

std::optional<double> FiatConverter::queryCurrencyRate(Market mk) {
Expand All @@ -106,7 +106,7 @@ std::optional<double> FiatConverter::queryCurrencyRateSource1(Market mk) {
const auto dataStr = _curlHandle1.query("/api/v7/convert", opts);

static constexpr bool kAllowExceptions = false;
const auto data = json::parse(dataStr, nullptr, kAllowExceptions);
const auto data = json::container::parse(dataStr, nullptr, kAllowExceptions);

//{"query":{"count":1},"results":{"EUR_KRW":{"id":"EUR_KRW","val":1329.475323,"to":"KRW","fr":"EUR"}}}
const auto resultsIt = data.find("results");
Expand All @@ -124,7 +124,7 @@ std::optional<double> FiatConverter::queryCurrencyRateSource1(Market mk) {
std::optional<double> FiatConverter::queryCurrencyRateSource2(Market mk) {
const auto dataStr = _curlHandle2.query("", CurlOptions(HttpRequestType::kGet));
static constexpr bool kAllowExceptions = false;
const json jsonData = json::parse(dataStr, nullptr, kAllowExceptions);
const json::container jsonData = json::container::parse(dataStr, nullptr, kAllowExceptions);
if (jsonData.is_discarded()) {
log::error("Invalid response received from fiat currency converter service's second source");
return {};
Expand Down
14 changes: 7 additions & 7 deletions src/api/common/src/withdrawalfees-crawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "cct_cctype.hpp"
#include "cct_const.hpp"
#include "cct_exception.hpp"
#include "cct_json.hpp"
#include "cct_json-container.hpp"
#include "cct_log.hpp"
#include "cct_string.hpp"
#include "coincenterinfo.hpp"
Expand Down Expand Up @@ -40,7 +40,7 @@ WithdrawalFeesCrawler::WithdrawalFeesCrawler(const CoincenterInfo& coincenterInf
CachedResultVault& cachedResultVault)
: _coincenterInfo(coincenterInfo),
_withdrawalFeesCache(CachedResultOptions(minDurationBetweenQueries, cachedResultVault), coincenterInfo) {
json data = GetWithdrawInfoFile(_coincenterInfo.dataDir()).readAllJson();
json::container data = GetWithdrawInfoFile(_coincenterInfo.dataDir()).readAllJson();
if (!data.empty()) {
const auto nowTime = Clock::now();
for (const auto& [exchangeName, exchangeData] : data.items()) {
Expand Down Expand Up @@ -114,13 +114,13 @@ WithdrawalFeesCrawler::WithdrawalInfoMaps WithdrawalFeesCrawler::WithdrawalFeesF
}

void WithdrawalFeesCrawler::updateCacheFile() const {
json data;
json::container data;
for (const std::string_view exchangeName : kSupportedExchanges) {
const auto [withdrawalInfoMapsPtr, latestUpdate] = _withdrawalFeesCache.retrieve(exchangeName);
if (withdrawalInfoMapsPtr != nullptr) {
const WithdrawalInfoMaps& withdrawalInfoMaps = *withdrawalInfoMapsPtr;

json exchangeData;
json::container exchangeData;
exchangeData["timeepoch"] = TimestampToSecondsSinceEpoch(latestUpdate);
for (const auto withdrawFee : withdrawalInfoMaps.first) {
string curCodeStr = withdrawFee.currencyCode().str();
Expand All @@ -132,7 +132,7 @@ void WithdrawalFeesCrawler::updateCacheFile() const {
data.emplace(exchangeName, std::move(exchangeData));
}
}
GetWithdrawInfoFile(_coincenterInfo.dataDir()).write(data);
GetWithdrawInfoFile(_coincenterInfo.dataDir()).writeJson(data);
}

WithdrawalFeesCrawler::WithdrawalInfoMaps WithdrawalFeesCrawler::WithdrawalFeesFunc::get1(
Expand All @@ -145,7 +145,7 @@ WithdrawalFeesCrawler::WithdrawalInfoMaps WithdrawalFeesCrawler::WithdrawalFeesF

if (!withdrawalFeesCsv.empty()) {
static constexpr bool kAllowExceptions = false;
const json jsonData = json::parse(withdrawalFeesCsv, nullptr, kAllowExceptions);
const json::container jsonData = json::container::parse(withdrawalFeesCsv, nullptr, kAllowExceptions);
const auto exchangesIt = jsonData.find("exchange");
if (jsonData.is_discarded() || exchangesIt == jsonData.end()) {
log::error("no exchange data found in source 1 - either site information unavailable or code to be updated");
Expand All @@ -157,7 +157,7 @@ WithdrawalFeesCrawler::WithdrawalInfoMaps WithdrawalFeesCrawler::WithdrawalFeesF
return ret;
}

for (const json& feeJson : *feesIt) {
for (const json::container& feeJson : *feesIt) {
const auto amountIt = feeJson.find("amount");
if (amountIt == feeJson.end() || !amountIt->is_number_float()) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/api/common/test/exchangepublicapi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "exchangepublicapi_mock.hpp"
#include "exchangepublicapitypes.hpp"
#include "fiatconverter.hpp"
#include "generalconfig.hpp"
#include "general-config.hpp"
#include "loadconfiguration.hpp"
#include "market.hpp"
#include "marketorderbook.hpp"
Expand Down Expand Up @@ -54,7 +54,7 @@ class ExchangePublicTest : public ::testing::Test {
protected:
settings::RunMode runMode = settings::RunMode::kTestKeys;
LoadConfiguration loadConfiguration{kDefaultDataDir, LoadConfiguration::ExchangeConfigFileType::kTest};
CoincenterInfo coincenterInfo{runMode, loadConfiguration, GeneralConfig(),
CoincenterInfo coincenterInfo{runMode, loadConfiguration, schema::GeneralConfig(), LoggingInfo(),
MonitoringInfo(), Reader(), StableCoinReader()};
CommonAPI commonAPI{coincenterInfo, Duration::max()};
FiatConverter fiatConverter{coincenterInfo, Duration::max(), FiatConverterReader()};
Expand Down
4 changes: 2 additions & 2 deletions src/api/common/test/fiatconverter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <string_view>

#include "besturlpicker.hpp"
#include "cct_json.hpp"
#include "cct_json-container.hpp"
#include "coincenterinfo.hpp"
#include "curlhandle.hpp"
#include "curloptions.hpp"
Expand Down Expand Up @@ -38,7 +38,7 @@ CurlHandle::CurlHandle([[maybe_unused]] BestURLPicker bestURLPicker,

// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
std::string_view CurlHandle::query([[maybe_unused]] std::string_view endpoint, const CurlOptions &opts) {
json jsonData;
json::container jsonData;

// Rates
std::string_view marketStr = opts.postData().get("q");
Expand Down
4 changes: 2 additions & 2 deletions src/api/exchanges/include/binancepublicapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <unordered_map>

#include "cachedresult.hpp"
#include "cct_json.hpp"
#include "cct_json-container.hpp"
#include "curlhandle.hpp"
#include "currencycode.hpp"
#include "currencyexchange.hpp"
Expand Down Expand Up @@ -83,7 +83,7 @@ class BinancePublic : public ExchangePublic {
};

struct ExchangeInfoFunc {
using ExchangeInfoDataByMarket = std::unordered_map<Market, json>;
using ExchangeInfoDataByMarket = std::unordered_map<Market, json::container>;

ExchangeInfoDataByMarket operator()();

Expand Down
2 changes: 1 addition & 1 deletion src/api/exchanges/include/bithumbprivateapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <unordered_map>

#include "cachedresult.hpp"
#include "cct_json.hpp"
#include "cct_json-container.hpp"
#include "curlhandle.hpp"
#include "exchangeprivateapi.hpp"
#include "exchangeprivateapitypes.hpp"
Expand Down
Loading

0 comments on commit 144b9f5

Please sign in to comment.