new api format, hide subtotals by default

This commit is contained in:
Victor Shyba 2019-07-30 20:04:36 -03:00 committed by Lex Berezhny
parent 59424a3e73
commit 35541bbbb9
2 changed files with 39 additions and 32 deletions

View file

@ -1027,19 +1027,21 @@ class Daemon(metaclass=JSONRPCServerType):
return self.wallet_manager.get_detailed_accounts(**kwargs)
@requires("wallet")
async def jsonrpc_account_balance(self, account_id=None, confirmations=0):
async def jsonrpc_account_balance(self, account_id=None, confirmations=0, reserved_subtotals=False):
"""
Return the balance of an account
Usage:
account_balance [<account_id>] [<address> | --address=<address>]
[<confirmations> | --confirmations=<confirmations>]
[<confirmations> | --confirmations=<confirmations>] [--reserved_subtotals]
Options:
--account_id=<account_id> : (str) If provided only the balance for this
account will be given. Otherwise default account.
--confirmations=<confirmations> : (int) Only include transactions with this many
confirmed blocks.
--reserved_subtotals : (bool) Include detailed reserved balances on
claims, tips and supports.
Returns:
(decimal) amount of lbry credits in wallet
@ -1047,11 +1049,20 @@ class Daemon(metaclass=JSONRPCServerType):
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_received, tips_sent, tips_balance, supports_balance = 0, 0, 0, 0
tips_balance, supports_balance = 0, 0
for amount, spent, from_me, to_me, height in await account.get_support_summary():
tips_sent += amount if from_me and not to_me else 0
tips_received += amount if to_me and not from_me else 0
if confirmations > 0 and not 0 < height <= self.ledger.headers.height - (confirmations - 1):
continue
if not spent and to_me:
@ -1061,14 +1072,12 @@ class Daemon(metaclass=JSONRPCServerType):
return {
'total': dewies_to_lbc(total),
'available': dewies_to_lbc(total - unavailable),
'reserved': {
'total': dewies_to_lbc(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)
},
'tips_received': dewies_to_lbc(tips_received),
'tips_sent': dewies_to_lbc(tips_sent)
}
}
@requires("wallet")

View file

@ -38,13 +38,12 @@ class TransactionCommandsTestCase(CommandTestCase):
await self.assertBalance(self.account, '11.0')
async def test_granular_balances(self):
initial_balance = await self.daemon.jsonrpc_account_balance()
initial_balance = await self.daemon.jsonrpc_account_balance(reserved_subtotals=True)
self.assertEqual({
'tips_received': '0.0',
'tips_sent': '0.0',
'total': '10.0',
'available': '10.0',
'reserved': {'total': '0.0', 'claims': '0.0', 'supports': '0.0', 'tips': '0.0'}
'reserved': '0.0',
'reserved_subtotals': {'claims': '0.0', 'supports': '0.0', 'tips': '0.0'}
}, initial_balance)
first_claim_id = self.get_claim_id(await self.stream_create('granularity', bid='3.0'))
await self.stream_update(first_claim_id, data=b'news', bid='1.0')
@ -53,29 +52,28 @@ class TransactionCommandsTestCase(CommandTestCase):
second_accound_address = await self.daemon.jsonrpc_address_unused(second_account_id)
await self.confirm_tx((await self.daemon.jsonrpc_account_send('1.0', second_accound_address)).id)
self.assertEqual({
'tips_received': '0.0',
'tips_sent': '0.0',
'total': '8.97741',
'available': '5.97741',
'reserved': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0', 'total': '3.0'}
}, await self.daemon.jsonrpc_account_balance())
'reserved': '3.0',
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'}
}, await self.daemon.jsonrpc_account_balance(reserved_subtotals=True))
second_claim_id = self.get_claim_id(await self.stream_create(
name='granularity-is-cool', account_id=second_account_id, bid='0.1'))
await self.daemon.jsonrpc_support_create(second_claim_id, '0.5', tip=True)
first_account_tip_txid = await self.confirm_tx((await self.daemon.jsonrpc_support_create(
first_claim_id, '0.3', tip=True, account_id=second_account_id)).id)
self.assertEqual({
'tips_received': '0.3',
'tips_sent': '0.5',
'total': '8.777264',
'available': '5.477264',
'reserved': {'claims': '1.0', 'supports': '2.0', 'tips': '0.3', 'total': '3.3'}
}, await self.daemon.jsonrpc_account_balance())
for with_subtotals in (True, False):
self.assertEqual({
'total': '8.777264',
'available': '5.477264',
'reserved': '3.3',
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.3'} if with_subtotals else None
}, await self.daemon.jsonrpc_account_balance(reserved_subtotals=with_subtotals))
await self.confirm_tx((await self.daemon.jsonrpc_support_abandon(txid=first_account_tip_txid, nout=0)).id)
self.assertEqual({
'tips_received': '0.3',
'tips_sent': '0.5',
'total': '8.777157',
'available': '5.777157',
'reserved': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0', 'total': '3.0'}
}, await self.daemon.jsonrpc_account_balance())
for with_subtotals in (True, False):
self.assertEqual({
'total': '8.777157',
'available': '5.777157',
'reserved': '3.0',
'reserved_subtotals': {'claims': '1.0', 'supports': '2.0', 'tips': '0.0'} if with_subtotals else None
}, await self.daemon.jsonrpc_account_balance(reserved_subtotals=with_subtotals))