-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminterapi.cpp
150 lines (139 loc) · 4.23 KB
/
minterapi.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "minterapi.h"
MinterApi::MinterApi()
{
http.setTimeout(HTTP_TIMEOUT_MS);
}
/*
MinterApi::MinterApi(String nodeUrl)
{
this->nodeUrl = nodeUrl;
}
*/
MinterApi::MinterApi(const String host, const uint16_t port)
{
// this->schema = schema;
this->host = host;
this->port = port;
http.setTimeout(HTTP_TIMEOUT_MS);
}
Status MinterApi::getStatus()
{
String json;
const uint8_t statusHttp = get(MAPI_STATUS, "", json);
Status status;
if (statusHttp == HTTP_RESULT_STATUS)
{
#ifdef MINTERAPI_DEBUG
Serial << "json:" << endl
<< json << endl;
#endif
// Status status;
const Status status = parseStatus(json);
}
return status;
}
uint8_t MinterApi::getAddress(const String address, const uint64_t height, Wallet &ResWallet)
{
uint8_t status=MINTERAPI_ERROR;
#if defined(ARDUINO)
const String _height = String((uint32_t)height);
#else
char _height[16] = {0};
// ultoa(height, _height, 10);
snprintf(_height, 16, "%llu", height);
#endif
// String params; //?address=Mx&height=0
// params.operator+=("address=").operator+=(address).operator+=("&height=").operator+=(_height);
const String params = "address=" + address + "&height=" + _height;
String json;
if (get(MAPI_ADDRESS, params, json) == HTTP_RESULT_STATUS)
{
#ifdef MINTERAPI_DEBUG
Serial << "json:" << endl << json << endl;
#endif
if ( parseWallet(json, ResWallet)==MINTERAPI_JSON_STATUS_OK ){
status=MINTERAPI_OK;
ResWallet.address = address;
}
}
return status;
}
Status MinterApi::parseStatus(const String json)
{
Status status;
#if ArduinoJson == 5
JsonObject &root = jsonBuffer.parseObject(json);
if (root.success())
{
#elif ArduinoJson == 6
DeserializationError error = deserializeJson(jsonBuffer, json, DeserializationOption::NestingLimit(100));
if (!error)
{
#endif
JsonObject &result = root["result"];
#if defined(ARDUINO)
String height = result["latest_block_height"].as<String>();
// status.height = atoll(height);
const char *strheight = height.c_str();
status.height = strtoull(strheight, NULL, 10);
status.datetime = result["latest_block_time"].as<String>();
status.network = result["tm_status"]["node_info"]["network"].as<String>();
#else
status.height = result["latest_block_height"].as<uint64_t>();
status.datetime.append(result["latest_block_time"].as<String>());
status.network.append(result["tm_status"]["node_info"]["network"].as<String>());
#endif
return status;
}
else
{
#ifdef MINTERAPI_DEBUG
Serial << "parseObject() failed" << endl;
#endif
return status;
}
}
uint8_t MinterApi::parseWallet(const String json, Wallet &wallet)
{
uint8_t status = MINTERAPI_JSON_STATUS_ERROR;
jsonBuffer.clear();
JsonObject &root = jsonBuffer.parseObject(json);
if (root.success())
{
JsonObject &result = root["result"];
wallet.count_txs = result["transaction_count"].as<uint32_t>();
JsonObject &balance = result["balance"];
for (JsonPair p : balance)
{
const char *key = p.key;
JsonVariant value = p.value;
String amount = value.as<String>();
#ifdef MINTERAPI_DEBUG
// Serial << key << "\t" << amount << "\t" << minterMatch.getAmount(amount) << endl;
#endif
BalancePair balancePair = {key, (float)minterMatch.getAmount(amount), amount};
wallet.balance.push_front(balancePair);
}
#ifdef MINTERAPI_DEBUG // #endif
// printWallet(wallet);
#endif
status = MINTERAPI_JSON_STATUS_OK;
}
else
{
#ifdef MINTERAPI_DEBUG
Serial << "parseObject() failed" << endl;
#endif
}
return status;
}
uint8_t MinterApi::get(const String method, const String params, String &content)
{
const String getUrl = String("/") + method + String("?") + params;
#ifdef _MINTERAPI_DEBUG
// url = url.operator+=("/").operator+=(method).operator+=("?").operator+=(params);
Serial << "MinterApi::get(" << method << "," << params << ") " << getUrl << endl;
#endif
uint8_t status = http.get(this->host, this->port, getUrl, content);
return status;
}