Skip to content

Commit

Permalink
Make transferTo() a no-op on same from/to accounts
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Tinker <michael.tinker@swirldslabs.com>
  • Loading branch information
tinker-michaelj committed Dec 7, 2023
1 parent a11fbf8 commit a75c46b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,14 @@ public void transferFromTo(@NonNull AccountID fromId, @NonNull AccountID toId, l
throw new IllegalArgumentException(
"Overflow on transfer of " + amount + " tinybars from " + fromId + " to " + toId);
}
accountStore.put(from.copyBuilder()
.tinybarBalance(from.tinybarBalance() - amount)
.build());
accountStore.put(
to.copyBuilder().tinybarBalance(to.tinybarBalance() + amount).build());
if (!from.accountIdOrThrow().equals(to.accountIdOrThrow())) {
accountStore.put(from.copyBuilder()
.tinybarBalance(from.tinybarBalance() - amount)
.build());
accountStore.put(to.copyBuilder()
.tinybarBalance(to.tinybarBalance() + amount)
.build());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,20 @@ void transfersRequestedValue() {
assertEquals(23L, postTransferReceiver.tinybarBalance());
}

@Test
void identicalFromToIsNoop() {
accountStore.put(Account.newBuilder()
.accountId(CONTRACT_ACCOUNT_ID)
.tinybarBalance(123L)
.smartContract(true)
.build());

subject.transferFromTo(CONTRACT_ACCOUNT_ID, CONTRACT_ACCOUNT_ID, 23L);

final var postTransfer = requireNonNull(accountState.get(CONTRACT_ACCOUNT_ID));
assertEquals(123L, postTransfer.tinybarBalance());
}

@Test
void refusesToTransferNegativeAmount() {
assertThrows(
Expand Down

0 comments on commit a75c46b

Please sign in to comment.