### Get Hexadecimal Representation of BlockHash Source: https://context7.com/bobthebuidler/evmspec/llms.txt Shows how to obtain the hexadecimal string representation of a BlockHash object. This is a standard way to display block identifiers. ```python from evmspec.data import BlockHash block_hash = BlockHash("0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890") print(block_hash.hex()) # Output: 0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890 ``` -------------------------------- ### Create and Decode Transaction Receipts (Python) Source: https://context7.com/bobthebuidler/evmspec/llms.txt Demonstrates how to create and decode TransactionReceipt and FullTransactionReceipt objects. It covers status interpretation, log creation, and accessing network-specific fields for Optimism and Arbitrum. ```python from evmspec.structs.receipt import TransactionReceipt, FullTransactionReceipt, Status from evmspec.data import TransactionHash, BlockNumber, Wei, Address from evmspec.data._ids import TransactionIndex from msgspec import Raw import json # Status enum from hex print(Status("0x1")) # Output: Status.success print(Status("0x0")) # Output: Status.failure # Create a transaction receipt with logs logs_data = [{ "address": "0x" + "11" * 20, "topics": ["0x" + "22" * 32], "data": "0x", "removed": False, "blockNumber": "0x1", "transactionHash": "0x" + "33" * 32, "logIndex": "0x0", "transactionIndex": "0x0", }] receipt = TransactionReceipt( transactionHash=TransactionHash("0x" + "33" * 32), blockNumber=BlockNumber(12345678), contractAddress=None, # None for non-contract-creation txs transactionIndex=TransactionIndex(5), status=Status.success, gasUsed=Wei(21000), cumulativeGasUsed=Wei(500000), _logs=Raw(json.dumps(logs_data).encode()), ) print(receipt.status) # Output: Status.success print(receipt.gasUsed) # Output: Wei(21000) # Logs are lazily decoded logs = receipt.logs print(len(logs)) # Output: 1 print(logs[0].address) # Output: 0x1111111111111111111111111111111111111111 # Network-specific fields (Optimism) # receipt.l1Fee, receipt.l1GasUsed, receipt.l1GasPrice, receipt.l1FeeScalar # Network-specific fields (Arbitrum) # receipt.l1BlockNumber, receipt.feeStats ``` -------------------------------- ### uint and Wei: Unsigned Integer and Ether Value Types in Python Source: https://context7.com/bobthebuidler/evmspec/llms.txt Illustrates the `uint` and `Wei` classes for handling unsigned integers and Ethereum's smallest denomination. It covers hexadecimal decoding, msgspec integration, and Ether value scaling. ```python from evmspec.data import Wei, uint, BlockNumber, Nonce, UnixTimestamp from decimal import Decimal # Create uint from hexadecimal num = uint.fromhex("0x1a") print(num) # Output: uint(26) # Decode hook for msgspec integration num = uint._decode_hook(uint, "0xff") print(num) # Output: uint(255) # Wei with scaled conversion to Ether wei_value = Wei.fromhex("0xde0b6b3a7640000") # 1 ETH in Wei print(wei_value) # Output: Wei(1000000000000000000) print(wei_value.scaled) # Output: Decimal('1') # Specialized uint types block_num = BlockNumber.fromhex("0x1234") print(block_num) # Output: BlockNumber(4660) nonce = Nonce.fromhex("0xa") print(nonce) # Output: Nonce(10) # UnixTimestamp with datetime conversion timestamp = UnixTimestamp(1638316800) print(timestamp.datetime) # Output: datetime.datetime(2021, 12, 1, 0, 0, tzinfo=datetime.timezone.utc) ``` -------------------------------- ### Interpret Event Logs and Topics (Python) Source: https://context7.com/bobthebuidler/evmspec/llms.txt Explains how to use Log, Topic, and Data classes for interpreting Ethereum event logs. It covers decoding data into addresses and unsigned integers, and constructing full log structures. ```python from evmspec.structs.log import Log, TinyLog, SmallLog, FullLog, Topic, Data from evmspec.data import Address, TransactionHash, BlockNumber from evmspec.data._ids import LogIndex, TransactionIndex # Data interpretation utilities data = Data("0x000000000000000000000000000000000000000000000000000000000000000a") print(data.as_uint) # Output: uint(10) print(data.as_uint256) # Output: uint256(10) # Address from padded data addr_data = Data("0x000000000000000000000000" + "11" * 20) print(addr_data.as_address) # Output: 0x1111111111111111111111111111111111111111 # Topic (32-byte indexed parameter) topic = Topic(bytes.fromhex("00" * 12 + "22" * 20)) print(topic.as_address) # Interpret as address print(topic.as_uint256) # Interpret as uint256 # Full log structure log = Log( topics=(Topic(bytes.fromhex("ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")),), # Transfer event address=Address("0x" + "11" * 20), data=Data("0x" + "00" * 31 + "0a"), # amount = 10 removed=False, blockNumber=BlockNumber(12345678), transactionHash=TransactionHash("0x" + "33" * 32), logIndex=LogIndex(0), transactionIndex=TransactionIndex(5), ) print(log.topic0.hex()) # Transfer event signature print(log.address) # Token contract address print(log.data.as_uint) # Transfer amount: uint(10) print(log.block) # Shorthand for blockNumber ``` -------------------------------- ### Create ErigonBlockHeader in Python Source: https://context7.com/bobthebuidler/evmspec/llms.txt Illustrates the creation of an 'ErigonBlockHeader' object, representing block headers from the Erigon Ethereum client. It shows how to instantiate the header with various fields like timestamp, parent hash, coinbase address, and difficulty, using types from evmspec and faster_hexbytes. ```python from evmspec.structs.header import ErigonBlockHeader from evmspec.data import Address, UnixTimestamp, uint from faster_hexbytes import HexBytes # Create an Erigon block header header = ErigonBlockHeader( timestamp=UnixTimestamp(1638316800), parentHash=HexBytes("0x" + "aa" * 32), uncleHash=HexBytes("0x" + "bb" * 32), coinbase=Address("0x" + "11" * 20), root=HexBytes("0x" + "cc" * 32), difficulty=uint(1000000), ) print(header.timestamp) # Output: UnixTimestamp(1638316800) print(header.timestamp.datetime) # datetime.datetime(2021, 12, 1, 0, 0, tzinfo=...) print(header.coinbase) # Miner address print(header.difficulty) # Block difficulty print(header.parentHash.hex()) # Parent block hash ``` -------------------------------- ### Understand Transaction Trace Types (Python) Source: https://context7.com/bobthebuidler/evmspec/llms.txt Introduces the FilterTrace structure for Parity-style transaction traces, including call, create, reward, and suicide actions. It shows how to interpret different call types and construct trace actions. ```python from evmspec.structs.trace import FilterTrace, call, create, reward, suicide from evmspec.structs.trace.call import Trace as CallTrace, Action, Result, Type from evmspec.structs.trace._base import _FilterTraceBase from evmspec.data import Address, TransactionHash, BlockHash, BlockNumber, Wei, uint from faster_hexbytes import HexBytes from msgspec import Raw import json # Call types print(Type.call) # Output: print(Type.delegatecall) # Output: print(Type.staticcall) # Output: # Call trace action action_data = { "callType": "call", "from": "0x" + "11" * 20, "to": "0x" + "22" * 20, "value": "0xde0b6b3a7640000", # 1 ETH "gas": "0x5208", "input": "0xa9059cbb", # transfer function selector } result_data = { "gasUsed": "0x5208", "output": "0x0000000000000000000000000000000000000000000000000000000000000001", } # Create a call trace (simulated - normally decoded from JSON-RPC) # trace: FilterTrace can be call.Trace, create.Trace, reward.Trace, or suicide.Trace # The trace type is determined by the "type" field in the JSON response ``` -------------------------------- ### Async Receipt and Log Fetching Source: https://context7.com/bobthebuidler/evmspec/llms.txt Illustrates asynchronous fetching of transaction receipts and logs using a TransactionHash object. This functionality requires the 'dank_mids' library. ```python # Requires dank_mids # from evmspec.data import TransactionHash, TransactionReceipt # tx_hash = TransactionHash("0x...") # receipt = await tx_hash.get_receipt(TransactionReceipt) # logs = await tx_hash.get_logs() ``` -------------------------------- ### Ethereum Block Structures: TinyBlock and Block Source: https://context7.com/bobthebuidler/evmspec/llms.txt Demonstrates the usage of `TinyBlock` and `Block` classes for representing Ethereum blocks. `TinyBlock` is optimized for minimal fields, while `Block` includes all standard fields. Transactions are lazily decoded. ```python from evmspec.structs.block import TinyBlock, Block from evmspec.data import UnixTimestamp, BlockNumber, BlockHash, Address, Wei, Nonce from msgspec import Raw from faster_hexbytes import HexBytes import json # TinyBlock with transaction hashes tx_hashes = ["0x" + "22" * 32] raw_txs = Raw(json.dumps(tx_hashes).encode()) tiny_block = TinyBlock(timestamp=UnixTimestamp(1638316800), _transactions=raw_txs) print(tiny_block.timestamp) # Output: UnixTimestamp(1638316800) print(tiny_block.timestamp.datetime) # Output: datetime.datetime(2021, 12, 1, 0, 0, tzinfo=...) txs = tiny_block.transactions # Lazily decoded print(len(txs)) # Output: 1 # Full block with all standard fields block = Block( timestamp=UnixTimestamp(1638316800), _transactions=Raw(b"[]"), number=BlockNumber(12345678), hash=BlockHash("0x" + "33" * 32), logsBloom=HexBytes("0x00"), receiptsRoot=HexBytes("0x" + "aa" * 32), extraData=HexBytes("0x"), nonce=Nonce(0), miner=Address("0x" + "11" * 20), gasLimit=Wei(30000000), gasUsed=Wei(21000), uncles=(), sha3Uncles=HexBytes("0x" + "bb" * 32), size=uint(1000), transactionsRoot=HexBytes("0x" + "cc" * 32), stateRoot=HexBytes("0x" + "dd" * 32), mixHash=HexBytes("0x" + "ee" * 32), parentHash=HexBytes("0x" + "ff" * 32), ) print(block.number) # Output: BlockNumber(12345678) print(block.miner) # Output: 0x1111111111111111111111111111111111111111 ``` -------------------------------- ### Decode Ethereum JSON-RPC Responses with msgspec in Python Source: https://context7.com/bobthebuidler/evmspec/llms.txt Demonstrates how to use the '_decode_hook' with msgspec for efficient decoding of JSON-RPC hex strings into typed Python objects. It shows creating typed decoders for 'Block' and 'TransactionReceipt' and directly decoding individual values like Address, Wei, and BlockNumber. ```python from evmspec.data import _decode_hook, Address, Wei, uint, BlockNumber from evmspec.structs.block import Block from evmspec.structs.receipt import TransactionReceipt from msgspec.json import Decoder import json # Create typed decoders with the decode hook block_decoder = Decoder(type=Block, dec_hook=_decode_hook) receipt_decoder = Decoder(type=TransactionReceipt, dec_hook=_decode_hook) # Example: Decode a block from JSON-RPC response block_json = b'''{ "timestamp": "0x61a6f400", "transactions": [], "number": "0xbc614e", "hash": "0x''' + b'33' * 32 + b'''", "logsBloom": "0x00", "receiptsRoot": "0x''' + b'aa' * 32 + b'''", "extraData": "0x", "nonce": "0x0", "miner": "0x''' + b'11' * 20 + b'''", "gasLimit": "0x1c9c380", "gasUsed": "0x5208", "uncles": [], "sha3Uncles": "0x''' + b'bb' * 32 + b'''", "size": "0x3e8", "transactionsRoot": "0x''' + b'cc' * 32 + b'''", "stateRoot": "0x''' + b'dd' * 32 + b'''", "mixHash": "0x''' + b'ee' * 32 + b'''", "parentHash": "0x''' + b'ff' * 32 + b'''" }''' # block = block_decoder.decode(block_json) # print(block.number) # BlockNumber decoded from hex # Direct use of decode hook for individual values address = _decode_hook(Address, "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae") print(address) # Output: 0xDe0B295669a9FD93d5F28D9Ec85E40f4cb697BAe wei_value = _decode_hook(Wei, "0xde0b6b3a7640000") print(wei_value) # Output: Wei(1000000000000000000) block_num = _decode_hook(BlockNumber, "0xbc614e") print(block_num) # Output: BlockNumber(12345678) ``` -------------------------------- ### HexBytes32, TransactionHash, and BlockHash in Python Source: https://context7.com/bobthebuidler/evmspec/llms.txt Demonstrates the use of `HexBytes32`, `TransactionHash`, and `BlockHash` classes for representing 32-byte hexadecimal values in Ethereum. `TransactionHash` supports optional async methods for fetching related data. ```python from evmspec.data import HexBytes32, TransactionHash, BlockHash # Create a 32-byte hash tx_hash = TransactionHash("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef") print(tx_hash.hex()) # Output: 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef ``` -------------------------------- ### Address: EIP-55 Checksummed Ethereum Addresses in Python Source: https://context7.com/bobthebuidler/evmspec/llms.txt Demonstrates the usage of the `Address` class from `evmspec.data` for handling EIP-55 checksummed Ethereum addresses. It shows address creation, checksum validation, and integration with msgspec decoders. ```python from evmspec.data import Address # Create an address (automatically checksummed) addr = Address("0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe") print(addr) # Output: 0xDe0B295669a9FD93d5F28D9Ec85E40f4cb697BAe # Use the checksum class method (with caching) addr = Address.checksum("0x52908400098527886e0f7030069857d2e4169ee7") print(addr) # Output: 0x52908400098527886E0F7030069857D2E4169EE7 # Decode hook for use with msgspec decoders addr = Address._decode_hook(Address, "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae") print(addr) # Output: 0xDe0B295669a9FD93d5F28D9Ec85E40f4cb697BAe ``` -------------------------------- ### Sized Unsigned Integers (uint8 to uint256) in Python Source: https://context7.com/bobthebuidler/evmspec/llms.txt Shows how to use sized unsigned integer types (uint8 to uint256) from the `evmspec.data.uints` module. These types enforce value constraints and are useful for interpreting EVM data with specific bit-widths. ```python from evmspec.data import uints from hexbytes import HexBytes # 8-bit unsigned integer val8 = uints.uint8(HexBytes('0x01')) print(val8) # Output: uint8(1) # 64-bit unsigned integer val64 = uints.uint64(HexBytes('0xFFFFFFFFFFFFFFFF')) print(val64) # Output: uint64(18446744073709551615) # 128-bit unsigned integer val128 = uints.uint128(HexBytes('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF')) print(val128) # Output: uint128(340282366920938463463374607431768211455) # 256-bit unsigned integer (common for EVM storage) val256 = uints.uint256(HexBytes('0x' + 'FF' * 32)) print(val256) # Output: uint256(115792089237316195423570985008687907853269984665640564039457584007913129639935) # Other bit sizes are dynamically generated (uint16, uint24, uint32, etc.) val40 = uints.uint40(HexBytes('0xFFFFFFFFFF')) print(val40) # Output: uint40(1099511627775) ``` -------------------------------- ### Strip Leading Zeros from HexBytes32 Source: https://context7.com/bobthebuidler/evmspec/llms.txt Demonstrates how to remove leading zeros from a HexBytes32 object. This is useful for cleaning up hexadecimal string representations. ```python from faster_hexbytes import HexBytes32 hb = HexBytes32("0x0000000000000000000000000000000000000000000000000000000000001234") print(hb.strip()) # Output: 1234 ``` -------------------------------- ### Define Base Trace Fields in Python Source: https://context7.com/bobthebuidler/evmspec/llms.txt Defines a dictionary 'base_trace_fields' with common fields for Ethereum trace data, including block number, hash, transaction details, and trace addresses. This structure is useful for representing and accessing trace-related information. ```python base_trace_fields = { "blockNumber": BlockNumber(12345678), "blockHash": BlockHash("0x" + "33" * 32), "transactionHash": TransactionHash("0x" + "44" * 32), "transactionPosition": 5, "traceAddress": [0, 1], # Path in the call tree "subtraces": uint(2), # Number of internal calls } ``` -------------------------------- ### Ethereum Transaction Types: Legacy, EIP-2930, EIP-1559, EIP-4844, EIP-7702 Source: https://context7.com/bobthebuidler/evmspec/llms.txt Demonstrates the creation and usage of various Ethereum transaction types supported by evmspec. This includes common fields and type-specific fields like access lists and dynamic fees. ```python from evmspec.structs.transaction import ( TransactionLegacy, Transaction2930, Transaction1559, Transaction4844, AccessListEntry ) from evmspec.data import Address, TransactionHash, BlockHash, BlockNumber, Wei, Nonce, uint from evmspec.data._ids import ChainId, TransactionIndex from faster_hexbytes import HexBytes from msgspec import Raw import json # Common transaction fields base_fields = { "input": HexBytes("0x"), "hash": TransactionHash("0x" + "33" * 32), "to": Address("0x" + "11" * 20), "gas": Wei(21000), "value": Wei(1000000000000000000), # 1 ETH "nonce": Nonce(5), "chainId": ChainId(1), "sender": Address("0x" + "22" * 20), "blockHash": BlockHash("0x" + "44" * 32), "blockNumber": BlockNumber(12345678), "transactionIndex": TransactionIndex(0), "v": uint(28), "r": HexBytes("0x1234"), "s": HexBytes("0x5678"), "gasPrice": Wei(20000000000), # 20 Gwei } # Legacy transaction (type 0x0) legacy_tx = TransactionLegacy(**base_fields) print(legacy_tx.sender) # Output: 0x2222222222222222222222222222222222222222 print(legacy_tx["from"]) # Access via dict-style (alias for sender) print(legacy_tx.block) # Shorthand for blockNumber # EIP-2930 transaction with access list (type 0x1) access_list_raw = Raw(json.dumps([ {"address": "0x" + "55" * 20, "storageKeys": ["0x" + "66" * 32]} ]).encode()) tx_2930 = Transaction2930(_accessList=access_list_raw, **base_fields) access_list = tx_2930.accessList # Lazily decoded print(len(access_list)) # Output: 1 print(access_list[0].address) # Output: 0x5555555555555555555555555555555555555555 # EIP-1559 transaction with dynamic fees (type 0x2) tx_1559 = Transaction1559( maxFeePerGas=Wei(30000000000), # 30 Gwei max maxPriorityFeePerGas=Wei(2000000000), # 2 Gwei tip **base_fields ) print(tx_1559.maxFeePerGas) # Output: Wei(30000000000) # EIP-4844 blob transaction (type 0x3) from evmspec.data import HexBytes32 tx_4844 = Transaction4844( maxFeePerGas=Wei(30000000000), maxPriorityFeePerGas=Wei(2000000000), maxFeePerBlobGas=Wei(1000000000), blobVersionedHashes=(HexBytes32("0x" + "77" * 32),), **base_fields ) print(tx_4844.maxFeePerBlobGas) # Output: Wei(1000000000) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.