Skip to content

Latest commit

ย 

History

25 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿฆ EVM Lending / Borrowing Protocol (PoC)

License: MIT Solidity Foundry

A single-market money market in the style of Compound III (Comet): one borrowable base asset (USDC), isolated supply-only collateral (WETH, wBTC), index-based interest accrual, and protocol-absorbed liquidations. Built as a proof of concept of DeFi lending primitives with a thesis of audit-ready rigor and provable solvency.

โš ๏ธ DISCLAIMER: This is a portfolio/educational project demonstrating advanced smart contract development. All code is written from scratch; the architecture is inspired by Compound III, never copied or forked. NOT audited - do not use in production.


๐ŸŽฏ What is this?

A DeFi money market where:

  • Suppliers deposit USDC and hold a rebasing balance (lmUSDC) that grows with interest
  • Borrowers post WETH/wBTC collateral and borrow USDC against it
  • Liquidator bots absorb underwater accounts and buy the seized collateral at a discount

One borrowable asset. Inert collateral. Every rounding direction favors the protocol, and solvency is designed to be provable, not assumed.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                            THE MODEL                             โ”‚
โ”‚                                                                  โ”‚
โ”‚   SUPPLIERS โ”€โ”€โ”€โ”€ USDC โ”€โ”€โ”€โ”€โ–บโ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                  โ”‚
โ”‚   (earn interest,          โ”‚                  โ”‚                  โ”‚
โ”‚    hold lmUSDC)            โ”‚  LENDING MARKET  โ”‚โ—„โ”€โ”€ WETH / wBTC   โ”‚
โ”‚                            โ”‚   (singleton)    โ”‚    BORROWERS     โ”‚
โ”‚   LIQUIDATORS โ—„โ”€ discount โ”€โ”ค                  โ”‚    (post inert   โ”‚
โ”‚   (absorb, then            โ”‚  one base asset  โ”‚โ”€โ”€โ”€โ”€ USDC โ”€โ”€โ–บ     โ”‚
โ”‚    buyCollateral)          โ”‚  derived reservesโ”‚    (borrow)      โ”‚
โ”‚                            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                  โ”‚
โ”‚                                                                  โ”‚
โ”‚         interest split: suppliers + reserves, by construction    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โœจ Key Features

Feature Description
Single-base market (Comet) One borrowable asset; collateral is deposit-only, bounding risk per asset.
Signed-principal accounting One int104 per account; supply and borrow are mutually exclusive states.
Rebasing ERC20 (lmUSDC) The market itself is the token; balances grow in place with accrual.
Jump-rate interest model Kinked borrow curve; supply rate derived so reserves never accrue negative.
Absorb liquidations Protocol wipes debt, seizes collateral, resells via buyCollateral.
Explicit bad debt Shortfalls recognized at absorb time; reserves are derived and can go negative visibly.
Pyth + Chainlink oracle Pull-based primary with confidence intervals; independent deviation anchor.
Immutable deployment No proxy, no parameter setters; owner limited to reserves and pause flags.

๐Ÿ—๏ธ Architecture

                      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                      โ”‚        LENDING MARKET           โ”‚
                      โ”‚  (accounting, custody, ERC20)   โ”‚
                      โ”‚                                 โ”‚
                      โ”‚  supply / withdraw / transfer   โ”‚
                      โ”‚  borrow / repay (signed paths)  โ”‚
                      โ”‚  accrue / absorb / buyCollateralโ”‚
                      โ”‚  getReserves / withdrawReserves โ”‚
                      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚               โ”‚
                    rates      โ–ผ               โ–ผ      validated prices
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚  INTEREST RATE MODEL โ”‚  โ”‚  PYTH + CHAINLINK ORACLE โ”‚
              โ”‚  (stateless, kinked  โ”‚  โ”‚  (staleness, confidence, โ”‚
              โ”‚   curve + derived    โ”‚  โ”‚   deviation anchor)      โ”‚
              โ”‚   supply rate)       โ”‚  โ”‚                          โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“š Documentation

Comprehensive documentation is available in /docs:

Document Description
๐Ÿ“‹ Complete Index Master index - start here
๐Ÿ—บ๏ธ Roadmap Implementation phases & progress (76 items)
๐Ÿ“– Guide 1: Fundamentals Money markets & the single-base model
๐Ÿงฎ Guide 2: Mathematics Indexes, rates, liquidation, rounding policy
๐Ÿ—๏ธ Guide 3: Architecture Contracts, state, flows, ADRs
โš–๏ธ Guide 4: Trade-offs Risks, mitigations, risk matrix
๐Ÿ’ป Guide 5: Implementation Interfaces, errors, access control
๐Ÿ”’ Guide 6: Security Threat model, invariants, testing plan

๐Ÿ› ๏ธ Tech Stack

Component Technology
Smart Contracts Solidity 0.8.26
Framework Foundry
Testing Forge (unit, fuzz, invariant, integration, fork)
Libraries OpenZeppelin v5, Solady
Oracles Pyth Network + Chainlink
Standards ERC-20 (rebasing)

๐Ÿš€ Getting Started

Prerequisites

Installation

# Clone the repository
git clone https://github.com/GushALKDev/evm-lending-borrowing-protocol.git
cd evm-lending-borrowing-protocol

# Install dependencies
forge install

# Build
forge build

# Run tests
forge test

# Run tests with coverage
forge coverage

Local Development

# Start local node
anvil

# Deploy to local node
forge script script/Deploy.s.sol --rpc-url http://localhost:8545 --broadcast

๐Ÿงช Testing

# Unit tests
forge test

# Fuzz tests (more runs)
forge test --fuzz-runs 10000

# Invariant tests
forge test --match-contract InvariantTest

# Integration tests (local deployment, mocked oracle stack)
forge test --match-path "test/integration/*"

# Fork tests (fork-tested oracle and token integration; needs a mainnet RPC)
forge test --match-path "test/fork/*" --fork-url $MAINNET_RPC_URL

# Coverage report
forge coverage --report lcov

On fork testing: the fork suite validates this protocol's real external dependencies on a mainnet fork: the PythChainlinkOracle against the live Pyth pull contract and live Chainlink feeds, and the market flows against the real USDC, WETH, and wBTC contracts. This protocol is not a fork of any lending protocol; the lending logic is original and self-contained, and is covered by the unit, fuzz, and invariant suites.

Key Invariants

The protocol maintains these properties at all times (full list in Guide 6):

// INV-1: exact integer accounting (load-bearing)
sum(positive principals) == totalSupplyBase;
sum(negative principals) == totalBorrowBase;

// INV-2: indexes only grow
baseSupplyIndex' >= baseSupplyIndex;  baseBorrowIndex' >= baseBorrowIndex;

// INV-3/4: every rounding favors the protocol; the residual accrues to reserves
getReserves() non-decreasing except by absorb and withdrawReserves;

// INV-9: no action leaves an account undercollateralized
isBorrowCollateralized(account) after every health-reducing call;

๐Ÿ“ Project Structure

evm-lending-borrowing-protocol/
โ”œโ”€โ”€ docs/                        # Comprehensive documentation
โ”‚   โ”œโ”€โ”€ README.md                # Master index
โ”‚   โ”œโ”€โ”€ ROADMAP.md               # Implementation roadmap
โ”‚   โ”œโ”€โ”€ 01-fundamentals.md       # Guide 1: Core concepts
โ”‚   โ”œโ”€โ”€ 02-mathematics.md        # Guide 2: Protocol math
โ”‚   โ”œโ”€โ”€ 03-architecture.md       # Guide 3: System design
โ”‚   โ”œโ”€โ”€ 04-tradeoffs.md          # Guide 4: Risks & solutions
โ”‚   โ”œโ”€โ”€ 05-implementation.md     # Guide 5: Solidity interfaces
โ”‚   โ””โ”€โ”€ 06-security.md           # Guide 6: Security analysis
โ”œโ”€โ”€ src/                         # Smart contracts
โ”‚   โ”œโ”€โ”€ LendingMarket.sol        # Singleton market (accounting + custody + ERC20)
โ”‚   โ”œโ”€โ”€ InterestRateModel.sol    # Kinked curve, derived supply rate
โ”‚   โ”œโ”€โ”€ PythChainlinkOracle.sol  # Price validation pipeline
โ”‚   โ””โ”€โ”€ interfaces/              # ILendingMarket, IInterestRateModel, IPriceOracle
โ”œโ”€โ”€ test/                        # Test files
โ”‚   โ”œโ”€โ”€ unit/                    # Unit tests
โ”‚   โ”œโ”€โ”€ fuzz/                    # Fuzz tests
โ”‚   โ”œโ”€โ”€ invariant/               # Invariant tests
โ”‚   โ”œโ”€โ”€ integration/             # End-to-end tests (local, mocked oracles)
โ”‚   โ””โ”€โ”€ fork/                    # Mainnet-fork tests (live Pyth/Chainlink, real tokens)
โ”œโ”€โ”€ script/                      # Deployment scripts
โ”œโ”€โ”€ LICENSE                      # MIT License
โ””โ”€โ”€ README.md

๐ŸŽ“ What This Project Demonstrates

This project showcases advanced smart contract development skills through original implementation from scratch:

Documentation (Complete):

  • Comet-style single-base money market architecture
  • Index-based accounting with signed principals and a rebasing ERC20
  • Directional rounding analysis with a provable reserve-growth theorem
  • Absorb liquidation model with explicit bad-debt accounting
  • Oracle design with confidence-band health policies (Pyth + Chainlink)
  • Full ADR record for every non-obvious decision
  • Threat model, 14 system invariants, and a unit, fuzz, invariant, and fork testing plan (fork-tested oracle and token integration)

Implementation (Pending - see ROADMAP):

  • Core contracts (LendingMarket, InterestRateModel, PythChainlinkOracle)
  • Full test pyramid: unit, fuzz, invariant, integration, and fork (oracle and token integration)
  • Deployment scripts and audit-prep pass

๐Ÿค Contributing

This is a portfolio project, but contributions are welcome! Feel free to:

  • Open issues for bugs or suggestions
  • Submit PRs for improvements
  • Fork and adapt for your own learning

๐Ÿ“œ License

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


๐Ÿ‘ค Author

[GushALKDev]


๐Ÿ™ Acknowledgments


Built with โค๏ธ to demonstrate what's possible in DeFi

About

Isolated, single-base money market on EVM. Compound III (Comet)-inspired architecture, fully original code. Index-based accounting, absorb liquidation, Pyth + Chainlink oracle, built around a provable-solvency thesis (unit, integration, fuzz, invariant and fork tested).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages