Merge pull request #92 from bugout-dev/storing-contract-data

Moonworm 0.4.0. `--prod`: shippable contracrts, fix of relative path to the project dir
pull/96/head v0.4.0
Yhtyyar Sahatov 2022-09-14 18:28:30 +03:00 zatwierdzone przez GitHub
commit 04d2860481
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
5 zmienionych plików z 127 dodań i 11 usunięć

Wyświetl plik

@ -100,8 +100,20 @@ def handle_brownie_generate(args: argparse.Namespace):
with open(build_file_path, "r") as ifp:
build = json.load(ifp)
relpath = os.path.relpath(project_directory, args.outdir)
splitted_relpath = [
f'"{item}"' for item in relpath.split(os.sep)
] # os.sep => '/' for unix '\' for windows subsystems
splitted_relpath_string = ",".join(splitted_relpath)
abi = build["abi"]
interface = generate_brownie_interface(abi, args.name)
interface = generate_brownie_interface(
abi,
build,
args.name,
splitted_relpath_string,
prod=args.prod,
)
write_file(interface, os.path.join(args.outdir, args.name + ".py"))
@ -381,6 +393,11 @@ def generate_argument_parser() -> argparse.ArgumentParser:
required=True,
help=f"Path to brownie project directory",
)
generate_brownie_parser.add_argument(
"--prod",
action="store_true",
help="Generate shippable python interface, in which abi and bytecode will be included inside the generated file",
)
generate_brownie_parser.set_defaults(func=handle_brownie_generate)
generate_parser = subcommands.add_parser(

Wyświetl plik

@ -12,12 +12,17 @@ from .basic import format_code, function_spec, get_constructor, make_annotation
BROWNIE_INTERFACE_TEMPLATE_PATH = os.path.join(
os.path.dirname(__file__), "brownie_contract.py.template"
)
BROWNIE_INTERFACE_PROD_TEMPLATE_PATH = os.path.join(
os.path.dirname(__file__), "brownie_contract_prod.py.template"
)
try:
with open(BROWNIE_INTERFACE_TEMPLATE_PATH, "r") as ifp:
BROWNIE_INTERFACE_TEMPLATE = ifp.read()
with open(BROWNIE_INTERFACE_PROD_TEMPLATE_PATH, "r") as ifp:
BROWNIE_INTERFACE_PROD_TEMPLATE = ifp.read()
except Exception as e:
logging.warn(
f"WARNING: Could not load cli template from {BROWNIE_INTERFACE_TEMPLATE_PATH}:"
f"WARNING: Could not load cli template from ({BROWNIE_INTERFACE_TEMPLATE_PATH})/({BROWNIE_INTERFACE_PROD_TEMPLATE_PATH}):"
)
logging.warn(e)
@ -887,7 +892,13 @@ def generate_brownie_cli(
def generate_brownie_interface(
abi: List[Dict[str, Any]], contract_name: str, cli: bool = True, format: bool = True
abi: List[Dict[str, Any]],
contract_build: Dict[str, Any],
contract_name: str,
relative_path: str,
cli: bool = True,
format: bool = True,
prod: bool = False,
) -> str:
contract_class = generate_brownie_contract_class(abi, contract_name)
module_body = [contract_class]
@ -897,11 +908,23 @@ def generate_brownie_interface(
module_body.extend(contract_cli_functions)
contract_body = cst.Module(body=module_body).code
content = BROWNIE_INTERFACE_TEMPLATE.format(
contract_body=contract_body,
moonworm_version=MOONWORM_VERSION,
)
if prod:
content = BROWNIE_INTERFACE_PROD_TEMPLATE.format(
contract_abi=abi,
contract_build={
"bytecode": contract_build["bytecode"],
"abi": contract_build["abi"],
"contractName": contract_build["contractName"],
},
contract_body=contract_body,
moonworm_version=MOONWORM_VERSION,
)
else:
content = BROWNIE_INTERFACE_TEMPLATE.format(
contract_body=contract_body,
moonworm_version=MOONWORM_VERSION,
relative_path=relative_path,
)
if format:
content = format_code(content)

Wyświetl plik

@ -12,7 +12,7 @@ from brownie.network.contract import ContractContainer
from eth_typing.evm import ChecksumAddress
PROJECT_DIRECTORY = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
PROJECT_DIRECTORY = os.path.abspath(os.path.join(os.path.dirname(__file__), {relative_path}))
BUILD_DIRECTORY = os.path.join(PROJECT_DIRECTORY, "build", "contracts")
def boolean_argument_type(raw_value: str) -> bool:
@ -66,4 +66,4 @@ def contract_from_build(abi_name: str) -> ContractContainer:
return ContractContainer(PROJECT, build)
{contract_body}
{contract_body}

Wyświetl plik

@ -0,0 +1,76 @@
# Code generated by moonworm : https://github.com/bugout-dev/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, ContractConstructor, TransactionReceiptType, ContractNotFound, _ContractBase
from eth_typing.evm import ChecksumAddress
CONTRACT_BUILD = {contract_build}
def get_abi_json(*args) -> List[Dict[str, Any]]:
return CONTRACT_BUILD["abi"]
# This is workaround to deploy new contract which has only bytecode and abi in brownie
class CustomContract(Contract):
@classmethod
def from_build_object(cls, build: Dict[str, Any]):
self = cls.__new__(cls)
self.bytecode = build["bytecode"]
_ContractBase.__init__(self, None, build, {{}}) # type: ignore
self.deploy = ContractConstructor(self, self._name)
self.addres = None
return self
def _add_from_tx(self, tx: TransactionReceiptType) -> None:
tx._confirmed.wait()
if tx.status and tx.contract_address is not None:
self.address = tx.contract_address
self.tx = tx
self._owner = tx.sender
try:
self.at(tx.contract_address, tx.sender, tx)
except ContractNotFound:
# if the contract self-destructed during deployment
pass
def at(
self,
address: str,
owner: Any = None,
tx: Optional[TransactionReceiptType] = None,
persist: bool = True,
) -> Contract:
contract = Contract.from_abi(self._name, self.address, self.abi)
contract.tx = self.tx
return contract
def contract_from_build(*args) -> CustomContract:
return CustomContract.from_build_object(CONTRACT_BUILD)
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
{contract_body}

Wyświetl plik

@ -1 +1 @@
MOONWORM_VERSION = "0.3.0"
MOONWORM_VERSION = "0.4.0"