stacks-network / stacks-network/docs
[Tutorial] Building an sBTC DeFi Lending Protocol
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 185
- Forks
- 273
- Avg merge
- 4d 13h
- Merged PRs (30d)
- 4
Description
Overview
Complete tutorial for building a production-ready sBTC lending protocol with collateralized borrowing, health factor tracking, and liquidation mechanics.
Gap in Current Documentation
What's Missing:
- No sBTC DeFi lending tutorial - DeFi use case page shows Hermetica and StackingDAO but no step-by-step lending protocol tutorial
- Existing DeFi lending example has issues:
- Uses STX instead of sBTC (misses emerging use case)
- Contract has bugs (borrow function doesn't accumulate existing loans properly)
- No tutorial or tests - just contract code
- Uses outdated Clarity syntax (
block-heightinstead ofstacks-block-height)
- sBTC Builder Quickstart is entry-level only - No advanced DeFi patterns
Where Users Look:
docs.stacks.co/get-started/use-cases/defi- Shows protocols but no tutorialsdocs.stacks.co/sbtc- Basics only, no DeFi tutorialsdocs.stacks.co/cookbook/clarity/example-contracts/defi-lending- Broken example
Proposed Solution
Add comprehensive sBTC DeFi lending tutorial to the cookbook or use cases section.
Tutorial Covers:
- Protocol design (collateral ratios, health factors, liquidations)
- Complete Clarity smart contract implementation
- Supply/withdraw for lenders
- Borrow/repay with position accumulation
- Health factor calculation
- Liquidation mechanics with incentives
- 24 comprehensive Clarinet SDK tests
- Production integration patterns
- Common pitfalls and solutions
Working Code & Tutorial
Repository: https://github.com/bastiatai/sbtc-lending
Key Features:
- ✅ Complete smart contract (253 lines)
- ✅ 24 Clarinet SDK tests (all passing)
- ✅ Comprehensive README with usage examples
- ✅ Step-by-step TUTORIAL.md
- ✅ Fixes bugs in existing cookbook example
- ✅ Uses Clarity 3+ syntax (
stacks-block-height) - ✅ Production integration guidance
Tutorial Outline
- Core Concepts - Collateralization, health factors, liquidation economics
- Project Setup - Clarinet project structure
- Smart Contract - Step-by-step contract development
- Constants and error codes
- Data storage design
- Supply/withdraw functions
- Borrow with position accumulation (fixes cookbook bug!)
- Repayment with proportional collateral return
- Health factor calculation
- Liquidation function
- Testing - Comprehensive Clarinet SDK tests
- Production Integration - Real sBTC token integration
- Common Pitfalls - What to avoid
Why This Matters
- Emerging Use Case - Bitcoin-backed lending is a killer DeFi app for sBTC
- Fixes Ecosystem Gap - Current lending example is broken and incomplete
- Educational Value - Teaches core DeFi patterns developers can adapt
- Production-Ready - Includes health factors, partial repayments, liquidations
Suggested Location
Add to either:
docs/get-started/use-cases/defi.md- Add "Building a Lending Protocol" sectiondocs/cookbook/clarity/tutorials/- Createsbtc-lending-protocol.md
Additional Context
This tutorial demonstrates the pattern requested in the DeFi use case page - showing developers how to build DeFi protocols, not just use existing ones.
The example fixes critical bugs in the existing cookbook lending example while using sBTC (the emerging asset on Stacks) instead of STX.
Ready for review and merge. All code tested and documented. Happy to address feedback or adjust structure to fit docs style guide.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing docs/get-started/use-cases/defi.md, docs/cookbook/clarity/example-contracts/defi-lending, and the proposed sbtc-lending repository and TUTORIAL.md. Check the documentation style and decide whether the tutorial belongs in the use-cases section or docs/cookbook/clarity/tutorials/. Done means a complete sBTC lending tutorial covers the listed concepts, implementation, tests, integration guidance, and pitfalls.
Written by the indexing model from the issue text.
Assessment
- Domain
- blockchain, documentation
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100