diff --git a/src/ape/api/accounts.py b/src/ape/api/accounts.py index 6400a865b7..223e9c3aaf 100644 --- a/src/ape/api/accounts.py +++ b/src/ape/api/accounts.py @@ -18,6 +18,7 @@ from ape.exceptions import ( AccountsError, AliasAlreadyInUseError, + ConversionError, MethodNonPayableError, MissingDeploymentBytecodeError, SignatureError, @@ -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 ) diff --git a/tests/functional/test_accounts.py b/tests/functional/test_accounts.py index 7fed67ca04..9f330de27f 100644 --- a/tests/functional/test_accounts.py +++ b/tests/functional/test_accounts.py @@ -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