# ๐Ÿ”— Chain Configuration Guide How to add and configure new chains in the DeFi Starter Kit. --- ## ๐Ÿ“‹ Overview This guide walks you through adding a new blockchain network to the DeFi Starter Kit. You'll need to configure: - ๐Ÿ”— RPC endpoints - ๐Ÿ“ Protocol contract addresses - ๐Ÿ’ฐ Token addresses - ๐Ÿ”ง Viem chain configuration --- ## ๐Ÿš€ Adding a New Chain ### 1๏ธโƒฃ Create Chain Config File Create a new file in `config/chains/` with your chain configuration: ```typescript // config/chains/yourchain.ts import type { ChainConfig } from '../types.js'; export const yourchain: ChainConfig = { chainId: 12345, // Your chain ID name: 'Your Chain', rpcUrl: process.env.YOURCHAIN_RPC_URL || 'https://rpc.yourchain.com', // Aave v3 aave: { poolAddressesProvider: '0x...', // Aave PoolAddressesProvider pool: '0x...', // Aave Pool }, // Uniswap uniswap: { swapRouter02: '0x...', // Uniswap SwapRouter02 universalRouter: '0x...', // Uniswap Universal Router permit2: '0x000000000022D473030F116dDEE9F6B43aC78BA3', // Permit2 (same across chains) quoterV2: '0x...', // Uniswap QuoterV2 }, // Protocolink protocolink: { router: '0x...', // Protocolink Router }, // Compound III compound3: { cometUsdc: '0x...', // Compound III Comet (if available) }, // Common Tokens tokens: { WETH: '0x...', USDC: '0x...', USDT: '0x...', DAI: '0x...', WBTC: '0x...', }, }; ``` ### 2๏ธโƒฃ Register Chain in Addresses Add your chain to `config/addresses.ts`: ```typescript import { yourchain } from './chains/yourchain.js'; export const chainConfigs: Record = { 1: mainnet, 8453: base, // ... other chains 12345: yourchain, // Add your chain }; // Re-export export { yourchain }; ``` ### 3๏ธโƒฃ Add Viem Chain Add your chain to `src/utils/chain-config.ts`: ```typescript import { yourChain } from 'viem/chains'; const viemChains = { 1: mainnet, 8453: base, // ... other chains 12345: yourChain, // Add your chain }; ``` ### 4๏ธโƒฃ Update Environment Variables Add RPC URL to `.env.example`: ```bash YOURCHAIN_RPC_URL=https://rpc.yourchain.com ``` ### 5๏ธโƒฃ Update Foundry Config Add RPC endpoint to `foundry.toml`: ```toml [rpc_endpoints] yourchain = "${YOURCHAIN_RPC_URL}" ``` --- ## ๐Ÿ“ Getting Official Addresses ### ๐Ÿฆ Aave v3 1. ๐Ÿ“š Check [Aave Documentation](https://docs.aave.com/developers/deployed-contracts/deployed-contracts) 2. ๐Ÿ” Find your chain in the deployed contracts list 3. ๐Ÿ“‹ Get `PoolAddressesProvider` address 4. ๐Ÿ”— Use `PoolAddressesProvider.getPool()` to get Pool address ### ๐Ÿ”„ Uniswap v3 1. ๐Ÿ“š Check [Uniswap Deployments](https://docs.uniswap.org/contracts/v3/reference/deployments) 2. ๐Ÿ” Find your chain's deployment page 3. ๐Ÿ“‹ Get addresses for: - `SwapRouter02` - `UniversalRouter` - `Permit2` (same address across all chains: `0x000000000022D473030F116dDEE9F6B43aC78BA3`) - `QuoterV2` ### ๐Ÿ”— Protocolink 1. ๐Ÿ“š Check [Protocolink Deployment Addresses](https://docs.protocolink.com/smart-contract/deployment-addresses) 2. ๐Ÿ” Find your chain 3. ๐Ÿ“‹ Get Router address ### ๐Ÿ›๏ธ Compound III 1. ๐Ÿ“š Check [Compound III Documentation](https://docs.compound.finance/) 2. ๐Ÿ” Find your chain's Comet addresses 3. ๐Ÿ“‹ Get Comet proxy address for your market ### ๐Ÿ’ฐ Common Tokens For each chain, you'll need addresses for: | Token | Description | |-------|-------------| | WETH | Wrapped Ether | | USDC | USD Coin | | USDT | Tether USD | | DAI | Dai Stablecoin | | WBTC | Wrapped Bitcoin | **Resources:** - ๐Ÿ” [Token Lists](https://tokenlists.org/) - ๐Ÿ” [CoinGecko](https://www.coingecko.com/) --- ## โœ… Verifying Addresses Always verify addresses from multiple sources: 1. โœ… Official protocol documentation 2. โœ… Block explorer (verify contract code) 3. โœ… Protocol GitHub repositories 4. โœ… Community resources (Discord, forums) --- ## ๐Ÿงช Testing Your Configuration After adding a new chain: ### 1. Test Chain Config Loading ```typescript import { getChainConfig } from './config/addresses.js'; const config = getChainConfig(12345); console.log(config); ``` ### 2. Test RPC Connection ```typescript import { createRpcClient } from './src/utils/chain-config.js'; const client = createRpcClient(12345); const blockNumber = await client.getBlockNumber(); console.log('Block number:', blockNumber); ``` ### 3. Test Address Resolution ```typescript import { getAavePoolAddress } from './src/utils/addresses.js'; const poolAddress = getAavePoolAddress(12345); console.log('Pool address:', poolAddress); ``` ### 4. Run Examples ```bash # Update example to use your chain ID tsx examples/ts/aave-supply-borrow.ts ``` ### 5. Run Tests ```bash # Update test to use your chain forge test --fork-url $YOURCHAIN_RPC_URL ``` --- ## ๐Ÿ”ง Common Issues ### โŒ RPC URL Not Working **Possible causes:** - โŒ RPC URL is incorrect - โŒ RPC provider doesn't support your chain - โŒ Rate limits exceeded **Solutions:** - โœ… Verify RPC URL is correct - โœ… Try alternative RPC providers - โœ… Check rate limits ### โŒ Addresses Not Found **Possible causes:** - โŒ Protocol not deployed on your chain - โŒ Addresses are incorrect (typos, wrong network) - โŒ Some protocols may not be available on all chains **Solutions:** - โœ… Verify protocol is deployed on your chain - โœ… Double-check addresses for typos - โœ… Check protocol documentation for chain support ### โŒ Token Addresses Wrong **Possible causes:** - โŒ Token addresses are incorrect - โŒ Token decimals differ - โŒ Tokens don't exist on your chain **Solutions:** - โœ… Verify token addresses on block explorer - โœ… Check token decimals - โœ… Ensure tokens exist on your chain --- ## ๐Ÿ“ Chain-Specific Notes ### ๐Ÿš€ Layer 2 Chains | Consideration | Description | |---------------|-------------| | Gas costs | Typically lower than mainnet | | Finality times | May differ from mainnet | | Protocol features | Some protocols may have L2-specific features | ### ๐Ÿงช Testnets | Consideration | Description | |---------------|-------------| | Addresses | Use testnet-specific addresses | | Tokens | Testnet tokens have no real value | | Protocol availability | Some protocols may not be available on testnets | --- ## ๐Ÿ’ก Best Practices 1. โœ… **Always verify addresses** - Don't trust a single source 2. โœ… **Use environment variables** - Never hardcode RPC URLs 3. โœ… **Test thoroughly** - Test on testnet before mainnet 4. โœ… **Document changes** - Update documentation when adding chains 5. โœ… **Keep addresses updated** - Protocols may upgrade contracts --- ## ๐Ÿ”— Resources | Resource | Link | |----------|------| | Aave Deployed Contracts | [docs.aave.com](https://docs.aave.com/developers/deployed-contracts/deployed-contracts) | | Uniswap Deployments | [docs.uniswap.org](https://docs.uniswap.org/contracts/v3/reference/deployments) | | Protocolink Deployment Addresses | [docs.protocolink.com](https://docs.protocolink.com/smart-contract/deployment-addresses) | | Compound III Documentation | [docs.compound.finance](https://docs.compound.finance/) | --- ## ๐Ÿ“š Related Documentation - ๐Ÿ“– [Environment Setup Guide](./ENV_SETUP.md) - ๐Ÿ” [Security Best Practices](./SECURITY.md) - ๐Ÿงช [Strategy Testing Guide](./STRATEGY_TESTING.md)