A decentralized lending platform built on Ethereum that allows users to borrow CORN tokens using ETH as collateral. This project is part of the SpeedRunEthereum challenges.
This lending dApp creates a simple but powerful over-collateralized lending system where users deposit ETH as collateral and borrow CORN tokens against it. The system maintains safety through liquidation mechanisms and collateralization ratios.
- ETH Collateral Deposits: Users deposit ETH to secure their borrowing position
- CORN Token Borrowing: Borrow CORN tokens up to 83.33% of collateral value (120% collateral ratio)
- Liquidation System: Positions below 120% collateral ratio can be liquidated by anyone
- Liquidator Incentives: 10% bonus reward for liquidators to ensure system stability
- Price Oracle Integration: Uses CornDEX contract as price oracle for ETH/CORN exchange rates
The project consists of four main contracts:
The core contract that handles:
- Collateral deposits and withdrawals
- CORN borrowing and repayment
- Position health calculations
- Liquidation mechanics
- Standard ERC20 token that users can borrow
- The borrowable asset in the lending system
- DEX contract for ETH/CORN swaps
- Acts as price oracle for collateral valuation
- Enables price manipulation for testing scenarios
- Utility contract for making large swaps
- Used to change asset ratios and test price movements
- Deposit Collateral: Users send ETH to the contract as collateral
- Borrow CORN: Users can borrow CORN tokens up to 83.33% of their ETH collateral value
- Maintain Health: Position must stay above 120% collateralization ratio
- Repay or Get Liquidated:
- Repay CORN to maintain position
- If ratio drops below 120%, anyone can liquidate for 10% profit
- Withdraw: Remove excess collateral after repaying loans
addCollateral(): Deposit ETH as collateral (payable)withdrawCollateral(uint256 amount): Withdraw excess collateralborrowCorn(uint256 borrowAmount): Borrow CORN against collateralrepayCorn(uint256 repayAmount): Repay borrowed CORN tokensliquidate(address user): Liquidate underwater positions
calculateCollateralValue(address user): Get collateral value in CORN termsisLiquidatable(address user): Check if position can be liquidated_calculatePositionRatio(address user): Calculate current collateral ratio
uint256 public constant COLLATERAL_RATIO = 120e18; // 120% minimum ratio
uint256 public constant LIQUIDATOR_REWARD = 10; // 10% liquidation bonus- Minimum Ratio: 120% (borrowers must have $1.20 in ETH for every $1.00 in CORN borrowed)
- Maximum Borrow: ~83.33% of collateral value
- Liquidation Trigger: When ratio falls below 120%
- Position becomes liquidatable when collateral ratio < 120%
- Liquidator repays borrower's CORN debt
- Liquidator receives equivalent ETH collateral + 10% bonus
- Original borrower keeps their CORN tokens but loses collateral
- Node.js (v18 LTS)
- Yarn (v1 or v2+)
- Git
# Clone the challenge
npx create-eth@1.0.2 -e challenge-over-collateralized-lending challenge-over-collateralized-lending
cd challenge-over-collateralized-lending
# Start local blockchain
yarn chain
# Deploy contracts (new terminal)
yarn deploy
# Start frontend (new terminal)
yarn start# Run market simulation with bots
yarn simulate
# Fresh deployment
yarn deploy --resetThis challenge demonstrates:
- Over-Collateralized Lending: Understanding why DeFi lending requires more collateral than borrowed value
- Liquidation Mechanics: Building automated systems to prevent bad debt
- Price Oracle Integration: Using external price feeds for collateral valuation
- Financial Risk Management: Implementing safety margins and incentive structures
- Smart Contract Security: Position validation and safe transfer patterns
# Generate deployer account
yarn generate
# Check balance
yarn account
# Deploy to testnet (get testnet ETH first)
yarn deploy --network sepolia
# Verify contracts
yarn verify --network sepolia# Configure target network in scaffold.config.ts
# Deploy frontend
yarn vercel- Basic Flow: Deposit ETH โ Borrow CORN โ Repay โ Withdraw
- Price Movement: Use +/- buttons to change CORN price and see position health change
- Liquidation: Create underwater position and liquidate it from another account
- Multi-Account: Use private browser tabs to simulate multiple users
- โ
addCollateral()andwithdrawCollateral()functions working - โ
borrowCorn()andrepayCorn()functions implemented - โ Helper functions for position calculations
- โ
liquidate()function with proper incentives - โ Position validation preventing unsafe withdrawals
- โ Frontend integration and wallet connection
- โ Contracts deployed to testnet
- โ Contract verification on Etherscan
- โ Frontend deployed to public URL
- Maintaining Price Exposure: Keep ETH exposure while accessing liquidity
- Leverage Trading: Borrow assets to increase position sizes
- Tax Optimization: Access funds without triggering taxable sale events
Built with ๐ฝ as part of the SpeedRunEthereum journey
Educational Purpose: This is a simplified lending protocol for learning. Production systems require additional security measures, proper oracles, and comprehensive audits.