Codegen Basics
Declaring the interface for blockchain protocol is not very useful unless we have a way to interact programmatically with it.
Codgen is the process of automatically generating code of that can interact with your Tx3 protocol in a particular way and in a particular general-purpose language.
Common Uses
Codegen could potentially be used for anything since it’s based on open templates. Here are a few categories of things that could be useful:
- client libs: generate bindings, glue code that allows you to execute the transaction building logic by calling language-specific functions that represent the templates in your protocol.
- servers: generate backend servers that wrap your protocol as an API service that hides all of the internal implementation details.
Supported Languages
Tx3 bindgen feature can currently generate code in the following target languages:
- Typescript
- Rust
- Go
- Python
We plan on adding more languages as we move forward.
Configuration
The codegen configuration lives inside the trix.toml file. You need to provide an entry for each language target. Here’s an example of the config that targets both Typescript and Rust:
[[bindings]]plugin = "ts-client"options = { standalone = true }
[[bindings]]plugin = "rust-client"output_dir = "./gen/rust"Each bindings entry has the following schema:
plugin: keyword indicating the target language. Valid values aretypescript,rust,go&python.output_dir: root location where the generated code artifacts will be output to.options: target-specific options that allows you to customize the output.
Code generation procedure
To run the code generation process, execute the following trix command from your CLI:
trix codegenIf things went well, you should have new code artifacts in the corresponding output locations as defined in your trix.toml file.
The output of the previous configuration example would yield the following new files:
gen/└── typescript/ ├── my-protocol.ts ├── test.ts ├── package.json └── tsconfig.jsongen/└── rust/ ├── my-protocol.rs └── Cargo.tomlWe won’t get into details of how to use the ts code, you can read more about it on the NodeJS bindings reference.