|
| 1 | +# Querying JSON Reports with jq |
| 2 | + |
| 3 | +This document provides practical recipes for parsing, filtering, and automating tasks using `soroban-debug` JSON output combined with the `jq` utility. |
| 4 | + |
| 5 | +By default, all JSON outputs generated by the debugger are compact for efficient piping and automation. To pretty-print JSON for local readability, you can pass the global `--pretty` flag: |
| 6 | +```bash |
| 7 | +# Compact JSON (ideal for piping) |
| 8 | +soroban-debug run --contract contract.wasm --function test --output json |
| 9 | + |
| 10 | +# Pretty-printed JSON (ideal for humans) |
| 11 | +soroban-debug run --contract contract.wasm --function test --output json --pretty |
| 12 | +``` |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## 1. Inspecting Execution Status & Result |
| 17 | + |
| 18 | +To verify whether a run succeeded and extract the return value: |
| 19 | +```bash |
| 20 | +soroban-debug run --contract contract.wasm --function my_func --output json | jq '.status' |
| 21 | +# Outputs: "success" or "error" |
| 22 | + |
| 23 | +soroban-debug run --contract contract.wasm --function my_func --output json | jq '.result.result' |
| 24 | +# Outputs the raw return value (e.g. "42") |
| 25 | +``` |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 2. Resource Budget Analysis |
| 30 | + |
| 31 | +Extract CPU instruction and memory usage from a debugger run: |
| 32 | +```bash |
| 33 | +soroban-debug run --contract contract.wasm --function my_func --output json \ |
| 34 | + | jq '.result.budget | {cpu: .cpu_instructions, mem: .memory_bytes}' |
| 35 | +``` |
| 36 | + |
| 37 | +Output: |
| 38 | +```json |
| 39 | +{ |
| 40 | + "cpu": 12450, |
| 41 | + "mem": 2048 |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## 3. Retrieving Contract Events |
| 48 | + |
| 49 | +Extract specific topics or data from emitted execution events: |
| 50 | +```bash |
| 51 | +soroban-debug run --contract contract.wasm --function my_func --output json \ |
| 52 | + | jq '.result.events[] | {topics: .topics, data: .data}' |
| 53 | +``` |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## 4. Querying Replay Diff Reports |
| 58 | + |
| 59 | +Replay an execution and inspect whether the storage state remains identical: |
| 60 | +```bash |
| 61 | +soroban-debug replay trace.json --contract contract.wasm --format json \ |
| 62 | + | jq '.result.storage_diff | {modified: .modified, unchanged: .unchanged_count}' |
| 63 | +``` |
| 64 | + |
| 65 | +Check if execution flow (call sequence) remains identical: |
| 66 | +```bash |
| 67 | +soroban-debug replay trace.json --contract contract.wasm --format json \ |
| 68 | + | jq '.result.flow_diff.identical' |
| 69 | +``` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## 5. Structured Error Handling |
| 74 | + |
| 75 | +On execution failure, you can gracefully query the error code, category, and suggestions: |
| 76 | +```bash |
| 77 | +soroban-debug run --contract contract.wasm --function my_func --output json \ |
| 78 | + | jq '.error | {code: .code, category: .category, suggestion: .suggestion}' |
| 79 | +``` |
| 80 | + |
| 81 | +Output: |
| 82 | +```json |
| 83 | +{ |
| 84 | + "code": "debugger::invalid_function", |
| 85 | + "category": "parser_failure", |
| 86 | + "suggestion": "Action: Ensure the function name is spelled exactly as exported by the contract..." |
| 87 | +} |
| 88 | +``` |
0 commit comments