Skip to main content

Deployment Guide: Local Development to Testnet

Panduan lengkap untuk deploy smart contracts dari local development environment ke testnet Stacks. Tutorial ini covers semua steps dari local testing sampai testnet deployment dan monitoring.

Overview Deployment Processโ€‹

Workshop Deployment Lifecycle:
โ”œโ”€โ”€ ๐Ÿงช Local Development: Clarinet console testing
โ”œโ”€โ”€ ๐Ÿ”ง Testnet Deployment: Public testing environment
โ”œโ”€โ”€ โœ… Testnet Validation: Comprehensive testing
โ”œโ”€โ”€ ๐Ÿ“Š Monitoring: Basic health checks
โ””โ”€โ”€ ๐ŸŽ“ Workshop Complete: Take-home projects ready

Prerequisitesโ€‹

1. Required Toolsโ€‹

# Workshop setup checklist:
- [ ] Clarinet installed dan working
- [ ] VS Code dengan Clarity extension
- [ ] Leather wallet setup
- [ ] Testnet STX dalam wallet
- [ ] Project created dengan clarinet new

2. Project Structureโ€‹

Ensure your project has proper structure:

your-project/
โ”œโ”€โ”€ Clarinet.toml # Project configuration
โ”œโ”€โ”€ contracts/ # Smart contracts directory
โ”‚ โ””โ”€โ”€ your-contract.clar # Main contract
โ”œโ”€โ”€ tests/ # Test files directory
โ”œโ”€โ”€ settings/ # Network settings
โ”‚ โ”œโ”€โ”€ Devnet.toml # Local development settings
โ”‚ โ””โ”€โ”€ Testnet.toml # Testnet settings
โ””โ”€โ”€ deployments/ # Deployment configurations

Local Development & Testingโ€‹

Step 1: Interactive Console Testingโ€‹

# Start local development environment
clarinet console

# Deploy contract locally
clarinet>> ::deploy_contract your-contract contracts/your-contract.clar

# Test read-only functions
clarinet>> (contract-call? .your-contract get-some-data)

# Test public functions
clarinet>> (contract-call? .your-contract some-public-function u123)

# Test dengan different accounts
clarinet>> ::set_tx_sender ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5
clarinet>> (contract-call? .your-contract some-function)

# Check balances
clarinet>> (stx-get-balance 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM)

# Exit console
clarinet>> ::quit

Step 2: Contract Validationโ€‹

# Check contract syntax
clarinet check

# Expected output:
# โœ“ your-contract syntax ok

# If errors, fix them before continuing

Step 3: Local Testing Checklistโ€‹

# Manual testing checklist:
- [ ] Contract deploys successfully
- [ ] Read-only functions return expected values
- [ ] Public functions work correctly
- [ ] Error conditions handled properly
- [ ] Access controls working (owner-only functions)
- [ ] Edge cases tested

Testnet Deploymentโ€‹

Step 1: Wallet Setupโ€‹

Ensure you have testnet STX:

# Check your wallet balance di Leather wallet
# Ensure you have at least 1 STX untuk deployment fees

# Faucet links untuk testnet STX:
# - https://explorer.stacks.co/sandbox/faucet
# - Join Stacks Discord untuk faucet access

Step 2: Testnet Configurationโ€‹

Edit settings/Testnet.toml if needed:

[network]
name = "testnet"
node_rpc_address = "https://stacks-node-api.testnet.stacks.co"
deployment_fee_rate = 10

[accounts.deployer]
mnemonic = "your testnet mnemonic phrase here"
balance = 100000000000000
derivation = "m/44'/5757'/0'/0/0"

Step 3: Generate Deployment Planโ€‹

# Generate deployment plan untuk testnet
clarinet deployment generate --testnet

# Review the deployment plan
cat deployments/default.testnet-plan.yaml

# Check estimated costs:
# - Contract deployment: ~0.05 STX
# - Network fees: Variable
# - Total recommended: 0.1-0.2 STX minimum

Step 4: Deploy to Testnetโ€‹

# Deploy ke testnet
clarinet deployment apply --testnet

# Expected output:
# โœ“ Contract deployed successfully
# Contract ID: ST...your-address.your-contract
# Transaction ID: 0x...

# Save deployment information
echo "$(date): Contract deployed to testnet" >> deployment-log.txt
echo "Contract Address: ST...your-address.your-contract" >> deployment-log.txt

Step 5: Verify Testnet Deploymentโ€‹

# Check contract on testnet explorer
# Visit: https://explorer.stacks.co/?chain=testnet
# Search untuk: ST...your-address.your-contract

# Test contract functions on testnet
curl -X POST "https://stacks-node-api.testnet.stacks.co/v2/contracts/call-read/ST...your-address/your-contract/get-some-data" \
-H "Content-Type: application/json" \
-d '{
"sender": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM",
"arguments": []
}'

Testnet Integration Testingโ€‹

Step 1: Basic Functionality Testโ€‹

# Test basic functions via API
node -e "
const fetch = require('node-fetch');

async function testContract() {
const response = await fetch('https://stacks-node-api.testnet.stacks.co/v2/contracts/call-read/YOUR_ADDRESS/your-contract/get-some-data', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
sender: 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM',
arguments: []
})
});

const result = await response.json();
console.log('Contract response:', result);
}

testContract();
"

Step 2: Integration dengan Frontend (Optional)โ€‹

// Basic frontend integration test
import { StacksTestnet } from '@stacks/network';
import { callReadOnlyFunction } from '@stacks/transactions';

