|
| 1 | +# TON Assembly Library API |
| 2 | + |
| 3 | +This document provides a brief overview of the main functions and types available when using `ton-assembly` as a |
| 4 | +library. |
| 5 | + |
| 6 | +The library allows for a full assembly/disassembly cycle: |
| 7 | +`string` -> `AST (Instr[])` -> `Cell` -> `Buffer (BOC)` -> `Cell` -> `AST (Instr[])` -> `string` |
| 8 | + |
| 9 | +## Core Modules |
| 10 | + |
| 11 | +The main functionality is exported through several modules. |
| 12 | + |
| 13 | +```typescript |
| 14 | +import {text, runtime, logs, trace} from "ton-assembly" |
| 15 | +``` |
| 16 | + |
| 17 | +## `text` Module: Parsing and Printing |
| 18 | + |
| 19 | +This module handles the conversion between assembly source code (`.tasm`) and its representation (`Instr[]`). |
| 20 | + |
| 21 | +- **`parse(filepath: string, code: string): ParseResult`** |
| 22 | + |
| 23 | + Parses a string of assembly code into an array of `Instr` objects. |
| 24 | + |
| 25 | + - `filepath`: A path for the file, used for error reporting. |
| 26 | + - `code`: The assembly code to parse. |
| 27 | + - **Returns:** A `ParseResult` object, which is either a `ParseSuccess` containing `instructions: Instr[]` or a |
| 28 | + `ParseFailure` with error details. |
| 29 | + |
| 30 | + **Example:** |
| 31 | + |
| 32 | + ```typescript |
| 33 | + const code = "PUSH s0" |
| 34 | + const result = text.parse("<code>", code) |
| 35 | + if (result.$ === "ParseSuccess") { |
| 36 | + const instructions = result.instructions |
| 37 | + // ... |
| 38 | + } |
| 39 | + ``` |
| 40 | + |
| 41 | +- **`print(instructions: Instr[]): string`** |
| 42 | + |
| 43 | + Converts an array of `Instr` objects back into a formatted assembly code string. |
| 44 | + |
| 45 | + **Example:** |
| 46 | + |
| 47 | + ```typescript |
| 48 | + const asmString = text.print(instructions) |
| 49 | + console.log(asmString) |
| 50 | + ``` |
| 51 | + |
| 52 | +## `runtime` Module: Compilation and Decompilation |
| 53 | + |
| 54 | +This module handles the conversion between the AST representation (`Instr[]`) and TVM's binary cell format. |
| 55 | + |
| 56 | +- **`compileCell(instructions: Instr[]): Cell`** |
| 57 | + |
| 58 | + Compiles an array of `Instr` objects into a single root `Cell`. |
| 59 | + |
| 60 | +- **`compile(instructions: Instr[]): Buffer`** |
| 61 | + |
| 62 | + A convenience wrapper around `compileCell` that returns the compiled cell as a BOC `Buffer`. |
| 63 | + |
| 64 | + **Example:** |
| 65 | + |
| 66 | + ```typescript |
| 67 | + const bocBuffer = runtime.compile(instructions) |
| 68 | + ``` |
| 69 | + |
| 70 | +- **`compileCellWithMapping(instructions: Instr[]): [Cell, Mapping]`** |
| 71 | + |
| 72 | + Compiles an array of `Instr` objects into a `Cell` along with mapping information for debugging and tracing. |
| 73 | + |
| 74 | + **Returns:** A tuple containing the compiled `Cell` and `Mapping` information that links cell offsets to source |
| 75 | + locations. |
| 76 | + |
| 77 | + **Example:** |
| 78 | + |
| 79 | + ```typescript |
| 80 | + const [cell, mapping] = runtime.compileCellWithMapping(instructions) |
| 81 | + ``` |
| 82 | + |
| 83 | +- **`decompileCell(cell: Cell): Instr[]`** |
| 84 | + |
| 85 | + Decompiles a TVM `Cell` into an array of `Instr` objects. |
| 86 | + |
| 87 | +- **`decompile(buffer: Buffer): Instr[]`** |
| 88 | + |
| 89 | + A convenience wrapper that decompiles a BOC `Buffer` into an array of `Instr` objects. |
| 90 | + |
| 91 | + **Example:** |
| 92 | + |
| 93 | + ```typescript |
| 94 | + import {Cell} from "@ton/core" |
| 95 | +
|
| 96 | + const cell = Cell.fromBoc(bocBuffer)[0] |
| 97 | + const decompiledInstr = runtime.decompileCell(cell) |
| 98 | + ``` |
| 99 | + |
| 100 | +## `logs` Module: Parsing TVM Execution Logs |
| 101 | + |
| 102 | +This module provides tools to parse the verbose execution logs generated by TVM emulators like `ton-sandbox`. This is |
| 103 | +extremely useful for debugging smart contracts. |
| 104 | + |
| 105 | +- **`parse(log: string): VmLine[]`** |
| 106 | + |
| 107 | + Parses a multi-line string containing the TVM execution log into a structured array of `VmLine` objects. Each object |
| 108 | + in the array represents a parsed line from the log. |
| 109 | + |
| 110 | + **VmLine Types:** |
| 111 | + |
| 112 | + - `VmLoc`: Current code location (`{ hash, offset }`). |
| 113 | + - `VmStack`: The state of the stack (`{ stack }`). |
| 114 | + - `VmExecute`: The instruction being executed (`{ instr }`). |
| 115 | + - `VmException`: An exception that occurred (`{ errno, message }`). |
| 116 | + - And others like `VmGasRemaining`, `VmFinalC5`, etc. |
| 117 | + |
| 118 | + **Example:** |
| 119 | + |
| 120 | + ```typescript |
| 121 | + import {logs} from "ton-assembly" |
| 122 | +
|
| 123 | + const executionLog = ` |
| 124 | + code cell hash:6DB0B8EFEF2B59D53B896E2A6EBCBBEF72BE9A1F8CD2DA1D0E8EA8F57C4F8AE0 offset:2608 |
| 125 | + stack: [98 100 0 101] |
| 126 | + execute PUSHINT 0 |
| 127 | + gas remaining: 999999998 |
| 128 | + changing gas limit to 100 |
| 129 | + handling exception code 2: stack underflow |
| 130 | + default exception handler, terminating vm with exit code 2 |
| 131 | + final c5:C{00000000} |
| 132 | + ` |
| 133 | +
|
| 134 | + const parsedLog = logs.parse(executionLog) |
| 135 | +
|
| 136 | + for (const line of parsedLog) { |
| 137 | + if (line.$ === "VmStack") { |
| 138 | + console.log("Stack contents:", line.stack) |
| 139 | + } |
| 140 | + if (line.$ === "VmExecute") { |
| 141 | + console.log("Executing:", line.instr) |
| 142 | + } |
| 143 | + } |
| 144 | + ``` |
| 145 | + |
| 146 | +## `trace` Module: Correlating Logs with Source Code |
| 147 | + |
| 148 | +This module connects the parsed logs with the source code, enabling step-by-step debugging. It uses the `MappingInfo` |
| 149 | +generated during compilation to link each executed instruction back to its original location in the `.tasm` file. |
| 150 | + |
| 151 | +- **`createMappingInfo(mapping: Mapping): MappingInfo`** |
| 152 | + |
| 153 | + Creates mapping information from the compilation mapping data. |
| 154 | + |
| 155 | + - `mapping`: The `Mapping` object obtained from `compileCellWithMapping` function. |
| 156 | + - **Returns:** A `MappingInfo` object that can be used for tracing. |
| 157 | + |
| 158 | + **Example:** |
| 159 | + |
| 160 | + ```typescript |
| 161 | + import {runtime, trace} from "ton-assembly" |
| 162 | +
|
| 163 | + const [cell, mapping] = runtime.compileCellWithMapping(instructions) |
| 164 | + const mappingInfo = trace.createMappingInfo(mapping) |
| 165 | + ``` |
| 166 | + |
| 167 | +- **`createTraceInfo(logs: string, mapping: MappingInfo, funcMapping?: FuncMapping): TraceInfo`** |
| 168 | + |
| 169 | + Creates a detailed execution trace. |
| 170 | + |
| 171 | + - `logs`: The raw execution log string. |
| 172 | + - `mapping`: The `MappingInfo` object obtained from the `createMappingInfo` function. |
| 173 | + - `funcMapping`: (Optional) Mapping information for FunC functions, if applicable. |
| 174 | + - **Returns:** A `TraceInfo` object containing an array of `Step` objects. Each `Step` includes the source |
| 175 | + location (`loc`), instruction name, stack state, and gas information. |
| 176 | + |
| 177 | + **Example:** |
| 178 | + |
| 179 | + ```typescript |
| 180 | + import {runtime, trace} from "ton-assembly" |
| 181 | +
|
| 182 | + // 1. Compile with mapping enabled |
| 183 | + const [cell, mapping] = runtime.compileCellWithMapping(instructions) |
| 184 | + const mappingInfo = trace.createMappingInfo(mapping) |
| 185 | +
|
| 186 | + // 2. Get execution logs (from sandbox or other emulator) |
| 187 | + const logs = getExecutionLogs() |
| 188 | +
|
| 189 | + // 3. Create the trace |
| 190 | + const traceInfo = trace.createTraceInfo(logs, mappingInfo) |
| 191 | +
|
| 192 | + // 4. Analyze the trace |
| 193 | + for (const step of traceInfo.steps) { |
| 194 | + if (step.loc) { |
| 195 | + console.log(`[${step.loc.file}:${step.loc.line}:${step.loc.col}]`, step.instructionName) |
| 196 | + } |
| 197 | + } |
| 198 | + ``` |
| 199 | + |
| 200 | +- **`createTraceInfoPerTransaction(logs: string, mapping: MappingInfo, funcMapping?: FuncMapping): TraceInfo[]`** |
| 201 | + |
| 202 | + Creates detailed execution traces per transaction. |
| 203 | + |
| 204 | + - `logs`: The raw execution log string containing multiple transactions. |
| 205 | + - `mapping`: The `MappingInfo` object. |
| 206 | + - `funcMapping`: (Optional) Mapping information for FunC functions. |
| 207 | + - **Returns:** An array of `TraceInfo` objects, one for each transaction found in the logs. |
| 208 | + |
| 209 | + **Example:** |
| 210 | + |
| 211 | + ```typescript |
| 212 | + import {trace} from "ton-assembly" |
| 213 | +
|
| 214 | + const traceInfos = trace.createTraceInfoPerTransaction(logs, mappingInfo) |
| 215 | +
|
| 216 | + traceInfos.forEach((transaction, i) => { |
| 217 | + console.log(`Transaction ${i}:`) |
| 218 | + transaction.steps.forEach(step => { |
| 219 | + console.log(` - ${step.instructionName} (gas: ${step.gasCost})`) |
| 220 | + }) |
| 221 | + }) |
| 222 | + ``` |
0 commit comments