Created
February 4, 2019 22:27
-
-
Save nsjames/2c709042f09d331abb35801adccd2491 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 getApiNodes = async () => { | |
const getBpData = async (bp, attempt = 0) => { | |
if(attempt >= 3) return {bp:bp.owner, error:'no bp.json'}; | |
const json = await fetch(`${bp.url}/bp.json`).then(x => x.json()).catch(() => null); | |
if(!json) return getBpData(bp, attempt+1); | |
let ssl_endpoint, api_endpoint; | |
json.nodes.map(x => { | |
if(!ssl_endpoint && x.ssl_endpoint && x.ssl_endpoint.length) ssl_endpoint = x.ssl_endpoint; | |
if(!api_endpoint && x.api_endpoint && x.api_endpoint.length) api_endpoint = x.api_endpoint; | |
}); | |
if(!ssl_endpoint || !ssl_endpoint.length) ssl_endpoint = null; | |
if(!api_endpoint || !api_endpoint.length) api_endpoint = null; | |
return {bp:bp.owner, ssl_endpoint, api_endpoint}; | |
} | |
const hasHistory = async data => { | |
const url = data.ssl_endpoint || data.api_endpoint; | |
if(!url) return 'No API Nodes'; | |
return fetch(`${url}/v1/history/get_key_accounts`, { | |
method:"POST", | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}, | |
body:JSON.stringify({ | |
"public_key":"EOS7w5aJCv5B7y3a6f4WCwPSvs6TpCAoRGnGpiLMsSWbmxaZdKigd" | |
}) | |
}).then(x => x.json()).then(res => { | |
return res.hasOwnProperty('account_names') && !!res.account_names.length | |
}).catch(() => false); | |
} | |
const corsEnabled = async data => { | |
const url = data.ssl_endpoint || data.api_endpoint; | |
if(!url) return 'No API Nodes'; | |
return fetch(`${url}/v1/chain/get_info`).then(res => { | |
const cors = res.headers._headers['access-control-allow-origin']; | |
return cors && cors[0] === '*'; | |
}).catch(err => { | |
console.log('err', err); | |
}); | |
} | |
fetch(`https://nodes.get-scatter.com/v1/chain/get_table_rows`, { | |
method:"POST", | |
body:JSON.stringify({ | |
"code": "eosio", | |
"json": true, | |
"limit": 500, | |
"scope": "eosio", | |
"table": "producers" | |
}) | |
}).then(x => x.json()).then(async res => { | |
const bps = res.rows.sort((a,b) => b.total_votes - a.total_votes); | |
const top50 = bps.slice(0, 21); | |
const bpData = await Promise.all(top50.map(async bp => { | |
const data = await getBpData(bp); | |
if(data.hasOwnProperty('error')) return data; | |
const result = Object.assign({hasHistory:await await hasHistory(data), hasCors:await await corsEnabled(data)}, data); | |
delete result.api_endpoint; | |
delete result.ssl_endpoint; | |
return result; | |
})); | |
console.log('bpData', bpData); | |
}).catch(err => { | |
console.error(err); | |
}) | |
}; | |
getApiNodes(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment