Skip to main content

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


_10
flow 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


_10
flow generate contract <name> [flags]

Example


_10
flow generate contract HelloWorld

This command creates a file cadence/contracts/HelloWorld.cdc with the following content:


_10
access(all) contract HelloWorld {
_10
init() {}
_10
}

info

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 to cadence/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


_10
flow generate transaction <name> [flags]

Example


_10
flow generate transaction TransferTokens

This command creates a file cadence/transactions/TransferTokens.cdc with the following content:


_10
transaction() {
_10
prepare() {}
_10
_10
execute {}
_10
}

Flags

  • --dir string - Directory to generate files in (defaults to cadence/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


_10
flow generate script <name> [flags]

Example


_10
flow generate script GetBalance

This command creates a file cadence/scripts/GetBalance.cdc with the following content:


_10
access(all) fun main() {}

Flags

  • --dir string - Directory to generate files in (defaults to cadence/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


_10
flow generate test <name> [flags]

Example


_10
flow 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 to cadence/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
_11
flow generate contract MyToken --dir=src/contracts
_11
_11
# Generate transaction in a custom directory
_11
flow generate transaction Transfer --dir=src/transactions
_11
_11
# Generate script in a custom directory
_11
flow generate script GetData --dir=src/scripts
_11
_11
# Generate test in a custom directory
_11
flow generate test MyToken --dir=src/tests

Project Structure

When using the default directories, the generate command creates the following structure:


_10
cadence/
_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.