Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHIA-712] Fix CATWallet pending_change calculation #18126

Merged
merged 14 commits into from
Jul 11, 2024
4 changes: 2 additions & 2 deletions chia/_tests/wallet/cat_wallet/test_cat_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ async def test_cat_spend(wallet_environments: WalletTestFramework) -> None:
"confirmed_wallet_balance": 0,
"unconfirmed_wallet_balance": 100,
"spendable_balance": 0,
"pending_change": 0,
"pending_change": 100, # A little weird but technically correct
"max_send_amount": 0,
"unspent_coin_count": 0,
"pending_coin_removal_count": 1, # The ephemeral eve spend
Expand All @@ -226,7 +226,7 @@ async def test_cat_spend(wallet_environments: WalletTestFramework) -> None:
"cat": {
"confirmed_wallet_balance": 100,
"spendable_balance": 100,
"pending_change": 0,
"pending_change": -100,
"max_send_amount": 100,
"unspent_coin_count": 1,
"pending_coin_removal_count": -1,
Expand Down
21 changes: 21 additions & 0 deletions chia/_tests/wallet/cat_wallet/test_trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ async def test_cat_trades(
"unconfirmed_wallet_balance": 0,
"spendable_balance": 0,
"max_send_amount": 0,
"pending_change": 0,
"unspent_coin_count": 0,
}
if credential_restricted
Expand Down Expand Up @@ -546,6 +547,7 @@ async def test_cat_trades(
"new cat": {
"unconfirmed_wallet_balance": -2,
"pending_coin_removal_count": 1,
"pending_change": 98,
"<=#spendable_balance": -2,
"<=#max_send_amount": -2,
},
Expand All @@ -572,6 +574,7 @@ async def test_cat_trades(
"new cat": {
"confirmed_wallet_balance": -2,
"pending_coin_removal_count": -1,
"pending_change": -98,
">#spendable_balance": 0,
">#max_send_amount": 0,
},
Expand Down Expand Up @@ -603,6 +606,7 @@ async def test_cat_trades(
"new cat": {
"unconfirmed_wallet_balance": 2,
"pending_coin_removal_count": 1,
"pending_change": 2, # This is a little weird but fits the current definition
},
"vc": {
"pending_coin_removal_count": 1,
Expand All @@ -618,6 +622,7 @@ async def test_cat_trades(
"confirmed_wallet_balance": 2,
"spendable_balance": 2,
"max_send_amount": 2,
"pending_change": -2,
"unspent_coin_count": 1,
"pending_coin_removal_count": -1,
},
Expand Down Expand Up @@ -885,6 +890,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"new cat": {
"unconfirmed_wallet_balance": -6,
"<=#spendable_balance": -6,
"pending_change": 92,
"<=#max_send_amount": -6,
"pending_coin_removal_count": 1,
},
Expand All @@ -907,6 +913,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
},
"new cat": {
"confirmed_wallet_balance": -6,
"pending_change": -92,
">#spendable_balance": 0,
">#max_send_amount": 0,
"pending_coin_removal_count": -1,
Expand Down Expand Up @@ -942,6 +949,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"new cat": {
"unconfirmed_wallet_balance": 6,
"pending_coin_removal_count": 1,
"pending_change": 6, # This is a little weird but fits the current definition
},
"vc": {
"pending_coin_removal_count": 1,
Expand All @@ -957,6 +965,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"confirmed_wallet_balance": 6,
"spendable_balance": 6,
"max_send_amount": 6,
"pending_change": -6,
"unspent_coin_count": 1,
"pending_coin_removal_count": -1,
},
Expand Down Expand Up @@ -1105,12 +1114,14 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"unconfirmed_wallet_balance": -8,
"<=#spendable_balance": -8,
"<=#max_send_amount": -8,
"pending_change": 1,
"pending_coin_removal_count": 2, # For the first time, we're using two coins in an offer
},
"new cat": {
"unconfirmed_wallet_balance": -9,
"<=#spendable_balance": -9,
"<=#max_send_amount": -9,
"pending_change": 83,
"pending_coin_removal_count": 1,
},
**(
Expand All @@ -1134,13 +1145,15 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"confirmed_wallet_balance": -8,
">#spendable_balance": 0,
">#max_send_amount": 0,
"pending_change": -1,
"pending_coin_removal_count": -2,
"unspent_coin_count": -1,
},
"new cat": {
"confirmed_wallet_balance": -9,
">#spendable_balance": 0,
">#max_send_amount": 0,
"pending_change": -83,
"pending_coin_removal_count": -1,
},
**(
Expand Down Expand Up @@ -1174,6 +1187,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"cat": {
"unconfirmed_wallet_balance": 8,
"pending_coin_removal_count": 1,
"pending_change": 8, # This is a little weird but fits the current definition
},
"vc": {
"pending_coin_removal_count": 1,
Expand All @@ -1189,6 +1203,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"confirmed_wallet_balance": 8,
"spendable_balance": 8,
"max_send_amount": 8,
"pending_change": -8,
"unspent_coin_count": 1,
"pending_coin_removal_count": -1,
},
Expand Down Expand Up @@ -1219,6 +1234,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"new cat": {
"unconfirmed_wallet_balance": 9,
"pending_coin_removal_count": 1,
"pending_change": 9, # This is a little weird but fits the current definition
},
"vc": {
"pending_coin_removal_count": 1,
Expand All @@ -1234,6 +1250,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"confirmed_wallet_balance": 9,
"spendable_balance": 9,
"max_send_amount": 9,
"pending_change": -9,
"unspent_coin_count": 1,
"pending_coin_removal_count": -1,
},
Expand Down Expand Up @@ -1476,6 +1493,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"unconfirmed_wallet_balance": -15,
"<=#spendable_balance": -15,
"<=#max_send_amount": -15,
"pending_change": 68,
"pending_coin_removal_count": 1,
},
**(
Expand Down Expand Up @@ -1505,6 +1523,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"confirmed_wallet_balance": -15,
">#spendable_balance": 0,
">#max_send_amount": 0,
"pending_change": -68,
"pending_coin_removal_count": -1,
},
**(
Expand Down Expand Up @@ -1538,6 +1557,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"new cat": {
"unconfirmed_wallet_balance": 15,
"pending_coin_removal_count": 1,
"pending_change": 15, # This is a little weird but fits the current definition
},
"vc": {
"pending_coin_removal_count": 1,
Expand All @@ -1553,6 +1573,7 @@ async def assert_trade_tx_number(wallet_node: WalletNode, trade_id: bytes32, num
"confirmed_wallet_balance": 15,
"spendable_balance": 15,
"max_send_amount": 15,
"pending_change": -15,
"unspent_coin_count": 1,
"pending_coin_removal_count": -1,
},
Expand Down
6 changes: 6 additions & 0 deletions chia/_tests/wallet/vc_wallet/test_vc_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ async def test_vc_lifecycle(wallet_environments: WalletTestFramework) -> None:
"unconfirmed_wallet_balance": -90,
"spendable_balance": -100,
"max_send_amount": -100,
"pending_change": 10,
"pending_coin_removal_count": 1,
},
},
Expand All @@ -387,6 +388,7 @@ async def test_vc_lifecycle(wallet_environments: WalletTestFramework) -> None:
"confirmed_wallet_balance": -90,
"spendable_balance": 10,
"max_send_amount": 10,
"pending_change": -10,
"pending_coin_removal_count": -1,
},
},
Expand Down Expand Up @@ -476,6 +478,7 @@ async def test_vc_lifecycle(wallet_environments: WalletTestFramework) -> None:
},
"crcat": {
"unconfirmed_wallet_balance": 90,
"pending_change": 90,
"pending_coin_removal_count": 1,
},
},
Expand All @@ -492,6 +495,7 @@ async def test_vc_lifecycle(wallet_environments: WalletTestFramework) -> None:
"confirmed_wallet_balance": 90,
"spendable_balance": 90,
"max_send_amount": 90,
"pending_change": -90,
"unspent_coin_count": 1,
"pending_coin_removal_count": -1,
},
Expand Down Expand Up @@ -543,6 +547,7 @@ async def test_vc_lifecycle(wallet_environments: WalletTestFramework) -> None:
"unconfirmed_wallet_balance": -50,
"spendable_balance": -90,
"max_send_amount": -90,
"pending_change": 40,
"pending_coin_removal_count": 1,
},
},
Expand All @@ -559,6 +564,7 @@ async def test_vc_lifecycle(wallet_environments: WalletTestFramework) -> None:
"confirmed_wallet_balance": -50, # should go straight to confirmed because we sent to ourselves
"spendable_balance": 40,
"max_send_amount": 40,
"pending_change": -40,
"pending_coin_removal_count": -1,
"unspent_coin_count": 1,
},
Expand Down
9 changes: 7 additions & 2 deletions chia/wallet/cat_wallet/cat_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ async def get_pending_change_balance(self) -> uint64:
unconfirmed_tx = await self.wallet_state_manager.tx_store.get_unconfirmed_for_wallet(self.id())
addition_amount = 0
for record in unconfirmed_tx:
if not record.is_in_mempool():
if not record.is_in_mempool() and record.spend_bundle is not None:
continue
our_spend = False
for coin in record.removals:
Expand All @@ -484,7 +484,12 @@ async def get_pending_change_balance(self) -> uint64:
continue

for coin in record.additions:
if await self.wallet_state_manager.does_coin_belong_to_wallet(coin, self.id()):
hint_dict = {
coin_id: bytes32(memos[0])
for coin_id, memos in record.memos
if len(memos) > 0 and len(memos[0]) == 32
}
if await self.wallet_state_manager.does_coin_belong_to_wallet(coin, self.id(), hint_dict=hint_dict):
addition_amount += coin.amount

return uint64(addition_amount)
Expand Down
Loading