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.
_11> flow generate_11Usage:_11 flow generate [command]_11_11Aliases:_11 generate, g_11_11Available Commands:_11 contract Generate a new contract_11 script Generate a new script_11 transaction Generate a new transaction
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.
_10flow generate contract [ContractName]
Usage Example​
_10> flow generate contract HelloWorld
This command creates a file cadence/contracts/HelloWorld.cdc
with the following content:
_10access(all) contract HelloWorld {_10 init() {}_10}
Generate Transaction​
For initializing a transaction, use the transaction
command. It sets up a new Cadence file with a template transaction structure.
_10flow generate transaction [TransactionName]
Usage Example​
_10> flow generate transaction SayHello
This command creates a file cadence/transactions/SayHello.cdc
with the following content:
_10transaction() {_10 prepare() {}_10_10 execute {}_10}
Generate Script​
Similarly, to start a new script, the script
command generates a Cadence file with a basic script structure.
_10flow generate script [ScriptName]
Usage Example​
_10> flow generate script ReadHello
This command creates a file cadence/scripts/ReadHello.cdc
with the following content:
_10access(all) fun main() {}
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.