Generating Cadence Boilerplate
The flow generate command provides a convenient way to create boilerplate template files for common Cadence code components. TO streamline the development process, this command automatically generates properly structured files with the correct syntax and organization.
Overview
_10flow generate [command]
Aliases: generate, g
The generate command supports four main subcommands to create different types of Cadence files:
- contract - Generate Cadence smart contract templates.
- script - Generate Cadence script templates.
- test - Generate Cadence test templates.
- transaction - Generate Cadence transaction templates.
Generate contract
Creates a new Cadence smart contract with a basic template structure.
Usage
_10flow generate contract <name> [flags]
Example
_10flow generate contract HelloWorld
This command creates a file cadence/contracts/HelloWorld.cdc with the following content:
_10access(all) contract HelloWorld {_10 init() {}_10}
When you generate a contract, a test file that corresponds to that contracts is also created automatically (unless --skip-tests is used). For example, when you generate HelloWorld, the contract will also create cadence/tests/HelloWorld.test.cdc.
Flags
--dir string- Directory to generate files in (defaults tocadence/contracts/).--skip-tests- Skip test file generation.-h, --help- Help for contract command.
Generate transaction
Creates a new Cadence transaction with a basic template structure.
Use
_10flow generate transaction <name> [flags]
Example
_10flow generate transaction TransferTokens
This command creates a file cadence/transactions/TransferTokens.cdc with the following content:
_10transaction() {_10 prepare() {}_10_10 execute {}_10}
Flags
--dir string- Directory to generate files in (defaults tocadence/transactions/).--skip-tests- Skip test file generation.-h, --help- Help for transaction command.
Generate script
Creates a new Cadence script with a basic template structure.
Use
_10flow generate script <name> [flags]
Example
_10flow generate script GetBalance
This command creates a file cadence/scripts/GetBalance.cdc with the following content:
_10access(all) fun main() {}
Flags
--dir string- Directory to generate files in (defaults tocadence/scripts/).--skip-tests- Skip test file generation.-h, --help- Help for script command.
Generate test
Creates a new Cadence test file with a basic template structure.
Use
_10flow generate test <name> [flags]
Example
_10flow generate test MyToken
This command creates a file cadence/tests/MyToken.test.cdc with a basic test structure.
After you generate a test, you can run it using flow test. For more information about how to write and run Cadence tests, see the Cadence Tests documentation.
Flags
--dir string- Directory to generate files in (defaults tocadence/tests/).--skip-tests- Skip test file generation.-h, --help- Help for test command.
Custom directory use
All generate commands support the --dir flag to specify a custom directory for the generated files. This is useful when your project requires a different organizational structure than the default.
Examples
_11# Generate contract in a custom directory_11flow generate contract MyToken --dir=src/contracts_11_11# Generate transaction in a custom directory _11flow generate transaction Transfer --dir=src/transactions_11_11# Generate script in a custom directory_11flow generate script GetData --dir=src/scripts_11_11# Generate test in a custom directory_11flow generate test MyToken --dir=src/tests
Project structure
When you use the default directories, the generate command creates the following structure:
_10cadence/_10├── contracts/_10│ └── MyToken.cdc_10├── scripts/_10│ └── GetBalance.cdc_10├── transactions/_10│ └── TransferTokens.cdc_10└── tests/_10 └── MyToken.test.cdc
The generate command is an essential tool to accelerate Flow development with standardized, well-structured boilerplate code for all common Cadence components.