@0x/abi-gen

中文文档
5.8.5发布于 2023/04/08Apache-2.0TypeScriptCommonJS近 30 天下载 3358

Generate contract wrappers from ABI and handlebars templates

原始使用文档
文档目录11 节
  1. Installation
  2. Usage
  3. How to write custom templates?
  4. Which data/context do I get in my templates?
  5. Output files
  6. Contributing
  7. Install dependencies
  8. Build
  9. Clean
  10. Lint
  11. CLI tests

ABI Gen

This package allows you to generate TypeScript or Python contract wrappers from ABI files. It's heavily inspired by Geth abigen but takes a different approach. You can write your custom handlebars templates which will allow you to seamlessly integrate the generated code into your existing codebase with existing conventions.

Here are the templates used to generate the contract wrappers used by 0x.js.

Installation

yarn add -g @0x/abi-gen

Usage

$ abi-gen --help
Options:
  --help               Show help                                       [boolean]
  --version            Show version number                             [boolean]
  --abis               Glob pattern to search for ABI JSON files
                                                             [string] [required]
  --output, -o, --out  Folder where to put the output files  [string] [required]
  --partials           Glob pattern for the partial template files      [string]
  --template           Path for the main template file that will be used to
                       generate each contract                [string] [required]
  --backend            The backing Ethereum library your app uses. For
                       TypeScript, either 'web3' or 'ethers'. Ethers
                       auto-converts small ints to numbers whereas Web3 doesn't.
                       For Python, the only possibility is Web3.py
                          [string] [choices: "web3", "ethers"] [default: "web3"]
  --network-id         ID of the network where contract ABIs are nested in
                       artifacts                          [number] [default: 50]
  --language           Language of output file to generate
              [string] [choices: "TypeScript", "Python"] [default: "TypeScript"]

Examples:
  abi-gen --abis 'src/artifacts/**/*.json'  Full usage example
  --out 'src/contracts/generated/'
  --partials
  'src/templates/partials/**/*.handlebars'
  --template
  'src/templates/contract.handlebars'

You're required to pass a glob template where your abi files are located. TL;DR - here is the example from 0x.js.

--abis 'src/artifacts/@(Exchange|Token|TokenTransferProxy|EtherToken|TokenRegistry).json

We could've just used --abis 'src/artifacts/*.json but we wanted to exclude some of the abi files.

The abi file should be either a Truffle contract artifact (a JSON object with an abi key) or a JSON abi array.

You need to also specify the location of your main template used for every contract --template as well as the partial templates --partials that can later be used from the main one.

How to write custom templates?

The best way to get started is to copy 0x.js templates and start adjusting them for your needs. We use handlebars template engine under the hood. You need to have a master template called contract.mustache. it will be used to generate each contract wrapper. Although - you don't need and probably shouldn't write all your logic in a single template file. You can write partial templates and as long as they are within a partials folder - they will be registered and available.

Which data/context do I get in my templates?

For now you don't get much on top of methods abi, some useful helpers and a contract name because it was enough for our use-case, but if you need something else - create a PR. See the type definition of what we pass to the render method.

Output files

Output files will be generated within the specified output folder. If you already have some files in that folder they will be overwritten.

Names of files, classes and methods will be converted to the standard naming conventions for the given target language.

Contributing

We welcome improvements and fixes from the wider community! To report bugs within this package, please create an issue in this repository.

Please read our contribution guidelines before getting started.

Install dependencies

If you don't have yarn workspaces enabled (Yarn < v1.0) - enable them:

yarn config set workspaces-experimental true

Then install dependencies

yarn install

You will also need to have Python 3 installed in order to build and run the tests of the command-line interface, which is integrated with the yarn build, yarn test, and yarn lint commands described below. More specifically, your local pip should resolve to the Python 3 version of pip, not a Python 2.x version.

Build

To build this package and all other monorepo packages that it depends on, run the following from the monorepo root directory:

PKG=@0x/abi-gen yarn build

Or continuously rebuild on change:

PKG=@0x/abi-gen yarn watch

Clean

yarn clean

Lint

yarn lint

CLI tests

The files in test-cli/ are used to test the output generated by running the command-line tool against a set of fixture contracts. These tests are integrated with yarn build, yarn test, etc, but can be run independently per the instructions below.

Compile fixture contracts:

yarn compile:sol

Compiling the fixture contracts into ABI is done ahead of time, and the ABI is checked in to git, in order to speed up test run time. Therefore, this compilation does not happen automatically as part of yarn build, and must be run explicitly after making any changes to the fixture contracts; and, any proposed changes to the fixture contracts should be accompanied by changes to the corresponding compilation artifacts.

Generate wrappers (and build them, in the case of TypeScript), and build unit tests:

yarn test_cli:build

Run unit tests and check diffs of generated wrappers vs known wrappers:

yarn test_cli

Known-good wrappers have been previously committed and are kept in test-cli/expected-output/{language}. They are intended to provide sample output and should be kept in sync with the generating code. When making changes to this project or @0x/abi-gen-templates, run yarn test_cli:prebuild to generate fresh code into test-cli/output/{language}, and then manually copy it to test-cli/expected-output/{language}.

Run linters against generated code:

yarn test_cli:lint

版本与发布记录

@0x/abi-gen 共发布 87 个版本,首次发布于 2018/10/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.8.5latest2023/04/083 年前
5.8.42023/04/083 年前
5.8.32023/01/263 年前
5.8.22022/10/134 年前
5.8.12022/08/184 年前
5.8.02022/03/164 年前

其他发布通道:protocolV3 → 4.4.0-beta.4。

关于 @0x/abi-gen 的常见问题

@0x/abi-gen 是什么?

Generate contract wrappers from ABI and handlebars templates

如何安装 @0x/abi-gen?

在项目目录运行 npm install @0x/abi-gen,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 abi-gen,可以用 npx abi-gen 直接运行。

@0x/abi-gen 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@0x/abi-gen 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@0x/abi-gen 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6.12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@0x/abi-gen 可以商用吗?

@0x/abi-gen 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@0x/abi-gen 的下载量有多少?

根据 npm 官方下载统计,@0x/abi-gen 在 2026/10/04 – 2026/11/02 这 30 天内被下载 3,358 次,日均约 112 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@0x/abi-gen 还在维护吗?

最新版本 5.8.5 发布于 2023/04/08(3 年前);至今共发布 87 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/10/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。