This repository is a simple FundMe dApp example built with Foundry.
The contract accepts ETH deposits only when the deposit is worth at least $5 USD, using a Chainlink ETH/USD price feed. The contract owner can withdraw the funds, and the contract supports both withdraw() and a gas-optimized cheaperWithdraw().
src/FundMe.sol- Main funding contractsrc/PriceConverter.sol- Library to convert ETH to USD using Chainlink price feedsscript/DeployFundMe.s.sol- Deployment script that usesHelperConfigto select the correct price feed addressscript/HelperConfig.s.sol- Local vs live network configuration, deploys a mock price feed for Anviltest/unit/FundMeTest.t.sol- Unit tests for funding and withdrawal behavior
- Foundry installed
forge,cast, andanvilavailable in your PATH
If the repository dependencies are not already installed, run:
forge installThen build the project:
forge buildRun the full test suite:
forge testRun only the FundMe unit tests:
forge test --match-contract FundMeTestStart a local node with Anvil:
anvilDeploy locally using the script; HelperConfig deploys a mock ETH/USD feed automatically on Anvil:
forge script script/DeployFundMe.s.sol:DeployFundMe --broadcast --rpc-url http://127.0.0.1:8545 --private-key <PRIVATE_KEY>Use a real RPC URL and private key. The deployment script will choose the correct price feed address for Sepolia.
forge script script/DeployFundMe.s.sol:DeployFundMe --rpc-url <RPC_URL> --private-key <PRIVATE_KEY> --broadcastfund()requires at leastMINIMUM_USDworth of ETH, calculated using the Chainlink price feedwithdraw()transfers the entire contract balance to the owner and resets funderscheaperWithdraw()performs the same action using a more gas-efficient loopfallback()andreceive()both callfund()so sending ETH directly also funds the contract
HelperConfigreturns a live Chainlink feed address on Sepolia and Mainnet- For local testing, it deploys a mock
MockETHUSDAggregatorwith an initial price - The contract stores funders and their funded amounts in mappings and arrays
forge fmt
forge test
forge build
anvil- Foundry docs: https://book.getfoundry.sh/
- Chainlink price feeds: https://docs.chain.link/data-feeds/