Last active
May 23, 2019 14:46
-
-
Save gaboesquivel/17eb831381e2003ca4a0aa5cf558ad08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EosApi = require('eosjs-api') | |
const get = require('lodash.get') | |
const fetch = require('node-fetch') | |
// gets data from mainnet | |
const getBlockProducersData = async () => { | |
const eos = EosApi({ | |
httpEndpoint: process.env.EOS_API_ENDPOINT, | |
verbose: false | |
}) | |
const { rows: producers } = await eos.getProducers({ json: true, limit: 1000 }) | |
const { rows: bpJsons } = await eos.getTableRows({ | |
json: true, | |
code: 'producerjson', | |
scope: 'producerjson', | |
table: 'producerjson', | |
limit: 1000 | |
}) | |
const allProducers = producers.reduce( | |
(result, producer) => [ | |
...result, | |
{ | |
owner: producer.owner, | |
system: { ...producer }, | |
bpJson: get(bpJsons.find(bpJson => bpJson.owner === producer.owner), 'json', {}) | |
} | |
], | |
[] | |
) | |
const requests = allProducers | |
.filter(({ bpJson, system }) => !Object.keys(bpJson) && system.url) | |
.map(({ system: { url } }) => { | |
let result = url | |
if (!url.startsWith('http')) { | |
result = `http://${url}` | |
} | |
if (!url.endsWith('.json')) { | |
result = `${result}/bp.json` | |
} | |
return result | |
}) | |
.map(url => | |
fetch(url) | |
.then(res => res.json()) | |
.catch(e => { | |
console.error(e) | |
}) | |
) | |
const allJsons = await Promise.all(requests) | |
const result = allProducers.map(producer => ({ | |
...producer, | |
bpJson: Object.keys(producer.bpJson).length | |
? producer.bpJson | |
: allJsons.find(bpJson => bpJson && bpJson.producer_account_name === producer.owner) || {} | |
})) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment