diff --git a/hydra/garaga/starknet/cli/smart_contract_project.py b/hydra/garaga/starknet/cli/smart_contract_project.py index 50023291..b79dc37f 100644 --- a/hydra/garaga/starknet/cli/smart_contract_project.py +++ b/hydra/garaga/starknet/cli/smart_contract_project.py @@ -9,6 +9,7 @@ from starknet_py.hash.sierra_class_hash import compute_sierra_class_hash from starknet_py.net.account.account import Account +from garaga.starknet.cli.utils import get_sierra_casm_artifacts from garaga.starknet.groth16_contract_generator.calldata import ( groth16_calldata_from_vk_and_proof, ) @@ -16,7 +17,6 @@ Groth16Proof, Groth16VerifyingKey, ) -from tests.contracts_e2e.contracts_test_utils import get_sierra_casm_artifacts class EmptyContract(Exception): diff --git a/hydra/garaga/starknet/cli/utils.py b/hydra/garaga/starknet/cli/utils.py index 7b1f1d5b..c5d03e77 100644 --- a/hydra/garaga/starknet/cli/utils.py +++ b/hydra/garaga/starknet/cli/utils.py @@ -1,5 +1,9 @@ import asyncio +import glob +import json import os +import shutil +import subprocess from enum import Enum import rich @@ -94,3 +98,70 @@ def voyager_link_tx(network: Network, tx_hash: int) -> str: def voyager_link_class(network: Network, class_hash: int) -> str: voyager_prefix = get_voyager_network_prefix(network) return f"https://{voyager_prefix}voyager.online/class/{hex(class_hash)}" + + +def scarb_build_contract_folder(contract_folder_path: str): + cmd = "scarb build" + print(f"Running command: {cmd} in directory: {contract_folder_path}") + try: + subprocess.run( + cmd, + cwd=contract_folder_path, + check=True, + shell=True, + capture_output=True, + text=True, + ) + except subprocess.CalledProcessError as e: + print(f"Error building contract: {e}") + print(f"Command output:\n{e.output}") + raise + except FileNotFoundError as e: + print(f"Error: {e}") + raise + except Exception as e: + print(f"An unexpected error occurred: {e}") + raise + + +def get_sierra_casm_artifacts( + contract_folder_path: str, +) -> tuple[None, None] | tuple[str, str]: + """ + Get the Sierra and CASM artifacts for a contract. + """ + target_dir = os.path.join(contract_folder_path, "target/dev/") + + # Clean the target/dev/ folder if it already exists + if os.path.exists(target_dir): + shutil.rmtree(target_dir) + os.makedirs(target_dir) + + scarb_build_contract_folder(contract_folder_path) + + artifacts_file = glob.glob(os.path.join(target_dir, "*.starknet_artifacts.json")) + assert ( + len(artifacts_file) == 1 + ), "Artifacts JSON file not found or multiple files found." + + with open(artifacts_file[0], "r") as f: + artifacts = json.load(f) + + contracts = artifacts["contracts"] + if len(contracts) == 0: + return None, None + + sierra_file = contracts[0]["artifacts"]["sierra"] + casm_file = contracts[0]["artifacts"]["casm"] + + sierra_path = os.path.join(target_dir, sierra_file) + casm_path = os.path.join(target_dir, casm_file) + + assert os.path.exists(sierra_path), f"Sierra file not found: {sierra_path}" + assert os.path.exists(casm_path), f"CASM file not found: {casm_path}" + + with open(sierra_path, "r") as f: + sierra = f.read() + with open(casm_path, "r") as f: + casm = f.read() + return sierra, casm diff --git a/pyproject.toml b/pyproject.toml index 64811938..743d6332 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "maturin" [project] name = "garaga" -version = "0.13.2.1" +version = "0.13.2.2" requires-python = ">=3.10,<3.11" dependencies = [ "fastecdsa", diff --git a/tests/contracts_e2e/contracts_test_utils.py b/tests/contracts_e2e/contracts_test_utils.py deleted file mode 100644 index 496a4f1f..00000000 --- a/tests/contracts_e2e/contracts_test_utils.py +++ /dev/null @@ -1,72 +0,0 @@ -import glob -import json -import os -import shutil -import subprocess - - -def scarb_build_contract_folder(contract_folder_path: str): - cmd = "scarb build" - print(f"Running command: {cmd} in directory: {contract_folder_path}") - try: - subprocess.run( - cmd, - cwd=contract_folder_path, - check=True, - shell=True, - capture_output=True, - text=True, - ) - except subprocess.CalledProcessError as e: - print(f"Error building contract: {e}") - print(f"Command output:\n{e.output}") - raise - except FileNotFoundError as e: - print(f"Error: {e}") - raise - except Exception as e: - print(f"An unexpected error occurred: {e}") - raise - - -def get_sierra_casm_artifacts( - contract_folder_path: str, -) -> tuple[None, None] | tuple[str, str]: - """ - Get the Sierra and CASM artifacts for a contract. - """ - target_dir = os.path.join(contract_folder_path, "target/dev/") - - # Clean the target/dev/ folder if it already exists - if os.path.exists(target_dir): - shutil.rmtree(target_dir) - os.makedirs(target_dir) - - scarb_build_contract_folder(contract_folder_path) - - artifacts_file = glob.glob(os.path.join(target_dir, "*.starknet_artifacts.json")) - assert ( - len(artifacts_file) == 1 - ), "Artifacts JSON file not found or multiple files found." - - with open(artifacts_file[0], "r") as f: - artifacts = json.load(f) - - contracts = artifacts["contracts"] - if len(contracts) == 0: - return None, None - - sierra_file = contracts[0]["artifacts"]["sierra"] - casm_file = contracts[0]["artifacts"]["casm"] - - sierra_path = os.path.join(target_dir, sierra_file) - casm_path = os.path.join(target_dir, casm_file) - - assert os.path.exists(sierra_path), f"Sierra file not found: {sierra_path}" - assert os.path.exists(casm_path), f"CASM file not found: {casm_path}" - - with open(sierra_path, "r") as f: - sierra = f.read() - with open(casm_path, "r") as f: - casm = f.read() - return sierra, casm