From ede048e567435731931d7dfca09531f9907a3422 Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Sat, 9 Nov 2024 20:01:57 +0100 Subject: [PATCH] [Code refactoring] - Extract Permanent curl option builder method from ExchangeConfig --- .../include/cache-file-updator-interface.hpp | 12 ++++ src/api/common/include/commonapi.hpp | 4 +- .../exchange-permanent-curl-options.hpp | 25 +++++++++ src/api/common/include/exchangebase.hpp | 56 ------------------- src/api/common/include/exchangeprivateapi.hpp | 7 ++- src/api/common/include/exchangepublicapi.hpp | 13 +++-- src/api/common/include/fiatconverter.hpp | 5 +- .../common/include/withdrawalfees-crawler.hpp | 5 +- .../src/exchange-permanent-curl-options.cpp | 33 +++++++++++ src/api/common/src/exchangeprivateapi.cpp | 4 ++ src/api/common/src/exchangepublicapi.cpp | 5 ++ .../exchanges/include/binancepublicapi.hpp | 5 +- src/api/exchanges/src/binanceprivateapi.cpp | 3 +- src/api/exchanges/src/binancepublicapi.cpp | 11 ++-- src/api/exchanges/src/bithumbprivateapi.cpp | 4 +- src/api/exchanges/src/bithumbpublicapi.cpp | 3 +- src/api/exchanges/src/huobiprivateapi.cpp | 3 +- src/api/exchanges/src/huobipublicapi.cpp | 3 +- src/api/exchanges/src/krakenprivateapi.cpp | 4 +- src/api/exchanges/src/krakenpublicapi.cpp | 3 +- src/api/exchanges/src/kucoinprivateapi.cpp | 3 +- src/api/exchanges/src/kucoinpublicapi.cpp | 3 +- src/api/exchanges/src/upbitprivateapi.cpp | 4 +- src/api/exchanges/src/upbitpublicapi.cpp | 3 +- src/api/interface/include/exchange.hpp | 5 +- src/basic-objects/include/currencycodeset.hpp | 2 + .../include/currencycodevector.hpp | 2 + src/objects/include/exchangeconfig.hpp | 9 +-- src/objects/src/exchangeconfig.cpp | 24 -------- 29 files changed, 131 insertions(+), 132 deletions(-) create mode 100644 src/api/common/include/cache-file-updator-interface.hpp create mode 100644 src/api/common/include/exchange-permanent-curl-options.hpp delete mode 100644 src/api/common/include/exchangebase.hpp create mode 100644 src/api/common/src/exchange-permanent-curl-options.cpp diff --git a/src/api/common/include/cache-file-updator-interface.hpp b/src/api/common/include/cache-file-updator-interface.hpp new file mode 100644 index 00000000..acd7478c --- /dev/null +++ b/src/api/common/include/cache-file-updator-interface.hpp @@ -0,0 +1,12 @@ +#pragma once + +namespace cct { + +class CacheFileUpdatorInterface { + public: + virtual ~CacheFileUpdatorInterface() = default; + + virtual void updateCacheFile() const {} +}; + +} // namespace cct \ No newline at end of file diff --git a/src/api/common/include/commonapi.hpp b/src/api/common/include/commonapi.hpp index cbca7458..ecaf8112 100644 --- a/src/api/common/include/commonapi.hpp +++ b/src/api/common/include/commonapi.hpp @@ -7,12 +7,12 @@ #include #include "binance-common-api.hpp" +#include "cache-file-updator-interface.hpp" #include "cachedresult.hpp" #include "curlhandle.hpp" #include "currencycode.hpp" #include "currencycodeset.hpp" #include "currencycodevector.hpp" -#include "exchangebase.hpp" #include "monetaryamount.hpp" #include "monetaryamountbycurrencyset.hpp" #include "timedef.hpp" @@ -22,7 +22,7 @@ namespace cct { class CoincenterInfo; namespace api { /// Public API connected to different exchanges, providing fast methods to retrieve huge amount of data. -class CommonAPI : public ExchangeBase { +class CommonAPI : public CacheFileUpdatorInterface { public: enum class AtInit : int8_t { kLoadFromFileCache, kNoLoadFromFileCache }; diff --git a/src/api/common/include/exchange-permanent-curl-options.hpp b/src/api/common/include/exchange-permanent-curl-options.hpp new file mode 100644 index 00000000..0944e996 --- /dev/null +++ b/src/api/common/include/exchange-permanent-curl-options.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +#include "permanentcurloptions.hpp" + +namespace cct { + +class ExchangeConfig; + +namespace api { +class ExchangePermanentCurlOptions { + public: + explicit ExchangePermanentCurlOptions(const ExchangeConfig &exchangeConfig); + + enum class Api : int8_t { Public, Private }; + + PermanentCurlOptions::Builder builderBase(Api api) const; + + private: + const ExchangeConfig &_exchangeConfig; +}; +} // namespace api + +} // namespace cct \ No newline at end of file diff --git a/src/api/common/include/exchangebase.hpp b/src/api/common/include/exchangebase.hpp deleted file mode 100644 index cded31d9..00000000 --- a/src/api/common/include/exchangebase.hpp +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once - -#include - -#include "cachedresultvault.hpp" - -namespace cct::api { - -/// RAII object whose lifetime triggers a special behavior of the CachedResults contained by the vault of this -/// Exchange: -/// - Next query will force external call and refresh the cache -/// - All subsequent queries will return the same cached value -/// This is to ensure constant, deterministic and up to date behavior of search algorithms during their process. -/// At the destruction of the returned handle, all the CachedResults' behavior will come back to standard. -class CacheFreezerRAII { - public: - CacheFreezerRAII() noexcept = default; - - explicit CacheFreezerRAII(CachedResultVault &cachedResultVault) : _pCachedResultVault(&cachedResultVault) { - cachedResultVault.freezeAll(); - } - - CacheFreezerRAII(const CacheFreezerRAII &) = delete; - CacheFreezerRAII(CacheFreezerRAII &&rhs) noexcept - : _pCachedResultVault(std::exchange(rhs._pCachedResultVault, nullptr)) {} - - CacheFreezerRAII &operator=(const CacheFreezerRAII &) = delete; - CacheFreezerRAII &operator=(CacheFreezerRAII &&rhs) noexcept { - if (this != &rhs) { - _pCachedResultVault = std::exchange(rhs._pCachedResultVault, nullptr); - } - return *this; - } - - void swap(CacheFreezerRAII &rhs) noexcept { std::swap(_pCachedResultVault, rhs._pCachedResultVault); } - - ~CacheFreezerRAII() { - if (_pCachedResultVault != nullptr) { - _pCachedResultVault->unfreezeAll(); - } - } - - private: - CachedResultVault *_pCachedResultVault{}; -}; - -class ExchangeBase { - public: - virtual ~ExchangeBase() = default; - - virtual void updateCacheFile() const {} - - protected: - ExchangeBase() = default; -}; -} // namespace cct::api \ No newline at end of file diff --git a/src/api/common/include/exchangeprivateapi.hpp b/src/api/common/include/exchangeprivateapi.hpp index 617ebfcf..8f095b4b 100644 --- a/src/api/common/include/exchangeprivateapi.hpp +++ b/src/api/common/include/exchangeprivateapi.hpp @@ -6,11 +6,11 @@ #include "apikey.hpp" #include "balanceoptions.hpp" #include "balanceportfolio.hpp" +#include "cache-file-updator-interface.hpp" #include "cachedresultvault.hpp" #include "currencycode.hpp" #include "currencyexchangeflatset.hpp" #include "depositsconstraints.hpp" -#include "exchangebase.hpp" #include "exchangeconfig.hpp" #include "exchangeprivateapitypes.hpp" #include "exchangepublicapi.hpp" @@ -20,6 +20,7 @@ #include "monetaryamountbycurrencyset.hpp" #include "orderid.hpp" #include "ordersconstraints.hpp" +#include "permanentcurloptions.hpp" #include "tradedamounts.hpp" #include "tradeinfo.hpp" #include "wallet.hpp" @@ -35,7 +36,7 @@ class WithdrawOptions; namespace api { class APIKey; -class ExchangePrivate : public ExchangeBase { +class ExchangePrivate : public CacheFileUpdatorInterface { public: virtual ~ExchangePrivate() = default; @@ -164,6 +165,8 @@ class ExchangePrivate : public ExchangeBase { TradedAmounts marketTrade(MonetaryAmount from, const TradeOptions &tradeOptions, Market mk); + PermanentCurlOptions::Builder permanentCurlOptionsBuilder() const; + ExchangePublic &_exchangePublic; CachedResultVault &_cachedResultVault{_exchangePublic._cachedResultVault}; const CoincenterInfo &_coincenterInfo; diff --git a/src/api/common/include/exchangepublicapi.hpp b/src/api/common/include/exchangepublicapi.hpp index 586e76ca..5519ea14 100644 --- a/src/api/common/include/exchangepublicapi.hpp +++ b/src/api/common/include/exchangepublicapi.hpp @@ -5,11 +5,11 @@ #include #include +#include "cache-file-updator-interface.hpp" #include "commonapi.hpp" #include "currencycode.hpp" #include "currencycodeset.hpp" #include "currencyexchangeflatset.hpp" -#include "exchangebase.hpp" #include "exchangepublicapitypes.hpp" #include "market-order-book-vector.hpp" #include "market-timestamp-set.hpp" @@ -17,6 +17,7 @@ #include "marketorderbook.hpp" #include "monetaryamount.hpp" #include "monetaryamountbycurrencyset.hpp" +#include "permanentcurloptions.hpp" #include "priceoptions.hpp" #include "public-trade-vector.hpp" #include "time-window.hpp" @@ -31,7 +32,7 @@ class FiatConverter; namespace api { -class ExchangePublic : public ExchangeBase { +class ExchangePublic : public CacheFileUpdatorInterface { public: static constexpr int kDefaultDepth = MarketOrderBook::kDefaultDepth; static constexpr int kNbLastTradesDefault = 100; @@ -191,6 +192,9 @@ class ExchangePublic : public ExchangeBase { MarketOrderBookVector pullMarketOrderBooksForReplay(Market market, TimeWindow timeWindow); protected: + ExchangePublic(std::string_view name, FiatConverter &fiatConverter, CommonAPI &commonApi, + const CoincenterInfo &coincenterInfo); + /// Retrieve the order book of given market. /// It should be more precise that previous version with possibility to go deeper. virtual MarketOrderBook queryOrderBook(Market mk, int depth = kDefaultDepth) = 0; @@ -198,10 +202,9 @@ class ExchangePublic : public ExchangeBase { /// Retrieve an ordered vector of recent last trades virtual PublicTradeVector queryLastTrades(Market mk, int nbTrades = kNbLastTradesDefault) = 0; - friend class ExchangePrivate; + PermanentCurlOptions::Builder permanentCurlOptionsBuilder() const; - ExchangePublic(std::string_view name, FiatConverter &fiatConverter, CommonAPI &commonApi, - const CoincenterInfo &coincenterInfo); + friend class ExchangePrivate; std::string_view _name; CachedResultVault _cachedResultVault; diff --git a/src/api/common/include/fiatconverter.hpp b/src/api/common/include/fiatconverter.hpp index 362cd2cc..dd6a5243 100644 --- a/src/api/common/include/fiatconverter.hpp +++ b/src/api/common/include/fiatconverter.hpp @@ -5,6 +5,7 @@ #include #include +#include "cache-file-updator-interface.hpp" #include "cct_string.hpp" #include "curlhandle.hpp" #include "currencycode.hpp" @@ -33,7 +34,7 @@ class CoincenterInfo; /// Fallback mechanism exists if api key does not exist or is expired. /// /// Conversion methods are thread safe. -class FiatConverter { +class FiatConverter : public CacheFileUpdatorInterface { public: static File GetRatesCacheFile(std::string_view dataDir); @@ -58,7 +59,7 @@ class FiatConverter { /// Store rates in a file to make data persistent. /// This method is not thread-safe and is expected to be called only once before end of normal termination of program. - void updateCacheFile() const; + void updateCacheFile() const override; private: struct PriceTimedValue { diff --git a/src/api/common/include/withdrawalfees-crawler.hpp b/src/api/common/include/withdrawalfees-crawler.hpp index 741803a6..1a93ae3f 100644 --- a/src/api/common/include/withdrawalfees-crawler.hpp +++ b/src/api/common/include/withdrawalfees-crawler.hpp @@ -4,6 +4,7 @@ #include #include +#include "cache-file-updator-interface.hpp" #include "cachedresult.hpp" #include "cachedresultvault.hpp" #include "coincenterinfo.hpp" @@ -17,7 +18,7 @@ namespace cct { /// This class is able to crawl some public withdrawal fees web pages in order to retrieve them from unofficial sources, /// which is better than nothing. This class is non thread-safe. -class WithdrawalFeesCrawler { +class WithdrawalFeesCrawler : public CacheFileUpdatorInterface { public: WithdrawalFeesCrawler(const CoincenterInfo& coincenterInfo, Duration minDurationBetweenQueries, CachedResultVault& cachedResultVault); @@ -27,7 +28,7 @@ class WithdrawalFeesCrawler { const WithdrawalInfoMaps& get(std::string_view exchangeName) { return _withdrawalFeesCache.get(exchangeName); } - void updateCacheFile() const; + void updateCacheFile() const override; private: class WithdrawalFeesFunc { diff --git a/src/api/common/src/exchange-permanent-curl-options.cpp b/src/api/common/src/exchange-permanent-curl-options.cpp new file mode 100644 index 00000000..0ff74c29 --- /dev/null +++ b/src/api/common/src/exchange-permanent-curl-options.cpp @@ -0,0 +1,33 @@ +#include "exchange-permanent-curl-options.hpp" + +#include "exchangeconfig.hpp" + +namespace cct::api { + +ExchangePermanentCurlOptions::ExchangePermanentCurlOptions(const ExchangeConfig &exchangeConfig) + : _exchangeConfig(exchangeConfig) {} + +PermanentCurlOptions::Builder ExchangePermanentCurlOptions::builderBase(Api api) const { + PermanentCurlOptions::Builder builder; + + builder.setAcceptedEncoding(_exchangeConfig.acceptEncoding()) + .setRequestCallLogLevel(_exchangeConfig.requestsCallLogLevel()) + .setRequestAnswerLogLevel(_exchangeConfig.requestsAnswerLogLevel()) + .setTimeout(_exchangeConfig.httpConfig().timeout()); + + switch (api) { + case Api::Private: + builder.setMinDurationBetweenQueries(_exchangeConfig.privateAPIRate()); + break; + case Api::Public: + builder.setMinDurationBetweenQueries(_exchangeConfig.publicAPIRate()) + .setTooManyErrorsPolicy(PermanentCurlOptions::TooManyErrorsPolicy::kReturnEmptyResponse); + break; + default: + break; + } + + return builder; +} + +} // namespace cct::api \ No newline at end of file diff --git a/src/api/common/src/exchangeprivateapi.cpp b/src/api/common/src/exchangeprivateapi.cpp index 79ec508d..66bc2b21 100644 --- a/src/api/common/src/exchangeprivateapi.cpp +++ b/src/api/common/src/exchangeprivateapi.cpp @@ -22,6 +22,7 @@ #include "deposit.hpp" #include "depositsconstraints.hpp" #include "durationstring.hpp" +#include "exchange-permanent-curl-options.hpp" #include "exchangeconfig.hpp" #include "exchangename.hpp" #include "exchangeprivateapitypes.hpp" @@ -638,4 +639,7 @@ SentWithdrawInfo ExchangePrivate::isWithdrawSuccessfullySent(const InitiatedWith return {currencyCode}; } +PermanentCurlOptions::Builder ExchangePrivate::permanentCurlOptionsBuilder() const { + return ExchangePermanentCurlOptions(exchangeConfig()).builderBase(ExchangePermanentCurlOptions::Api::Private); +} } // namespace cct::api diff --git a/src/api/common/src/exchangepublicapi.cpp b/src/api/common/src/exchangepublicapi.cpp index 7a87cb7b..0b377ca4 100644 --- a/src/api/common/src/exchangepublicapi.cpp +++ b/src/api/common/src/exchangepublicapi.cpp @@ -24,6 +24,7 @@ #include "commonapi.hpp" #include "currencycode.hpp" #include "currencycodeset.hpp" +#include "exchange-permanent-curl-options.hpp" #include "exchangeconfig.hpp" #include "exchangepublicapitypes.hpp" #include "fiatconverter.hpp" @@ -527,4 +528,8 @@ AbstractMarketDataSerializer &ExchangePublic::getMarketDataSerializer() { return *_marketDataSerializerPtr; } +PermanentCurlOptions::Builder ExchangePublic::permanentCurlOptionsBuilder() const { + return ExchangePermanentCurlOptions(exchangeConfig()).builderBase(ExchangePermanentCurlOptions::Api::Public); +} + } // namespace cct::api diff --git a/src/api/exchanges/include/binancepublicapi.hpp b/src/api/exchanges/include/binancepublicapi.hpp index 37631233..7837f460 100644 --- a/src/api/exchanges/include/binancepublicapi.hpp +++ b/src/api/exchanges/include/binancepublicapi.hpp @@ -76,10 +76,10 @@ class BinancePublic : public ExchangePublic { MonetaryAmount computePriceForNotional(Market mk, int avgPriceMins); struct CommonInfo { - CommonInfo(const CoincenterInfo& coincenterInfo, const ExchangeConfig& exchangeConfig, settings::RunMode runMode); + CommonInfo(CurlHandle& curlHandle, const ExchangeConfig& exchangeConfig); const ExchangeConfig& _exchangeConfig; - CurlHandle _curlHandle; + CurlHandle& _curlHandle; }; struct ExchangeInfoFunc { @@ -124,6 +124,7 @@ class BinancePublic : public ExchangePublic { CommonInfo& _commonInfo; }; + CurlHandle _curlHandle; CommonInfo _commonInfo; CachedResult _exchangeConfigCache; CachedResult _marketsCache; diff --git a/src/api/exchanges/src/binanceprivateapi.cpp b/src/api/exchanges/src/binanceprivateapi.cpp index b4e9859e..8b941578 100644 --- a/src/api/exchanges/src/binanceprivateapi.cpp +++ b/src/api/exchanges/src/binanceprivateapi.cpp @@ -223,8 +223,7 @@ json::container PrivateQuery(CurlHandle& curlHandle, const APIKey& apiKey, HttpR BinancePrivate::BinancePrivate(const CoincenterInfo& coincenterInfo, BinancePublic& binancePublic, const APIKey& apiKey) : ExchangePrivate(coincenterInfo, binancePublic, apiKey), - _curlHandle(BinancePublic::kURLBases, coincenterInfo.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPrivate).build(), + _curlHandle(BinancePublic::kURLBases, coincenterInfo.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), coincenterInfo.getRunMode()), _tradableCurrenciesCache( CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kCurrencies), _cachedResultVault), _curlHandle, diff --git a/src/api/exchanges/src/binancepublicapi.cpp b/src/api/exchanges/src/binancepublicapi.cpp index d423726f..a515cd67 100644 --- a/src/api/exchanges/src/binancepublicapi.cpp +++ b/src/api/exchanges/src/binancepublicapi.cpp @@ -88,7 +88,9 @@ VolAndPriNbDecimals QueryVolAndPriNbDecimals(const ExchangeInfoDataByMarket& exc BinancePublic::BinancePublic(const CoincenterInfo& coincenterInfo, FiatConverter& fiatConverter, api::CommonAPI& commonAPI) : ExchangePublic("binance", fiatConverter, commonAPI, coincenterInfo), - _commonInfo(coincenterInfo, exchangeConfig(), coincenterInfo.getRunMode()), + _curlHandle(kURLBases, coincenterInfo.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), + coincenterInfo.getRunMode()), + _commonInfo(_curlHandle, exchangeConfig()), _exchangeConfigCache( CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kCurrencies), _cachedResultVault), _commonInfo), @@ -135,11 +137,8 @@ std::optional BinancePublic::queryWithdrawalFee(CurrencyCode cur return withdrawFee; } -BinancePublic::CommonInfo::CommonInfo(const CoincenterInfo& coincenterInfo, const ExchangeConfig& exchangeConfig, - settings::RunMode runMode) - : _exchangeConfig(exchangeConfig), - _curlHandle(kURLBases, coincenterInfo.metricGatewayPtr(), - _exchangeConfig.curlOptionsBuilderBase(ExchangeConfig::Api::kPublic).build(), runMode) {} +BinancePublic::CommonInfo::CommonInfo(CurlHandle& curlHandle, const ExchangeConfig& exchangeConfig) + : _exchangeConfig(exchangeConfig), _curlHandle(curlHandle) {} MarketSet BinancePublic::MarketsFunc::operator()() { BinancePublic::ExchangeInfoFunc::ExchangeInfoDataByMarket exchangeInfoData = _exchangeConfigCache.get(); diff --git a/src/api/exchanges/src/bithumbprivateapi.cpp b/src/api/exchanges/src/bithumbprivateapi.cpp index 71576679..bd019166 100644 --- a/src/api/exchanges/src/bithumbprivateapi.cpp +++ b/src/api/exchanges/src/bithumbprivateapi.cpp @@ -320,8 +320,8 @@ File GetBithumbCurrencyInfoMapCache(std::string_view dataDir) { BithumbPrivate::BithumbPrivate(const CoincenterInfo& config, BithumbPublic& bithumbPublic, const APIKey& apiKey) : ExchangePrivate(config, bithumbPublic, apiKey), - _curlHandle(BithumbPublic::kUrlBase, config.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPrivate).build(), config.getRunMode()), + _curlHandle(BithumbPublic::kUrlBase, config.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), + config.getRunMode()), _currencyOrderInfoRefreshTime(exchangeConfig().getAPICallUpdateFrequency(kCurrencyInfoBithumb)), _depositWalletsCache( CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kDepositWallet), _cachedResultVault), diff --git a/src/api/exchanges/src/bithumbpublicapi.cpp b/src/api/exchanges/src/bithumbpublicapi.cpp index 62762563..c10b8f3c 100644 --- a/src/api/exchanges/src/bithumbpublicapi.cpp +++ b/src/api/exchanges/src/bithumbpublicapi.cpp @@ -94,8 +94,7 @@ json::container PublicQuery(CurlHandle& curlHandle, std::string_view endpoint, C BithumbPublic::BithumbPublic(const CoincenterInfo& config, FiatConverter& fiatConverter, CommonAPI& commonAPI) : ExchangePublic(kExchangeName, fiatConverter, commonAPI, config), - _curlHandle(kUrlBase, config.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPublic).build(), config.getRunMode()), + _curlHandle(kUrlBase, config.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), config.getRunMode()), _tradableCurrenciesCache( CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kCurrencies), _cachedResultVault), config, commonAPI, _curlHandle), diff --git a/src/api/exchanges/src/huobiprivateapi.cpp b/src/api/exchanges/src/huobiprivateapi.cpp index 1aecd569..f5ab8ec9 100644 --- a/src/api/exchanges/src/huobiprivateapi.cpp +++ b/src/api/exchanges/src/huobiprivateapi.cpp @@ -145,8 +145,7 @@ constexpr std::string_view kBaseUrlOrders = "/v1/order/orders/"; HuobiPrivate::HuobiPrivate(const CoincenterInfo& coincenterInfo, HuobiPublic& huobiPublic, const APIKey& apiKey) : ExchangePrivate(coincenterInfo, huobiPublic, apiKey), - _curlHandle(HuobiPublic::kURLBases, coincenterInfo.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPrivate).build(), + _curlHandle(HuobiPublic::kURLBases, coincenterInfo.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), coincenterInfo.getRunMode()), _accountIdCache(CachedResultOptions(std::chrono::hours(48), _cachedResultVault), _curlHandle, apiKey), _depositWalletsCache( diff --git a/src/api/exchanges/src/huobipublicapi.cpp b/src/api/exchanges/src/huobipublicapi.cpp index a792b214..f1da6dde 100644 --- a/src/api/exchanges/src/huobipublicapi.cpp +++ b/src/api/exchanges/src/huobipublicapi.cpp @@ -85,8 +85,7 @@ json::container PublicQuery(CurlHandle& curlHandle, std::string_view endpoint, HuobiPublic::HuobiPublic(const CoincenterInfo& config, FiatConverter& fiatConverter, api::CommonAPI& commonAPI) : ExchangePublic("huobi", fiatConverter, commonAPI, config), - _curlHandle(kURLBases, config.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPublic).build(), config.getRunMode()), + _curlHandle(kURLBases, config.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), config.getRunMode()), _healthCheckCurlHandle( kHealthCheckBaseUrl, config.metricGatewayPtr(), PermanentCurlOptions::Builder().setMinDurationBetweenQueries(exchangeConfig().publicAPIRate()).build(), diff --git a/src/api/exchanges/src/krakenprivateapi.cpp b/src/api/exchanges/src/krakenprivateapi.cpp index f91e6f77..4492d2b2 100644 --- a/src/api/exchanges/src/krakenprivateapi.cpp +++ b/src/api/exchanges/src/krakenprivateapi.cpp @@ -131,8 +131,8 @@ auto PrivateQuery(CurlHandle& curlHandle, const APIKey& apiKey, std::string_view KrakenPrivate::KrakenPrivate(const CoincenterInfo& config, KrakenPublic& krakenPublic, const APIKey& apiKey) : ExchangePrivate(config, krakenPublic, apiKey), - _curlHandle(KrakenPublic::kUrlBase, config.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPrivate).build(), config.getRunMode()), + _curlHandle(KrakenPublic::kUrlBase, config.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), + config.getRunMode()), _depositWalletsCache( CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kDepositWallet), _cachedResultVault), _curlHandle, _apiKey, krakenPublic) {} diff --git a/src/api/exchanges/src/krakenpublicapi.cpp b/src/api/exchanges/src/krakenpublicapi.cpp index 73f9940c..3bb7a02a 100644 --- a/src/api/exchanges/src/krakenpublicapi.cpp +++ b/src/api/exchanges/src/krakenpublicapi.cpp @@ -99,8 +99,7 @@ bool CheckCurrencyExchange(std::string_view krakenEntryCurrencyCode, std::string KrakenPublic::KrakenPublic(const CoincenterInfo& config, FiatConverter& fiatConverter, CommonAPI& commonAPI) : ExchangePublic(kExchangeName, fiatConverter, commonAPI, config), - _curlHandle(kUrlBase, config.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPublic).build(), config.getRunMode()), + _curlHandle(kUrlBase, config.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), config.getRunMode()), _tradableCurrenciesCache( CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kCurrencies), _cachedResultVault), config, commonAPI, _curlHandle, exchangeConfig()), diff --git a/src/api/exchanges/src/kucoinprivateapi.cpp b/src/api/exchanges/src/kucoinprivateapi.cpp index 7c22d768..927a40bb 100644 --- a/src/api/exchanges/src/kucoinprivateapi.cpp +++ b/src/api/exchanges/src/kucoinprivateapi.cpp @@ -161,8 +161,7 @@ bool EnsureEnoughAmountIn(CurlHandle& curlHandle, const APIKey& apiKey, Monetary KucoinPrivate::KucoinPrivate(const CoincenterInfo& coincenterInfo, KucoinPublic& kucoinPublic, const APIKey& apiKey) : ExchangePrivate(coincenterInfo, kucoinPublic, apiKey), - _curlHandle(KucoinPublic::kUrlBase, coincenterInfo.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPrivate).build(), + _curlHandle(KucoinPublic::kUrlBase, coincenterInfo.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), coincenterInfo.getRunMode()), _depositWalletsCache( CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kDepositWallet), _cachedResultVault), diff --git a/src/api/exchanges/src/kucoinpublicapi.cpp b/src/api/exchanges/src/kucoinpublicapi.cpp index 07487e69..5ad0c598 100644 --- a/src/api/exchanges/src/kucoinpublicapi.cpp +++ b/src/api/exchanges/src/kucoinpublicapi.cpp @@ -69,8 +69,7 @@ json::container PublicQuery(CurlHandle& curlHandle, std::string_view endpoint, KucoinPublic::KucoinPublic(const CoincenterInfo& config, FiatConverter& fiatConverter, api::CommonAPI& commonAPI) : ExchangePublic("kucoin", fiatConverter, commonAPI, config), - _curlHandle(kUrlBase, config.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPublic).build(), config.getRunMode()), + _curlHandle(kUrlBase, config.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), config.getRunMode()), _tradableCurrenciesCache( CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kCurrencies), _cachedResultVault), _curlHandle, _coincenterInfo, commonAPI), diff --git a/src/api/exchanges/src/upbitprivateapi.cpp b/src/api/exchanges/src/upbitprivateapi.cpp index 622f6db4..f0aaada0 100644 --- a/src/api/exchanges/src/upbitprivateapi.cpp +++ b/src/api/exchanges/src/upbitprivateapi.cpp @@ -110,8 +110,8 @@ json::container PrivateQuery(CurlHandle& curlHandle, const APIKey& apiKey, HttpR UpbitPrivate::UpbitPrivate(const CoincenterInfo& config, UpbitPublic& upbitPublic, const APIKey& apiKey) : ExchangePrivate(config, upbitPublic, apiKey), - _curlHandle(UpbitPublic::kUrlBase, config.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPrivate).build(), config.getRunMode()), + _curlHandle(UpbitPublic::kUrlBase, config.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), + config.getRunMode()), _tradableCurrenciesCache( CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kCurrencies), _cachedResultVault), _curlHandle, _apiKey, exchangeConfig(), upbitPublic._commonApi), diff --git a/src/api/exchanges/src/upbitpublicapi.cpp b/src/api/exchanges/src/upbitpublicapi.cpp index 7f4c56ff..144021fc 100644 --- a/src/api/exchanges/src/upbitpublicapi.cpp +++ b/src/api/exchanges/src/upbitpublicapi.cpp @@ -64,8 +64,7 @@ json::container PublicQuery(CurlHandle& curlHandle, std::string_view endpoint, UpbitPublic::UpbitPublic(const CoincenterInfo& config, FiatConverter& fiatConverter, CommonAPI& commonAPI) : ExchangePublic("upbit", fiatConverter, commonAPI, config), - _curlHandle(kUrlBase, config.metricGatewayPtr(), - exchangeConfig().curlOptionsBuilderBase(ExchangeConfig::Api::kPublic).build(), config.getRunMode()), + _curlHandle(kUrlBase, config.metricGatewayPtr(), permanentCurlOptionsBuilder().build(), config.getRunMode()), _marketsCache(CachedResultOptions(exchangeConfig().getAPICallUpdateFrequency(kMarkets), _cachedResultVault), _curlHandle, exchangeConfig()), _tradableCurrenciesCache( diff --git a/src/api/interface/include/exchange.hpp b/src/api/interface/include/exchange.hpp index f6a5b12e..80b49f8e 100644 --- a/src/api/interface/include/exchange.hpp +++ b/src/api/interface/include/exchange.hpp @@ -5,6 +5,7 @@ #include #include +#include "cache-file-updator-interface.hpp" #include "cct_exception.hpp" #include "currencycode.hpp" #include "currencyexchange.hpp" @@ -22,7 +23,7 @@ namespace cct { -class Exchange { +class Exchange : public CacheFileUpdatorInterface { public: using ExchangePublic = api::ExchangePublic; using ExchangePrivate = api::ExchangePrivate; @@ -112,7 +113,7 @@ class Exchange { return name() == exchangeName.name() && (!exchangeName.isKeyNameDefined() || keyName() == exchangeName.keyName()); } - void updateCacheFile() const; + void updateCacheFile() const override; /// unique_ptr is always trivially relocatable whatever the underlying type. using trivially_relocatable = std::true_type; diff --git a/src/basic-objects/include/currencycodeset.hpp b/src/basic-objects/include/currencycodeset.hpp index bd6c1f67..5b3c72ec 100644 --- a/src/basic-objects/include/currencycodeset.hpp +++ b/src/basic-objects/include/currencycodeset.hpp @@ -4,5 +4,7 @@ #include "currencycode.hpp" namespace cct { + using CurrencyCodeSet = FlatSet; + } \ No newline at end of file diff --git a/src/basic-objects/include/currencycodevector.hpp b/src/basic-objects/include/currencycodevector.hpp index fe06ad14..c573cdda 100644 --- a/src/basic-objects/include/currencycodevector.hpp +++ b/src/basic-objects/include/currencycodevector.hpp @@ -4,5 +4,7 @@ #include "currencycode.hpp" namespace cct { + using CurrencyCodeVector = vector; + } \ No newline at end of file diff --git a/src/objects/include/exchangeconfig.hpp b/src/objects/include/exchangeconfig.hpp index 008e77cf..4d3e552e 100644 --- a/src/objects/include/exchangeconfig.hpp +++ b/src/objects/include/exchangeconfig.hpp @@ -12,7 +12,6 @@ #include "http-config.hpp" #include "monetaryamount.hpp" #include "monetaryamountbycurrencyset.hpp" -#include "permanentcurloptions.hpp" #include "timedef.hpp" #include "tradeconfig.hpp" @@ -62,8 +61,8 @@ class ExchangeConfig { /// Apply the general maker fee defined for this exchange on given MonetaryAmount. /// In other words, convert a gross amount into a net amount with maker fees - MonetaryAmount applyFee(MonetaryAmount mk, FeeType feeType) const { - return mk * (feeType == FeeType::kMaker ? _generalMakerRatio : _generalTakerRatio); + MonetaryAmount applyFee(MonetaryAmount ma, FeeType feeType) const { + return ma * (feeType == FeeType::kMaker ? _generalMakerRatio : _generalTakerRatio); } MonetaryAmount getMakerFeeRatio() const { return _generalMakerRatio; } @@ -105,10 +104,6 @@ class ExchangeConfig { const TradeConfig &tradeConfig() const { return _tradeConfig; } - enum class Api : int8_t { kPublic, kPrivate }; - - PermanentCurlOptions::Builder curlOptionsBuilderBase(Api api) const; - bool withMarketDataSerialization() const { return _withMarketSerialization; } private: diff --git a/src/objects/src/exchangeconfig.cpp b/src/objects/src/exchangeconfig.cpp index ce9e8406..fe131a30 100644 --- a/src/objects/src/exchangeconfig.cpp +++ b/src/objects/src/exchangeconfig.cpp @@ -16,7 +16,6 @@ #include "http-config.hpp" #include "monetaryamount.hpp" #include "monetaryamountbycurrencyset.hpp" -#include "permanentcurloptions.hpp" #include "timedef.hpp" #include "tradeconfig.hpp" @@ -125,27 +124,4 @@ ExchangeConfig::ExchangeConfig( } } -PermanentCurlOptions::Builder ExchangeConfig::curlOptionsBuilderBase(Api api) const { - PermanentCurlOptions::Builder builder; - - builder.setAcceptedEncoding(acceptEncoding()) - .setRequestCallLogLevel(requestsCallLogLevel()) - .setRequestAnswerLogLevel(requestsAnswerLogLevel()) - .setTimeout(httpConfig().timeout()); - - switch (api) { - case Api::kPrivate: - builder.setMinDurationBetweenQueries(privateAPIRate()); - break; - case Api::kPublic: - builder.setMinDurationBetweenQueries(publicAPIRate()) - .setTooManyErrorsPolicy(PermanentCurlOptions::TooManyErrorsPolicy::kReturnEmptyResponse); - break; - default: - break; - } - - return builder; -} - } // namespace cct