TzGen Code Generator
TzGo's TzGen integration enables creating Golang struct types and smart contract interface bindings based on a contract's entrypoint specification. The generated code can be used to deploy, bootstrap, and call any chosen smart contract and interact with all of its entrypoints including views. The bindings read/decode data found in transaction parameters, transaction receipts (storage and bigmap updates), and contract storage as well as write/encode type-conform parameters for sending transactions.
TzGen (original repo: jeanschmitt/tzgen) started as an external project developed by Jean Schmitt (Ubisoft). It was released as OSS under the MIT license and is using TzGo primitives. Since TzGen is less known and unmaintained, we decided to integrate TzGen into TzGo by refactoring and extending it towards common type and naming conventions to avoid code duplication. This integration is subject to further improvement in subsequent TzGo releases.
💡 TzGen is a game changer for Tezos Go developers. When using TzGen you don't have to worry about details of Micheline or TzGo. TzGen produces all interfaces and types you'll need to call any of your contract's entrypoints and to read its storage and bigmap entries.
How to use TzGen
You can run tzgen
manually which will write an auto-generated Go source file that you can build with your project and check in to your repository.
From a deployed contract
tzgen -name <name> -pkg <pkg> -address <addr> -out <file.go>
For example, with the hDAO token contract:
tzgen -name hdao -pkg contracts -address KT1AFA2mwNUMNd4SsujE1YYp29vd8BZejyKW -out ./examples/hdao/contract.go
From a Micheline file
tzgen -name <name> -pkg <pkg> -src <file.json> -out <file.go>
For example, with a hello.json
file:
tzgen -name hello -pkg contracts -src ./hello.json -out ./examples/tzgen/hello.go
Go Generate
You can also use tzgen
in combination with the go generate
tool when you like to create fresh interface definitions at build time. To use go generate you need to do two things:
- Add a go generate comment into a Go source code file:
//go:generate go run -mod=mod github.com/trilitech/tzgo/cmd/tzgen -name <name> -pkg <pkg> -address <addr> -out <file.go>
- Call
go generate ./path-to-your-pkg-or-cmd
to make Go call whatever script you have defined in 1.
go generate ./cmd/myprog