diff --git a/lib/coins.py b/lib/coins.py index 66aa621..6c5c7b6 100644 --- a/lib/coins.py +++ b/lib/coins.py @@ -2,6 +2,7 @@ # and warranty status of this software. +from decimal import Decimal import inspect import sys @@ -20,6 +21,7 @@ class Coin(object): # Not sure if these are coin-specific HEADER_LEN = 80 DEFAULT_RPC_PORT = 8332 + VALUE_PER_COIN = 100000000 @staticmethod def coins(): @@ -131,6 +133,10 @@ class Coin(object): d = Deserializer(block[cls.HEADER_LEN:]) return d.read_block() + @classmethod + def decimal_value(cls, value): + return Decimal(value) / cls.VALUE_PER_COIN + class Bitcoin(Coin): NAME = "Bitcoin" diff --git a/query.py b/query.py index 1cae0ae..63cdc72 100644 --- a/query.py +++ b/query.py @@ -37,6 +37,9 @@ def main(): utxo.tx_pos, utxo.height, utxo.value)) if n is None: print('No UTXOs') + balance = db.get_balance(hash160) + print('Balance: {} {}'.format(coin.decimal_value(balance), + coin.SHORTNAME)) if __name__ == '__main__': main() diff --git a/server/db.py b/server/db.py index 7f0efde..35c7e47 100644 --- a/server/db.py +++ b/server/db.py @@ -460,7 +460,7 @@ class DB(object): def get_balance(self, hash160): '''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): '''Generator that yields all UTXOs for an address sorted in no diff --git a/server_main.py b/server_main.py index 70d0884..0ebd347 100755 --- a/server_main.py +++ b/server_main.py @@ -22,10 +22,11 @@ def main_loop(): logging.info('switching current directory to {}'.format(env.db_dir)) os.chdir(env.db_dir) + server = Server(env) + tasks = server.async_tasks() + loop = asyncio.get_event_loop() try: - server = Server(env, loop) - tasks = server.async_tasks() loop.run_until_complete(asyncio.gather(*tasks)) finally: loop.close()