stacks-dev
npx skills add https://github.com/kenny-stacks/stacks-skills --skill stacks-dev
Agent 安装分布
Skill 文档
Stacks Development Assistant
This skill guides you through Clarity smart contract development using an enforced test-driven development (TDD) workflow with the Clarinet CLI toolkit.
Core Capabilities
- Design-First Approach: Define contract requirements, data structures, and public interfaces before coding
- Test-Driven Development: Write comprehensive tests before implementation (enforced)
- Clarinet Integration: Leverage Clarinet for project scaffolding, testing, coverage, and deployment
- Quality Assurance: Verify 90%+ coverage and security patterns before deployment
- Frontend Integration: Connect contracts to web applications using Stacks.js
When This Skill Activates
This skill automatically activates when you mention:
- “Stacks” or “Stacks blockchain”
- “Clarity” or “Clarity smart contract”
- “Clarinet” or “Clarinet project”
- Working with
.clarfiles orClarinet.toml
Workflow Overview
Development follows 5 sequential phases with verification gates between each:
Phase 1 Phase 2 Phase 3 Phase 4 Phase 5
Design -> Tests -> Implementation -> Verification -> Frontend
[user gate] [auto gate] [auto gate] [auto+user] [user gate]
Progress Format: Each phase shows Phase X/5: [Phase Name]
Gate Types:
- User Gate: Requires your confirmation before proceeding
- Auto Gate: Verification must pass automatically
- Auto+User: Automated checks plus your security review
Navigation: You can request to jump phases, but I’ll warn about skipping verification.
Phase 1/5: Design
Purpose: Define contract requirements before writing any code.
Project Setup
Before designing, ensure Clarinet environment is ready:
-
Verify Clarinet installed: Run
clarinet --version- If missing: Install via
brew install clarinet(macOS) or see Clarinet releases
- If missing: Install via
-
Check project structure:
- If
Clarinet.tomlexists: Use existing project - If missing: Run
clarinet new <project-name>to initialize - If fresh start needed: Confirm before overwriting existing project
- If
Reference: For CLI details, see references/clarity-cli.md
Steps
-
Gather Requirements
- What problem does this contract solve?
- Who are the users (principals)?
- What operations do they need?
-
Define Data Structures
- State variables (
define-data-var) - Maps for key-value storage (
define-map) - Constants for fixed values (
define-constant)
- State variables (
-
Specify Public Interface
- Public functions with inputs/outputs
- Read-only functions for queries
- Error codes and response types
-
Consider Upgradability
- Will this contract need updates?
- Should it delegate to other contracts?
Reference: For design patterns, see references/clarity-design.md
Verification
Before proceeding, confirm:
- Clarinet project initialized (
Clarinet.tomlexists) - Project structure valid (
contracts/,tests/,settings/directories) - Requirements documented and understood
- Data structures defined (vars, maps, constants)
- Public function signatures specified
- Error codes defined
- Upgradability approach decided
Gate: User Confirmation
When you confirm the design is complete, we proceed to Phase 2.
Ready to proceed? Confirm or request changes.
Phase 2/5: Tests
TDD Status: [â followed | â skipped]
Purpose: Write comprehensive unit tests BEFORE implementation.
Why Tests First?
Test-driven development ensures:
- Contract behavior is specified before code exists
- Implementation matches your requirements
- Regressions are caught automatically
- Edge cases are considered early
Collaborative Test Generation
I’ll propose test scenarios organized by category:
Happy Path:
- [Scenario description with expected outcome]
Edge Cases:
- [Zero/boundary values, empty inputs]
Error Handling:
- [Authorization failures, validation errors]
Review and approve the scenarios. After approval, I’ll write all test code in batch.
Steps
-
Create Contract Scaffold
clarinet contract new my-contract clarinet check # Verify scaffold is validCreates:
contracts/my-contract.clarandtests/my-contract.test.tsI’ll automatically runclarinet checkto confirm both files created and syntax valid. -
I Propose Test Scenarios Based on the design doc, I’ll suggest test scenarios for each function
-
You Review and Approve Confirm the scenarios cover your requirements
-
I Write Test Files Implement all approved scenarios in batch
-
Run Tests (Should Fail)
clarinet testTests should fail now – no implementation exists yet (RED phase)
Reference: For testing patterns, see references/clarity-tdd.md
TDD Compliance
If you request contract code before tests exist, I’ll gently redirect: “Following TDD, let’s write tests first. This helps catch edge cases early.”
If you prefer to proceed anyway, I’ll continue and track the workflow:
- TDD: â followed – Tests written before implementation
- TDD: â skipped – Contract written before tests (increased coverage threshold)
Verification (Automatic)
I will verify:
- Test file exists for each contract
- All public functions have at least one test
-
clarinet checkpasses (no syntax errors) -
clarinet testruns (tests fail as expected – RED phase)
Gate: Automated Verification
When verification passes, we proceed automatically to Phase 3.
If verification fails:
- I’ll identify missing tests or issues
- Attempt to fix automatically
- Re-run verification
- Escalate to you if issues persist after 3 attempts
Phase 3/5: Implementation
TDD Status: [â followed | â skipped]
Purpose: Write Clarity code to pass tests.
Steps
-
Implement Data Structures
- Add
define-data-vardeclarations - Add
define-mapdeclarations - Add
define-constantvalues
- Add
-
Implement Functions One at a Time
- Start with simplest function
- Run tests after each function
- Move to next when tests pass
-
Apply Best Practices
- Use meaningful error codes (
err u100with comments) - Avoid unnecessary
beginblocks - Use
asserts!for validation - Keep functions focused
- Use meaningful error codes (
-
Iterate Until Green
clarinet testContinue until all tests pass.
Reference: For implementation patterns, see references/clarity-implementation.md
Best Practices Review
After each function, I’ll review for Clarity best practices:
Coding Style:
- Sequential asserts instead of nested if
- Meaningful error codes (HTTP-like: 400, 401, 404)
- No unnecessary begin blocks
- No unwrap-panic (use unwrap! with error codes)
Storage:
- Hash storage for large data
- Minimal on-chain storage
- Efficient map usage
Upgradability:
- Dynamic principals for upgradable references
- Data/logic separation where appropriate
I’ll auto-fix mechanical violations (unnecessary begin, unwrap-panic) and explain what changed. For structural changes (nested if â asserts), I’ll ask first.
Automatic Validation
After each contract edit, I run clarinet check automatically:
- Pass: Continue to tests
- Fail: Parse errors, attempt auto-fix (up to 3x), then escalate
Common auto-fixes: unnecessary begin removal, unwrap-panic replacement with unwrap!.
Complex errors (type mismatches, missing functions) require your guidance.
Reference: For error patterns, see references/clarity-cli.md
Verification (Automatic)
I will verify:
- All tests pass (
clarinet testexits 0) - No syntax errors (
clarinet checkpasses) - Best practices applied
Gate: Automated Verification
When all tests pass, we proceed automatically to Phase 4.
If tests fail:
- I’ll analyze the failure
- Attempt to fix the implementation
- Re-run tests
- Escalate to you if issues persist after 3 attempts
Phase 4/5: Verification
TDD Status: [â followed | â skipped – 95% coverage required]
Purpose: Ensure quality through coverage and security review.
Steps
-
Run Coverage Analysis
npm run test:coverageUses Vitest with Clarinet SDK for coverage reporting.
-
Verify Coverage Threshold
- Target: 90%+ line coverage (95% if TDD skipped)
- Identify any uncovered functions
-
Security Review
- Check for common vulnerabilities
- Review error handling
- Verify access control
-
Fix and Re-run
- Add tests for uncovered code
- Fix security issues
- Re-verify until thresholds met
Reference: For coverage commands, see references/clarity-cli.md
Coverage Display
After every test run, I’ll show coverage status:
Coverage: 87% (target: 90%)
âââ Lines: 87%
âââ Functions: 100%
âââ Branches: 75%
âââ Statements: 88%
Uncovered:
- transfer() line 45: zero-amount validation branch
- admin-set-fee() lines 67-70: entire function
Coverage Gap Analysis
When below 90%, I’ll identify specific gaps and suggest tests:
“Line 45 is the zero-amount check in transfer(). Suggested test: ‘should reject transfer with zero amount â ERR-INVALID-AMOUNT'”
Accept suggestions or provide your own test scenarios.
Coverage Gate Override
Standard threshold: 90%
If coverage is below 90% and you want to proceed anyway:
- Say “proceed anyway” or “skip coverage gate”
- I’ll note the override and continue to Phase 5
- No justification required
If TDD was skipped: Threshold increases to 95%
Verification (Automatic)
I will verify:
- Coverage >= 90% (or 95% if TDD skipped)
- All functions tested
- Uncovered lines identified with suggested tests
- Security review completed
Auto-Fix Loop
If coverage < 90%:
- Identify uncovered functions/lines
- Write additional tests
- Re-run coverage analysis
- Repeat up to 3 attempts
- Escalate if threshold not achieved
Gate: Automated + User Confirmation
When automated checks pass, I’ll present a security summary for your review.
What you’ll review:
- Coverage report summary
- Any security considerations
- Confirmation to proceed to frontend
Phase 5/5: Frontend
Purpose: Integrate contract with web application.
Devnet Lifecycle
Before frontend development, start local blockchain:
- Verify Docker running (required for devnet)
- Start devnet in a dedicated terminal:
clarinet devnet start - Wait for health check – Node responds at
localhost:20443 - End session: Run
clarinet devnet stopwhen finished
Contracts auto-deploy to devnet on startup.
Console Testing
For interactive contract exploration before frontend:
clarinet console
Common commands:
(contract-call? .my-contract my-function arg1)– Call functions::set_tx_sender ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM– Switch sender::get_assets_maps– View balances
Note: Console is for manual exploration; tests use Vitest.
Package Installation
Install Stacks.js packages for frontend:
npm install @stacks/connect @stacks/transactions @stacks/network
Steps
-
Connect Wallet
- Use
@stacks/connectfor wallet integration - Handle wallet connection/disconnection
- Use
-
Call Contract Functions
- Use
@stacks/transactionsfor contract calls - Critical: Add post-conditions for STX/token transfers
- Handle transaction signing and results
- Use
-
Handle Errors
- Parse contract responses
- Display user-friendly messages
Security Note
Always use PostConditionMode.Deny for token transfers. Post-conditions abort transactions if unexpected asset transfers occur, protecting users from malicious contracts.
Reference: For frontend patterns, see references/clarity-frontend.md
Deployment Safety
Network-tiered confirmation requirements:
| Network | Confirmation | Notes |
|---|---|---|
| Devnet | Auto-deploy | No confirmation needed |
| Testnet | Show plan, confirm | Preview costs first |
| Mainnet | Explicit confirm | Show costs, verify twice |
Switching to higher-tier network always requires confirmation.
Verification (Manual)
You verify:
- Devnet started and healthy
- Contracts deployed to devnet
- Wallet connects successfully
- Contract calls work from frontend
- (Optional) Deployed to testnet/mainnet with verification
Gate: User Verification
This is the final phase. You verify the integration works as expected.
Deployment options after verification:
- Stay on devnet for more testing
- Deploy to testnet (requires confirmation)
- Deploy to mainnet (requires explicit confirmation)
Navigation
Jumping Between Phases
You can request to jump to any phase:
- “Go to design” – Returns to Phase 1
- “Skip to implementation” – Jumps to Phase 3
- “Review tests” – Returns to Phase 2
Phase Skipping Warning
If you request to skip phases:
I’ll warn: “Skipping [Phase X] increases risk. You’ll need to complete [skipped verification] before deployment. Continue anyway?”
If you confirm:
- I’ll track what was skipped
- Required verification before deployment increases
- Coverage threshold may increase (95%+ for skipped TDD)
Backward Navigation
If you want to revisit an earlier phase:
I’ll ask: “This returns to [Phase X] and may require re-verification of later phases. Continue?”
This ensures you understand that changes may cascade.
Verification Behavior
Automatic Verification After Each Phase
When you complete a phase, I automatically:
- Run verification checks (files, commands)
- Report summary: “All checks passed” or “X issues found”
- Offer details if issues exist
Auto-Fix Attempts
When verification fails:
- Attempt 1: Identify issue, apply fix, re-verify
- Attempt 2: Try alternative fix, re-verify
- Attempt 3: Final attempt with different approach
- Escalate: Present issues and ask for your guidance
Issue Reporting
Summary format:
Verification: 2 issues found
- Missing test for `transfer` function
- Coverage at 85% (target: 90%)
Attempting auto-fix (1/3)...
After fixes:
Verification: All checks passed
- Tests: 12/12 passing
- Coverage: 94%
Ready to proceed to [Next Phase].
Quick Reference
Essential Commands
# Project setup
clarinet new my-project
clarinet contract new my-contract
# Development
clarinet check # Syntax validation
clarinet console # Interactive REPL
# Testing
clarinet test # Run all tests
npm run test:coverage # With coverage report (Vitest)
# Deployment
clarinet devnet start # Local blockchain
clarinet deployment apply -p deployments/default.devnet-plan.yaml
Phase Summary
| Phase | Purpose | Gate | Reference |
|---|---|---|---|
| 1. Design | Define requirements | User confirmation | clarity-design.md |
| 2. Tests | Write tests first | Auto verification | clarity-tdd.md |
| 3. Implement | Pass tests | Auto verification | clarity-cli.md |
| 4. Verify | Coverage + security | Auto + user | clarity-cli.md |
| 5. Frontend | Web integration | User verification | clarity-frontend.md |
Getting Help
- “Show me design patterns” – Loads design reference
- “How do I test this?” – Loads TDD reference
- “What clarinet commands?” – Loads CLI reference
- “Help with frontend” – Loads frontend reference
Quality Standards
This skill enforces:
- TDD Required: Tests must exist before implementation
- 90% Coverage: Minimum threshold before deployment
- Error Handling: All public functions must handle errors
- Security Review: Manual review before mainnet deployment
Exceptions require explicit acknowledgment and increase verification requirements.
stacks-dev v0.1.0 | Apache-2.0 | Report Issues