Skip to content

Commit

Permalink
feat: alias tx.gas for tx.gas_limit and tx.hash for `tx.txn_has…
Browse files Browse the repository at this point in the history
…h` (#2431)
  • Loading branch information
antazoey authored Dec 23, 2024
1 parent d5e1a6f commit 7ccc2f2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/ape/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,19 @@ def validate_gas_limit(cls, value):

return value

@property
def gas(self) -> Optional[int]:
"""
Alias for ``.gas_limit``.
"""
return self.gas_limit

@property
def raise_on_revert(self) -> bool:
"""
``True`` means VM-reverts should raise exceptions.
``False`` allows getting failed receipts.
"""
return self._raise_on_revert

@raise_on_revert.setter
Expand All @@ -122,6 +133,14 @@ def txn_hash(self) -> HexBytes:
The calculated hash of the transaction.
"""

# TODO: In 0.9, simply rename txn_hash to hash.
@property
def hash(self) -> HexBytes:
"""
Alias for ``self.txn_hash``.
"""
return self.txn_hash

@property
def receipt(self) -> Optional["ReceiptAPI"]:
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/functional/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ def test_txn_hash_and_receipt(owner, eth_tester_provider, ethereum, kwargs):
txn = owner.prepare_transaction(txn)
txn = owner.sign_transaction(txn)
assert txn

# Show the .hash alias works.
assert txn.hash == txn.txn_hash

actual = to_hex(txn.txn_hash)
receipt = eth_tester_provider.send_transaction(txn)

Expand Down Expand Up @@ -406,3 +410,10 @@ def serialize_transaction(self) -> bytes:
my_tx = MyTransaction.model_validate({"chain_id": chain_id, "type": tx_type})
assert my_tx.chain_id == chain_id
assert my_tx.type == tx_type


def test_gas(ethereum):
tx = ethereum.create_transaction(gas_limit=123)
assert tx.gas_limit == 123
# Show the `gas` alias works.
assert tx.gas == 123

0 comments on commit 7ccc2f2

Please sign in to comment.