def transaction_spent(thash): data = Transaction().spent(thash) outputs = [] if data["error"] is None: result = data["result"] for output in result: item = {"spent": output["spent"]} if output["spent"]: block = Block().height(output["height"])["result"] item["txid"] = output["txid"] item["vin"] = output["vin"] item["status"] = { "confirmed": True, "block_height": block["height"], "block_hash": block["hash"], "block_time": block["time"] } outputs.append(item) return jsonify(outputs) else: return Response("Transaction not found", mimetype="text/plain", status=404)
def plain_block_hash(height): data = Block().height(height) if data["error"] is None: return Response(data["result"]["hash"], mimetype="text/plain") else: return Response("Block not found", mimetype="text/plain", status=404)
def block_by_hash(args, bhash): offset = args["offset"] data = Block().hash(bhash) if data["error"] is None: data["result"]["tx"] = data["result"]["tx"][offset:offset + 10] return jsonify(data)
def blocks_by_range(args, height): offset = args["offset"] if offset > 100: offset = 100 result = Block().range(height, offset) return jsonify(utils.response(result))
def block_by_height(args, height): offset = args["offset"] data = Block().height(height) if data["error"] is None: data["result"]["tx"] = data["result"]["tx"][offset:offset + 10] return jsonify(data)
def subscription_loop(): bestblockhash = None mempool = [] while True: data = General().info() if "result" in data: if "bestblockhash" in data["result"]: if data["result"]["bestblockhash"] != bestblockhash: bestblockhash = data["result"]["bestblockhash"] sio.emit("block.update", utils.response({ "height": data["result"]["blocks"], "hash": bestblockhash }), room="blocks") updates = Block().inputs(bestblockhash) for address in updates: mempool = list(set(mempool) - set(updates[address])) sio.emit("address.update", utils.response({ "address": address, "tx": updates[address], "height": data["result"]["blocks"], "hash": bestblockhash }), room=address) data = General().mempool() temp_mempool = [] if not data["error"]: updates = Transaction.addresses(data["result"]["tx"]) for address in updates: updates[address] = list( set(updates[address]) - set(mempool)) temp_mempool += updates[address] if len(updates[address]) > 0: sio.emit("address.update", utils.response({ "address": address, "tx": updates[address], "height": None, "hash": None }), room=address) mempool = list(set(mempool + temp_mempool)) sio.sleep(0)
def block_by_hash(bhash): offset = request.args.get("offset") offset = int(0 if offset is None else offset) data = Block().hash(bhash) if data["error"] is None: data["result"]["tx"] = data["result"]["tx"][offset:offset + 10] return jsonify(data)
def block_hash(bhash): data = Block().hash(bhash) if data["error"] is None: result = data["result"] return Esplora().block(result) else: return Response("Block not found", mimetype="text/plain", status=404)
def blocks_by_range(height): offset = request.args.get("offset") offset = int(0 if offset is None else offset) if offset > 100: offset = 100 result = Block().range(height, offset) return jsonify(utils.response(result))
def get(self, height): parser = reqparse.RequestParser() parser.add_argument('offset', type=int, default=30) args = parser.parse_args() if args['offset'] > 100: args['offset'] = 100 result = Block().range(height, args['offset']) return utils.response(result)
def get(self, bhash): parser = reqparse.RequestParser() parser.add_argument('offset', type=int, default=0) args = parser.parse_args() data = Block().hash(bhash) if data['error'] is None: data['result']['tx'] = data['result']['tx'][ args['offset']:args['offset'] + 10] return data
def get(self, bhash): parser = reqparse.RequestParser() parser.add_argument("offset", type=int, default=0) args = parser.parse_args() data = Block().hash(bhash) if data["error"] is None: data["result"]["tx"] = data["result"]["tx"][ args["offset"]:args["offset"] + 10] return data
def blocks_range(height): data = General().info() blocks = [] if not height: height = data["result"]["blocks"] data = Block().range(height, config.block_page) for block in data: blocks.append(Esplora().block(block)) return jsonify(blocks)
def block_status(bhash): data = Block().hash(bhash) next_best = None height = None best = False if data["error"] is None: result = data["result"] next_best = result[ "nextblockhash"] if "nextblockhash" in result else None height = result["height"] best = True return {"in_best_chain": best, "height": height, "next_best": next_best}
def init(): bestblockhash = None while True: data = General().info() if data['result']['bestblockhash'] != bestblockhash: bestblockhash = data['result']['bestblockhash'] sio.emit('block.update', utils.response({ 'height': data['result']['blocks'], 'hash': bestblockhash }), room='blocks') updates = Block().inputs(bestblockhash) for address in updates: mempool = list(set(state.mempool) - set(updates[address])) if address in state.rooms: sio.emit('address.update', utils.response({ 'address': address, 'tx': updates[address], 'height': data['result']['blocks'], 'hash': bestblockhash }), room=address) data = General().mempool() updates = Transaction().addresses(data['result']['tx']) temp_mempool = [] for address in updates: updates[address] = list(set(updates[address]) - set(mempool)) temp_mempool += updates[address] if address in state.rooms: if len(updates[address]) > 0: sio.emit('address.update', utils.response({ 'address': address, 'tx': updates[address], 'height': None, 'hash': None }), room=address) mempool = list(set(mempool + temp_mempool))
def block_transactions(bhash, start=0): data = Block().hash(bhash) transactions = [] if start % config.tx_page != 0: return Response( f"start index must be a multipication of {config.tx_page}", mimetype="text/plain", status=400) if data["error"] is None: result = data["result"] for thash in result["tx"][start:start + config.tx_page]: transaction = Transaction().info(thash)["result"] transactions.append(Esplora().transaction(transaction)) return jsonify(transactions) else: return Response("Block not found", mimetype="text/plain", status=404)
def get(self, height): return Block().get(height)
def hash_by_height(height): return jsonify(Block().get(height))