* Install Py packages in dep. order, not in parallel Install Python packages in dependency order, not in parallel. * sra_client.py: Add `./setup.py clean` * Fix python package dependency ordering... ...and include a script to produce the proper ordering. * sra_client.py: reformat whitespace in doctest * contract_wrappers.py: don't auto-import wrappers This was discovered while minimizing CircleCI steps to dianose a problem with running the Launch Kit Backend in CircleCI. These classes should be imported via the zero_ex.contract_wrappers.exchange and zero_ex.contract_wrappers.erc20_token modules, respectively. We permitted importing them from just zero_ex.contract_wrappers back when they were the only wrappers we had, but now that we have so many different contracts being wrapped, this is just another list to keep manually updated (which, obviously is error prone, since it slipped through the cracks already), so it's better to just not support this type of import. * abi-gen/Py: doc contract method attributes Without this, generated documentation was not including the class members that represent the contract methods, rendering the usage unclear. * sra_client.py: disable tests in CI * abi-gen/Py: strip repeated spaces from devdoc * contract_wrappers.py: gen docs for all wrappers... ...except for the dummy tokens. * sra_client.py/test: change launch kit docker image Previously these teses were using 0xorg/launch-kit-ci, but that was a one-off thing created just for CI, back before there was a regularly maintained docker image of Launch Kit. Changed to use 0xorg/launch-kit-backend since it's regularly maintained/updated. Because the -backend image is using a different Linux distribution (Alpine), the commands used to wait for ganache startup also had to change. The tag used, 74bcc39, is provisional due to the pending Issue at https://github.com/0xProject/0x-launch-kit-backend/issues/73 . When that issue is resolved, the tag suffix on the imag name should be removed. * Migrate from Web3.py 4.x to 5.x * sra_client.py: checksum address in doctest Due to problem with launch-kit-backend, documented at https://github.com/0xProject/0x-launch-kit-backend/issues/73 , we need to checksum the makerAddress, in the order retrieved from the relayer, before filling it, otherwise Web3.py gives this error: InvalidAddress('Web3.py only accepts checksum addresses. The software that gave you this non-checksum address should be considered unsafe, please file it as a bug on their platform. Try using an ENS name instead. Or, if you must accept lower safety, use Web3.toChecksumAddress(lower_case_address).', '0x5409ed021d9299bf6814279a6a1411a7e866a631') * Update CHANGELOGs * sra_client.py: make CHANGELOG be REVESE chrono. Formerly CHANGELOG was in chronological order. Now it's in reverse chronological order. * abi-gen/Py: fix missing space in sanitized devdoc
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 @0x/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=@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 CLI output against a set of dummy contracts.
Compile dummy contracts and generate wrappers:
yarn compile:sol
Build generated wrappers and 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}
.