const network = new StacksTestnet();

const contractAddress = 'ST...your-address';
const contractName = 'your-contract';
const functionName = 'get-some-data';

// Call read-only function
callReadOnlyFunction({
contractAddress,
contractName,
functionName,
functionArgs: [],
network,
senderAddress: 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM',
})
.then(result => {
console.log('Contract call result:', result);
})
.catch(error => {
console.error('Contract call error:', error);
});

Workshop Project Completionโ€‹

Workshop Deployment Checklistโ€‹

# โœ… Workshop Testing Checklist
# [ ] Local development setup working
# [ ] Contract deployed locally via console
# [ ] Core functions tested interactively
# [ ] Testnet deployment successful
# [ ] Basic functionality verified on testnet
# [ ] Project documented
# [ ] Code ready untuk take-home development

Workshop Security Basicsโ€‹

# Basic Security Checklist untuk Workshop:
# [ ] No private keys committed to code
# [ ] Input validation implemented
# [ ] Access controls basic (owner-only functions)
# [ ] Error handling untuk edge cases
# [ ] Only testnet addresses used
# [ ] Test data only (no real funds)

Workshop Completion Stepsโ€‹

Step 1: Document Your Projectโ€‹

Create README.md for your project:

# My Stacks Workshop Project

## Project Description
- **Type**: [Tic Tac Toe Game / Token System / NFT Collection / DAO]
- **Workshop Date**: [Date]
- **Status**: Working prototype

## Features Implemented
- [ ] Core contract functions
- [ ] Local testing completed
- [ ] Testnet deployment
- [ ] Basic error handling

## How to Run
1. `clarinet console`
2. `::deploy_contract [contract-name] contracts/[file].clar`
3. Test functions interactively

## Testnet Deployment
- **Contract Address**: [Your testnet address]
- **Explorer Link**: [Testnet explorer link]

## Next Steps
- [ ] Add more features
- [ ] Improve error handling
- [ ] Add frontend interface
- [ ] Learn advanced Clarity concepts

Step 2: Code Cleanupโ€‹

# Clean up your code
# Remove any hardcoded test values
# Add comments untuk important functions
# Organize code structure
# Remove debug code

# Example cleanup:
# Before:
(define-constant test-value u123456) ;; Remove this

# After:
;; Add proper comments
(define-constant default-bet-amount u1000000) ;; 1 STX default bet

Step 3: Prepare for Take-Home Developmentโ€‹

# Create development plan
echo "## Development Roadmap

### Phase 1: Core Features (Completed in Workshop)
- [x] Basic contract structure
- [x] Core functions implemented
- [x] Local testing
- [x] Testnet deployment

### Phase 2: Enhancements (Post-Workshop)
- [ ] Add more sophisticated error handling
- [ ] Implement additional features
- [ ] Add comprehensive tests
- [ ] Create frontend interface

### Phase 3: Advanced Features (Future)
- [ ] Add advanced game mechanics (for games)
- [ ] Implement governance features (for DAOs)
- [ ] Add marketplace functionality (for NFTs)
- [ ] Create mobile-friendly interface

### Learning Resources
- Stacks Documentation: https://docs.stacks.co/
- Clarity Language Reference: https://docs.stacks.co/clarity/
- Stacks Discord Community: https://discord.gg/stacks
" > DEVELOPMENT_PLAN.md

Troubleshooting Workshop Deploymentsโ€‹

Common Issues & Solutionsโ€‹

# Issue 1: Contract deployment fails
# Solution: Check testnet STX balance
# Visit: https://explorer.stacks.co/?chain=testnet
# Search your address untuk balance

# Issue 2: Transaction stuck in mempool
# Solution: Wait 10-15 minutes, or increase fee
clarinet deployment apply --testnet --fee-rate=20

# Issue 3: Contract function errors
# Solution: Test locally first
clarinet console
# Debug the function interactively

# Issue 4: Wallet connection issues
# Solution: Ensure Leather wallet is on testnet mode
# Check network setting dalam wallet

# Issue 5: API call errors
# Solution: Verify contract address dan function names
curl -s "https://stacks-node-api.testnet.stacks.co/v2/contracts/interface/YOUR_ADDRESS/your-contract"

Debug Deploymentโ€‹

# Check deployment status
clarinet deployment apply --testnet --dry-run

# Verify account nonce
curl -s "https://stacks-node-api.testnet.stacks.co/extended/v1/address/YOUR_ADDRESS/nonces"

# Check recent transactions
curl -s "https://stacks-node-api.testnet.stacks.co/extended/v1/address/YOUR_ADDRESS/transactions"

Next Steps After Workshopโ€‹

Immediate Actions (Week 1)โ€‹

  • Complete documentation
  • Share project dengan community
  • Join Stacks Discord untuk support
  • Plan next features untuk implementation

Short-term Goals (Month 1)โ€‹

  • Add frontend interface
  • Implement additional contract features
  • Learn advanced Clarity concepts
  • Connect dengan other developers

Long-term Goals (3+ Months)โ€‹

  • Build production-ready application
  • Learn about Stacks ecosystem tools
  • Contribute to open source projects
  • Consider hackathon participation

Congratulations! ๐ŸŽ‰

You have successfully:

  • โœ… Built your first Stacks smart contract
  • โœ… Deployed to testnet
  • โœ… Learned Clarity programming fundamentals
  • โœ… Gained hands-on blockchain development experience

Keep building and welcome to the Stacks developer community!