1. Your First Contract Call
In Getting Started, we returned a hard-coded 42. Real contracts take calldata, emit logs, and sometimes revert with an error payload.
In this tutorial, you will build a 23-byte contract that:
- Reads two 32-byte integers
aandbfrom calldata. - Adds them (
a + b). - Emits a
LOG1event carrying the sum. - Returns the sum as a 32-byte word.
Then we will deliberately trigger a REVERT and an OutOfGas error so you can see the exact difference inside CallResult.
Design the bytecode before touching Zig
Here is a first draft of the adder's opening instructions:
PC Opcode Stack after (top on left) What happens
───────────────────────────────────────────────────────────────────────────
00 PUSH1 0x00 [0] offset 0
02 CALLDATALOAD [a] read calldata[0..32]
03 PUSH1 0x20 [32, a] offset 32
05 CALLDATALOAD [b, a] read calldata[32..64]
06 ADD [a+b] wrapping 256-bit add
07 DUP1 [a+b, a+b] keep a copy for later? no, store once
08 PUSH1 0x00 [0, a+b, a+b] memory offset 0
0a MSTORE [a+b] memory[0..32] = a + b
0b POP [] clean stack (or reuse as topic!)Wait — look at PC 07 and 0b! Why throw away the extra copy on the stack when LOG1 wants a topic? If we push a topic tag (say 0xdead), we can drop DUP1/POP and save 2 bytes and 5 gas!
The finished contract is 23 bytes:
00 PUSH1 0x00 # calldata offset 0
02 CALLDATALOAD # stack: [a]
03 PUSH1 0x20 # calldata offset 32
05 CALLDATALOAD # stack: [b, a]
06 ADD # stack: [sum]
07 PUSH1 0x00 # memory offset 0
09 MSTORE # memory[0..32] = sum; stack: []
0a PUSH2 0xdead # topic0 = 0xdead (event tag)
0d PUSH1 0x20 # log data length = 32 bytes
0f PUSH1 0x00 # log data offset = 0
11 LOG1 # emit log(topic0=0xdead, data=memory[0..32])
12 PUSH1 0x20 # return length = 32
14 PUSH1 0x00 # return offset = 0
16 RETURN # return memory[0..32]Watch the stack evolve when we pass a = 17 and b = 25:
Reads calldata[0..32].
Reads calldata[32..64].
Pops 25 and 17, pushes 42.
Writes 42 to mem[0..32] (+3 mem expansion).
375 base + 375 topic + 32×8 data bytes.
Returns 32-byte word 42.
Run it in Zig
const std = @import("std");
const guillotine = @import("guillotine_mini");
const voltaire = @import("voltaire");
pub fn main() !void {
const Evm = guillotine.Evm(.{});
var vm: Evm = undefined;
try vm.init(
std.heap.page_allocator,
null,
.CANCUN,
null,
voltaire.ZERO_ADDRESS,
0,
null,
);
defer vm.deinit();
const target = voltaire.Address.fromU256(0x100);
const bytecode = [_]u8{
0x60, 0x00, // PUSH1 0
0x35, // CALLDATALOAD
0x60, 0x20, // PUSH1 32
0x35, // CALLDATALOAD
0x01, // ADD
0x60, 0x00, // PUSH1 0
0x52, // MSTORE
0x61, 0xde, 0xad, // PUSH2 0xdead (topic0)
0x60, 0x20, // PUSH1 32 (log size)
0x60, 0x00, // PUSH1 0 (log offset)
0xa1, // LOG1
0x60, 0x20, // PUSH1 32 (return size)
0x60, 0x00, // PUSH1 0 (return offset)
0xf3, // RETURN
};
try vm.code.put(target, &bytecode);
// Pack two big-endian 32-byte words: a = 17, b = 25
var input: [64]u8 = undefined;
std.mem.writeInt(u256, input[0..32], 17, .big);
std.mem.writeInt(u256, input[32..64], 25, .big);
const result = vm.call(.{ .call = .{
.caller = voltaire.ZERO_ADDRESS,
.to = target,
.value = 0,
.input = &input,
.gas = 100_000,
} });
std.debug.assert(result.success);
const sum = std.mem.readInt(u256, result.output[0..32], .big);
std.debug.print("sum={d}, logs={d}, gas_used={d}\n", .{
sum,
result.logs.len,
result.gasConsumed(100_000),
});
std.debug.print("log[0].topic0=0x{x}\n", .{result.logs[0].topics[0]});
}Now compare REVERT vs CallError
Swap the final opcode 0xf3 (RETURN) for 0xfd (REVERT) and run again. Look at what changes in CallResult:
| Field | RETURN (0xf3) | REVERT (0xfd) | OutOfGas / InvalidJump |
|---|---|---|---|
result.success | true | false | false |
result.gas_left | unspent gas | unspent gas | 0 (all gas burned) |
result.output | return bytes (42) | revert payload (42) | empty (&.{}) |
result.logs | 1 log (0xdead) | empty (logs rolled back!) | empty |
result.error_info | null | null | error name (e.g. "OutOfGas") |
Next
In Tutorial 2: State, Storage & Snapshots, we add persistent storage (SSTORE/SLOAD), see which seeded state survives vm.call(), and watch copy-on-write snapshots undo a reverted nested call.