A production-grade, audit-ready decentralized lending system inspired by Aave v2/v3. This project includes UUPS upgradeable smart contracts, an event-driven blockchain indexer, a NestJS backend with Redis caching, and a distributed fault-tolerant liquidation bot.
The system consists of six tightly integrated components:
- Smart Contracts: UUPS upgradeable Solidity contracts with fixed-point
wad/raymath for precise interest accrual. - PostgreSQL Database: Normalized schema with efficient indexing for rapid querying.
- Blockchain Indexer: Event-driven, reorg-safe, idempotent processor that synchronizes on-chain state to the database.
- NestJS Backend: Clean-architecture REST API with Redis caching and BullMQ background jobs.
- Liquidation Bot: Distributed, fault-tolerant service scanning user health factors and executing liquidations.
- Redis Infrastructure: Handles API rate limiting, caching, and distributed locks for the bot infrastructure.
- Robust Smart Contracts: Features dual-slope interest rate models, reentrancy guards, and access control.
- Wad & Ray Mathematics: Industry-standard precision for compound interest and financial calculations to prevent precision loss.
- Reorg-Safe Indexing: The custom indexer handles blockchain reorganizations seamlessly with rollback mechanisms.
- Enterprise Backend: Built with NestJS, utilizing Redis token bucket rate-limiting and global exception filters.
- High-Performance Liquidation: Automated, profit-estimating bot with distributed Redis locking to prevent concurrent executions.
- Comprehensive Testing: Covered by Foundry unit, fuzz, and invariant tests, along with Jest-based backend testing.
aave-lending/
├── contracts/ # Foundry-based smart contracts (Core, Libraries, Oracle, Tokenization)
├── backend/ # NestJS API backend (PostgreSQL + TypeORM)
├── indexer/ # Blockchain event listener and processor
├── liquidation-bot/ # Automated health factor scanner and executor
├── docker-compose.yml # Local infrastructure (Postgres, Redis)
└── README.md
-
Clone the repository:
git clone <repository_url> cd aave-lending
-
Start Infrastructure (PostgreSQL & Redis):
docker-compose up -d
-
Install Backend Dependencies & Run:
cd backend npm install cp .env.example .env # Configure your DB/Redis credentials npm run start:dev
-
Compile Smart Contracts:
cd ../contracts forge build
The contracts are heavily tested using the Foundry framework. This includes unit tests, fuzz testing, and invariant tests to ensure protocol solvency.
cd contracts
forge test -vvvThe backend utilizes Jest for unit and end-to-end testing, fully typed and configured.
cd backend
npm run testThis system implements multiple security layers:
- Smart Contracts: Full coverage with zero-floating-point math.
nonReentrantmodifiers on all pool interactions. Strict Access Control roles via UUPS. - Database: TypeORM parameterized queries to prevent SQL injection. Strict DB-level constraints (e.g., balances >= 0).
- Network Layer: Redis-backed distributed rate limiters to prevent DDoS and API abuse. Distributed bot locks prevent gas wars and duplicate executions.
This project is licensed under the MIT License.