diff --git a/src/api/common/include/exchangepublicapi.hpp b/src/api/common/include/exchangepublicapi.hpp index 6c3eb721..c673aaec 100644 --- a/src/api/common/include/exchangepublicapi.hpp +++ b/src/api/common/include/exchangepublicapi.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -51,19 +52,19 @@ class ExchangePublic : public ExchangeBase { /// Attempts to convert amount into a target currency. /// Conversion is made according to given price options, which uses the 'Maker' prices by default. - std::optional convert(MonetaryAmount a, CurrencyCode toCurrency, + std::optional convert(MonetaryAmount from, CurrencyCode toCurrency, const PriceOptions &priceOptions = PriceOptions()) { MarketOrderBookMap marketOrderBookMap; Fiats fiats = queryFiats(); MarketSet markets; - MarketsPath conversionPath = findMarketsPath(a.currencyCode(), toCurrency, markets, fiats, true); - return convert(a, toCurrency, conversionPath, fiats, marketOrderBookMap, priceOptions); + MarketsPath conversionPath = findMarketsPath(from.currencyCode(), toCurrency, markets, fiats, true); + return convert(from, toCurrency, conversionPath, fiats, marketOrderBookMap, priceOptions); } /// Attempts to convert amount into a target currency. /// Conversion is made according to given price options, which uses the 'Maker' prices by default. /// No external calls is made with this version, it has all what it needs - std::optional convert(MonetaryAmount a, CurrencyCode toCurrency, const MarketsPath &conversionPath, + std::optional convert(MonetaryAmount from, CurrencyCode toCurrency, const MarketsPath &conversionPath, const Fiats &fiats, MarketOrderBookMap &marketOrderBookMap, const PriceOptions &priceOptions = PriceOptions()); @@ -178,6 +179,8 @@ class ExchangePublic : public ExchangeBase { CommonAPI &_commonApi; const CoincenterInfo &_coincenterInfo; const ExchangeInfo &_exchangeInfo; + std::mutex _tradableMarketsMutex; + std::mutex _allOrderBooksMutex; }; } // namespace api } // namespace cct diff --git a/src/api/common/src/exchangepublicapi.cpp b/src/api/common/src/exchangepublicapi.cpp index 75418a47..acc875fd 100644 --- a/src/api/common/src/exchangepublicapi.cpp +++ b/src/api/common/src/exchangepublicapi.cpp @@ -33,12 +33,12 @@ ExchangePublic::ExchangePublic(std::string_view name, FiatConverter &fiatConvert _coincenterInfo(coincenterInfo), _exchangeInfo(coincenterInfo.exchangeInfo(name)) {} -std::optional ExchangePublic::convert(MonetaryAmount amount, CurrencyCode toCurrency, +std::optional ExchangePublic::convert(MonetaryAmount from, CurrencyCode toCurrency, const MarketsPath &conversionPath, const Fiats &fiats, MarketOrderBookMap &marketOrderBookMap, const PriceOptions &priceOptions) { - if (amount.currencyCode() == toCurrency) { - return amount; + if (from.currencyCode() == toCurrency) { + return from; } if (conversionPath.empty()) { return std::nullopt; @@ -46,9 +46,9 @@ std::optional ExchangePublic::convert(MonetaryAmount amount, Cur const ExchangeInfo::FeeType feeType = priceOptions.isTakerStrategy() ? ExchangeInfo::FeeType::kTaker : ExchangeInfo::FeeType::kMaker; for (Market mk : conversionPath) { - CurrencyCode mFromCurrencyCode = amount.currencyCode(); + CurrencyCode mFromCurrencyCode = from.currencyCode(); assert(mk.canTrade(mFromCurrencyCode)); - CurrencyCode mToCurrencyCode = mk.base() == amount.currencyCode() ? mk.quote() : mk.base(); + CurrencyCode mToCurrencyCode = mk.base() == from.currencyCode() ? mk.quote() : mk.base(); std::optional optFiatLikeFrom = _coincenterInfo.fiatCurrencyIfStableCoin(mFromCurrencyCode); CurrencyCode fiatFromLikeCurCode = (optFiatLikeFrom ? *optFiatLikeFrom : mFromCurrencyCode); std::optional optFiatLikeTo = _coincenterInfo.fiatCurrencyIfStableCoin(mToCurrencyCode); @@ -56,9 +56,10 @@ std::optional ExchangePublic::convert(MonetaryAmount amount, Cur bool isFromFiatLike = optFiatLikeFrom || fiats.contains(mFromCurrencyCode); bool isToFiatLike = optFiatLikeTo || fiats.contains(mToCurrencyCode); if (isFromFiatLike && isToFiatLike) { - amount = _fiatConverter.convert(MonetaryAmount(amount, fiatFromLikeCurCode), fiatToLikeCurCode); + from = _fiatConverter.convert(MonetaryAmount(from, fiatFromLikeCurCode), fiatToLikeCurCode); } else { if (marketOrderBookMap.empty()) { + std::lock_guard guard(_allOrderBooksMutex); marketOrderBookMap = queryAllApproximatedOrderBooks(1); } auto it = marketOrderBookMap.find(mk); @@ -66,14 +67,14 @@ std::optional ExchangePublic::convert(MonetaryAmount amount, Cur return std::nullopt; } const MarketOrderBook &marketOrderbook = it->second; - std::optional optA = marketOrderbook.convert(amount, priceOptions); + std::optional optA = marketOrderbook.convert(from, priceOptions); if (!optA) { return std::nullopt; } - amount = _exchangeInfo.applyFee(*optA, feeType); + from = _exchangeInfo.applyFee(*optA, feeType); } } - return amount; + return from; } namespace { @@ -145,6 +146,7 @@ MarketsPath ExchangePublic::findMarketsPath(CurrencyCode fromCurrency, CurrencyC return ret; } if (markets.empty()) { + std::lock_guard guard(_tradableMarketsMutex); markets = queryTradableMarkets(); if (markets.empty()) { log::error("No markets retrieved for {}", _name);