Fix TX amount calculation in trade manager (#16934)

This commit is contained in:
Matt Hauff 2023-12-07 06:53:36 -08:00 committed by GitHub
parent 186ce3ab31
commit 68afc8c31c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View File

@ -706,10 +706,22 @@ class TradeManager:
removal_tree_hash = Program.to([coin_as_list(rem) for rem in grouped_removals]).get_tree_hash()
# We also need to calculate the sent amount
removed: int = sum(c.amount for c in grouped_removals)
removed_ids: List[bytes32] = [c.name() for c in grouped_removals]
all_additions_from_grouped_removals: List[Coin] = [
c for c in all_additions if c.parent_coin_info in removed_ids
]
potential_change_coins: List[Coin] = addition_dict[wid] if wid in addition_dict else []
change_coins: List[Coin] = [c for c in potential_change_coins if c.parent_coin_info in all_removals]
change_amount: int = sum(c.amount for c in change_coins)
sent_amount: int = removed - change_amount
sent_amount: int = (
removed
- change_amount
- (
removed - sum(c.amount for c in all_additions_from_grouped_removals) # removals - additions == fees
if wallet == self.wallet_state_manager.main_wallet
else 0
)
)
txs.append(
TransactionRecord(
confirmed_at_height=uint32(0),

View File

@ -660,10 +660,16 @@ async def test_cat_trades(
Offer.from_bytes(trade_make.offer),
peer,
wallet_environments.tx_config,
fee=uint64(1),
)
assert trade_take is not None
assert tx_records is not None
# Testing a precious display bug real quick
xch_tx: TransactionRecord = next(tx for tx in tx_records if tx.wallet_id == 1)
assert xch_tx.amount == 3
assert xch_tx.fee_amount == 1
await wallet_environments.process_pending_states(
[
WalletStateTransition(
@ -696,9 +702,9 @@ async def test_cat_trades(
WalletStateTransition(
pre_block_balance_updates={
"xch": {
"unconfirmed_wallet_balance": -3,
"<=#spendable_balance": -3,
"<=#max_send_amount": -3,
"unconfirmed_wallet_balance": -4, # -3 for offer, -1 for fee
"<=#spendable_balance": -4,
"<=#max_send_amount": -4,
"pending_coin_removal_count": 1,
},
"cat": {
@ -723,7 +729,7 @@ async def test_cat_trades(
},
post_block_balance_updates={
"xch": {
"confirmed_wallet_balance": -3,
"confirmed_wallet_balance": -4,
">#spendable_balance": 0,
">#max_send_amount": 0,
"pending_coin_removal_count": -1,