@0x/abi-gen 中文文档教程
ABI Gen
这个包允许您从 ABI 文件生成 TypeScript 或 Python 合同包装器。 它深受 Geth abigen 的启发,但需要一个不同的方法。 您可以编写您的自定义车把模板,这将使您能够根据现有约定将生成的代码无缝集成到您现有的代码库中。
这里是用于生成合约包装器的模板由 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'
你需要传递一个glob< /code> a> 你的 abi 文件所在的模板。 TL;DR - 这是来自 0x.js 的示例。
--abis 'src/artifacts/@(Exchange|Token|TokenTransferProxy|EtherToken|TokenRegistry).json
我们可以直接使用 --abis 'src/artifacts/*.json< /code> 但我们想排除一些 abi 文件。
abi 文件应该是 Truffle 合约工件(带有 abi 键的 JSON 对象)或 JSON abi 数组。
您还需要指定用于每个合同的主模板的位置 --template
以及稍后可以从主模板使用的部分模板 --partials
.
How to write custom templates?
开始的最佳方法是复制 0x.js 模板 和开始根据您的需要调整它们。 我们在后台使用 handlebars 模板引擎。 您需要一个名为 contract.mustache
的主模板。 它将用于生成每个合同包装器。 虽然 - 您不需要也不应该将所有逻辑都写在一个模板文件中。 您可以编写 partial 模板,只要它们在 partials 文件夹中 - 它们就会被注册并可用。
Which data/context do I get in my templates?
现在你对 abi 方法、一些有用的帮助器和合同名称了解不多,因为这对我们的用例来说已经足够了,但如果你需要其他东西——创建一个 PR。 查看我们传递给渲染方法。
Output files
输出文件将在指定的输出文件夹中生成。 如果该文件夹中已有一些文件,它们将被覆盖。
文件、类和方法的名称将转换为给定目标语言的标准命名约定。
Contributing
我们欢迎来自更广泛社区的改进和修复! 要报告此包中的错误,请在此存储库中创建一个问题。
请在开始之前阅读我们的贡献指南。
Install dependencies
如果您没有启用 yarn workspaces (Yarn < v1.0) - 启用它们:
yarn config set workspaces-experimental true
然后安装依赖
yarn install
项 您还需要安装 Python 3 才能构建和运行命令行界面的测试,即与下面描述的 yarn build
、yarn test
和 yarn lint
命令集成。 更具体地说,您的本地 pip
应该解析为 pip
的 Python 3 版本,而不是 Python 2.x 版本。
Build
要构建此包及其依赖的所有其他 monorepo 包,请从 monorepo 根目录运行以下
PKG=@0x/abi-gen yarn build
:
PKG=@0x/abi-gen yarn watch
Clean
yarn clean
Lint
yarn lint
CLI tests
命令 通过针对一组夹具合同运行命令行工具。 这些测试与 yarn build
、yarn test
等集成,但可以按照以下说明独立运行。
编译fixture contracts:
yarn compile:sol
提前将fixture contracts编译成ABI,并将ABI签入git
,以加快测试运行时间。 因此,此编译不会作为 yarn build
的一部分自动发生,并且必须在对 fixture 契约进行任何更改后显式运行; 并且,对 fixture contracts 的任何提议更改都应伴随对相应编译工件的更改。
生成包装器(并构建它们,在 TypeScript 的情况下),并构建单元测试:
yarn test_cli:build
运行单元测试并检查生成的包装器与已知包装器的差异:
yarn test_cli
已知良好的包装器之前已经提交并保存在 test-cli /预期输出/{语言}。 它们旨在提供样本输出,并应与生成代码保持同步。 更改此项目或
@0x/abi-gen-templates
时,运行 yarn test_cli:prebuild
以将新代码生成到 test-cli/output/{ language}
,然后手动复制到test-cli/expected-output/{language}
。
针对生成的代码运行 linters:
yarn test_cli:lint
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