-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblockClockClient.cpp
99 lines (73 loc) · 2.74 KB
/
blockClockClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "blockClockClient.h"
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include "WiFiManager.h"
#include "blockClockTypes.h"
#include "blockClockUtils.h"
#include "timeManager.h"
const String MEMPOOL_BASEURL = "https://mempool.space/api";
const String COINLIB_BASEURL = "https://coinlib.io/api/v1";
ApiClient::ApiClient(const String& apiKey) : coinlibApiKey(apiKey) {}
String ApiClient::getBlockHeight() {
http.begin(MEMPOOL_BASEURL + "/blocks/tip/height");
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
return http.getString();
}
return "ERR " + httpCode;
}
RecommendedFees ApiClient::getRecommendedFees() {
RecommendedFees recommendedFees;
http.begin(MEMPOOL_BASEURL + "/v1/fees/recommended");
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
StaticJsonDocument<192> httpResponseJson;
String httpResponseBody = http.getString();
deserializeJson(httpResponseJson, httpResponseBody);
recommendedFees.high = httpResponseJson["fastestFee"];
recommendedFees.medium = httpResponseJson["halfHourFee"];
recommendedFees.low = httpResponseJson["hourFee"];
recommendedFees.noPriority = httpResponseJson["economyFee"];
recommendedFees.error = false;
return recommendedFees;
}
recommendedFees.high = 0;
recommendedFees.medium = 0;
recommendedFees.low = 0;
recommendedFees.noPriority = 0;
recommendedFees.error = true;
return recommendedFees;
}
PriceData ApiClient::getBitcoinPrice(CurrencyState currencyState) {
String currency = currencyStateToString(currencyState);
DynamicJsonDocument doc(4096);
const String url = COINLIB_BASEURL + "/coin?key=" + coinlibApiKey +
"&pref=" + currency + "&symbol=BTC";
http.begin(url);
int httpCode = http.GET();
PriceData priceData;
priceData.currency = currencyState;
if (httpCode == HTTP_CODE_OK) {
String httpResponseBody = http.getString();
deserializeJson(doc, httpResponseBody);
String delta1h = replaceCommaWithDot(doc["delta_1h"]);
String delta24h = replaceCommaWithDot(doc["delta_24h"]);
String delta7d = replaceCommaWithDot(doc["delta_7d"]);
String delta30d = replaceCommaWithDot(doc["delta_30d"]);
priceData.price = intWithThousandSeparator((int)doc["price"]);
priceData.change1h = delta1h.toFloat();
priceData.change24h = delta24h.toFloat();
priceData.change7d = delta7d.toFloat();
priceData.change30d = delta30d.toFloat();
priceData.timestamp = getTimestampFromRTC();
priceData.error = false;
return priceData;
}
priceData.price = "ERR " + String(httpCode);
priceData.change1h = (float)0;
priceData.change24h = (float)0;
priceData.change7d = (float)0;
priceData.change30d = (float)0;
priceData.error = true;
return priceData;
}