-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Cryptowatch API (it has been switch off). Use another data sou…
…rce for fiats.
- Loading branch information
Showing
41 changed files
with
360 additions
and
422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
#include <mutex> | ||
|
||
#include "cachedresult.hpp" | ||
#include "cct_flatset.hpp" | ||
#include "curlhandle.hpp" | ||
#include "currencycode.hpp" | ||
#include "exchangebase.hpp" | ||
#include "timedef.hpp" | ||
|
||
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 { | ||
public: | ||
using Fiats = FlatSet<CurrencyCode>; | ||
|
||
CommonAPI(const CoincenterInfo &config, Duration fiatsUpdateFrequency = std::chrono::hours(96), | ||
bool loadFromFileCacheAtInit = true); | ||
|
||
/// Returns a new set of fiat currencies. | ||
Fiats queryFiats() { | ||
std::lock_guard<std::mutex> guard(_fiatsMutex); | ||
return _fiatsCache.get(); | ||
} | ||
|
||
/// Tells whether given currency code is a fiat currency or not. | ||
/// Fiat currencies are traditional currencies, such as EUR, USD, GBP, KRW, etc. | ||
/// Information here: https://en.wikipedia.org/wiki/Fiat_money | ||
bool queryIsCurrencyCodeFiat(CurrencyCode currencyCode) { | ||
std::lock_guard<std::mutex> guard(_fiatsMutex); | ||
return _fiatsCache.get().contains(currencyCode); | ||
} | ||
|
||
void updateCacheFile() const override; | ||
|
||
private: | ||
struct FiatsFunc { | ||
FiatsFunc(); | ||
|
||
Fiats operator()(); | ||
|
||
CurlHandle _curlHandle; | ||
}; | ||
|
||
CachedResultVault _cachedResultVault; | ||
const CoincenterInfo &_coincenterInfo; | ||
std::mutex _fiatsMutex; | ||
CachedResult<FiatsFunc> _fiatsCache; | ||
}; | ||
} // namespace api | ||
} // namespace cct |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include "commonapi.hpp" | ||
|
||
#include <cctype> | ||
#include <string_view> | ||
|
||
#include "cct_exception.hpp" | ||
#include "cct_json.hpp" | ||
#include "cct_log.hpp" | ||
#include "coincenterinfo.hpp" | ||
#include "curloptions.hpp" | ||
#include "file.hpp" | ||
#include "permanentcurloptions.hpp" | ||
#include "timedef.hpp" | ||
|
||
namespace cct::api { | ||
namespace { | ||
|
||
File GetFiatCacheFile(std::string_view dataDir) { | ||
return {dataDir, File::Type::kCache, "fiatcache.json", File::IfError::kNoThrow}; | ||
} | ||
|
||
} // namespace | ||
|
||
CommonAPI::CommonAPI(const CoincenterInfo& config, Duration fiatsUpdateFrequency, bool loadFromFileCacheAtInit) | ||
: _coincenterInfo(config), _fiatsCache(CachedResultOptions(fiatsUpdateFrequency, _cachedResultVault)) { | ||
if (loadFromFileCacheAtInit) { | ||
json data = GetFiatCacheFile(_coincenterInfo.dataDir()).readAllJson(); | ||
if (!data.empty()) { | ||
int64_t timeEpoch = data["timeepoch"].get<int64_t>(); | ||
auto& fiatsFile = data["fiats"]; | ||
Fiats fiats; | ||
fiats.reserve(static_cast<Fiats::size_type>(fiatsFile.size())); | ||
for (json& 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&>())); | ||
} | ||
log::debug("Loaded {} fiats from cache file", fiats.size()); | ||
_fiatsCache.set(std::move(fiats), TimePoint(std::chrono::seconds(timeEpoch))); | ||
} | ||
} | ||
} | ||
|
||
namespace { | ||
constexpr std::string_view kFiatsUrl = "https://datahub.io/core/currency-codes/r/codes-all.json"; | ||
} | ||
|
||
CommonAPI::FiatsFunc::FiatsFunc() | ||
: _curlHandle(kFiatsUrl, nullptr, PermanentCurlOptions::Builder().setFollowLocation().build()) {} | ||
|
||
CommonAPI::Fiats CommonAPI::FiatsFunc::operator()() { | ||
json dataCSV = json::parse(_curlHandle.query("", CurlOptions(HttpRequestType::kGet))); | ||
Fiats fiats; | ||
for (const json& fiatData : dataCSV) { | ||
static constexpr std::string_view kCodeKey = "AlphabeticCode"; | ||
static constexpr std::string_view kWithdrawalDateKey = "WithdrawalDate"; | ||
auto codeIt = fiatData.find(kCodeKey); | ||
auto withdrawalDateIt = fiatData.find(kWithdrawalDateKey); | ||
if (codeIt != fiatData.end() && !codeIt->is_null() && withdrawalDateIt != fiatData.end() && | ||
withdrawalDateIt->is_null()) { | ||
fiats.insert(CurrencyCode(codeIt->get<std::string_view>())); | ||
log::debug("Stored {} fiat", codeIt->get<std::string_view>()); | ||
} | ||
} | ||
if (fiats.empty()) { | ||
throw exception("Error parsing currency codes, no fiats found in {}", dataCSV.dump()); | ||
} | ||
|
||
log::info("Stored {} fiats", fiats.size()); | ||
return fiats; | ||
} | ||
|
||
void CommonAPI::updateCacheFile() const { | ||
File fiatsCacheFile = GetFiatCacheFile(_coincenterInfo.dataDir()); | ||
json data = fiatsCacheFile.readAllJson(); | ||
auto fiatsPtrLastUpdatedTimePair = _fiatsCache.retrieve(); | ||
auto timeEpochIt = data.find("timeepoch"); | ||
if (timeEpochIt != data.end()) { | ||
int64_t lastTimeFileUpdated = timeEpochIt->get<int64_t>(); | ||
if (TimePoint(std::chrono::seconds(lastTimeFileUpdated)) >= fiatsPtrLastUpdatedTimePair.second) { | ||
return; // No update | ||
} | ||
} | ||
data.clear(); | ||
if (fiatsPtrLastUpdatedTimePair.first != nullptr) { | ||
for (CurrencyCode fiatCode : *fiatsPtrLastUpdatedTimePair.first) { | ||
data["fiats"].emplace_back(fiatCode.str()); | ||
} | ||
data["timeepoch"] = TimestampToS(fiatsPtrLastUpdatedTimePair.second); | ||
fiatsCacheFile.write(data); | ||
} | ||
} | ||
} // namespace cct::api |
Oops, something went wrong.