Skip to content

Commit

Permalink
fix: improve error for transfer argument mixup (#2445)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Dec 23, 2024
1 parent 68d0df8 commit 0686379
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/ape/api/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ape.exceptions import (
AccountsError,
AliasAlreadyInUseError,
ConversionError,
MethodNonPayableError,
MissingDeploymentBytecodeError,
SignatureError,
Expand Down Expand Up @@ -220,8 +221,18 @@ def transfer(
Returns:
:class:`~ape.api.transactions.ReceiptAPI`
"""
if isinstance(account, int):
raise AccountsError(
"Cannot use integer-type for the `receiver` argument in the "
"`.transfer()` method (this protects against accidentally passing "
"the `value` as the `receiver`)."
)

try:
receiver = self.conversion_manager.convert(account, AddressType)
except ConversionError as err:
raise AccountsError(f"Invalid `receiver` value: '{account}'.") from err

receiver = self.conversion_manager.convert(account, AddressType)
txn = self.provider.network.ecosystem.create_transaction(
sender=self.address, receiver=receiver, **kwargs
)
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,27 @@ def test_transfer_value_of_0(sender, receiver):
assert receiver.balance == initial_balance


def test_transfer_mixed_up_sender_and_value(sender, receiver):
"""
Testing the case where the user mixes up the argument order,
it should show a nicer error than it was previously, as this is
a common and easy mistake.
"""
expected = (
r"Cannot use integer-type for the `receiver` "
r"argument in the `\.transfer\(\)` method \(this "
r"protects against accidentally passing the "
r"`value` as the `receiver`\)."
)
with pytest.raises(AccountsError, match=expected):
sender.transfer(123, receiver)

# Similarly show using currency-str (may fail for different error).
expected = r"Invalid `receiver` value: '123 wei'\."
with pytest.raises(AccountsError, match=expected):
sender.transfer("123 wei", receiver)


def test_deploy(owner, contract_container, clean_contract_caches):
contract = owner.deploy(contract_container, 0)
assert contract.address
Expand Down

0 comments on commit 0686379

Please sign in to comment.