{"library":"pyrevm","title":"pyrevm: Python bindings for revm EVM","description":"pyrevm provides Python bindings to revm, a high-performance Ethereum Virtual Machine (EVM) written in Rust. It enables Python developers to simulate Ethereum transactions, execute smart contracts, and interact with the EVM environment directly from Python, offering a fast and efficient way to prototype and test EVM-based logic. The current version is 0.3.7, and the project maintains an active development cadence, often mirroring the underlying revm library's updates.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install pyrevm"],"cli":null},"imports":["from pyrevm import EVM","from pyrevm import CfgEnv","from pyrevm import BlockEnv","from pyrevm import TxEnv"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"from pyrevm import EVM, CfgEnv, BlockEnv, TxEnv\n\n# Example setup: a simple contract and an account\n# Contract that returns 0 for a specific function call\nCONTRACT_CODE = bytes.fromhex(\"6080604052348015600f57600080fd5b506004361060285760003560e01c806306fdde0314602d575b600080fd5b603a603c565b005b60005481565b\")\nCONTRACT_ADDRESS = bytes.fromhex(\"1234567890123456789012345678901234567890\")\nCALLER_ADDRESS = bytes.fromhex(\"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd\")\n\n# Function selector for the example contract (returns 0)\nFUNCTION_SELECTOR = bytes.fromhex(\"06fdde03\")\n\ndef run_evm_simulation():\n    # 1. Initialize environment configurations\n    cfg_env = CfgEnv(chain_id=1)\n    block_env = BlockEnv(\n        number=1, timestamp=1, gas_limit=1_000_000_000,\n        miner=bytes.fromhex(\"0000000000000000000000000000000000000000\"), basefee=1\n    )\n    tx_env = TxEnv(\n        caller=CALLER_ADDRESS, gas_limit=1_000_000_000, gas_price=1, value=0,\n        data=FUNCTION_SELECTOR, transact_to=CONTRACT_ADDRESS\n    )\n\n    # 2. Create an EVM instance\n    evm = EVM(\n        cfg=cfg_env,\n        block=block_env,\n        tx=tx_env # Initial transaction environment\n    )\n\n    # 3. Insert initial state (e.g., contract code and account balances)\n    evm.insert_account_info(\n        CONTRACT_ADDRESS,\n        code=CONTRACT_CODE,\n        balance=0,\n        nonce=0\n    )\n    # Give caller some balance for transactions\n    evm.insert_account_info(\n        CALLER_ADDRESS,\n        balance=1_000_000_000_000_000_000, # 1 ETH\n        nonce=0\n    )\n\n    # 4. Perform a contract call\n    call_result = evm.call_raw() # Uses tx_env set during EVM init\n    \n    print(f\"--- Contract Call Simulation ---\")\n    print(f\"Status: {call_result.status}\")\n    print(f\"Gas Used: {call_result.gas_used}\")\n    print(f\"Exit Reason: {call_result.exit_reason}\")\n    print(f\"Output: {call_result.result.hex()}\")\n\n    # 5. Perform a simple value transfer transaction\n    recipient_address = bytes.fromhex(\"feeefeeefeeefeeefeeefeeefeeefeeefeeefeee\")\n    transfer_amount = 1000 # wei\n\n    # Update tx_env for the transaction\n    tx_env.data = b'' # No data for simple transfer\n    tx_env.transact_to = recipient_address\n    tx_env.value = transfer_amount\n\n    # Perform the transaction\n    transact_result = evm.transact_raw() # Uses updated tx_env\n    \n    print(f\"\\n--- Value Transfer Simulation ---\")\n    print(f\"Status: {transact_result.status}\")\n    print(f\"Gas Used: {transact_result.gas_used}\")\n    print(f\"Exit Reason: {transact_result.exit_reason}\")\n    print(f\"Output: {transact_result.result.hex()}\")\n\nif __name__ == \"__main__\":\n    run_evm_simulation()","lang":"python","description":"This quickstart demonstrates how to initialize an EVM instance, set up account states, and simulate both a contract call and a value transfer transaction using `pyrevm`. It requires `pyrevm` to be installed. Addresses and contract bytecode are provided as raw bytes for simplicity.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}