forked from algoduino/algoduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgoduino.cpp
335 lines (258 loc) · 9.74 KB
/
Algoduino.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Algoduino.cpp - Algorand IoT library for Arduino
* Copyright (c) 2020 Algoduino. All right reserved.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#include "Algoduino.h"
#include <Arduino.h>
#define ARDUINOJSON_USE_LONG_LONG 1
#include <ArduinoJson.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#else
#include <WiFi.h>
#include <HTTPClient.h>
#endif
constexpr char *fingerprint = "B8 7C 7E 8F 45 E3 0C 87 72 1A 85 47 71 F8 80 4D E2 BC 8B FE";
Algoduino::Algoduino(String ssid, String password, String apiKey, Network network)
{
_ssid = ssid;
_password = password;
_apiKey = apiKey;
switch (network)
{
case BETANET:
_endpoint = "https://betanet-algorand.api.purestake.io/ps1";
break;
case TESTNET:
_endpoint = "https://testnet-algorand.api.purestake.io/ps1";
break;
case MAINNET:
_endpoint = "https://mainnet-algorand.api.purestake.io/ps1";
break;
default:
_endpoint = "https://testnet-algorand.api.purestake.io/ps1";
}
}
void Algoduino::begin(void)
{
WiFi.mode(WIFI_STA);
WiFi.begin(_ssid.c_str(), _password.c_str());
}
String Algoduino::getHealth(void)
{
return _fetch("/health");
}
AccountInformation Algoduino::getAccountInformation(String address)
{
AccountInformation accountInformation;
const size_t capacity = JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(8) + 330;
DynamicJsonDocument doc(capacity);
String json = _fetch("/v1/account/" + address);
deserializeJson(doc, json);
accountInformation.round = doc["round"];
accountInformation.address = doc["address"].as<String>();
accountInformation.amount = doc["amount"];
accountInformation.pendingrewards = doc["pendingrewards"];
accountInformation.amountwithoutpendingrewards = doc["amountwithoutpendingrewards"];
accountInformation.rewards = doc["rewards"];
accountInformation.status = doc["status"].as<String>();
JsonObject participation = doc["participation"];
accountInformation.participation.partpkb64 = participation["partpkb64"].as<String>();
accountInformation.participation.vrfpkb64 = participation["vrfpkb64"].as<String>();
accountInformation.participation.votefst = participation["votefst"];
accountInformation.participation.votelst = participation["votelst"];
accountInformation.participation.votekd = participation["votekd"];
return accountInformation;
}
String Algoduino::getConfirmedTransactionInformation(String address, String txid)
{
return _fetch("/v1/account/" + address + "/transaction/" + txid);
}
String Algoduino::getConfirmedTransactionsList(String address)
{
return _fetch("/v1/account/" + address + "/transactions");
}
String Algoduino::getPendingTransactionInformation(String txid)
{
return _fetch("/v1/transactions/pending/" + txid);
}
String Algoduino::getPendingTransactionsList(void)
{
return _fetch("/v1/transactions/pending");
}
String Algoduino::getPendingTransactionsList(String address)
{
return _fetch("/v1/account/" + address + "/transactions/pending");
}
AssetInformation Algoduino::getAssetInformation(String index)
{
AssetInformation assetInformation;
const size_t capacity = JSON_OBJECT_SIZE(11) + 480;
DynamicJsonDocument doc(capacity);
String json = _fetch("/v1/asset/" + index);
deserializeJson(doc, json);
assetInformation.creator = doc["creator"].as<String>();
assetInformation.total = doc["total"];
assetInformation.decimals = doc["decimals"];
assetInformation.defaultfrozen = doc["defaultfrozen"];
assetInformation.unitname = doc["unitname"].as<String>();
assetInformation.assetname = doc["assetname"].as<String>();
assetInformation.url = doc["url"].as<String>();
assetInformation.managerkey = doc["managerkey"].as<String>();
assetInformation.reserveaddr = doc["reserveaddr"].as<String>();
assetInformation.freezeaddr = doc["freezeaddr"].as<String>();
assetInformation.clawbackaddr = doc["clawbackaddr"].as<String>();
return assetInformation;
}
String Algoduino::getAssetsList(String max, String index)
{
return _fetch("/v1/assets?max=" + max + "&assetIdx=" + index);
}
String Algoduino::getBlock(String round)
{
return _fetch("/v1/block/" + round);
}
LedgerSupply Algoduino::getLedgerSupply(void)
{
LedgerSupply ledgerSupply;
const size_t capacity = JSON_OBJECT_SIZE(3) + 40;
DynamicJsonDocument doc(capacity);
String json = _fetch("/v1/ledger/supply");
deserializeJson(doc, json);
ledgerSupply.round = doc["round"];
ledgerSupply.totalMoney = doc["totalMoney"];
ledgerSupply.onlineMoney = doc["onlineMoney"];
return ledgerSupply;
}
Status Algoduino::getStatus(void)
{
Status status;
const size_t capacity = JSON_OBJECT_SIZE(9) + 410;
DynamicJsonDocument doc(capacity);
String json = _fetch("/v1/status");
deserializeJson(doc, json);
status.lastRound = doc["lastRound"];
status.lastConsensusVersion = doc["lastConsensusVersion"].as<String>();
status.nextConsensusVersion = doc["nextConsensusVersion"].as<String>();
status.nextConsensusVersionRound = doc["nextConsensusVersionRound"];
status.nextConsensusVersionSupported = doc["nextConsensusVersionSupported"];
status.timeSinceLastRound = doc["timeSinceLastRound"];
status.catchupTime = doc["catchupTime"];
status.hasSyncedSinceStartup = doc["hasSyncedSinceStartup"];
status.stoppedAtUnsupportedRound = doc["stoppedAtUnsupportedRound"];
return status;
}
Status Algoduino::getStatus(String round)
{
Status status;
const size_t capacity = JSON_OBJECT_SIZE(9) + 410;
DynamicJsonDocument doc(capacity);
String json = _fetch("/v1/status/wait-for-block-after/" + round);
deserializeJson(doc, json);
status.lastRound = doc["lastRound"];
status.lastConsensusVersion = doc["lastConsensusVersion"].as<String>();
status.nextConsensusVersion = doc["nextConsensusVersion"].as<String>();
status.nextConsensusVersionRound = doc["nextConsensusVersionRound"];
status.nextConsensusVersionSupported = doc["nextConsensusVersionSupported"];
status.timeSinceLastRound = doc["timeSinceLastRound"];
status.catchupTime = doc["catchupTime"];
status.hasSyncedSinceStartup = doc["hasSyncedSinceStartup"];
status.stoppedAtUnsupportedRound = doc["stoppedAtUnsupportedRound"];
return status;
}
TransactionInformation Algoduino::getTransactionInformation(String txid)
{
TransactionInformation transactionInformation;
const size_t capacity = JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(12) + 420;
DynamicJsonDocument doc(capacity);
String json = _fetch("/v1/transaction/" + txid);
deserializeJson(doc, json);
JsonObject obj = doc.as<JsonObject>();
transactionInformation.type = obj["type"].as<String>();
transactionInformation.tx = obj["tx"].as<String>();
transactionInformation.from = obj["from"].as<String>();
transactionInformation.fee = obj["fee"];
transactionInformation.first_round = obj["first-round"];
transactionInformation.last_round = obj["last-round"];
transactionInformation.noteb64 = obj["noteb64"].as<String>();
transactionInformation.round = obj["round"];
JsonObject curxfer = doc["curxfer"];
transactionInformation.curxfer.id = curxfer["id"];
transactionInformation.curxfer.amt = curxfer["amt"];
transactionInformation.curxfer.snd = curxfer["snd"].as<String>();
transactionInformation.curxfer.rcv = curxfer["rcv"].as<String>();
transactionInformation.curxfer.closeto = curxfer["closeto"].as<String>();
transactionInformation.fromrewards = doc["fromrewards"];
transactionInformation.genesisID = doc["genesisID"].as<String>();
transactionInformation.genesishashb64 = doc["genesishashb64"].as<String>();
return transactionInformation;
}
int Algoduino::getTransactionsFee(void)
{
const size_t capacity = JSON_OBJECT_SIZE(1) + 10;
DynamicJsonDocument doc(capacity);
String json = _fetch("/v1/transactions/fee");
deserializeJson(doc, json);
int fee = doc["fee"];
return fee;
}
TransactionParams Algoduino::getTransactionParams(void)
{
TransactionParams transactionParams;
const size_t capacity = JSON_OBJECT_SIZE(6) + 240;
DynamicJsonDocument doc(capacity);
String json = _fetch("/v1/transactions/params");
deserializeJson(doc, json);
transactionParams.fee = doc["fee"];
transactionParams.genesisID = doc["genesisID"].as<String>();
transactionParams.genesishashb64 = doc["genesishashb64"].as<String>();
transactionParams.lastRound = doc["lastRound"];
transactionParams.consensusVersion = doc["consensusVersion"].as<String>();
transactionParams.minFee = doc["minFee"];
return transactionParams;
}
Version Algoduino::getVersions(void)
{
Version version;
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(6) + 210;
DynamicJsonDocument doc(capacity);
String json = _fetch("/versions");
deserializeJson(doc, json);
version.versions = doc["versions"][0].as<String>();
version.genesis_id = doc["genesis_id"].as<String>();
version.genesis_hash_b64 = doc["genesis_hash_b64"].as<String>();
JsonObject build = doc["build"];
version.build.major = build["major"];
version.build.minor = build["minor"];
version.build.build_number = build["build_number"];
version.build.commit_hash = build["commit_hash"].as<String>();
version.build.branch = build["branch"].as<String>();
version.build.channel = build["channel"].as<String>();
return version;
}
String Algoduino::_fetch(String route)
{
String payload = "[void]";
if ((WiFi.status() == WL_CONNECTED))
{
HTTPClient https;
if (https.begin(_endpoint + route, fingerprint))
{
https.addHeader("accept", "application/json");
https.addHeader("x-api-key", _apiKey);
int httpCode = https.GET();
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
payload = https.getString();
}
}
https.end();
}
}
return payload;
}