added txo_sum command

This commit is contained in:
Lex Berezhny 2020-03-20 19:07:16 -04:00
parent d6d83a5c76
commit 6293e227ea
4 changed files with 90 additions and 25 deletions

View file

@ -4106,17 +4106,37 @@ class Daemon(metaclass=JSONRPCServerType):
return self.wallet_manager.get_transaction(txid) return self.wallet_manager.get_transaction(txid)
TXO_DOC = """ TXO_DOC = """
List transaction outputs. List and sum transaction outputs.
""" """
@requires(WALLET_COMPONENT) @staticmethod
def jsonrpc_txo_list( def _constrain_txo_from_kwargs(
self, account_id=None, type=None, txid=None, # pylint: disable=redefined-builtin constraints, type=None, txid=None, # pylint: disable=redefined-builtin
claim_id=None, name=None, unspent=False, claim_id=None, name=None, unspent=False,
is_my_input_or_output=None, exclude_internal_transfers=False, is_my_input_or_output=None, exclude_internal_transfers=False,
is_my_output=None, is_not_my_output=None, is_my_output=None, is_not_my_output=None,
is_my_input=None, is_not_my_input=None, is_my_input=None, is_not_my_input=None):
wallet_id=None, page=None, page_size=None, resolve=False): constraints['unspent'] = unspent
constraints['exclude_internal_transfers'] = exclude_internal_transfers
if is_my_input_or_output is True:
constraints['is_my_input_or_output'] = True
else:
if is_my_input is True:
constraints['is_my_input'] = True
elif is_not_my_input is True:
constraints['is_my_input'] = False
if is_my_output is True:
constraints['is_my_output'] = True
elif is_not_my_output is True:
constraints['is_my_output'] = False
database.constrain_single_or_list(constraints, 'txo_type', type, lambda x: TXO_TYPES[x])
database.constrain_single_or_list(constraints, 'claim_id', claim_id)
database.constrain_single_or_list(constraints, 'claim_name', name)
database.constrain_single_or_list(constraints, 'txid', txid)
return constraints
@requires(WALLET_COMPONENT)
def jsonrpc_txo_list(self, account_id=None, wallet_id=None, page=None, page_size=None, resolve=False, **kwargs):
""" """
List my transaction outputs. List my transaction outputs.
@ -4166,28 +4186,56 @@ class Daemon(metaclass=JSONRPCServerType):
claim_count = partial(self.ledger.get_txo_count, wallet=wallet, accounts=wallet.accounts, read_only=True) claim_count = partial(self.ledger.get_txo_count, wallet=wallet, accounts=wallet.accounts, read_only=True)
constraints = { constraints = {
'resolve': resolve, 'resolve': resolve,
'unspent': unspent,
'exclude_internal_transfers': exclude_internal_transfers,
'include_is_spent': True, 'include_is_spent': True,
'include_is_my_input': True, 'include_is_my_input': True,
'include_is_my_output': True, 'include_is_my_output': True,
} }
if is_my_input_or_output is True: self._constrain_txo_from_kwargs(constraints, **kwargs)
constraints['is_my_input_or_output'] = True
else:
if is_my_input is True:
constraints['is_my_input'] = True
elif is_not_my_input is True:
constraints['is_my_input'] = False
if is_my_output is True:
constraints['is_my_output'] = True
elif is_not_my_output is True:
constraints['is_my_output'] = False
database.constrain_single_or_list(constraints, 'txo_type', type, lambda x: TXO_TYPES[x])
database.constrain_single_or_list(constraints, 'claim_id', claim_id)
database.constrain_single_or_list(constraints, 'claim_name', name)
return paginate_rows(claims, claim_count, page, page_size, **constraints) return paginate_rows(claims, claim_count, page, page_size, **constraints)
@requires(WALLET_COMPONENT)
def jsonrpc_txo_sum(self, account_id=None, wallet_id=None, **kwargs):
"""
Sum transaction outputs.
Usage:
txo_list [--account_id=<account_id>] [--type=<type>...] [--txid=<txid>...]
[--claim_id=<claim_id>...] [--name=<name>...] [--unspent]
[--is_my_input_or_output |
[[--is_my_output | --is_not_my_output] [--is_my_input | --is_not_my_input]]
]
[--exclude_internal_transfers]
[--wallet_id=<wallet_id>]
Options:
--type=<type> : (str or list) claim type: stream, channel, support,
purchase, collection, repost, other
--txid=<txid> : (str or list) transaction id of outputs
--claim_id=<claim_id> : (str or list) claim id
--name=<name> : (str or list) claim name
--unspent : (bool) hide spent outputs, show only unspent ones
--is_my_input_or_output : (bool) txos which have your inputs or your outputs,
if using this flag the other related flags
are ignored (--is_my_output, --is_my_input, etc)
--is_my_output : (bool) show outputs controlled by you
--is_not_my_output : (bool) show outputs not controlled by you
--is_my_input : (bool) show outputs created by you
--is_not_my_input : (bool) show outputs not created by you
--exclude_internal_transfers: (bool) excludes any outputs that are exactly this combination:
"--is_my_input --is_my_output --type=other"
this allows to exclude "change" payments, this
flag can be used in combination with any of the other flags
--account_id=<account_id> : (str) id of the account to query
--wallet_id=<wallet_id> : (str) restrict results to specific wallet
Returns: {Paginated[Output]}
"""
wallet = self.wallet_manager.get_wallet_or_default(wallet_id)
return self.ledger.get_txo_sum(
wallet=wallet, accounts=[wallet.get_account_or_error(account_id)] if account_id else wallet.accounts,
read_only=True, **self._constrain_txo_from_kwargs({}, **kwargs)
)
UTXO_DOC = """ UTXO_DOC = """
Unspent transaction management. Unspent transaction management.
""" """

