Basic Usage¶
Learn the fundamentals of working with Zig EVM.
EVM Lifecycle¶
Creating an Instance¶
Resetting State¶
Reset execution state while preserving account data:
Configuration¶
Gas Limit¶
Set the maximum gas for execution:
Block Context¶
Set block-related information:
Transaction Context¶
Set transaction-related information:
Account Management¶
Setting Balances¶
Setting Contract Code¶
Storage Operations¶
Executing Code¶
Basic Execution¶
Execution with Calldata¶
Handling Results¶
EVMResult Structure¶
| Field | Description |
|---|---|
success | Whether execution completed without error |
error_code | Error code (if failed) |
error_name | Human-readable error name |
gas_used | Gas consumed during execution |
gas_remaining | Gas remaining after execution |
return_data | Data returned by RETURN opcode |
reverted | Whether REVERT was called |
Checking Results¶
result = evm.execute(code)
if result.success:
print(f"Execution successful")
print(f"Gas used: {result.gas_used}")
print(f"Return data: {result.return_data.hex()}")
elif result.reverted:
print(f"Transaction reverted")
print(f"Revert reason: {result.return_data}")
else:
print(f"Execution failed: {result.error_name}")
const result = evm.execute(code);
if (result.success) {
console.log('Execution successful');
console.log(`Gas used: ${result.gasUsed}`);
console.log(`Return data: ${result.returnData.toString('hex')}`);
} else if (result.reverted) {
console.log('Transaction reverted');
console.log(`Revert reason: ${result.returnData}`);
} else {
console.log(`Execution failed: ${result.errorName}`);
}
Reading Logs¶
Access logs emitted during execution:
Debugging¶
Stack Inspection¶
Memory Inspection¶
Error Handling¶
Error Codes¶
| Code | Name | Description |
|---|---|---|
| 0 | OK | Success |
| 1 | OutOfGas | Insufficient gas |
| 2 | StackUnderflow | Pop from empty stack |
| 3 | StackOverflow | Stack exceeds 1024 items |
| 4 | InvalidOpcode | Unknown opcode |
| 5 | InvalidJump | Jump to non-JUMPDEST |
| 6 | Revert | REVERT opcode called |
| 7 | StaticCallViolation | State modification in static call |
| 8 | OutOfMemory | Memory allocation failed |
| 9 | CallDepthExceeded | Call depth > 1024 |
| 10 | InsufficientBalance | Not enough balance |
Handling Errors¶
Next Steps¶
- Architecture - Deep dive into EVM internals
- Parallel Execution - Process transactions in parallel
- API Reference - Complete API documentation