@0xproject/abi-gen 中文文档教程

发布于 3年前 浏览 31 项目主页 更新于 3年前

ABI Gen

这个包允许你从 ABI 文件生成 TypeScript 合同包装器。 它深受 Geth abigen 的启发,但需要一个不同的方法。 您可以编写您的自定义车把模板,这将使您能够根据现有约定将生成的代码无缝集成到您现有的代码库中。

这里是用于生成0x使用的合同包装器的模板。 js.e

Installation

yarn add -g @0xproject/abi-gen

Usage

abi-gen
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. Either 'web3'
                       or 'ethers'. Ethers auto-converts small ints to numbers
                       whereas Web3 doesn't.
                          [string] [choices: "web3", "ethers"] [default: "web3"]
  --network-id         ID of the network where contract ABIs are nested in
                       artifacts                          [number] [default: 50]

你需要传递一个 你的 abi 文件所在的 glob 模板。 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

输出文件将在输出文件夹中生成,其名称将转换为驼峰式大小写并取自 abi 文件名。 如果该文件夹中已有一些文件,它们将被覆盖。

Contributing

我们欢迎来自更广泛社区的改进和修复! 要报告此包中的错误,请在此存储库中创建一个问题。

请在开始之前阅读我们的贡献指南

Install dependencies

如果您没有启用 yarn workspaces (Yarn < v1.0) - 启用它们:

yarn config set workspaces-experimental true

然后安装依赖

yarn install

Build

项 要构建此包和它所依赖的所有其他 monorepo 包,请从 monorepo 根目录运行以下命令:

PKG=@0xproject/abi-gen yarn build

或持续重建关于改变:

PKG=@0xproject/abi-gen yarn watch

Clean

yarn clean

Lint

yarn lint

ABI Gen

This package allows you to generate TypeScript 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.e

Installation

yarn add -g @0xproject/abi-gen

Usage

abi-gen
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. Either 'web3'
                       or 'ethers'. Ethers auto-converts small ints to numbers
                       whereas Web3 doesn't.
                          [string] [choices: "web3", "ethers"] [default: "web3"]
  --network-id         ID of the network where contract ABIs are nested in
                       artifacts                          [number] [default: 50]

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 an output folder with names converted to camel case and taken from abi file names. If you already have some files in that folder they will be overwritten.

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

Build

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

PKG=@0xproject/abi-gen yarn build

Or continuously rebuild on change:

PKG=@0xproject/abi-gen yarn watch

Clean

yarn clean

Lint

yarn lint
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文