From 8cf48eb5f7960f44393a926f00266c25d99d0321 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Tue, 30 Jul 2019 20:18:00 -0300 Subject: [PATCH] move granular balances logic to account method --- lbry/lbry/extras/daemon/Daemon.py | 35 ++----------------------------- lbry/lbry/wallet/account.py | 33 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/lbry/lbry/extras/daemon/Daemon.py b/lbry/lbry/extras/daemon/Daemon.py index 0cd44cce6..1c84dd126 100644 --- a/lbry/lbry/extras/daemon/Daemon.py +++ b/lbry/lbry/extras/daemon/Daemon.py @@ -14,7 +14,7 @@ from typing import Callable, Optional, List from binascii import hexlify, unhexlify from traceback import format_exc from aiohttp import web -from functools import wraps, partial +from functools import wraps from google.protobuf.message import DecodeError from torba.client.wallet import Wallet from torba.client.baseaccount import SingleKey, HierarchicalDeterministic @@ -1047,38 +1047,7 @@ class Daemon(metaclass=JSONRPCServerType): (decimal) amount of lbry credits in wallet """ account = self.get_account_or_default(account_id) - get_total_balance = partial(account.get_balance, confirmations=confirmations, include_claims=True) - total = await get_total_balance() - if not reserved_subtotals: - reserved = await account.get_balance( - confirmations=confirmations, include_claims=True, - claim_type__or={'is_claim': True, 'is_support': True, 'is_update': True} - ) - return { - 'total': dewies_to_lbc(total), - 'reserved': dewies_to_lbc(reserved), - 'available': dewies_to_lbc(total - reserved), - 'reserved_subtotals': None - } - claims_balance = await get_total_balance(claim_type__or={'is_claim':True, 'is_update': True}) - tips_balance, supports_balance = 0, 0 - for amount, spent, from_me, to_me, height in await account.get_support_summary(): - if confirmations > 0 and not 0 < height <= self.ledger.headers.height - (confirmations - 1): - continue - if not spent and to_me: - tips_balance += amount if not from_me else 0 - supports_balance += amount if from_me else 0 - unavailable = claims_balance + supports_balance + tips_balance - return { - 'total': dewies_to_lbc(total), - 'available': dewies_to_lbc(total - unavailable), - 'reserved': dewies_to_lbc(unavailable), - 'reserved_subtotals': { - 'claims': dewies_to_lbc(claims_balance), - 'supports': dewies_to_lbc(supports_balance), - 'tips': dewies_to_lbc(tips_balance) - } - } + return await account.get_granular_balances(confirmations=confirmations, reserved_subtotals=reserved_subtotals) @requires("wallet") async def jsonrpc_account_add( diff --git a/lbry/lbry/wallet/account.py b/lbry/lbry/wallet/account.py index 899f19437..2e503859f 100644 --- a/lbry/lbry/wallet/account.py +++ b/lbry/lbry/wallet/account.py @@ -1,9 +1,11 @@ import json import logging +from functools import partial from hashlib import sha256 from string import hexdigits import ecdsa +from lbry.wallet.dewies import dewies_to_lbc from torba.client.baseaccount import BaseAccount, HierarchicalDeterministic @@ -75,6 +77,37 @@ class Account(BaseAccount): constraints.update({'is_claim': 0, 'is_update': 0, 'is_support': 0}) return super().get_balance(confirmations, **constraints) + async def get_granular_balances(self, confirmations=0, reserved_subtotals=False): + tips_balance, supports_balance, claims_balance = 0, 0, 0 + get_total_balance = partial(self.get_balance, confirmations=confirmations, include_claims=True) + total = await get_total_balance() + if reserved_subtotals: + claims_balance = await get_total_balance(claim_type__or={'is_claim':True, 'is_update': True}) + for amount, spent, from_me, to_me, height in await self.get_support_summary(): + if confirmations > 0 and not 0 < height <= self.ledger.headers.height - (confirmations - 1): + continue + if not spent and to_me: + if from_me: + supports_balance += amount + else: + tips_balance += amount + reserved = claims_balance + supports_balance + tips_balance + else: + reserved = await self.get_balance( + confirmations=confirmations, include_claims=True, + claim_type__or={'is_claim': True, 'is_support': True, 'is_update': True} + ) + return { + 'total': dewies_to_lbc(total), + 'available': dewies_to_lbc(total - reserved), + 'reserved': dewies_to_lbc(reserved), + 'reserved_subtotals': { + 'claims': dewies_to_lbc(claims_balance), + 'supports': dewies_to_lbc(supports_balance), + 'tips': dewies_to_lbc(tips_balance) + } if reserved_subtotals else None + } + @classmethod def get_private_key_from_seed(cls, ledger, seed: str, password: str): return super().get_private_key_from_seed(