-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcatalog_renewables_purchases.js
75 lines (56 loc) · 2.85 KB
/
catalog_renewables_purchases.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
let axios = require("axios")
// let api_header = require('./zl_api_header.json') // API is now public
let getEnergy = require("./getEnergy")
const json2csvparse = require('json2csv')
const fs = require('fs')
let findLocation = require("../filecoin-sp-locations/findLocation").findLocation
// This uses the Zero Labs API to look at every Filecoin minerID associated with EACs
// in their system, and for each one:
// Finds the location
// Determines the (upper bound) amount of energy this node has used over the network lifetime
// Compares total renewables purchases to energy consumption
// Then saves the output as EAC_purchase_summary.csv
start = '2020-07-01'
end = '2022-03-08' // ie today's date
async function catalog_renewables_purchases() {
// First get the list of nodes that Zero Labs has records for
requestString = 'https://proofs-api.zerolabs.green/api/partners/filecoin/nodes'
var nodesResult = await axios.get(requestString) // API key should no longer be necessary
// var nodesResult = await axios.get(
// requestString, {
// headers:api_header
// })
// Will return dataset including minerIDs and country of each
outData = nodesResult.data.map(x => {
return {"minerID":x.id,
"country" : findLocation({"minerID": x.id, "loc_version": '1.2.0'}).country
}})
// console.log(outData)
// For each node in the ZL system, examine energy purchases
for (i=0; i<outData.length; i++){
console.log(outData[i])
// Get Transaction data
requestString = `https://proofs-api.zerolabs.green/api/partners/filecoin/nodes/${outData[i].minerID}/transactions`
var transactionsResult = await axios.get(requestString)
// Record transaction data
outData[i].pageUrl = transactionsResult.data.pageUrl
outData[i].deliveredEACs_MWh = transactionsResult.data.recsTotal/1e6
// Get Contracts data
requestString = `https://proofs-api.zerolabs.green/api/partners/filecoin/nodes/${outData[i].minerID}/contracts`
var contractsResult = await axios.get(requestString)
// Sum and record volume under contract
openVol = contractsResult.data.contracts.reduce((previousValue, elem) => {
newVol = Number.parseInt(elem.openVolume)
return previousValue + newVol
},0)
outData[i].openEACContracts_MWh = openVol/1e6
outData[i].AllEACs_MWh = outData[i].deliveredEACs_MWh + outData[i].openEACContracts_MWh
// Compare to the volume of energy consumed by this node over the history of the network
enResult = await getEnergy.get_total_energy_data(start, end, outData[i].minerID)
outData[i].EnergyConsumed_Upper_MWh = enResult.total_energy_upper_MWh
outData[i].Renewable_Consumed_Ratio = outData[i].AllEACs_MWh / outData[i].EnergyConsumed_Upper_MWh
}
// console.log(outData)
fs.writeFileSync('EAC_purchase_summary.csv', json2csvparse.parse(outData))
}
module.exports = {catalog_renewables_purchases}