Skip to content

AugustoL/erc20-flash-lender

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

97 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ERC20 Flash Lender

⚠️ DISCLAIMER: This is a personal/educational project that has NOT been professionally audited. Do NOT use with real funds on mainnet without a comprehensive security audit.

A flash loan protocol for ERC20 tokens with proportional fee sharing among liquidity providers. Built using OpenZeppelin's battle-tested contracts with comprehensive precision attack protections and reviewed by AI systems, but requires professional audit before production use.

Features

  • πŸš€ Flash Loans: Instant, uncollateralized loans for MEV operations
  • 🎯 Multi-Token Flash Loans: Borrow multiple tokens simultaneously in a single transaction
  • πŸ’° Fee Sharing: Proportional fee distribution among liquidity providers
  • πŸ†• Fee Harvesting: Withdraw earned fees while keeping principal staked for compound growth
  • πŸ—³οΈ Democratic Governance: LPs vote on fee rates with share-weighted voting
  • ⏰ Delayed Execution: 10-block delay for governance decisions
  • πŸ›‘οΈ Security First: Comprehensive protection against precision attacks and common DeFi exploits
  • ⚑ Ultra-Low Fees: Default 0.01% LP fee with 1% of it as management fee (as % of LP fee)
  • οΏ½ Emergency Pause (Withdraw-Only): Owner can enable withdraw-only mode during incidents
  • β›” Non-Upgradeable: Fixed implementation; migrations happen via pause β†’ withdraw β†’ redeploy
  • πŸ“Š Share-Based: Fair fee distribution using share-based accounting with virtual shares protection
  • πŸ”„ Advanced Executors: Gas-optimized executor contracts for complex multi-step operations

Return on Investment (ROI)

Potential APY ranges from 1-10% (conservative) to 50-200%+ (high utilization) depending on flash loan adoption and fee governance.

πŸ“Š View Detailed ROI Analysis

Security Features

Precision Attack Protections

  • βœ… Virtual Shares: A minimal amount of virtual shares minted to owner on first deposit to prevent share manipulation
  • βœ… Minimum Deposit: 100M wei (1e8) minimum deposit requirement to make dust attacks uneconomical
  • βœ… Fixed Entry/Exit Fees: 100 wei fixed fees (not percentage-based) that accumulate as permanent dust
  • βœ… Minimum Withdrawal: Rejects withdrawals below minimum threshold after exit fees
  • βœ… Precision Loss Prevention: Direct fee calculation without nested division to prevent rounding manipulation

Standard DeFi Protections

  • βœ… Reentrancy protection
  • βœ… Share dilution attack prevention
  • βœ… Arithmetic underflow/overflow protection
  • βœ… Interface compliance checking
  • βœ… Fee caps and validation
  • βœ… Share-weighted governance voting
  • βœ… Time-delayed execution system
  • βœ… Proposal validation and cleanup

Quick Start

Installation

npm install

Compilation

npm run build

Testing

# Run all tests
npm run test

# Run with gas reporting
npm run test:gas

# Run with coverage
npm run test:coverage

Deployment

# Local deployment
npm run deploy:localhost

# Testnet deployment
npm run deploy:sepolia

# Mainnet deployment
npm run deploy:mainnet

Contracts Overview

Core Functions

  • deposit(token, amount) - Deposit tokens to earn fees
  • withdraw(token) - Withdraw principal + accumulated fees
  • withdrawFees(token) - Withdraw only fees while keeping principal staked
  • flashLoan(token, amount, receiver, data) - Execute single-token flash loan
  • flashLoanMultiple(tokens[], amounts[], receiver, data) - Execute multi-token flash loan

Admin Functions

  • setManagementFee(percentage) - Set management fee as % of LP fee (1-5%)
  • withdrawManagementFees(token) - Withdraw collected fees
  • emergencyPause() - Toggle emergency pause (withdraw-only mode)

LP Governance Functions

  • voteForLPFee(token, feeAmountBps) - Vote for LP fee amount (share-weighted)
  • proposeLPFeeChange(token, newFeeBps) - Propose fee change based on governance
  • executeLPFeeChange(token, newFeeBps) - Execute approved fee change after delay

View Functions

  • getWithdrawableAmount(token, user) - Preview withdrawal amount
  • getEffectiveLPFee(token) - Get current LP fee rate

Fee Structure

Fee Type Default Rate Range Description
LP Fee 0.01% (1 bps) 0-1% (0-100 bps) Goes to liquidity providers (set by LP governance)
Management Fee 1% of LP fee 1-5% of LP fee Percentage of LP fee that goes to protocol owner
Entry/Exit Fee 100 wei (fixed) Fixed Precision attack protection

πŸ“Š View Usage Examples

Advanced Features

  • πŸ”§ Flash Loan Executors: Gas-optimized contracts for multi-operation flash loans
  • πŸš€ Multi-Token Flash Loans: Borrow multiple tokens simultaneously (see contract docs)
  • πŸ’° Fee Harvesting: Withdraw earned fees while keeping principal staked (see contract docs)

πŸ“– Flash Loan Executors Guide | ERC20FlashLender Contract

Development

Environment Setup

  1. Copy .env.example to .env
  2. Fill in your private keys and RPC URLs
  3. Install dependencies: npm install

Available Scripts

# Development
npm run build          # Compile contracts
npm run clean          # Clean artifacts
npm run typechain      # Generate TypeScript bindings

# Testing  
npm run test           # Run tests
npm run test:coverage  # Coverage report
npm run test:gas       # Gas usage report

# Code Quality
npm run lint           # Lint Solidity code
npm run format         # Format all code

# Local Development Environment
npm run node           # Start local Hardhat node
npm run deploy:dev     # Deploy with test tokens & setup (localhost)

# Production Deployment
npm run deploy:localhost  # Deploy to localhost
npm run deploy:sepolia    # Deploy to Sepolia
npm run deploy:mainnet    # Deploy to Mainnet

# Verification
npm run verify:sepolia    # Verify on Sepolia
npm run verify:mainnet    # Verify on Mainnet

Development Environment Setup

The project provides three development scripts for different workflows:

Option 1: Full Development Environment (Recommended)

npm run dev

Runs the complete development stack - compiles contracts, starts Hardhat node, deploys contracts, and launches the React app.

Option 2: App Development Only

npm run dev:app

Starts only the React development server. Use this when developing frontend features with the GitHub Pages deployed contracts.

Option 3: Local Node Only

npm run dev:node

Starts only the Hardhat node with deployed contracts. Use this to connect the GitHub Pages app to your local blockchain for testing.

The full development script (npm run dev) will:

  • 🏦 Deploy the ERC20FlashLender contract
  • πŸͺ™ Deploy 4 test tokens (TUSDC, TDAI, TWETH, TWBTC) with different decimals
  • πŸ‘₯ Set up 4 test accounts with token balances
  • πŸ’° Make initial deposits to get the pools started
  • πŸ“„ Save deployment info to deployment-dev.json
  • πŸ”§ Provide ready-to-use contract interaction snippets

Test Tokens Deployed:

  • TUSDC (6 decimals): Test USDC - 1B supply
  • TDAI (18 decimals): Test DAI - 1B supply
  • TWETH (18 decimals): Test WETH - 100K supply
  • TWBTC (8 decimals): Test WBTC - 21K supply

Test Accounts Setup:

  • Deployer: Contract owner with all permissions
  • User1, User2, User3: Each gets 10K of every test token

This setup provides a complete local testing environment with realistic token scenarios and pre-funded accounts for immediate testing of flash loans, deposits, withdrawals, and governance features.

React DApp Frontend

The project includes a comprehensive React-based decentralized application (DApp) that provides a user-friendly interface for interacting with the ERC20 Flash Lender protocol.

✨ DApp Features

  • 🎨 Modern UI/UX: Built with React 18 and modern design patterns
  • 🌐 Web3 Integration: Seamless wallet connection via RainbowKit and Wagmi
  • πŸ“Š Real-time Dashboard: Live pool statistics, user positions, and earnings tracking
  • πŸ’° Pool Management: Deposit, withdraw, and harvest fees with intuitive controls
  • πŸ—³οΈ Governance Interface: Vote on fee rates and propose changes through the UI
  • πŸ“ˆ Activity Tracking: Comprehensive transaction history and analytics
  • πŸŒ™ Theme Support: Light/dark mode with user preferences
  • ⚑ Performance Optimized: Lazy loading, memoization, and code splitting
  • πŸ›‘οΈ Error Boundaries: Graceful error handling with fallback UI
  • πŸ”’ Web3 Security: Built-in provider validation and secure transaction handling
  • πŸ“± Responsive Design: Works seamlessly on desktop and mobile devices

πŸš€ Quick Start (Frontend)

# Option 1: Full development environment (recommended)
npm run dev

# Option 2: Frontend development only (uses GitHub Pages contracts)
npm run dev:app

# Option 3: Build for production
cd app && npm run build

πŸ“ DApp Architecture

app/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ components/           # React components
β”‚   β”‚   β”œβ”€β”€ common/          # Reusable UI components
β”‚   β”‚   └── pages/           # Page-level components
β”‚   β”œβ”€β”€ context/             # React context providers
β”‚   β”œβ”€β”€ hooks/               # Custom React hooks
β”‚   β”œβ”€β”€ services/            # Data services and API layers
β”‚   β”œβ”€β”€ utils/               # Utility functions and constants
β”‚   └── types/               # TypeScript type definitions
β”œβ”€β”€ public/                  # Static assets
└── package.json            # Dependencies and scripts

πŸ”§ Configuration

The DApp automatically detects your network and connects to the appropriate contracts.

For full local development:

npm run dev  # All-in-one: compile, deploy, and start app

For frontend-only development:

npm run dev:app  # Uses GitHub Pages deployed contracts

For testing GitHub Pages app with local blockchain:

npm run dev:node  # Start local node, then use GitHub Pages app

Connect MetaMask to localhost:8545 when using local development options.

πŸ“– DApp Documentation

For detailed frontend documentation, component guides, and development setup, see app/README.md.

πŸ”’ Frontend Provider Policy

This DApp is wallet-only by design:

  • No hosted/public RPCs are used for reads or writes.
  • A user wallet provider (e.g., MetaMask via window.ethereum) is required to load data and interact.
  • Server-side rendering is disabled to avoid any backend RPC dependencies.
  • If no wallet is connected, the UI will show a connect-wallet prompt and will not perform chain queries until connected.

Architecture

ERC20FlashLender
β”œβ”€β”€ Liquidity Management
β”‚   β”œβ”€β”€ deposit() - Add tokens to pool (min 100M wei, 100 wei entry fee)
β”‚   β”œβ”€β”€ withdraw() - Remove tokens + fees (100 wei exit fee, min threshold check)
β”‚   β”œβ”€β”€ Share-based accounting with virtual shares (1000 virtual shares)
β”‚   └── Precision attack protections
β”œβ”€β”€ Flash Loans
β”‚   β”œβ”€β”€ flashLoan() - Execute single-token loan
β”‚   β”œβ”€β”€ flashLoanMultiple() - Execute multi-token loan (up to 20 tokens)
β”‚   β”œβ”€β”€ Interface validation (IFlashLoanReceiver & IMultiFlashLoanReceiver)
β”‚   β”œβ”€β”€ Fee collection with precision fixes
β”‚   β”œβ”€β”€ Duplicate token detection
β”‚   └── Minimum fee enforcement for large loans
β”œβ”€β”€ LP Governance
β”‚   β”œβ”€β”€ voteForLPFee() - Share-weighted voting (including virtual shares)
β”‚   β”œβ”€β”€ proposeLPFeeChange() - Democratic proposals
β”‚   └── executeLPFeeChange() - Delayed execution
β”œβ”€β”€ Administration
β”‚   β”œβ”€β”€ Management fee control (1-5% of LP fee)
β”‚   β”œβ”€β”€ Emergency pause (withdraw-only mode)
β”‚   └── Owner functions (limited scope)
└── Security Layer
    β”œβ”€β”€ Virtual shares dilution (VIRTUAL_SHARES = 1000)
    β”œβ”€β”€ Minimum deposit enforcement (MINIMUM_DEPOSIT = 1e8)
    β”œβ”€β”€ Fixed fee dust accumulation (ENTRY_EXIT_FEE = 100 wei)
    β”œβ”€β”€ Multi-token validation and gas limits
    └── Withdrawal validation and thresholds

⚠️ Security Considerations

🚨 CRITICAL: Audit Status

THIS CONTRACT HAS NOT BEEN PROFESSIONALLY AUDITED

  • ❌ No professional security audit performed
  • βœ… AI-assisted code review completed
  • βœ… Built on OpenZeppelin's audited contracts
  • βœ… Comprehensive test suite (75+ passing tests)
  • βœ… Security best practices implemented
  • βœ… Precision attack protections implemented

DO NOT USE WITH REAL FUNDS ON MAINNET WITHOUT A PROFESSIONAL AUDIT

Risk Factors

  • Unaudited code risk - Primary concern
  • Smart contract risk
  • Admin key risk (management fees and pause authority)
  • Liquidity risk
  • Governance manipulation risk (voting power concentration)
  • Time delay risks (governance proposals can be front-run)
  • Oracle dependencies (if added)

Precision Attack Mitigations

This protocol implements comprehensive protections against precision-based attacks:

  1. Virtual Shares (1000): Automatically minted to owner on first deposit

    • Dilutes small attackers' ability to manipulate share calculations
    • Makes it impossible to achieve meaningful ownership with dust amounts
  2. Minimum Deposit (100M wei): Enforced on all deposits

    • Makes dust attacks economically unviable
    • Ensures meaningful stake for all participants
  3. Fixed Entry/Exit Fees (100 wei each):

    • Not percentage-based, creates permanent dust accumulation
    • Reduces profitability of repeated small operations
    • Acts as economic deterrent for precision attacks
  4. Minimum Withdrawal Validation:

    • Rejects withdrawals below minimum threshold after exit fees
    • Prevents dust withdrawals that could be used for manipulation
  5. Precision-Safe Fee Calculations:

    • Direct multiplication instead of nested division
    • Prevents rounding manipulation in fee calculations

These protections work together to make precision attacks both technically difficult and economically unviable.

Best Practices

  • Use multisig for owner/pauser accounts
  • Implement time locks for configuration changes where feasible (pause remains immediate)
  • Monitor for unusual activity
  • Keep emergency procedures ready

πŸ›‘ Emergency Pause & Incident Runbook

The protocol is non-upgradeable and includes an emergency pause that switches the system to withdraw-only mode to protect users during incidents.

What the pause does:

  • Disables: deposit, flashLoan, flashLoanMultiple, withdrawFees, governance actions (voteForLPFee, proposeLPFeeChange, executeLPFeeChange).
  • Allows: withdraw of principal + accumulated fees, read-only views, owner withdrawManagementFees (optional policy to pause this, but current implementation allows owner-only).

Who can pause:

  • The contract owner (strongly recommended to be a multisig). Function: emergencyPause() toggles the state.

How to verify pause state:

  • Call paused() on the lender contract; true means withdraw-only mode is active.

Suggested incident flow:

  1. Detect anomaly β†’ call emergencyPause() to enable withdraw-only mode.
  2. Communicate status and guidance to LPs (withdraw if necessary).
  3. Diagnose and decide whether to unpause or migrate.
  4. For migrations: keep paused, deploy a new version, publish addresses, guide users to withdraw from old and deposit into new.
  5. Unpause only when risk is cleared or migration is complete.

Gas Optimization

  • Efficient share-based accounting
  • Minimal storage operations
  • Optimized compiler settings
  • Gas reporter integration

License

GPL-3.0-or-later License - see LICENSE file for details.

⚠️ This software is provided "as is" without warranty. Use at your own risk.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

About

Flash loan platform for ERC20 tokens

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •