Skip to content

Commit

Permalink
chore: last minut revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Jul 24, 2024
1 parent 4e001ed commit 3701c99
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
11 changes: 6 additions & 5 deletions docs/userguides/reverts.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Calling it may look like:
receipt = contract.setNumber(123, sender=not_owner)
```

And when it fails, you may see a stacktrace like this:
And when it fails, Ape shows a stacktrace like this:

```shell
File "$HOME/ApeProjects/ape-project/scripts/fail.py", line 8, in main
Expand All @@ -41,7 +41,7 @@ setNumber
ERROR: (ContractLogicError) !authorized
```

One way to allow exceptions is to simply use `try:` / `except:` blocks:
One way to handle exceptions is to simply use `try:` / `except:` blocks:

```python
from ape.exceptions import ContractLogicError
Expand Down Expand Up @@ -84,8 +84,7 @@ require(msg.sender == owner); // @dev !authorized
## Custom Errors

As of Solidity 0.8.4, custom errors have been introduced to the ABI.
To make assertions on custom errors, you can use the types defined on your contracts.

In Ape, custom errors are available on contract-instances.
For example, if you have a contract like:

```solidity
Expand All @@ -104,7 +103,7 @@ contract MyContract {
}
```

If you have an instance of this contract assigned to variable `contract`, you can reference the custom exception by doing:
And if you have an instance of this contract assigned to variable `contract`, you can reference the custom exception by doing:

```python
contract.Unauthorized
Expand Down Expand Up @@ -134,3 +133,5 @@ try:
except FallbackNotDefinedError:
print("fallback not defined")
```

Next, learn how to test your contracts' errors using the `ape.reverts` context-manager in the [testing guide](../testing.html#testing-transaction-rseverts).
8 changes: 6 additions & 2 deletions src/ape/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ class TransactionAPI(BaseInterfaceModel):
model_config = ConfigDict(populate_by_name=True)

def __init__(self, *args, **kwargs):
raise_on_revert = kwargs.pop("raise_on_revert", True)
super().__init__(*args, **kwargs)
self._raise_on_revert = kwargs.pop("raise_on_revert", True)
self._raise_on_revert = raise_on_revert

@field_validator("gas_limit", mode="before")
@classmethod
Expand Down Expand Up @@ -466,7 +467,10 @@ def await_confirmations(self) -> "ReceiptAPI":
sender_nonce = self.provider.get_nonce(self.sender)
iteration += 1
if iteration == iterations_timeout:
raise TransactionError("Timeout waiting for sender's nonce to increase.")
tx_err = TransactionError("Timeout waiting for sender's nonce to increase.")
self.error = tx_err
if self.transaction.raise_on_revert:
raise tx_err

if self.required_confirmations == 0:
# The transaction might not yet be confirmed but
Expand Down
3 changes: 1 addition & 2 deletions src/ape_ethereum/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,9 @@ def raise_for_status(self):
txn_hash = HexBytes(self.txn_hash).hex()
err = TransactionError(f"Transaction '{txn_hash}' failed.", txn=self)

self.error = err
if err and self.transaction.raise_on_revert:
raise err
elif err:
self.error = err

def show_trace(self, verbose: bool = False, file: IO[str] = sys.stdout):
self.trace.show(verbose=verbose, file=file)
Expand Down
4 changes: 2 additions & 2 deletions src/ape_run/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._namespace = {}
self._command_called = None
self._has_warned_missing_hook: dict[Path, bool] = {}
self._has_warned_missing_hook: set[Path] = set()

def invoke(self, ctx: Context) -> Any:
try:
Expand Down Expand Up @@ -157,7 +157,7 @@ def call(network):
else:
if relative_filepath not in self._has_warned_missing_hook:
logger.warning(f"No 'main' method or 'cli' command in script: {relative_filepath}")
self._has_warned_missing_hook[relative_filepath] = True
self._has_warned_missing_hook.add(relative_filepath)

@click.command(
cls=ConnectedProviderCommand,
Expand Down
1 change: 0 additions & 1 deletion src/ape_test/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ def send_call(

state = kwargs.pop("state_override", None)
call_kwargs: dict = {"block_identifier": block_id, "state_override": state}

raise_on_revert = kwargs.get("raise_on_revert", txn.raise_on_revert)

# Remove unneeded properties
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/geth/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ def test_revert_out_of_gas_error(geth_account, geth_second_account, geth_provide
assert err.value.txn is not None


@geth_process_test
def test_revert_out_of_gas_error_allow(geth_account, geth_second_account, geth_provider):
tx = geth_account.transfer(geth_second_account, 1, gas_limit=1, raise_on_revert=False)
assert tx.failed
assert isinstance(tx.error, OutOfGasError)


@geth_process_test
def test_revert_allow(test_accounts, geth_contract):
not_owner = test_accounts[0]
Expand Down

0 comments on commit 3701c99

Please sign in to comment.