View file

@ -572,6 +572,9 @@ class CommandTestCase(IntegrationTestCase):
async def txo_list(self, *args, **kwargs): async def txo_list(self, *args, **kwargs):
return (await self.out(self.daemon.jsonrpc_txo_list(*args, **kwargs)))['items'] return (await self.out(self.daemon.jsonrpc_txo_list(*args, **kwargs)))['items']
async def txo_sum(self, *args, **kwargs):
return await self.out(self.daemon.jsonrpc_txo_sum(*args, **kwargs))
async def claim_list(self, *args, **kwargs): async def claim_list(self, *args, **kwargs):
return (await self.out(self.daemon.jsonrpc_claim_list(*args, **kwargs)))['items'] return (await self.out(self.daemon.jsonrpc_claim_list(*args, **kwargs)))['items']

View file

@ -274,6 +274,9 @@ class Ledger(metaclass=LedgerRegistry):
def get_txo_count(self, **constraints): def get_txo_count(self, **constraints):
return self.db.get_txo_count(**constraints) return self.db.get_txo_count(**constraints)
def get_txo_sum(self, **constraints):
return self.db.get_txo_sum(**constraints)
def get_transactions(self, **constraints): def get_transactions(self, **constraints):
return self.db.get_transactions(**constraints) return self.db.get_transactions(**constraints)

View file

@ -9,6 +9,7 @@ from lbry.error import InsufficientFundsError
from lbry.extras.daemon.daemon import DEFAULT_PAGE_SIZE from lbry.extras.daemon.daemon import DEFAULT_PAGE_SIZE
from lbry.testcase import CommandTestCase from lbry.testcase import CommandTestCase
from lbry.wallet.transaction import Transaction from lbry.wallet.transaction import Transaction
from lbry.wallet.util import satoshis_to_coins as lbc
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -420,11 +421,21 @@ class TransactionCommands(ClaimTestCase):
class TransactionOutputCommands(ClaimTestCase): class TransactionOutputCommands(ClaimTestCase):
async def test_txo_list_filtering(self): async def test_txo_list_and_sum_filtering(self):
channel_id = self.get_claim_id(await self.channel_create()) channel_id = self.get_claim_id(await self.channel_create())
self.assertEqual('1.0', lbc(await self.txo_sum(type='channel', unspent=True)))
await self.channel_update(channel_id, bid='0.5') await self.channel_update(channel_id, bid='0.5')
stream_id = self.get_claim_id(await self.stream_create()) self.assertEqual('0.5', lbc(await self.txo_sum(type='channel', unspent=True)))
await self.stream_update(stream_id, bid='0.5') self.assertEqual('1.5', lbc(await self.txo_sum(type='channel')))
stream_id = self.get_claim_id(await self.stream_create(bid='1.3'))
self.assertEqual('1.3', lbc(await self.txo_sum(type='stream', unspent=True)))
await self.stream_update(stream_id, bid='0.7')
self.assertEqual('0.7', lbc(await self.txo_sum(type='stream', unspent=True)))
self.assertEqual('2.0', lbc(await self.txo_sum(type='stream')))
self.assertEqual('1.2', lbc(await self.txo_sum(type=['stream', 'channel'], unspent=True)))
self.assertEqual('3.5', lbc(await self.txo_sum(type=['stream', 'channel'])))
# type filtering # type filtering
r = await self.txo_list(type='channel') r = await self.txo_list(type='channel')