|
| 1 | +## AbigenJS |
| 2 | + |
| 3 | +Generate Go bindings from artifacts via abigen.wasm compiled from [Abigen](https://geth.ethereum.org/docs/tools/abigen), without requiring the Go toolchain. |
| 4 | + |
| 5 | +### Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install -g abigenjs |
| 9 | +``` |
| 10 | + |
| 11 | +This package includes: |
| 12 | + |
| 13 | +- Runtime CLI: `abigenjs` |
| 14 | +- Programmatic API: `Generator` (CommonJS module at `src/abigen/generator.cjs`) |
| 15 | + |
| 16 | +### CLI Usage |
| 17 | + |
| 18 | +```bash |
| 19 | +abigenjs [-o <outDir>] [-v <v1|v2>] [--deployable] [--abigen-path <path>] [--verbose|--quiet] [--clean] <paths to JSONs/dirs...> |
| 20 | +``` |
| 21 | + |
| 22 | +- **Defaults**: `-o generated-types/bindings`, `-v v2`. |
| 23 | +- **Inputs**: JSON files or directories (recursively scanned) containing contract artifacts or ABI-only JSON. |
| 24 | +- **Artifacts**: expected fields are `contractName`, `sourceName`, and `abi`. If `--deployable` is set, `bytecode` is also required. |
| 25 | +- **ABI-only inputs**: If a JSON file is either (a) a raw ABI array or (b) an object with an `abi` array (and optional `bytecode`), AbigenJS will infer `contractName` from the filename and use an empty `sourceName`. When `--deployable` is passed but `bytecode` is missing, non-deployable bindings will still be generated and a warning will be printed. |
| 26 | +- **abigen.wasm**: A packaged `abigen.wasm` is used by default; `--abigen-path` lets you override the path if needed. |
| 27 | +- **Quiet mode**: `--quiet` suppresses all non-error output and warnings, and overrides `--verbose`. |
| 28 | + |
| 29 | +Examples: |
| 30 | + |
| 31 | +```bash |
| 32 | +# Generate without deployable bindings from a directory of artifacts |
| 33 | +abigenjs -o ./gen -v v1 tests/mock_data |
| 34 | + |
| 35 | +# Generate with deployable bindings for a single artifact file |
| 36 | +abigenjs --deployable tests/mock_data/ERC20Mock.json |
| 37 | + |
| 38 | +# Use a custom abigen.wasm path (optional) |
| 39 | +abigenjs -o ./gen -v v1 --abigen-path ./bin/abigen.wasm tests/mock_data |
| 40 | +``` |
| 41 | + |
| 42 | +### Programmatic API |
| 43 | + |
| 44 | +```ts |
| 45 | +// ESM context |
| 46 | +import { createRequire } from "module"; |
| 47 | +const require = createRequire(import.meta.url); |
| 48 | +const Generator = require("abigenjs/dist/src/abigen/generator.cjs"); |
| 49 | + |
| 50 | +const gen = new Generator("./gen", "v1"); |
| 51 | +await gen.clean(); |
| 52 | +await gen.generate([artifact1, artifact2], /* deployable */ false, /* verbose */ false); |
| 53 | +``` |
| 54 | + |
| 55 | +### Limitations |
| 56 | + |
| 57 | +- **Environment variables are not forwarded into `abigen.wasm`**: The embedded Go WASM runtime intentionally omits passing host ENV to the binary. |
0 commit comments