Cadence Boilerplate Generation
Introduction
Flow CLI now includes a feature to automatically generate boilerplate code for contracts, transactions, and scripts. This feature enhances the development experience by simplifying the initial setup of various components in Flow.
Generate Contract
To create a new contract with basic structure, use the contract
command. It creates a new Cadence file with a template contract definition.
Usage Example
This command creates a file cadence/contracts/HelloWorld.cdc
with the following content:
Generate Transaction
For initializing a transaction, use the transaction
command. It sets up a new Cadence file with a template transaction structure.
Usage Example
This command creates a file cadence/transactions/SayHello.cdc
with the following content:
Generate Script
Similarly, to start a new script, the script
command generates a Cadence file with a basic script structure.
Usage Example
This command creates a file cadence/scripts/ReadHello.cdc
with the following content:
Optional --dir
Flag
The --dir
flag is an optional feature in the Flow CLI generate
commands, allowing you to specify a custom directory for the generated contract, transaction, or script files. If this flag is not provided, the CLI adheres to the recommended project setup:
-
Contracts are generated in the
cadence/contracts
directory. -
Transactions are generated in the
cadence/transactions
directory. -
Scripts are generated in the
cadence/scripts
directory. -
Usage:
--dir=<directory_name>
-
Example:
flow generate contract HelloWorld --dir=custom_contracts
Use the --dir
flag only if your project requires a different organizational structure than the default.