This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelium-api.js
212 lines (181 loc) · 6.7 KB
/
helium-api.js
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
const request = require("request-promise-native");
const DB = require('./db');
require('dotenv').config();
const log = function (message) {
console.log("helium-hotspots: " + message);
};
const httpGet = async function (url) {
log(`GET ${url}`);
try {
return await request({
url: url,
method: "GET",
json: true,
headers: {
'User-Agent': 'helium-discord-bot'
}
})
} catch(error) {
console.log("Error in httpGet =>", error);
// TODO sleep 1s and retry if it's a 503
// await new Promise(r => setTimeout(r, 2000));
}
};
const fetchHotspotsForOwner = async function (owner) {
const rsp = await httpGet(`https://api.helium.io/v1/accounts/${owner}/hotspots`);
return rsp.data;
};
const fetchHotspotByName = async function (name) {
const rsp = await httpGet(`https://api.helium.io/v1/hotspots/name/${name}`);
return rsp.data;
};
const fetchRewardSumForHotspot = async function (address) {
const rsp = await httpGet(`https://api.helium.io/v1/hotspots/${address}/rewards/sum${queryParams()}`);
return rsp.data;
};
// fetches first 2 pages of activity data; 1st page is usually empty
const fetchActivityForHotspot = async function (address, cursor=null) {
const cursorParam = !!cursor ? `?cursor=${cursor}` : '';
const rsp = await httpGet(`https://api.helium.io/v1/hotspots/${address}/activity${cursorParam}`);
if(rsp.data && rsp.data.length > 0){
console.log("fetchActivityForHotspot: found some results, length =>", rsp.data.length);
return rsp.data;
}
else if(!!rsp.cursor) {
console.log("fetchActivityForHotspot: fetching another page, no results in previous page, cursor =>", rsp.cursor);
return (await fetchActivityForHotspot(address, rsp['cursor']));
}
else {
console.error("fetchActivityForHotspot: no results and no cursor, giving up");
return [];
}
};
const fetchHotspotDetails = async function (address) {
const rsp = await httpGet(`https://api.helium.io/v1/hotspots/${address}`);
return rsp.data;
};
const fetchValidatorDetails = async function (address) {
const rsp = await httpGet(`https://api.helium.io/v1/validators/${address}`);
return rsp.data;
}
const fetchTotalRewardsForValidator = async function (address) {
const url = `https://api.helium.io/v1/validators/${address}/rewards/sum?min_time=2020-01-01&max_time=2050-01-01`;
const rsp = await httpGet(url);
// rsp = await httpGet(`${url}&cursor=${rsp['cursor']}`);
return rsp.data;
};
const formatDate = function (date) {
// hard-set to midnight UTC on the date specified
// return date.toISOString().substr(0, 10) + "T00:00:00Z";
return date.toISOString();
};
const queryParams = function() {
return `?${dateTimeParams()}`
}
const dateTimeParams = function () {
const maxTime = new Date(Date.now()); // right now
const minTime = new Date(Date.now() - 864e5); // 24 hours ago
return "max_time=" + formatDate(maxTime) + "&min_time=" + formatDate(minTime);
};
const listValidators = function (guildID) {
const validators = DB.getValidators(guildID);
if (validators === undefined) { return undefined; }
return new Map(validators.map(x => [x['address'], x['name']] ));
};
const listOwners = function (guildID) {
const owners = DB.getOwners(guildID);
if (owners === undefined) { return undefined; }
return new Map(owners.map(x => [x['address'], x['name']] ));
};
const listHotspots = function (guildID) {
const hotspots = DB.getHotspots(guildID);
if (hotspots === undefined) { return undefined; }
return new Map(hotspots.map(x => [x['address'], x['name']] ));
};
const getValidatorStats = async function (guildID) {
if (listValidators(guildID) === undefined || listValidators(guildID).size == 0) {
log("getValidatorStats: VALIDATORS are not set, please add some using the bot commands (see `helium help`)");
return undefined;
}
const validators = [];
for (let validator of listValidators(guildID)) {
let _validator = await fetchTotalRewardsForValidator(validator[0]);
const details = await fetchValidatorDetails(validator[0]);
// console.debug("Loaded validator: ", _validator, details);
_validator['address'] = validator[0];
_validator['displayName'] = validator[1] || details['name'];
_validator['penalty'] = details['penalty'];
validators.push(_validator);
}
return validators;
}
const getHotspotStats = async function (guildID) {
// abort if HOTSPOT_OWNERS is not set (see .env)
if (listOwners(guildID) === undefined && listHotspots(guildID) === undefined) {
log("helium-hotspots: missing both owners and hotspots, please add some using the bot commands");
return;
}
// fetch hotspots for our owners
// TODO make this execute in parallel
let hotspots = [];
if (listOwners(guildID) !== undefined) {
for (let owner of listOwners(guildID)) {
let _hotspots = await fetchHotspotsForOwner(owner[0]);
log(`Found ${_hotspots.length} hotspots for ${owner[0]} owned by ${owner[1]}`);
for (let hotspot of _hotspots) {
hotspot['displayName'] = owner[1];
hotspots.push(hotspot);
}
}
}
if (listHotspots(guildID) !== undefined) {
for (let hotspot of listHotspots(guildID)) {
let _hotspot = await fetchHotspotDetails(hotspot[0]);
_hotspot['displayName'] = hotspot[1];
hotspots.push(_hotspot);
}
}
// hydrate with reward and activity data
// TODO make this execute in parallel
for (let hotspot of hotspots) {
let rewards = await fetchRewardSumForHotspot(hotspot["address"], dateTimeParams());
if (rewards["sum"] == null) {
rewards["sum"] = 0;
}
hotspot["rewards_24h"] = (parseInt(rewards["sum"]) / 100000000);
}
// sort by reward amount
hotspots = hotspots.sort(function (a, b) {
if (a["rewards_24h"] > b["rewards_24h"]) return -1;
if (a["rewards_24h"] < b["rewards_24h"]) return 1;
return 0;
});
return hotspots;
};
const getHotspotActivity = async function (name_or_address) {
const hotspot = await getAddressForHotspot(name_or_address);
const activity = await fetchActivityForHotspot(hotspot);
activity.hotspot = hotspot;
return activity;
};
// "hotspot" argument can be address, or name with or without hyphens
// e.g. both "slow-burgundy-mandrill" and "slow burgundy mandrill" are valid
const getAddressForHotspot = async function(hotspot) {
hotspot = hotspot.trim();
if(hotspot.includes('-') || hotspot.includes(' ')){
hotspot = hotspot.replace(/ /g, '-');
const details = await fetchHotspotByName(hotspot.toLowerCase());
if(!details){
throw(`Failed to fetch address for hotspot name ${hotspot}`);
}
return details[0]['address'];
} else {
return hotspot;
}
};
module.exports = {
getValidatorStats,
getHotspotStats,
getHotspotActivity,
getAddressForHotspot
};