Generating Cadence Boilerplate
The flow generate
command provides a convenient way to create boilerplate template files for common Cadence code components. This command streamlines the development process by automatically generating properly structured files with the correct syntax and organization.
Overview
_10flow generate [command]
Aliases: generate
, g
The generate command supports four main subcommands for creating 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 generating a contract, a corresponding test file will also be created automatically (unless --skip-tests
is used). For example, generating HelloWorld
contract will also create cadence/tests/HelloWorld.test.cdc
.
Flags
--dir string
- Directory to generate files in (defaults tocadence/contracts/
)--skip-tests
- Skip generating test files-h, --help
- Help for contract command
Generate Transaction
Creates a new Cadence transaction with a basic template structure.
Usage
_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 generating test files-h, --help
- Help for transaction command
Generate Script
Creates a new Cadence script with a basic template structure.
Usage
_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 generating test files-h, --help
- Help for script command
Generate Test
Creates a new Cadence test file with a basic template structure.
Usage
_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 generating a test, you can run it using flow test
. For more information about writing and running Cadence tests, see the Cadence Tests documentation.
Flags
--dir string
- Directory to generate files in (defaults tocadence/tests/
)--skip-tests
- Skip generating test files-h, --help
- Help for test command
Custom Directory Usage
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 using 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 for accelerating Flow development by providing standardized, well-structured boilerplate code for all common Cadence components.