# ๐Ÿš€ DeFi Starter Kit > A comprehensive TypeScript + Foundry starter kit for building on top of core DeFi protocols including Aave v3, Uniswap v3/v4, Protocolink, Compound III, Balancer v3, and Curve crvUSD. [![TypeScript](https://img.shields.io/badge/TypeScript-5.5-blue.svg)](https://www.typescriptlang.org/) [![Foundry](https://img.shields.io/badge/Foundry-Latest-orange.svg)](https://getfoundry.sh/) [![License](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE) --- ## โœจ Features | Feature | Description | Status | |---------|-------------|--------| | ๐ŸŒ **Multi-chain** | Ethereum, Base, Arbitrum, Optimism, Polygon | โœ… | | ๐Ÿ”’ **Type-safe** | Full TypeScript types for all addresses and configurations | โœ… | | ๐Ÿญ **Production-ready** | All examples include error handling, slippage protection | โœ… | | ๐Ÿงช **Comprehensive testing** | Foundry fork tests for all major integrations | โœ… | | ๐Ÿ› ๏ธ **Modern tooling** | viem, Foundry, Protocolink SDK | โœ… | | ๐Ÿ” **Security focus** | Security checklists, best practices documented | โœ… | | ๐Ÿ”Œ **Extensible** | Easy to add new chains, protocols, examples | โœ… | --- ## ๐Ÿš€ Quick Start ### ๐Ÿ“ฆ Installation ```bash # Install dependencies pnpm install # Install Foundry (if not already installed) curl -L https://foundry.paradigm.xyz | bash foundryup ``` ### โš™๏ธ Environment Setup Before running tests, set up your environment variables: ```bash # 1. Copy example environment file cp .env.example .env # 2. Edit .env and add your RPC URLs # MAINNET_RPC_URL=https://mainnet.infura.io/v3/YOUR_KEY # 3. Verify setup pnpm run check:env pnpm run verify:setup ``` > ๐Ÿ“– See [docs/ENV_SETUP.md](./docs/ENV_SETUP.md) for detailed setup instructions. --- ## ๐Ÿงช DeFi Strategy Testing The project includes a comprehensive DeFi strategy testing CLI for testing strategies against local mainnet forks. ### ๐ŸŽฏ Quick Commands ```bash # Run a strategy scenario pnpm run strat run scenarios/aave/leveraged-long.yml # Run with custom network and reports pnpm run strat run scenarios/aave/leveraged-long.yml \ --network mainnet \ --report out/run.json \ --html out/report.html # Fuzz test a scenario pnpm run strat fuzz scenarios/aave/leveraged-long.yml --iters 100 --seed 42 # List available failure injections pnpm run strat failures # Compare two runs pnpm run strat compare out/run1.json out/run2.json # Test script with real fork export MAINNET_RPC_URL=https://mainnet.infura.io/v3/YOUR_KEY pnpm run strat:test ``` ### โœจ Strategy Testing Features - โœ… **Aave v3 adapter** - Supply, borrow, repay, withdraw, flash loans - โœ… **Uniswap v3 adapter** - Swaps with slippage protection - โœ… **Compound v3 adapter** - Supply, borrow, repay - โœ… **Failure injection** - Oracle shocks, time travel, liquidity shocks - โœ… **Fuzzing** - Parameterized inputs for edge case discovery - โœ… **Automatic token funding** - Via whale impersonation - โœ… **Multiple reports** - HTML, JSON, and JUnit XML > ๐Ÿ“– See [docs/STRATEGY_TESTING.md](./docs/STRATEGY_TESTING.md) for comprehensive documentation and [scenarios/README.md](./scenarios/README.md) for example scenarios. --- ## ๐ŸŽ“ Examples ### ๐Ÿ“ Run Examples ```bash # Aave supply and borrow tsx examples/ts/aave-supply-borrow.ts # Uniswap v3 swap tsx examples/ts/uniswap-v3-swap.ts # Protocolink multi-protocol composition tsx examples/ts/protocolink-compose.ts # Compound III supply and borrow tsx examples/ts/compound3-supply-borrow.ts ``` ### ๐Ÿงช Run Tests ```bash # Run Foundry tests forge test # Run tests with fork forge test --fork-url $MAINNET_RPC_URL ``` ### ๐Ÿ–ฅ๏ธ Use CLI ```bash # Build a transaction plan pnpm run cli build-plan -- --chain 1 # Get a quote pnpm run cli quote -- --protocol uniswapv3 --type swap --token-in USDC --token-out WETH --amount 1000 # Execute a plan pnpm run cli execute -- --chain 1 --plan plan.json ``` --- ## ๐Ÿ“ Project Structure ``` . โ”œโ”€โ”€ config/ โ”‚ โ”œโ”€โ”€ chains/ # ๐Ÿ”— Chain-specific configurations โ”‚ โ”‚ โ”œโ”€โ”€ mainnet.ts โ”‚ โ”‚ โ”œโ”€โ”€ base.ts โ”‚ โ”‚ โ””โ”€โ”€ ... โ”‚ โ””โ”€โ”€ addresses.ts # ๐Ÿ“ Address exports โ”œโ”€โ”€ contracts/ โ”‚ โ”œโ”€โ”€ examples/ # ๐Ÿ“œ Solidity example contracts โ”‚ โ””โ”€โ”€ interfaces/ # ๐Ÿ”Œ Contract interfaces โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ cli/ # ๐Ÿ–ฅ๏ธ CLI implementation โ”‚ โ”œโ”€โ”€ strat/ # ๐Ÿงช Strategy testing framework โ”‚ โ””โ”€โ”€ utils/ # ๐Ÿ› ๏ธ Utility functions โ”œโ”€โ”€ examples/ โ”‚ โ”œโ”€โ”€ ts/ # ๐Ÿ“˜ TypeScript examples โ”‚ โ””โ”€โ”€ subgraphs/ # ๐Ÿ” Subgraph queries โ”œโ”€โ”€ test/ # ๐Ÿงช Foundry tests โ””โ”€โ”€ docs/ # ๐Ÿ“š Documentation ``` --- ## ๐Ÿ”Œ Supported Protocols ### ๐Ÿฆ Aave v3 - โœ… Supply and borrow - โœ… Flash loans (single and multi-asset) - โœ… Pool discovery via PoolAddressesProvider ### ๐Ÿ”„ Uniswap v3/v4 - โœ… Token swaps - โœ… TWAP oracles - โœ… Permit2 integration - โœ… Universal Router ### ๐Ÿ”— Protocolink - โœ… Multi-protocol composition - โœ… Batch transactions - โœ… Permit2 integration ### ๐Ÿ›๏ธ Compound III - โœ… Supply collateral - โœ… Borrow base asset ### ๐Ÿ”ท Additional Protocols - โš™๏ธ Balancer v3 - โš™๏ธ Curve crvUSD --- ## ๐Ÿ“˜ Code Examples ### ๐Ÿฆ Aave v3: Supply and Borrow ```typescript import { createWalletRpcClient } from './src/utils/chain-config.js'; import { getAavePoolAddress } from './src/utils/addresses.js'; const walletClient = createWalletRpcClient(1, privateKey); const poolAddress = getAavePoolAddress(1); // Supply collateral await walletClient.writeContract({ address: poolAddress, abi: POOL_ABI, functionName: 'supply', args: [asset, amount, account, 0], }); // Borrow await walletClient.writeContract({ address: poolAddress, abi: POOL_ABI, functionName: 'borrow', args: [debtAsset, borrowAmount, 2, 0, account], }); ``` ### ๐Ÿ”„ Uniswap v3: Swap ```typescript import { getUniswapSwapRouter02 } from './src/utils/addresses.js'; const routerAddress = getUniswapSwapRouter02(1); await walletClient.writeContract({ address: routerAddress, abi: SWAP_ROUTER_ABI, functionName: 'exactInputSingle', args: [swapParams], }); ``` ### ๐Ÿ”— Protocolink: Multi-Protocol Composition ```typescript import * as api from '@protocolink/api'; // Build swap logic const swapQuotation = await api.protocols.uniswapv3.getSwapTokenQuotation(chainId, { input: { token: USDC, amount: '1000' }, tokenOut: WBTC, slippage: 100, }); const swapLogic = api.protocols.uniswapv3.newSwapTokenLogic(swapQuotation); // Build supply logic const supplyQuotation = await api.protocols.aavev3.getSupplyQuotation(chainId, { input: swapQuotation.output, }); const supplyLogic = api.protocols.aavev3.newSupplyLogic(supplyQuotation); // Execute const routerData = await api.router.getRouterData(chainId, { account, logics: [swapLogic, supplyLogic], }); ``` --- ## ๐Ÿ“š Documentation | Document | Description | |----------|-------------| | ๐Ÿ“– [Integration Guide](./docs/INTEGRATION_GUIDE.md) | Step-by-step integration guide | | ๐Ÿ” [Security Best Practices](./docs/SECURITY.md) | Security checklist and best practices | | ๐Ÿ”— [Chain Configuration](./docs/CHAIN_CONFIG.md) | How to add new chains | | ๐Ÿงช [Strategy Testing](./docs/STRATEGY_TESTING.md) | Comprehensive strategy testing guide | | โš™๏ธ [Environment Setup](./docs/ENV_SETUP.md) | Environment variable configuration | --- ## ๐ŸŒ Supported Chains | Chain | Chain ID | Status | |-------|----------|--------| | Ethereum Mainnet | 1 | โœ… | | Base | 8453 | โœ… | | Arbitrum One | 42161 | โœ… | | Optimism | 10 | โœ… | | Polygon | 137 | โœ… | --- ## ๐Ÿ” Security > โš ๏ธ **IMPORTANT**: This is a starter kit for learning and development. Before deploying to production: 1. โœ… Review all security best practices in [docs/SECURITY.md](./docs/SECURITY.md) 2. โœ… Get professional security audits 3. โœ… Test thoroughly on testnets 4. โœ… Start with small amounts on mainnet 5. โœ… Understand the risks of each protocol --- ## ๐Ÿค Contributing Contributions are welcome! Please: 1. ๐Ÿด Fork the repository 2. ๐ŸŒฟ Create a feature branch 3. โœ๏ธ Make your changes 4. ๐Ÿงช Add tests 5. ๐Ÿ“ค Submit a pull request --- ## ๐Ÿ“„ License MIT --- ## ๐Ÿ”— Resources | Resource | Link | |----------|------| | Aave Documentation | [docs.aave.com](https://docs.aave.com/) | | Uniswap Documentation | [docs.uniswap.org](https://docs.uniswap.org/) | | Protocolink Documentation | [docs.protocolink.com](https://docs.protocolink.com/) | | Compound III Documentation | [docs.compound.finance](https://docs.compound.finance/) | | Viem Documentation | [viem.sh](https://viem.sh/) | | Foundry Documentation | [book.getfoundry.sh](https://book.getfoundry.sh/) | --- ## โš ๏ธ Disclaimer This software is provided "as is" without warranty of any kind. Use at your own risk. The authors are not responsible for any losses incurred from using this software.