Browse Source

Print balance in query tool

master
Neil Booth 8 years ago
parent
commit
ca11710fdd
  1. 6
      lib/coins.py
  2. 3
      query.py
  3. 2
      server/db.py
  4. 5
      server_main.py

6
lib/coins.py

@ -2,6 +2,7 @@
# and warranty status of this software. # and warranty status of this software.
from decimal import Decimal
import inspect import inspect
import sys import sys
@ -20,6 +21,7 @@ class Coin(object):
# Not sure if these are coin-specific # Not sure if these are coin-specific
HEADER_LEN = 80 HEADER_LEN = 80
DEFAULT_RPC_PORT = 8332 DEFAULT_RPC_PORT = 8332
VALUE_PER_COIN = 100000000
@staticmethod @staticmethod
def coins(): def coins():
@ -131,6 +133,10 @@ class Coin(object):
d = Deserializer(block[cls.HEADER_LEN:]) d = Deserializer(block[cls.HEADER_LEN:])
return d.read_block() return d.read_block()
@classmethod
def decimal_value(cls, value):
return Decimal(value) / cls.VALUE_PER_COIN
class Bitcoin(Coin): class Bitcoin(Coin):
NAME = "Bitcoin" NAME = "Bitcoin"

3
query.py

@ -37,6 +37,9 @@ def main():
utxo.tx_pos, utxo.height, utxo.value)) utxo.tx_pos, utxo.height, utxo.value))
if n is None: if n is None:
print('No UTXOs') print('No UTXOs')
balance = db.get_balance(hash160)
print('Balance: {} {}'.format(coin.decimal_value(balance),
coin.SHORTNAME))
if __name__ == '__main__': if __name__ == '__main__':
main() main()

2
server/db.py

@ -460,7 +460,7 @@ class DB(object):
def get_balance(self, hash160): def get_balance(self, hash160):
'''Returns the confirmed balance of an address.''' '''Returns the confirmed balance of an address.'''
return sum(utxo.value for utxo in self.get_utxos(hash_160, limit=None)) return sum(utxo.value for utxo in self.get_utxos(hash160, limit=None))
def get_utxos(self, hash160, limit=1000): def get_utxos(self, hash160, limit=1000):
'''Generator that yields all UTXOs for an address sorted in no '''Generator that yields all UTXOs for an address sorted in no

5
server_main.py

@ -22,10 +22,11 @@ def main_loop():
logging.info('switching current directory to {}'.format(env.db_dir)) logging.info('switching current directory to {}'.format(env.db_dir))
os.chdir(env.db_dir) os.chdir(env.db_dir)
server = Server(env)
tasks = server.async_tasks()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
try: try:
server = Server(env, loop)
tasks = server.async_tasks()
loop.run_until_complete(asyncio.gather(*tasks)) loop.run_until_complete(asyncio.gather(*tasks))
finally: finally:
loop.close() loop.close()

Loading…
Cancel
Save