crytic-compile

0.3.11 · active · verified Thu Apr 16

crytic-compile is an abstraction layer for smart contract build systems, facilitating the compilation of Solidity and Vyper projects across various environments. It supports direct `solc` compilation as well as frameworks like Foundry, Hardhat, Truffle, and fetching verified contracts from block explorers like Etherscan and Sourcify. The library is actively maintained by Trail of Bits and receives frequent minor updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to compile a local Solidity project using `crytic-compile`. It first creates a dummy Foundry project structure and then calls `compile_all` to process it. The output provides access to various compilation artifacts like contract names from the detected platform. An commented example for Etherscan compilation is also provided.

import os
from crytic_compile import compile_all

# Assuming a simple Solidity project in 'my_contract_project/'
# with a 'contracts/MyContract.sol' file.
# For Foundry, ensure foundry.toml is present.

project_path = './my_contract_project'
if not os.path.exists(project_path):
    os.makedirs(os.path.join(project_path, 'contracts'), exist_ok=True)
    with open(os.path.join(project_path, 'contracts', 'MyContract.sol'), 'w') as f:
        f.write("""
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyContract {
    uint public value;

    constructor(uint _value) {
        value = _value;
    }

    function setValue(uint _newValue) public {
        value = _newValue;
    }
}
""")
    # Simulate a foundry.toml for automatic detection
    with open(os.path.join(project_path, 'foundry.toml'), 'w') as f:
        f.write("[profile.default]\nsolc = '0.8.19'\n")

print(f"Compiling project at: {project_path}")
try:
    compilations = compile_all(project_path)
    for compilation_unit in compilations:
        print(f"  Compiled project type: {compilation_unit.platform.NAME}")
        for source_unit in compilation_unit.source_units.values():
            for contract_name in source_unit.contracts_names:
                print(f"    Contract: {contract_name}")
                # Access ABI, bytecode, etc.
                # abi = source_unit.abis[contract_name]
                # bytecode = source_unit.bytecode_runtime(contract_name)
                # print(f"      ABI: {abi[:50]}...")
                # print(f"      Bytecode: {bytecode[:50]}...")
except Exception as e:
    print(f"Error during compilation: {e}")

# Example of direct Etherscan compilation (requires an API key for rate limits)
# For a public Etherscan contract, you can use:
# etherscan_target = "etherscan:0x514910771af9ca656af84075aa92a562ae978e2b" # LINK token
# try:
#     print(f"\nCompiling from Etherscan: {etherscan_target}")
#     etherscan_compilation = compile_all(etherscan_target)[0]
#     for source_unit in etherscan_compilation.source_units.values():
#         for contract_name in source_unit.contracts_names:
#             print(f"  Etherscan Contract: {contract_name}")
# except Exception as e:
#     print(f"Error during Etherscan compilation: {e}")

view raw JSON →