Deploying your contracts
Clarinet provides comprehensive deployment tools to help you move your contracts from local development to production networks. Whether you're testing on devnet, staging on testnet, or launching on mainnet, Clarinet streamlines the entire deployment process.
Generating deployment plans
Deployment plans are YAML files that define exactly how your contracts will be deployed. They specify the order, configuration, and network details for each contract, ensuring reproducible deployments across all environments. To create a deployment plan for any network, run the following command:
$clarinet deployments generate --testnetAnalyzing contracts...Calculating deployment costs...Generating deployment planCreated file deployments/default.testnet-plan.yaml
This creates a deployment plan based on your project's configuration:
---id: 0name: Devnet deploymentnetwork: devnetstacks-node: "http://localhost:20443"bitcoin-node: "http://devnet:devnet@localhost:18443"plan:batches:- id: 0transactions:- contract-publish:contract-name: counterexpected-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGMcost: 6940path: contracts/counter.claranchor-block-only: trueclarity-version: 2epoch: "2.5"
Deployment plan structure
Key components of a deployment plan:
Field | Description |
---|---|
id | Unique identifier for the deployment |
name | Human-readable deployment name |
network | Target network (devnet/testnet/mainnet) |
stacks-node | RPC endpoint for the Stacks node |
bitcoin-node | RPC endpoint for the Bitcoin node |
plan.batches | Array of deployment batches |
Deployment specifications
Deployment behavior is configured in two places:
Project configuration (Clarinet.toml
):
- Clarity version for contracts
- Contract dependencies
- Epoch requirements
Network configuration (settings/<Network>.toml
):
- Account details and balances
- Network endpoints
- Custom deployment accounts
Example network configuration:
[network]name = "testnet"deployment_fee_rate = 10[accounts.deployer]mnemonic = "<YOUR TESTNET MNEMONIC>"balance = 100_000_000_000_000derivation = "m/44'/5757'/0'/0/0"
Working with contract requirements
Your project can reference contracts that already exist on the blockchain. This is especially useful for implementing standardized traits.
Adding requirements
Add external contract dependencies:
$clarinet requirements add SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-traitAdded requirement SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-traitUpdated Clarinet.toml
This updates your project configuration:
[project]name = "my-nft-project"requirements = [{ contract_id = "SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait" }]
When deploying, Clarinet automatically:
- Downloads the contract via the Stacks API
- Remaps the principal to a local account for devnet
- Ensures the requirement is available before deploying your contracts
Deploying to different networks
Quick deployment for local development
Devnet automatically deploys contracts when started:
$clarinet devnet start[32mStarting devnet...[0m[32mDeploying contracts...[0m[90mDeploying[0m counter.clar [32m✓[0m ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.counter[90mDeploying[0m token.clar [32m✓[0m ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.token
For manual deployment to a running devnet:
$clarinet deployments apply --devnet
See local development for detailed devnet configuration.
Cost estimation and optimization
Fee levels
Choose the right fee level for your deployment:
$clarinet deployments generate --testnet --manual-cost
Fee level options:
--low-cost
: Minimize fees, slower confirmation--medium-cost
: Balanced fees and speed--high-cost
: Priority inclusion--manual-cost
: Choose interactively
Analyzing deployment costs
Get detailed cost breakdown before deploying:
$clarinet deployments generate --testnet --medium-cost
Advanced deployment configurations
Multi-batch deployments
Deploy complex systems with dependencies using batches:
plan:batches:- id: 0transactions:- contract-publish:contract-name: trait-definitionspath: contracts/traits.clarclarity-version: 2- id: 1transactions:- contract-publish:contract-name: tokenpath: contracts/token.clardepends_on: ["trait-definitions"]- contract-publish:contract-name: oraclepath: contracts/oracle.clar- id: 2transactions:- contract-publish:contract-name: defi-poolpath: contracts/defi-pool.clardepends_on: ["token", "oracle"]
Batches ensure:
- Dependencies are deployed first
- Parallel deployment within batches
- Sequential execution between batches
Transaction types
Deployment plans support various transaction types:
Transaction Type | Description |
---|---|
Contract Operations | Involves publishing and calling contracts, specifying sender, cost, and path |
Asset Transfers | Transfers assets like STX or BTC, specifying sender, recipient, and amounts |
With contract operations:
- contract-publish:contract-name: my-contractexpected-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGMcost: 5960path: contracts/my-contract.clarclarity-version: 2- contract-call:contract-id: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.my-contractexpected-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGMmethod: initializeparameters:- u1000000- "Token Name"cost: 5960
With asset transfers:
- stx-transfer:expected-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGMrecipient: ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AGmstx-amount: 1000000memo: '0x48656c6c6f' # "Hello" in hex- btc-transfer:expected-sender: mjSrB3wS4xab3kYqFktwBzfTdPg367ZJ2drecipient: bcrt1qnxknq3wqtphv7sfwy07m7e4sr6ut9yt6ed99jgsats-amount: 100000000sats-per-byte: 10
Manual plan customization
You can manually edit deployment plans for complex scenarios:
When deploying, Clarinet prompts to overwrite manual changes. Type no
to keep your customizations.
Contract initialization example:
- id: 3transactions:- contract-call:contract-id: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.tokenmethod: initializeparameters:- u1000000 # Initial supply- { name: "My Token", symbol: "MTK", decimals: u6 }expected-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
Common issues
Next steps
After successful deployment:
- Monitor your contracts using the Stacks Explorer
- Set up event notifications with Chainhook
- Learn about contract upgrades
- Configure monitoring and alerts for your contracts