Skip to content

Commit 7520b53

Browse files
authored
fix: could not get transaction hash for sequencer transactions (#40)
1 parent 2274bfb commit 7520b53

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,44 @@ Installing this plugin adds support for the Optimism ecosystem:
5050
ape console --network optimism:sepolia
5151
```
5252

53+
### OP Stack
54+
55+
Use the `optimism` base-class in any custom networks using the OP stack.
56+
For example, to configure a custom network for Fraxtal network, add the following to your `pyproject.toml`
57+
58+
```toml
59+
[[tool.ape.networks.custom]]
60+
name = "mainnet"
61+
ecosystem = "fraxtal"
62+
# Tell Ape to use optimism as the base plugin, instead of ethereum.
63+
base_ecosystem_plugin = "optimism"
64+
chain_id = 252
65+
66+
# (optional): Configure an RPC. Else, Ape will select a random one automatically.
67+
[tool.ape.node.fraxtal.mainnet]
68+
uri = "https://rpc.frax.com"
69+
```
70+
71+
Or equivalent `ape-config.yaml`:
72+
73+
```yaml
74+
networks:
75+
custom:
76+
- name: mainnet
77+
ecosystem: fraxtal
78+
base_ecosystem_plugin: optimism
79+
80+
node:
81+
fraxtal:
82+
mainnet:
83+
uri: https://rpc.frax.com
84+
```
85+
86+
There are two main benefits of using Optimism as the base-class instead of Ethereum for networks using the OP stack:
87+
88+
1. **Closer defaults**: The block time default is `2` for Optimism networks, which may be a better default value than Ethereum's higher block time parameters.
89+
2. **Existence of System Transactions**: The Optimism base-class is aware of system transactions, which are transactions invoked by the sequencer.
90+
5391
## Development
5492

5593
Comments, questions, criticisms and pull requests are welcomed.

ape_optimism/ecosystem.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TYPE_CHECKING, cast
1+
from typing import cast
22

33
from ape.api import TransactionAPI
44
from ape.exceptions import ApeException, APINotImplementedError
@@ -8,9 +8,7 @@
88
NetworkConfig,
99
create_network_config,
1010
)
11-
12-
if TYPE_CHECKING:
13-
from eth_pydantic_types import HexBytes
11+
from eth_pydantic_types import HexBytes
1412

1513
NETWORKS = {
1614
# chain_id, network_id
@@ -34,16 +32,30 @@ class OptimismConfig(BaseEthereumConfig):
3432

3533
class SystemTransaction(TransactionAPI):
3634
type: int = SYSTEM_TRANSACTION
35+
_hash: HexBytes
36+
37+
def __init__(self, *args, **kwargs):
38+
hash = kwargs.pop("hash", None)
39+
super().__init__(*args, **kwargs)
40+
self._hash = hash
3741

3842
@property
3943
def txn_hash(self) -> "HexBytes":
40-
raise APINotImplementedError("Unable to calculate the hash of system transactions.")
44+
return self._hash
4145

4246
def serialize_transaction(self) -> bytes:
4347
raise APINotImplementedError("Unable to serialize system transactions.")
4448

4549

4650
class Optimism(Ethereum):
51+
"""
52+
A base class for Optimism or OP-stack networks.
53+
The main differences from Ethereum are:
54+
55+
1. Different network defaults (block time is only '2', by default).
56+
2. Recognition of "System transactions" (type 126).
57+
"""
58+
4759
@property
4860
def config(self) -> OptimismConfig: # type: ignore
4961
return cast(OptimismConfig, self.config_manager.get_config("optimism"))

tests/test_ecosystem.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from ape_ethereum.transactions import TransactionType
33
from ethpm_types.abi import MethodABI
4+
from hexbytes import HexBytes
45

56
from ape_optimism.ecosystem import SYSTEM_TRANSACTION, SystemTransaction
67

@@ -45,6 +46,7 @@ def test_create_transaction_type_2(optimism, tx_kwargs):
4546

4647

4748
def test_create_transaction_type_126(optimism):
49+
txn_hash = "0x417b3339801514042a4b3fa24658931438389996b29d0749cc7555a13a9ad89a"
4850
data = {
4951
"chainId": 0,
5052
"to": "0x4200000000000000000000000000000000000015",
@@ -55,10 +57,12 @@ def test_create_transaction_type_126(optimism):
5557
"data": "0x",
5658
"type": 126,
5759
"accessList": [],
60+
"hash": HexBytes(txn_hash),
5861
}
5962
actual = optimism.create_transaction(**data)
6063
assert isinstance(actual, SystemTransaction)
6164
assert actual.type == SYSTEM_TRANSACTION
65+
assert actual.txn_hash == HexBytes(txn_hash)
6266

6367

6468
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)