return self.unanswered_requests == {}
- def synchronous_get(self, requests, timeout=100000000):
- # todo: use generators, unanswered_requests should be a list of arrays...
- queue = Queue.Queue()
- ids = self.send(requests, lambda i,r: queue.put(r))
- id2 = ids[:]
- res = {}
- while ids:
- r = queue.get(True, timeout)
- _id = r.get('id')
- if _id in ids:
- ids.remove(_id)
- res[_id] = r.get('result')
- out = []
- for _id in id2:
- out.append(res[_id])
- return out
-
def start(self, queue):
self.queue = queue
with self.lock: return self.running
+ def synchronous_get(self, requests, timeout=100000000):
+ queue = Queue.Queue()
+ ids = self.interface.send(requests, lambda i,r: queue.put(r))
+ id2 = ids[:]
+ res = {}
+ while ids:
+ r = queue.get(True, timeout)
+ _id = r.get('id')
+ if _id in ids:
+ ids.remove(_id)
+ res[_id] = r.get('result')
+ out = []
+ for _id in id2:
+ out.append(res[_id])
+ return out
+
+
def retrieve_transaction(self, tx_hash, tx_height=0):
import transaction
- r = self.interface.synchronous_get([ ('blockchain.transaction.get',[tx_hash, tx_height]) ])[0]
+ r = self.synchronous_get([ ('blockchain.transaction.get',[tx_hash, tx_height]) ])[0]
if r:
return transaction.Transaction(r)
import sys
from electrum import Interface
from electrum import bitcoin, Transaction
+from electrum import Network, SimpleConfig
-def get_transaction(interface, tx_hash, tx_height):
- raw_tx = interface.synchronous_get([(
+def get_transaction(network, tx_hash, tx_height):
+ raw_tx = network.synchronous_get([(
'blockchain.transaction.get', [tx_hash, tx_height])])[0]
tx = Transaction(raw_tx)
return tx
-def get_history(interface, addr):
- transactions = interface.synchronous_get([(
+def get_history(network, addr):
+ transactions = network.synchronous_get([(
'blockchain.address.get_history', [addr])])[0]
transactions.sort(key=lambda x: x["height"])
return [(i["tx_hash"], i["height"]) for i in transactions]
-def get_addr_balance(interface, address):
+def get_addr_balance(network, address):
prevout_values = {}
- h = get_history(interface, address)
+ h = get_history(network, address)
if h == ['*']:
return 0, 0
c = u = 0
# fetch transactions
for tx_hash, tx_height in h:
transactions[(tx_hash, tx_height)] = get_transaction(
- interface, tx_hash, tx_height)
+ network, tx_hash, tx_height)
for tx_hash, tx_height in h:
tx = transactions[(tx_hash, tx_height)]
def main(address):
- interface = Interface()
- interface.start()
- c, u = get_addr_balance(interface, address)
+ network = Network(SimpleConfig({'server':'btc.it-zone.org:110:s','verbose':False}))
+ network.start(wait=True)
+ c, u = get_addr_balance(network, address)
print("Balance - confirmed: %d (%.8f BTC), unconfirmed: %d (%.8f BTC)" %
(c, c / 100000000., u, u / 100000000.))