Fund-Me Smart Contract: This is a minimal project allowing user to fund the contract owner with donations. The smart contract accepts ETH as donations, dominated in USD. Donations has minimum USD value , otherwise they are rejected. The value is priced using a Chainlink Price Feed , and the smart contract keeps track of the doners in case they are to be rewarded in the future.
The FundMe Project smart contract enables transparent, "decentralized crowdfunding", acting like a programmable Kickstarter on the blockchain. Its core purpose is to "securely accept native cryptocurrency donations (fund)", "track contributor data", "enforce a minimum USD value using Chainlink oracles", and allow "authorized withdrawals (withdraw)".
The FundMe contract enables decentralized fundraising with the following key features:
- Minimum funding requirement of $5 USD (converted from ETH)
- Real-time ETH/USD price conversion using Chainlink oracles
- Owner-only can withdraw functionality
- Automatic fund() execution via fallback and receive functions
It demonstrates:
- Price feed integration via AggregatorV3Interface
- Mocking Chainlink feeds for local testing
- Foundry-style unit, integration tests
- Minimum Contribution: $5 USD equivalent in ETH
- Price Oracle Integration: Uses Chainlink ETH/USD price feeds (Sepolia testnet)
- Access Control: Only contract deployer can withdraw funds
- Fallback Support: Automatically processes direct ETH transfers as donations
- Solidity - Smart contract programming language
- Foundry(forge, cast, anvil) - Development framework and testing suite
- Chainlink Price/Data Feeds (real + mock)
- Chainlink Brownie Contracts
- Solidity ^0.8.x
- Chainlink contracts
- Apart of being able to deploy on local blockchain (like Anvil) . Access to Ethereum free testnet (Sepolia) for deployment, is needed. Which is typically similar to mainnet deployment.
-
Clone the repository:
git clone https://github.com/legendarycode3/fund-me-smart-contract
cd fund-me-smart-contract -
Install dependencies:
forge install smartcontractkit/chainlink-brownie-contracts@0.6.1
forge install foundry-rs/forge-std
make install Or forge install
-
Build the project:
make build
Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
Foundry consists of:
- Forge: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- Cast: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- Anvil: Local Ethereum node, akin to Ganache, Hardhat Network.
- Chisel: Fast, utilitarian, and verbose solidity REPL.
$ forge buildRun all tests:
$ forge testOr
make testRun tests with verbosity:
make test -vvvRun specific test:
forge test --mt testFunctionNameTest coverage:
make coverageExample test scenarios:
- Funding below minimum threshold (should revert)
- Successful funding above minimum
- Non-owner withdrawal attempt (should revert)
- Owner withdrawal functionality
- Price conversion accuracy
$ forge fmt$ forge snapshot$ anvil$ forge script script/DeployFundMe.s.sol:DeployFundMe --rpc-url <your_rpc_url> --private-key <your_private_key>Deploy to Sepolia testnet:
make deploy-sepolia
$ cast <subcommand>$ forge --help
$ anvil --help
$ cast --help- fund(): Allows users to contribute funds to the campaign. Requires the sent amount to meet the minimum USD requirement.
- getPrice(): Retrieves the latest ETH to USD conversion rate using Chainlink's AggregatorV3Interface.
- getVersion(): Retrieves the version of the Chainlink AggregatorV3Interface being used.
- getConversionRate(uint256 ethAmount): Calculates the equivalent USD amount for the provided ETH amount.
- withdraw(): Enables the contract owner to be able to pull all funds (Ether) out of the contract to their own wallet.
Create a .env file with the following variables:
SEPOLIA_RPC_URL=your_sepolia_rpc_url
ETHERSCAN_API_KEY=your_etherscan_api_key ├──script
│ ├── DeployFundMe.s.sol
│ ├── HelperConfig.s.sol
│ └── Interactions.s.sol
├── src
├── PriceConverterLibrary.sol
│ └── FundMe.sol
└── test
├── integration
│ └── FundMeTestIntegrationOrIntetractionTest.t.sol
├── mocks
│ └── MockV3Aggregator.sol
└── unit
└── FundMeTest.t.sol
├── foundry.toml
└── README.md
The contract implements several gas optimization techniques:
- Immutable owner variable
- Constant minimum USD value
- Efficient array resetting in withdrawal
- Foundry Book
- Foundry Fundamentals
- Solidity Documentation
- Foundry Documentation
- Chainlink Documentation
LegendaryCode
- LinkedIn: @legendarycode3
- Twitter: @legendary_code_
- Github: @legendarycode3