Skip to content

nitroxgas/SimpleLoan

Repository files navigation

Billau

AAVE-inspired lending protocol with formal verification on Liquid Network

Fantasma Protocol

License: MIT Network: Liquid Language: SimplicityHL Status: Development Progress: 92% LOC: 18K

AAVE-inspired lending protocol with formal verification on Liquid Network

Billau is a decentralized lending protocol that brings AAVE's battle-tested architecture to Bitcoin's Liquid sidechain using formally verified smart contracts. Built with SimplicityHL and adapted for the UTXO model, Billau enables users to supply assets, borrow against collateral, and participate in a trustless lending market with mathematical guarantees of correctness.


🎯 Project Objectives

  1. Formal Verification First: All smart contract logic is mathematically proven correct before deployment
  2. Bitcoin-Native DeFi: Leverage Liquid Network's Bitcoin security with enhanced privacy and asset issuance
  3. UTXO Innovation: Adapt proven EVM lending patterns to UTXO architecture with cumulative index accounting
  4. Institutional Grade: Provide lending infrastructure suitable for institutional adoption with formal guarantees
  5. Open Source: Build transparent, auditable, community-driven financial infrastructure

✨ Core Features

🏦 Lending Operations

  • Supply Assets: Deposit tokenized BTC, USDT, or other Liquid assets to earn interest
  • Borrow Against Collateral: Take loans using supplied assets as collateral with configurable LTV ratios
  • Dynamic Interest Rates: Market-driven rates based on pool utilization (AAVE's interest rate model)
  • Cross-Asset Borrowing: Deposit BTC, borrow USDT (or any supported asset pair)
  • Flexible Repayment: Partial or full repayment with automatic interest accrual

πŸ›‘οΈ Safety & Liquidation

  • Health Factor Monitoring: Real-time tracking of position safety (collateral value vs. debt)
  • Automated Liquidation: Liquidators can close undercollateralized positions to protect protocol solvency
  • Liquidation Bonus: Incentivize liquidators with discounted collateral (e.g., 5% bonus)
  • Oracle Integration: Signed price feeds with staleness checks (10-minute threshold)

πŸ”¬ Formal Verification

  • SimplicityHL Validators: All on-chain logic written in formally verifiable language
  • Mathematical Proofs: Coq proofs for critical invariants (solvency, collateralization, index monotonicity)
  • Proven Safety Properties: No fund loss, no unauthorized access, bounded interest accrual
  • Independent Verification: Modular validators can be verified in isolation

πŸ’Ž UTXO-Specific Innovations

  • Cumulative Index Accounting: aToken value grows via liquidityIndex (RAY 10^27 precision) without updating all balances
  • State UTXOs: Reserve pools and debt positions represented as consumable/creatable UTXOs
  • Atomic State Transitions: All operations consume old state and create new state in single transaction
  • Off-Chain Coordinator: Handles UTXO race conditions with optimistic concurrency and transaction assembly

πŸ—οΈ Architecture

High-Level Components

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Frontend (React + TS)                    β”‚
β”‚  Dashboard β”‚ Supply β”‚ Borrow β”‚ Repay β”‚ Liquidate β”‚ Positions β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚ REST API
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Backend (Python + FastAPI)                      β”‚
β”‚  Coordinator β”‚ Services β”‚ API Routes β”‚ Oracle Integration    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚ Elements SDK
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Liquid Network (Testnet)                    β”‚
β”‚  Reserve UTXOs β”‚ Debt UTXOs β”‚ aTokens β”‚ Oracle Signatures    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚ Validates
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           SimplicityHL Validators (Formally Verified)        β”‚
β”‚  Reserve Validator β”‚ Debt Validator β”‚ Oracle Validator       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Technology Stack

Smart Contracts:

  • SimplicityHL: Formally verifiable smart contract language
  • Coq: Theorem prover for mathematical correctness proofs
  • Elements/Liquid: Bitcoin sidechain with asset issuance and confidential transactions

Backend:

  • Python 3.11+: Backend coordinator and API server
  • FastAPI: Modern async web framework with OpenAPI
  • SQLAlchemy: ORM for off-chain state management
  • SQLite: Local database for position tracking and caching
  • Elements SDK: Python bindings for Liquid Network integration

Frontend:

  • React 18: Modern UI framework with hooks
  • TypeScript: Type-safe JavaScript
  • TailwindCSS: Utility-first CSS framework
  • shadcn/ui: Beautiful, accessible component library
  • Lucide Icons: Modern icon set

Testing:

  • pytest: Python unit and integration tests
  • Hypothesis: Property-based testing for invariants
  • Jest: Frontend component testing
  • Formal Verification: Coq proofs for validators

πŸš€ Quick Start

Prerequisites

Required:

  • Python 3.11+
  • Node.js 18+
  • Git

Optional (for validators):

  • Elements Core (Liquid node)
  • SimplicityHL compiler (simc)
  • Coq proof assistant

Quick Commands

# Setup testnet environment (automated)
./scripts/setup_testnet.sh

# Mint test tokens
./scripts/mint_test_tokens.sh

# Start backend API
cd backend && uvicorn src.main:app --reload

# Start frontend
cd frontend && npm start

# View API docs
# http://localhost:8000/docs

# Compile validators
./scripts/compile_validators.sh

# Verify proofs
./scripts/verify_proofs.sh

# Deploy validators
./scripts/deploy_validators.sh

Installation

# Clone repository
git clone https://github.com/nitroxgas/SimpleLoan.git billau
cd billau

# Setup Elements regtest node
./scripts/setup_testnet.sh

# Setup backend
cd backend
python -m venv venv
source venv/bin/activate  # On Windows: .\venv\Scripts\activate
pip install -r requirements.txt
alembic upgrade head

# Setup frontend
cd ../frontend
npm install

# Start development servers
# Terminal 1: Elements node
elementsd -regtest

# Terminal 2: Backend
cd backend && uvicorn src.main:app --reload

# Terminal 3: Frontend
cd frontend && npm start

For detailed setup instructions, see Quickstart Guide.


πŸ“ Project Structure

fantasma/
β”œβ”€β”€ validators/              # SimplicityHL validators & Coq proofs
β”‚   β”œβ”€β”€ lib/                # RAY math library
β”‚   β”œβ”€β”€ reserve/            # Reserve validator + solvency proofs
β”‚   β”œβ”€β”€ debt/               # Debt validator + health factor proofs
β”‚   └── oracle/             # Oracle validator
β”‚
β”œβ”€β”€ backend/                # Python FastAPI backend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ api/           # REST API routes & schemas
β”‚   β”‚   β”œβ”€β”€ models/        # SQLAlchemy models
β”‚   β”‚   β”œβ”€β”€ services/      # Business logic services
β”‚   β”‚   └── utils/         # RAY math, Liquid client, logger
β”‚   └── alembic/           # Database migrations
β”‚
β”œβ”€β”€ frontend/               # React + TypeScript frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ pages/         # Dashboard, Supply, Borrow, Liquidate
β”‚   β”‚   β”œβ”€β”€ components/    # Reusable UI components
β”‚   β”‚   β”œβ”€β”€ hooks/         # Custom React hooks
β”‚   β”‚   └── services/      # API client
β”‚
β”œβ”€β”€ scripts/                # Deployment & setup scripts
β”‚   β”œβ”€β”€ setup_testnet.sh   # Automated testnet setup
β”‚   β”œβ”€β”€ mint_test_tokens.sh# Token minting
β”‚   β”œβ”€β”€ compile_validators.sh  # Compile SimplicityHL
β”‚   β”œβ”€β”€ verify_proofs.sh   # Verify Coq proofs
β”‚   └── deploy_validators.sh   # Deploy to Liquid
β”‚
β”œβ”€β”€ specs/                  # Project specifications
β”‚   └── 001-utxo-lending-protocol/
β”‚       β”œβ”€β”€ spec.md        # Feature specification
β”‚       β”œβ”€β”€ plan.md        # Implementation plan
β”‚       β”œβ”€β”€ data-model.md  # Data structures
β”‚       β”œβ”€β”€ tasks.md       # Task breakdown
β”‚       └── quickstart.md  # Setup guide
β”‚
β”œβ”€β”€ development_docs/       # Development notes & summaries
β”‚   β”œβ”€β”€ PHASE_8_COMPLETE.md
β”‚   β”œβ”€β”€ T070-T080_SUMMARY.md
β”‚   β”œβ”€β”€ T086-T088-T091-T092_SUMMARY.md
β”‚   └── ...
β”‚
β”œβ”€β”€ docs/                   # Additional documentation
β”‚   └── validators/        # Validator-specific docs
β”‚
β”œβ”€β”€ shared/                 # Shared types & constants
β”‚   β”œβ”€β”€ constants/         # RAY constants
β”‚   └── types/             # TypeScript type definitions
β”‚
└── .specify/              # Project metadata & archives
    └── ARCHIVE_2025-11-06.md

πŸ“– Documentation

Core Specifications

Validator Documentation

API & Frontend

Project Status

Development Documentation

Detailed implementation notes and phase summaries are available in the development_docs/ directory.


πŸ“Š Project Statistics

Implementation Progress

  • Tasks Completed: 87/95 (92%)
  • Total Lines of Code: ~18,000 LOC
  • Files: 117+ files across all components

Code Breakdown

Component Lines of Code Files Status
SimplicityHL Validators 2,100 4 βœ… Complete
Coq Formal Proofs 780 3 βœ… Complete
Backend (Python) ~8,000 50+ βœ… Complete
Frontend (React/TS) ~4,000 25+ βœ… Complete
Scripts & Docs ~3,000 35+ βœ… Complete

Phase Completion

Phase Tasks Progress
1. Setup 9/9 100% βœ…
2. Foundational 15/15 100% βœ…
3. Supply 13/13 100% βœ…
4. Borrow 10/10 100% βœ…
5. Liquidate 8/8 100% βœ…
6. Withdraw 5/5 100% βœ…
7. Interest Rates 6/6 100% βœ…
8. Validators 11/11 100% βœ…
9. Polish 10/18 56% ⏳

πŸ§ͺ Testing

# Backend unit tests
cd backend
pytest tests/unit/ -v

# Property-based tests (invariant checking)
pytest tests/property/ -v --hypothesis-show-statistics

# Integration tests (requires running Elements node)
pytest tests/integration/ -v

# Frontend tests
cd frontend
npm test

# Coverage
npm test -- --coverage

πŸ” Security

Formal Verification

All validators undergo formal verification before deployment:

  1. Specification: Write formal spec defining invariants
  2. Implementation: Implement validator in SimplicityHL
  3. Proof: Prove implementation satisfies specification using Coq
  4. Extraction: Compile to Simplicity bytecode
  5. Audit: External security audit of code and proofs

Key Invariants

  • Solvency: totalBorrowed ≀ totalLiquidity (always)
  • Collateralization: healthFactor β‰₯ 1.0 (or liquidatable)
  • Index Monotonicity: Indices never decrease
  • Conservation: Asset amounts conserved across transactions

Security Audits

  • Internal review (in progress)
  • External audit (planned after testnet deployment)
  • Bug bounty program (planned for mainnet)

πŸ—ΊοΈ Roadmap

Phase 1: MVP βœ… Complete (92%)

  • Project constitution and specification
  • Implementation plan and architecture design
  • Reserve and Debt validators (SimplicityHL) - 1,535 LOC
  • Oracle validator (SimplicityHL) - 330 LOC
  • RAY math library (SimplicityHL) - 235 LOC
  • Formal verification proofs (Coq) - 780 LOC
  • Backend coordinator and API - Fully functional
  • Frontend UI - Complete with all features
  • Supply, Withdraw, Borrow, Repay, Liquidate - All implemented
  • Dynamic interest rates - AAVE-style model
  • Dashboard with reserve info - Real-time
  • Transaction history tracking - Audit trail
  • OpenAPI documentation - Comprehensive

Phase 2: Testnet

  • Deploy to Liquid testnet
  • Integration testing with real testnet transactions
  • Economic simulations and stress testing
  • Community testing program
  • Documentation and tutorials

Phase 3: Mainnet Preparation

  • External security audit
  • Bug bounty program
  • Governance framework
  • Liquidity mining incentives
  • Mainnet deployment (pending audit approval)

Future Enhancements

  • Stable rate borrowing
  • Multiple collateral types per position
  • Flash loans
  • Governance token and DAO
  • Confidential transactions support
  • Additional asset support

πŸ’¬ Getting Help

Documentation Resources

Common Issues

  1. Backend won't start: Check Python version (3.11+), run alembic upgrade head
  2. Frontend build errors: Delete node_modules, run npm install again
  3. Validator compilation: Requires simc compiler from Simplicity repository
  4. Elements node: Use ./scripts/setup_testnet.sh for automated setup

Support


🀝 Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

Development Workflow

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Follow the constitution principles (see .specify/memory/constitution.md)
  4. Write tests for all new code
  5. Ensure all tests pass (pytest and npm test)
  6. Commit with conventional commits (feat:, fix:, docs:, etc.)
  7. Push to your branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

Code Standards

  • Python: Follow PEP 8, use type hints, 100% test coverage for critical paths
  • TypeScript: Follow Airbnb style guide, strict mode enabled
  • SimplicityHL: Follow formal verification best practices
  • Commits: Use Conventional Commits

πŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.

MIT License

Copyright (c) 2025 Billau Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

πŸ”— Links


πŸ™ Acknowledgments

  • AAVE: For pioneering the lending pool architecture that inspired this protocol
  • Blockstream: For developing Liquid Network and Simplicity
  • Elements Project: For the Elements sidechain platform
  • Bitcoin Community: For the foundation of trustless, decentralized finance

⚠️ Disclaimer

This software is experimental and under active development.

  • Development Phase: 92% complete (87/95 tasks)
  • Testnet Ready: Validators and backend implemented, not yet deployed
  • No Mainnet: Not audited or approved for mainnet deployment
  • Use at Own Risk: Educational and testing purposes only
  • No Warranties: Provided "as is" without any warranties or guarantees

Current Status

  • βœ… Implementation: Core protocol complete with formal verification
  • βœ… Validators: SimplicityHL code written and Coq proofs verified
  • ⏳ Testing: Integration tests and security audit pending
  • ❌ Deployment: Not yet deployed to Liquid testnet or mainnet

Before Production Use

  1. βœ… Complete formal verification (DONE)
  2. ⏳ Complete integration testing suite
  3. ⏳ External security audit
  4. ⏳ Bug bounty program
  5. ⏳ Testnet deployment and community testing
  6. ❌ Mainnet deployment (after audit approval)

Built with ❀️ by the Billau community

Bringing formally verified DeFi to Bitcoin

Report Bug Β· Request Feature Β· Documentation

About

A lending protocol implemented in SimplicityHL, inpired on the AAVE protocol

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •