Add Project Contracts
Generate a Contract
Create a new contract file using the Flow CLI:
_10flow generate contract Foo
This command creates cadence/contracts/Foo.cdc with a basic contract template and automatically adds it to your flow.json configuration.
Add a Contract to Configuration
If you have an existing contract file, add it to your project configuration using the CLI:
_10flow config add contract
Follow the interactive prompts:
- Contract name: Enter the contract name (e.g.,
Foo) - Contract filename: Enter the path to your contract file (e.g.,
./cadence/contracts/Foo.cdc) - Add aliases: Optionally add network aliases for dependencies
You can also use flags to specify all details at once:
_10flow config add contract \_10 --name Foo \_10 --filename ./cadence/contracts/Foo.cdc
What gets added to flow.json:
_10{_10 "contracts": {_10 "Foo": "./cadence/contracts/Foo.cdc"_10 }_10}
Configure Contract Deployment Targets
Once a contract is added to your configuration, configure deployment targets using the CLI:
_10flow config add deployment
Follow the interactive prompts:
- Network: Select the network (e.g.,
testnet,mainnet,emulator) - Account: Select the account to deploy to (e.g.,
my-testnet-account) - Contract: Select the contract to deploy (e.g.,
Foo) - Deploy more contracts: Choose
yesto add additional contracts to the same deployment
You can also use flags to specify all details:
_10flow config add deployment \_10 --network testnet \_10 --account my-testnet-account \_10 --contract Foo
What gets added to flow.json:
_10{_10 "deployments": {_10 "testnet": {_10 "my-testnet-account": ["Foo"]_10 }_10 }_10}
Add Multiple Contracts to a Deployment
To deploy multiple contracts to the same account, run the deployment configuration command multiple times or use the interactive prompt to add more contracts:
_10flow config add deployment --network testnet --account my-testnet-account --contract Bar
This adds Bar to the existing deployment:
_10{_10 "deployments": {_10 "testnet": {_10 "my-testnet-account": ["Foo", "Bar"]_10 }_10 }_10}
Remove Contracts and Deployments
Remove contracts or deployments using the CLI:
_10# Remove a contract from configuration_10flow config remove contract Foo_10_10# Remove a contract from a specific deployment_10flow config remove deployment testnet my-testnet-account Foo
Best Practices
- Use CLI commands: Always use
flow config addandflow config removeinstead of manually editingflow.json - Generate contracts: Use
flow generate contractto create new contracts with proper structure - Verify configuration: Use
flow accounts listand check yourflow.jsonto verify your configuration - Network-specific deployments: Configure separate deployments for each network (emulator, testnet, mainnet)
For more information, see Manage Configuration and Production Deployment.