-
Notifications
You must be signed in to change notification settings - Fork 7
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 #121 from moonstream-to/foundry-projects
Generate Brownie interface for Foundry project
- Loading branch information
Showing
4 changed files
with
143 additions
and
8 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,85 @@ | ||
# Code generated by moonworm : https://github.com/moonstream-to/moonworm | ||
# Moonworm version : {moonworm_version} | ||
|
||
import argparse | ||
import json | ||
import os | ||
from pathlib import Path | ||
from typing import Any, Dict, List, Optional, Union | ||
|
||
from brownie import Contract, network, project | ||
from brownie.network.contract import ContractContainer | ||
from eth_typing.evm import ChecksumAddress | ||
|
||
|
||
PROJECT_DIRECTORY = os.path.abspath(os.path.join(os.path.dirname(__file__), {relative_path})) | ||
BUILD_DIRECTORY = os.path.join(PROJECT_DIRECTORY, "out", "{build_subdir}") | ||
|
||
def boolean_argument_type(raw_value: str) -> bool: | ||
TRUE_VALUES = ["1", "t", "y", "true", "yes"] | ||
FALSE_VALUES = ["0", "f", "n", "false", "no"] | ||
|
||
if raw_value.lower() in TRUE_VALUES: | ||
return True | ||
elif raw_value.lower() in FALSE_VALUES: | ||
return False | ||
|
||
raise ValueError( | ||
f"Invalid boolean argument: {{raw_value}}. Value must be one of: {{','.join(TRUE_VALUES + FALSE_VALUES)}}" | ||
) | ||
|
||
def bytes_argument_type(raw_value: str) -> str: | ||
return raw_value | ||
|
||
def get_abi_json(abi_name: str) -> List[Dict[str, Any]]: | ||
abi_full_path = os.path.join(BUILD_DIRECTORY, f"{{abi_name}}.json") | ||
if not os.path.isfile(abi_full_path): | ||
raise IOError( | ||
f"File does not exist: {{abi_full_path}}. Maybe you have to compile the smart contracts?" | ||
) | ||
|
||
with open(abi_full_path, "r") as ifp: | ||
build = json.load(ifp) | ||
|
||
abi_json = build.get("abi") | ||
if abi_json is None: | ||
raise ValueError(f"Could not find ABI definition in: {{abi_full_path}}") | ||
|
||
return abi_json | ||
|
||
|
||
def contract_from_build(abi_name: str) -> ContractContainer: | ||
# This is workaround because brownie currently doesn't support loading the same project multiple | ||
# times. This causes problems when using multiple contracts from the same project in the same | ||
# python project. | ||
PROJECT = project.main.Project("moonworm", Path(PROJECT_DIRECTORY)) | ||
|
||
abi_full_path = os.path.join(BUILD_DIRECTORY, f"{{abi_name}}.json") | ||
if not os.path.isfile(abi_full_path): | ||
raise IOError( | ||
f"File does not exist: {{abi_full_path}}. Maybe you have to compile the smart contracts?" | ||
) | ||
|
||
with open(abi_full_path, "r") as ifp: | ||
foundry_build = json.load(ifp) | ||
|
||
build = {{ | ||
"type": "contract", | ||
"ast": foundry_build["ast"], | ||
"abi": foundry_build["abi"], | ||
"contractName": abi_name, | ||
"compiler": {{ | ||
"version": foundry_build["metadata"]["compiler"]["version"], | ||
}}, | ||
"language": foundry_build["metadata"]["language"], | ||
"bytecode": foundry_build["bytecode"]["object"], | ||
"sourceMap": foundry_build["bytecode"]["sourceMap"], | ||
"deployedBytecode": foundry_build["deployedBytecode"]["object"], | ||
"deployedSourceMap": foundry_build["deployedBytecode"]["sourceMap"], | ||
"pcMap": {{}}, | ||
}} | ||
|
||
return ContractContainer(PROJECT, build) | ||
|
||
|
||
{contract_body} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
MOONWORM_VERSION = "0.7.2" | ||
MOONWORM_VERSION = "0.8.0" |