def estimate_fee(nblocks=6, network="btc"): url = "%s/utils/estimatefee?nbBlocks=%d" % \ (BET_URL if network == "testnet" else BE_URL, int(nblocks)) data = json.loads(make_request(url)).decode('utf-8') btc_to_satoshi = lambda b: int(b*1e8 + 0.5) btcfee = data.get(str(nblocks), None) return btc_to_satoshi(btcfee)
def history(*args): # Valid input formats: history([addr1, addr2,addr3], "btc") # history(addr1, addr2, addr3, "testnet") if len(args) == 0 or (len(args)==1 and args[0] in ('testnet','btc')): return [] addrs, network = parse_addr_args(*args) if network == "btc": txs = [] for addr in addrs: offset = 0 while 1: gathered = False while not gathered: try: data = make_request('https://blockchain.info/address/%s?format=json&offset=%s' % (addr, offset)) gathered = True except Exception as e: try: sys.stderr.write(e.read().strip()) except: sys.stderr.write(str(e)) gathered = False try: jsonobj = json.loads(data) except: raise Exception("Failed to decode data: "+data) txs.extend(jsonobj["txs"]) if len(jsonobj["txs"]) < 50: break offset += 50 sys.stderr.write("Fetching more transactions... "+str(offset)+'\n') outs = {} for tx in txs: for o in tx["out"]: if o.get('addr', None) in addrs: key = str(tx["tx_index"])+':'+str(o["n"]) outs[key] = { "address": o["addr"], "value": o["value"], "output": tx["hash"]+':'+str(o["n"]), "block_height": tx.get("block_height", None) } for tx in txs: # if output is spent adds "spend": "spending_TxID:i" for i, inp in enumerate(tx["inputs"]): if "prev_out" in inp: if inp["prev_out"].get("addr", None) in addrs: key = str(inp["prev_out"]["tx_index"]) + \ ':'+str(inp["prev_out"]["n"]) if outs.get(key): outs[key]["spend"] = tx["hash"] + ':' + str(i) return [outs[k] for k in outs] elif network == "testnet": txs = [] # using https://api.biteasy.com/blockchain/v1/transactions?address=_ADDR_ for addr in addrs: # or offset = 0 while 1: gathered = False while not gathered: try: data = make_request("%s/txs/?address=%s" % (BET_URL, addr)) gathered = True except Exception as e: try: sys.stderr.write(e.read().strip()) except: sys.stderr.write(str(e)) gathered = False try: jsonobj = json.loads(data) except: raise Exception("Failed to decode data: " + data) txs.extend(jsonobj['txs']) #assert addr == jsonobj.get("addrStr"), "Tx data doesn't match address %s" % addr #if len(jsonobj['data']['address']['inout_count_total']) >= 300: # because records=300 # break #offset += 100 #sys.stderr.write("Fetching more transactions... " + str(offset) + '\n') outs = {} from bitcoin.main import hex_to_b58check, btc_to_satoshi for tx in txs: for o in tx.get("vout"): if o.get('scriptPubKey', None) and hex_to_b58check(o.get("scriptPubKey"])["hex"], 111) == addr: key = str(tx["time"]) + ':' + str(o["n"]) outs[key] = { "address": addr, "value": btc_to_satoshi(o["value"]), "output": "{0}:{1}".format(tx["txid"], str(o["n"])), } blkhash = tx.get("blockhash") try: bheight = get_block_height(blkhash, 'testnet') outs[key].update({"block_height": int(bheight)}) except: sys.stderr.write("Couldn't get blockheight for %s.\nUsing blocktime instead" % blkhash) outs[key].update({"block_time": tx.get("time")})