-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1043 from eth-brownie/feat-hardhat
Support for Hardhat Network
- Loading branch information
Showing
9 changed files
with
183 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from typing import Callable, Dict, List, Optional | ||
|
||
from web3 import Web3 | ||
|
||
from brownie.network.middlewares import BrownieMiddlewareABC | ||
|
||
|
||
class HardhatMiddleWare(BrownieMiddlewareABC): | ||
@classmethod | ||
def get_layer(cls, w3: Web3, network_type: str) -> Optional[int]: | ||
if w3.clientVersion.lower().startswith("hardhat"): | ||
return -100 | ||
else: | ||
return None | ||
|
||
def process_request(self, make_request: Callable, method: str, params: List) -> Dict: | ||
result = make_request(method, params) | ||
|
||
# modify Hardhat transaction error to mimick the format that Ganache uses | ||
if method in ("eth_call", "eth_sendTransaction") and "error" in result: | ||
message = result["error"]["message"] | ||
if message.startswith("Error: VM Exception") or message.startswith( | ||
"Error: Transaction reverted" | ||
): | ||
if method == "eth_call": | ||
# ganache returns a txid even on a failed eth_call, which is weird, | ||
# but we still mimick it here for the sake of consistency | ||
txid = "0x" | ||
else: | ||
txid = result["error"]["data"]["txHash"] | ||
data: Dict = {} | ||
result["error"]["data"] = {txid: data} | ||
message = message.split(": ", maxsplit=1)[-1] | ||
if message == "Transaction reverted without a reason": | ||
data.update({"error": "revert", "reason": None}) | ||
elif message.startswith("revert"): | ||
data.update({"error": "revert", "reason": message[7:]}) | ||
else: | ||
data["error"] = message | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#!/usr/bin/python3 | ||
|
||
import sys | ||
import warnings | ||
from pathlib import Path | ||
from subprocess import DEVNULL, PIPE | ||
from typing import Dict, List, Optional | ||
|
||
import psutil | ||
from requests.exceptions import ConnectionError as RequestsConnectionError | ||
|
||
from brownie.exceptions import InvalidArgumentWarning, RPCRequestError | ||
from brownie.network.web3 import web3 | ||
|
||
CLI_FLAGS = {"port": "--port", "fork": "--fork", "fork_block": "--fork-block-number"} | ||
IGNORED_SETTINGS = ["chain_id"] | ||
|
||
HARDHAT_CONFIG = """ | ||
// autogenerated by brownie | ||
// do not modify the existing settings | ||
module.exports = { | ||
networks: { | ||
hardhat: { | ||
hardfork: "berlin", | ||
throwOnTransactionFailures: true, | ||
throwOnCallFailures: true | ||
} | ||
} | ||
}""" | ||
|
||
|
||
def launch(cmd: str, **kwargs: Dict) -> None: | ||
"""Launches the RPC client. | ||
Args: | ||
cmd: command string to execute as subprocess""" | ||
# if sys.platform == "win32" and not cmd.split(" ")[0].endswith(".cmd"): | ||
# if " " in cmd: | ||
# cmd = cmd.replace(" ", ".cmd ", 1) | ||
# else: | ||
# cmd += ".cmd" | ||
cmd_list = cmd.split(" ") | ||
for key, value in [(k, v) for k, v in kwargs.items() if v and k not in IGNORED_SETTINGS]: | ||
try: | ||
cmd_list.extend([CLI_FLAGS[key], str(value)]) | ||
except KeyError: | ||
warnings.warn( | ||
f"Ignoring invalid commandline setting for hardhat: " | ||
f'"{key}" with value "{value}".', | ||
InvalidArgumentWarning, | ||
) | ||
print(f"\nLaunching '{' '.join(cmd_list)}'...") | ||
out = DEVNULL if sys.platform == "win32" else PIPE | ||
|
||
# check parent folders for existence of a hardhat config, so this folder is | ||
# considered a hardhat project. if none is found, create one. | ||
config_exists = False | ||
for path in Path("hardhat.config.js").absolute().parents: | ||
if path.joinpath("hardhat.config.js").exists(): | ||
config_exists = True | ||
break | ||
if not config_exists: | ||
with Path("hardhat.config.js").open("w") as fp: | ||
fp.write(HARDHAT_CONFIG) | ||
|
||
return psutil.Popen(cmd_list, stdin=DEVNULL, stdout=out, stderr=out) | ||
|
||
|
||
def on_connection() -> None: | ||
gas_limit = web3.eth.getBlock("latest").gasLimit | ||
web3.provider.make_request("evm_setBlockGasLimit", [hex(gas_limit)]) # type: ignore | ||
|
||
|
||
def _request(method: str, args: List) -> int: | ||
try: | ||
response = web3.provider.make_request(method, args) # type: ignore | ||
if "result" in response: | ||
return response["result"] | ||
except (AttributeError, RequestsConnectionError): | ||
raise RPCRequestError("Web3 is not connected.") | ||
raise RPCRequestError(response["error"]["message"]) | ||
|
||
|
||
def sleep(seconds: int) -> int: | ||
return _request("evm_increaseTime", [seconds]) | ||
|
||
|
||
def mine(timestamp: Optional[int] = None) -> None: | ||
params = [timestamp] if timestamp else [] | ||
_request("evm_mine", params) | ||
|
||
|
||
def snapshot() -> int: | ||
return _request("evm_snapshot", []) | ||
|
||
|
||
def revert(snapshot_id: int) -> None: | ||
_request("evm_revert", [snapshot_id]) | ||
|
||
|
||
def unlock_account(address: str) -> None: | ||
web3.provider.make_request("hardhat_impersonateAccount", [address]) # type: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters