Skip to content

Repository files navigation

DeFi DApp

A DeFi lending and staking DApp inspired by Aave, built with Solidity and Foundry.

The project explores core DeFi concepts such as borrowing, collateral management, staking, smart-contract interactions, and Solidity-based testing.

⚠️ Educational Project: This project is intended for learning and experimentation. It has not been audited and should not be used with real funds.

🚀 Overview

The goal of this project is to build a structured DeFi protocol where users can interact with smart contracts for lending/borrowing and staking functionality.

The project currently includes:

  • Borrowing functionality
  • Staking functionality
  • Solidity smart contracts
  • Foundry-based testing
  • Local Ethereum development with Anvil
  • Deployment scripts
  • Gas analysis and snapshots

The repository is built using Foundry, which provides Forge for testing, Cast for blockchain interaction, Anvil for a local Ethereum node, and Chisel as a Solidity REPL.

🏗️ Project Structure

DEFI-DApp/
│
├── Contracts/
│   ├── Borrow.sol
│   ├── Staking.sol
│   └── libraries/
│
├── src/
│   └── Counter.sol
│
├── test/
│   ├── Borrow.t.sol
│   └── Counter.t.sol
│
├── script/
│   └── ...
│
├── lib/
│   └── ...
│
├── .github/
│   └── workflows/
│
├── foundry.toml
├── foundry.lock
├── package.json
└── README.md

The current repository contains dedicated Borrow.sol and Staking.sol contracts and a Borrow.t.sol test file.

🧠 DeFi Concepts

This project is designed to explore several important DeFi concepts:

Borrowing

Users can interact with the borrowing system through smart contracts, with the protocol managing the associated lending/borrowing logic.

Staking

The project also contains a staking contract for exploring how users can lock assets in a smart contract and participate in a staking mechanism.

Smart Contract Architecture

The project separates protocol functionality into different contracts rather than putting the complete protocol into a single contract.

This makes the system easier to develop, test, and extend.

🧪 Testing

Testing is written in Solidity using Foundry/Forge.

Run the complete test suite:

forge test

Run tests with detailed traces:

forge test -vvvv

Run only the borrowing tests:

forge test --match-path test/Borrow.t.sol

Run a specific test:

forge test --match-test testFunctionName

Generate a gas report:

forge test --gas-report

Retry previously failed tests:

forge test --rerun

🔀 Fuzz Testing

Foundry supports fuzz testing by automatically generating different inputs for Solidity test functions.

Example:

function testFuzz_Borrow(uint256 amount) public {
    amount = bound(amount, 1, 100 ether);

    // Test borrowing logic
}

Increase the number of fuzz runs:

forge test --fuzz-runs 10000

Fuzz testing helps discover edge cases that may not be covered by manually selected inputs.

⏱️ Testing Time-Dependent Logic

DeFi protocols often depend on timestamps.

Foundry cheatcodes can be used to manipulate blockchain time during testing:

vm.warp(block.timestamp + 7 days);

This makes it possible to test:

  • Staking periods
  • Loan deadlines
  • Interest calculations
  • Expiration conditions
  • Time-based withdrawals

⛽ Gas Testing

Generate a gas report:

forge test --gas-report

Create a gas snapshot:

forge snapshot

Gas analysis is useful when optimizing DeFi contracts because transaction costs can have a significant impact on protocol usability.

🛠️ Tech Stack

  • Solidity
  • Foundry
  • Forge
  • Anvil
  • Cast
  • OpenZeppelin where applicable
  • Ethereum / EVM

⚙️ Installation

Prerequisites

Install Foundry before starting.

Then clone the repository:

git clone https://github.com/mohit-solidity/DEFI-DApp.git
cd DEFI-DApp

Install dependencies:

forge install

Build the contracts:

forge build

Run the tests:

forge test

🏃 Local Development

Start a local Ethereum node:

anvil

You can then deploy contracts against the local Anvil network using a Foundry deployment script.

Example:

forge script script/Counter.s.sol:CounterScript \
    --rpc-url http://127.0.0.1:8545 \
    --broadcast

Replace the script and deployment parameters with the appropriate protocol deployment script when deploying the DeFi contracts.

📦 Useful Foundry Commands

Build

forge build

Test

forge test

Format Solidity

forge fmt

Gas Snapshot

forge snapshot

Local Blockchain

anvil

Blockchain Interaction

cast <subcommand>

Help

forge --help
anvil --help
cast --help

These are the standard Foundry workflows currently documented in the repository.

🔐 Security

This project is not production-ready and has not undergone a professional smart-contract audit.

Before using a DeFi protocol with real funds, additional work would be required, including:

  • Comprehensive unit testing
  • Fuzz testing
  • Invariant testing
  • Static analysis
  • Reentrancy analysis
  • Access-control review
  • Oracle manipulation testing
  • Economic/security analysis
  • Professional auditing

🎯 Learning Objectives

This project is intended to help develop practical experience with:

  • Solidity smart-contract development
  • DeFi protocol architecture
  • Lending and borrowing mechanisms
  • Staking mechanisms
  • Foundry
  • Forge testing
  • Fuzz testing
  • Solidity cheatcodes
  • Gas optimization
  • Local blockchain development
  • Smart-contract security

📚 Resources

👨‍💻 Author

Mohit Sharma

GitHub: https://github.com/mohit-solidity

⭐ Support

If you find this project useful for learning Solidity and DeFi development, consider giving the repository a ⭐.


Disclaimer: This project is for educational purposes only. Do not deposit real funds or use the contracts in production without thorough testing and professional security auditing.