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

fix: improve error for transfer argument mixup #2445

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading