diff --git a/lbrynet/extras/wallet/manager.py b/lbrynet/extras/wallet/manager.py index 0b58b6bc0..051c39d18 100644 --- a/lbrynet/extras/wallet/manager.py +++ b/lbrynet/extras/wallet/manager.py @@ -307,7 +307,8 @@ class LbryWalletManager(BaseWalletManager): 'support_info': [], 'abandon_info': [] } - if all([txi.is_my_account for txi in tx.inputs]): + is_my_inputs = all([txi.is_my_account for txi in tx.inputs]) + if is_my_inputs: # fees only matter if we are the ones paying them item['value'] = dewies_to_lbc(tx.net_account_balance+tx.fee) item['fee'] = dewies_to_lbc(-tx.fee) @@ -325,34 +326,53 @@ class LbryWalletManager(BaseWalletManager): 'nout': txo.position }) for txo in tx.my_update_outputs: - item['update_info'].append({ - 'address': txo.get_address(account.ledger), - 'balance_delta': dewies_to_lbc(-txo.amount), - 'amount': dewies_to_lbc(txo.amount), - 'claim_id': txo.claim_id, - 'claim_name': txo.claim_name, - 'nout': txo.position - }) + if is_my_inputs: # updating my own claim + previous = None + for txi in tx.inputs: + if txi.txo_ref.txo is not None: + other_txo = txi.txo_ref.txo + if other_txo.is_claim and other_txo.claim_id == txo.claim_id: + previous = other_txo + break + if previous is None: + raise ValueError( + "Invalid claim update state, expected to find previous claim in input." + ) + item['update_info'].append({ + 'address': txo.get_address(account.ledger), + 'balance_delta': dewies_to_lbc(previous.amount-txo.amount), + 'amount': dewies_to_lbc(txo.amount), + 'claim_id': txo.claim_id, + 'claim_name': txo.claim_name, + 'nout': txo.position + }) + else: # someone sent us their claim + item['update_info'].append({ + 'address': txo.get_address(account.ledger), + 'balance_delta': dewies_to_lbc(0), + 'amount': dewies_to_lbc(txo.amount), + 'claim_id': txo.claim_id, + 'claim_name': txo.claim_name, + 'nout': txo.position + }) for txo in tx.my_support_outputs: - is_tip = next(tx.my_inputs, None) is None item['support_info'].append({ 'address': txo.get_address(account.ledger), - 'balance_delta': dewies_to_lbc(txo.amount if is_tip else -txo.amount), + 'balance_delta': dewies_to_lbc(txo.amount if not is_my_inputs else -txo.amount), 'amount': dewies_to_lbc(txo.amount), 'claim_id': txo.claim_id, 'claim_name': txo.claim_name, - 'is_tip': is_tip, + 'is_tip': not is_my_inputs, 'nout': txo.position }) for txo in tx.other_support_outputs: - is_tip = next(tx.my_inputs, None) is not None item['support_info'].append({ 'address': txo.get_address(account.ledger), 'balance_delta': dewies_to_lbc(-txo.amount), 'amount': dewies_to_lbc(txo.amount), 'claim_id': txo.claim_id, 'claim_name': txo.claim_name, - 'is_tip': is_tip, + 'is_tip': is_my_inputs, 'nout': txo.position }) for txo in tx.my_abandon_outputs: diff --git a/lbrynet/extras/wallet/transaction.py b/lbrynet/extras/wallet/transaction.py index fea2fff52..d27c09fa9 100644 --- a/lbrynet/extras/wallet/transaction.py +++ b/lbrynet/extras/wallet/transaction.py @@ -39,6 +39,10 @@ class Output(BaseOutput): name_fee = len(self.script.values['claim_name']) * ledger.fee_per_name_char return max(name_fee, super().get_fee(ledger)) + @property + def is_claim(self) -> bool: + return self.script.is_claim_name or self.script.is_update_claim + @property def claim_id(self) -> str: if self.script.is_claim_name: @@ -57,7 +61,7 @@ class Output(BaseOutput): @property def claim(self) -> ClaimDict: - if self.script.is_claim_name or self.script.is_update_claim: + if self.is_claim: return smart_decode(self.script.values['claim']) raise ValueError('Only claim name and claim update have the claim payload.') diff --git a/tests/integration/wallet/test_commands.py b/tests/integration/wallet/test_commands.py index a81f7a32a..ec22d151c 100644 --- a/tests/integration/wallet/test_commands.py +++ b/tests/integration/wallet/test_commands.py @@ -494,6 +494,36 @@ class ClaimManagement(CommandTestCase): await self.generate(1) return claim + async def test_create_update_and_abandon_claim(self): + self.assertEqual('10.0', await self.daemon.jsonrpc_account_balance()) + + claim = await self.make_claim(amount='2.5') # creates new claim + txs = await self.out(self.daemon.jsonrpc_transaction_list()) + self.assertEqual(len(txs[0]['claim_info']), 1) + self.assertEqual(txs[0]['claim_info'][0]['balance_delta'], '-2.5') + self.assertEqual(txs[0]['claim_info'][0]['claim_id'], claim['claim_id']) + self.assertEqual(txs[0]['value'], '0.0') + self.assertEqual(txs[0]['fee'], '-0.020107') + self.assertEqual('7.479893', await self.daemon.jsonrpc_account_balance()) + + await self.make_claim(amount='1.0') # updates previous claim + txs = await self.out(self.daemon.jsonrpc_transaction_list()) + self.assertEqual(len(txs[0]['update_info']), 1) + self.assertEqual(txs[0]['update_info'][0]['balance_delta'], '1.5') + self.assertEqual(txs[0]['update_info'][0]['claim_id'], claim['claim_id']) + self.assertEqual(txs[0]['value'], '0.0') + self.assertEqual(txs[0]['fee'], '-0.0001985') + self.assertEqual('8.9796945', await self.daemon.jsonrpc_account_balance()) + + await self.out(self.daemon.jsonrpc_claim_abandon(claim['claim_id'])) + txs = await self.out(self.daemon.jsonrpc_transaction_list()) + self.assertEqual(len(txs[0]['abandon_info']), 1) + self.assertEqual(txs[0]['abandon_info'][0]['balance_delta'], '1.0') + self.assertEqual(txs[0]['abandon_info'][0]['claim_id'], claim['claim_id']) + self.assertEqual(txs[0]['value'], '0.0') + self.assertEqual(txs[0]['fee'], '-0.000107') + self.assertEqual('9.9795875', await self.daemon.jsonrpc_account_balance()) + async def test_update_claim_holding_address(self): other_account_id = (await self.daemon.jsonrpc_account_create('second account'))['id'] other_account = self.daemon.get_account_or_error(other_account_id)