Skip to content

Commit 2ac2a62

Browse files
committed
feat: change 3rd party price source to kraken
1 parent 064fe8f commit 2ac2a62

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/lib/price_notify.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "price_notify.hpp"
22

3-
const char *wsServerPrice = "wss://ws.coincap.io/prices?assets=bitcoin";
3+
const char *wsServerPrice = "wss://ws.kraken.com/v2";
44

55
WebSocketsClient webSocket;
66
uint currentPrice = 90000;
@@ -14,7 +14,7 @@ void onWebsocketPriceEvent(WStype_t type, uint8_t * payload, size_t length);
1414

1515
void setupPriceNotify()
1616
{
17-
webSocket.beginSSL("ws.coincap.io", 443, "/prices?assets=bitcoin");
17+
webSocket.beginSSL("ws.kraken.com", 443, "/v2");
1818
webSocket.onEvent([](WStype_t type, uint8_t * payload, size_t length) {
1919
onWebsocketPriceEvent(type, payload, length);
2020
});
@@ -32,21 +32,30 @@ void onWebsocketPriceEvent(WStype_t type, uint8_t * payload, size_t length) {
3232
case WStype_CONNECTED:
3333
{
3434
Serial.println("Connected to " + String(wsServerPrice));
35-
priceNotifyInit = true;
35+
36+
JsonDocument doc;
37+
doc["method"] = "subscribe";
38+
JsonObject params = doc["params"].to<JsonObject>();
39+
params["channel"] = "ticker";
40+
params["symbol"][0] = "BTC/USD";
41+
42+
webSocket.sendTXT(doc.as<String>().c_str());
3643
break;
3744
}
3845
case WStype_TEXT:
3946
{
4047
JsonDocument doc;
4148
deserializeJson(doc, (char *)payload);
4249

43-
if (doc["bitcoin"].is<JsonObject>())
50+
if (doc["data"][0].is<JsonObject>())
4451
{
45-
if (currentPrice != doc["bitcoin"].as<long>())
52+
float price = doc["data"][0]["last"].as<float>();
53+
uint roundedPrice = round(price);
54+
if (currentPrice != roundedPrice)
4655
{
47-
processNewPrice(doc["bitcoin"].as<long>(), CURRENCY_USD);
56+
processNewPrice(roundedPrice, CURRENCY_USD);
4857
}
49-
}
58+
}
5059
break;
5160
}
5261
case WStype_BIN:

0 commit comments

Comments
 (0)