minigun-sv
Version:
Lightweight Bitcoin Stress Test Tool.
32 lines (28 loc) • 1.19 kB
JavaScript
const fetch = require('node-fetch')
const fs = require('fs')
function getUTXOs(addr){
return fetch(`https://api.bitindex.network/api/addr/${addr.toString()}/utxo`).then(res=>{
//global.log.log(res)
if(!global.fromAPI)throw new Error("Load from files.")
if (res.status==200) return res.json()
else {
global.log.log("[Minigun] API status: " + res.status)
throw new Error('Request not successful.')
}
}).then(utxos=>{
if(!utxos instanceof Array)throw new Error('Request not successful.')
return utxos.filter(utxo=>utxo.height!=undefined).filter(utxo=>utxo.confirmations>0)
}).catch(err=>{
global.log.log('[Minigun] Not loading ammo from API, return cached ammo: ' + addr)
//global.log.log(err)
var cached = []
try{
cached = JSON.parse(fs.readFileSync(`${addr}.utxos`).toString())
global.log.log('[Minigun] Cached ammo: ' + cached.length)
}catch(err2){
global.log.log('[Minigun] Failed loading cached ammo: ')
}
return cached
})
}
module.exports.getUTXOs = getUTXOs