-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoracle-to-csv.js
92 lines (79 loc) · 2.85 KB
/
oracle-to-csv.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
/*
* Ryan Patridge
* 2021-07-05
*
* Exports Helium oracle USD prices at block heights to CSV.
*
*/
const httplib = require("@helium/http");
const fs = require('fs');
const SCRIPT_START_TIME = Date.now();
const OUTPUT_FILE_NAME = "oracle_prices_" + SCRIPT_START_TIME + ".csv";
const MAX_QUERIES = 8760; // Maximum number of queries to run against the Helium API. Assuming maximum 24 price changes per day * 365 days * 1 API query per price change.
const LOWEST_BLOCK_INDEX = 1; // Return block price data dating back to this block index. Lowest possible value is 1 (the genesis block).
(async function main() {
const client = new httplib.Client();
//console.log(client.network.endpoint); //= https://api.helium.io/v1
console.log("Output file: " + OUTPUT_FILE_NAME);
let fileStream;
try {
fileStream = fs.openSync(OUTPUT_FILE_NAME, 'a'); // Create a stream for appending, to avoid overwriting an existing file.
// Write column headers to the output file.
fs.appendFileSync(fileStream, "Block Height,Price\n");
// https://github.com/helium/helium-js/blob/master/packages/http/src/resources/Oracle.ts
const oOracleCurrentPrice = await client.oracle.getCurrentPrice();
//console.log(JSON.stringify(oOracleCurrentPrice));
/*
* Example oracle.getCurrentPrice() and oracle.oOraclePriceAtBlock() JSON.
*/
/*
{
"price": {
"type": {
"ticker": "USD",
"decimalPlaces": "8",
"coefficient": "0.00000001"
},
"integerBalance": 1292555385,
"bigInteger": "1292555385",
"bigBalance": "12.92555385",
"floatBalance": 12.92555385
},
"height": 907620
}
*/
let sPrice = oOracleCurrentPrice.price.bigBalance;
let iFromHeight = oOracleCurrentPrice.height;
let iQueryCount = 0;
do {
// Write the price and height to the output file.
fs.appendFileSync(fileStream, iFromHeight + "," + sPrice + "\n");
console.log(iFromHeight + ": " + sPrice);
if (iFromHeight > 1) {
if (iFromHeight > LOWEST_BLOCK_INDEX) {
if (iQueryCount < MAX_QUERIES) {
const oOraclePriceAtBlock = await client.oracle.getPriceAtBlock(iFromHeight - 1); // Get the previous price and price change height.
sPrice = oOraclePriceAtBlock.price.bigBalance;
iFromHeight = oOraclePriceAtBlock.height;
iQueryCount++;
} else {
console.log("WARNING: Reached configurable limit (MAX_QUERIES): " + MAX_QUERIES);
break;
}
} else {
console.log("WARNING: Reached configurable limit (LOWEST_BLOCK_INDEX): " + LOWEST_BLOCK_INDEX);
break;
}
} else {
break; // Reached genesis block.
}
} while (true)
} catch (ex) {
console.log("ERROR: Exporting transactions to file: " + OUTPUT_FILE_NAME);
console.log(ex);
} finally {
// Close the file stream.
if (fileStream) fs.closeSync(fileStream);
} // try file append
})().catch (ex => { console.error(ex) });