Skip to content

Commit 299d289

Browse files
committed
fix: accumulate only when account_id and is_approved both match
Fixes #2253 Signed-off-by: Mohit Yadav <ymohit799057@gmail.com>
1 parent eafd934 commit 299d289

2 files changed

Lines changed: 40 additions & 32 deletions

File tree

src/hiero_sdk_python/tokens/abstract_token_transfer_transaction.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ def _add_token_transfer(
155155
if transfer.account_id == account_id:
156156
transfer.amount += amount
157157
transfer.expected_decimals = expected_decimals
158-
transfer.is_approved = is_approved
158+
if transfer.account_id == account_id and transfer.is_approved == is_approved:
159+
transfer.amount += amount
160+
transfer.expected_decimals = expected_decimals
159161
return
160162

161163
self.token_transfers[token_id].append(

tests/unit/transfer_transaction_test.py

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ def test_approved_token_transfer_with_decimals(mock_account_ids):
453453

454454

455455
def test_approved_token_transfer_accumulation(mock_account_ids):
456-
"""Test that approved token transfers accumulate for the same account."""
456+
"""Test that approved token transfers are stored as separate entries from normal ones."""
457457
account_id_1, account_id_2, _, token_id_1, _ = mock_account_ids
458458
transfer_tx = TransferTransaction()
459459

@@ -465,62 +465,68 @@ def test_approved_token_transfer_accumulation(mock_account_ids):
465465
transfer_1 = transfer_tx.token_transfers[token_id_1][0]
466466
transfer_2 = transfer_tx.token_transfers[token_id_1][1]
467467
assert transfer_1.amount == 500
468-
assert transfer_1.is_approved is True
468+
assert transfer_1.is_approved is False
469469
assert transfer_1.expected_decimals is None
470470
assert transfer_2.amount == 300
471471
assert transfer_2.is_approved is False
472472
assert transfer_2.expected_decimals is None
473473

474-
# Add approved transfer with decimals for account_1 (accumulates)
474+
# Add approved transfer with decimals for account_1 (separate entry, not merged)
475475
transfer_tx.add_approved_token_transfer_with_decimals(token_id_1, account_id_1, 200, 8)
476476

477-
# Verify accumulation
478-
transfer_1 = transfer_tx.token_transfers[token_id_1][0]
479-
transfer_2 = transfer_tx.token_transfers[token_id_1][1]
480-
assert transfer_1.amount == 700 # 500 + 200
481-
assert transfer_1.is_approved is False # unchanged
482-
assert transfer_1.expected_decimals == 8 # updated from the accumulation
483-
assert transfer_2.amount == 300 # unchanged
484-
assert transfer_2.is_approved is False # unchanged
485-
assert transfer_2.expected_decimals is None # unchanged
477+
# Verify stored as separate entries
478+
transfers = transfer_tx.token_transfers[token_id_1]
479+
assert len(transfers) == 3 # account_1 normal, account_2 normal, account_1 approved
480+
481+
assert transfers[0].amount == 500 # unchanged
482+
assert transfers[0].is_approved is False # unchanged
483+
assert transfers[1].amount == 300 # unchanged
484+
assert transfers[1].is_approved is False # unchanged
485+
assert transfers[2].amount == 200
486+
assert transfers[2].is_approved is True
487+
assert transfers[2].expected_decimals == 8
486488

487489

488-
def test_is_approved_updated_normal_then_approved(mock_account_ids):
489-
"""is_approved becomes True when an approved call follows a normal one (last-call-wins)."""
490+
def test_normal_and_approved_transfers_kept_separate(mock_account_ids):
491+
"""Normal and approved transfers for the same account are stored as separate entries."""
490492
account_id_1, account_id_2, _, token_id_1, _ = mock_account_ids
491493
transfer_tx = TransferTransaction()
492494

493495
transfer_tx.add_token_transfer(token_id_1, account_id_1, 500)
494496
transfer_tx.add_token_transfer(token_id_1, account_id_2, -500)
495-
496-
transfer_1 = transfer_tx.token_transfers[token_id_1][0]
497-
assert transfer_1.is_approved is False
498-
499497
transfer_tx.add_approved_token_transfer(token_id_1, account_id_1, 200)
500498
transfer_tx.add_token_transfer(token_id_1, account_id_2, -200)
501499

502-
transfer_1 = transfer_tx.token_transfers[token_id_1][0]
503-
assert transfer_1.amount == 700
504-
assert transfer_1.is_approved is True
500+
transfers = transfer_tx.token_transfers[token_id_1]
501+
assert len(transfers) == 3 # account_1 normal, account_2 accumulated, account_1 approved
505502

503+
assert transfers[0].amount == 500
504+
assert transfers[0].is_approved is False
506505

507-
def test_is_approved_updated_approved_then_normal(mock_account_ids):
508-
"""is_approved becomes False when a normal call follows an approved one (last-call-wins)."""
506+
assert transfers[1].amount == -700 # -500 + -200 accumulated
507+
assert transfers[1].is_approved is False
508+
509+
assert transfers[2].amount == 200
510+
assert transfers[2].is_approved is True
511+
512+
513+
def test_same_approved_transfers_accumulate(mock_account_ids):
514+
"""Two approved transfers for the same account DO accumulate."""
509515
account_id_1, account_id_2, _, token_id_1, _ = mock_account_ids
510516
transfer_tx = TransferTransaction()
511517

512-
transfer_tx.add_approved_token_transfer(token_id_1, account_id_1, 500)
518+
transfer_tx.add_approved_token_transfer(token_id_1, account_id_1, 300)
519+
transfer_tx.add_approved_token_transfer(token_id_1, account_id_1, 200)
513520
transfer_tx.add_token_transfer(token_id_1, account_id_2, -500)
514521

515-
transfer_1 = transfer_tx.token_transfers[token_id_1][0]
516-
assert transfer_1.is_approved is True
522+
transfers = transfer_tx.token_transfers[token_id_1]
523+
assert len(transfers) == 2 # account_1 approved (merged), account_2 normal
517524

518-
transfer_tx.add_token_transfer(token_id_1, account_id_1, 200)
519-
transfer_tx.add_token_transfer(token_id_1, account_id_2, -200)
525+
assert transfers[0].amount == 500 # 300 + 200 accumulated
526+
assert transfers[0].is_approved is True
520527

521-
transfer_1 = transfer_tx.token_transfers[token_id_1][0]
522-
assert transfer_1.amount == 700
523-
assert transfer_1.is_approved is False
528+
assert transfers[1].amount == -500
529+
assert transfers[1].is_approved is False
524530

525531

526532
def test_add_approved_token_transfer_no_decimals(mock_account_ids):

0 commit comments

Comments
 (0)