-
Notifications
You must be signed in to change notification settings - Fork 272
[Tutorial] Building an sBTC DeFi Lending Protocol #1839
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.