Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Consolidate reset method
Convert block_number and fork_url to properties
  • Loading branch information
BowTiedDevil committed Oct 23, 2024
1 parent 2d99e09 commit 7ad5a55
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/degenbot/anvil_fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def build_anvil_command(path_to_anvil: pathlib.Path) -> list[str]: # pragma: no
raise AnvilNotFound
path_to_anvil = pathlib.Path(_path_to_anvil)

self.fork_url = fork_url
self.port = self._get_free_port_number()
self.ipc_path = ipc_path
self.ipc_provider_kwargs = (
Expand All @@ -112,9 +111,10 @@ def build_anvil_command(path_to_anvil: pathlib.Path) -> list[str]: # pragma: no
)
self.w3 = Web3(IPCProvider(ipc_path=self.ipc_filename, **self.ipc_provider_kwargs))

self._initial_block_number = (
self._block_number = (
fork_block if fork_block is not None else self.w3.eth.get_block_number()
)
self._fork_url = fork_url

if middlewares is not None:
for middleware, layer in middlewares:
Expand All @@ -135,6 +135,14 @@ def build_anvil_command(path_to_anvil: pathlib.Path) -> list[str]: # pragma: no
if coinbase is not None:
self.set_coinbase(coinbase)

@property
def block_number(self) -> int:
return self._block_number

@property
def fork_url(self) -> str:
return self._fork_url

@property
def http_url(self) -> str:
return f"http://localhost:{self.port}"
Expand Down Expand Up @@ -199,31 +207,28 @@ def mine(self) -> None:
params=[],
)

def _reset(
self,
fork_url: str,
block_number: int | None,
) -> None:
forking_params: dict[str, Any] = {"jsonRpcUrl": fork_url}
if block_number:
forking_params["blockNumber"] = block_number

self.w3.provider.make_request(
method=RPCEndpoint("anvil_reset"),
params=[{"forking": forking_params}],
)

def reset(
self,
fork_url: str | None = None,
block_number: int | None = None,
) -> None:
self._reset(
fork_url=fork_url if fork_url is not None else self.fork_url,
block_number=block_number,
self.w3.provider.make_request(
method=RPCEndpoint("anvil_reset"),
params=[
{
"forking": {
"jsonRpcUrl": fork_url if fork_url is not None else self.fork_url,
"blockNumber": block_number
if block_number is not None
else self.block_number,
}
}
],
)
if fork_url:
self.fork_url = fork_url
if fork_url is not None:
self._fork_url = fork_url
if block_number is not None:
self._block_number = block_number

def reset_to_transaction_hash(self, transaction_hash: str) -> None:
"""
Expand Down

0 comments on commit 7ad5a55

Please sign in to comment.