Skip to content

legendarycode3/fund-me-smart-contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

116 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ABOUT PROJECT:

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)".

Project Overview

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

Features

  • 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

Technology Stack

  • Solidity - Smart contract programming language
  • Foundry(forge, cast, anvil) - Development framework and testing suite
  • Chainlink Price/Data Feeds (real + mock)
  • Chainlink Brownie Contracts

Getting Started

Prerequisites /

  • 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.

Installation

  1. Clone the repository:

       git clone https://github.com/legendarycode3/fund-me-smart-contract 
       cd fund-me-smart-contract
  2. Install dependencies:

      forge install smartcontractkit/chainlink-brownie-contracts@0.6.1  
      forge install foundry-rs/forge-std 
      make install  Or   
      forge install
  3. Build the project:

      make build

Foundry

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.

Documentation

https://book.getfoundry.sh/

Usage

Build

$ forge build

Testing

Run all tests:

$ forge test

Or

make test

Run tests with verbosity:

make test -vvv

Run specific test:

forge test --mt testFunctionName

Test coverage:

make coverage

Example test scenarios:

  • Funding below minimum threshold (should revert)
  • Successful funding above minimum
  • Non-owner withdrawal attempt (should revert)
  • Owner withdrawal functionality
  • Price conversion accuracy

Format

$ forge fmt

Gas Snapshots

$ forge snapshot

Anvil

$ anvil

Deploy

$ 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

$ cast <subcommand>

Help

$ forge --help
$ anvil --help
$ cast --help

Smart Contract Details

Functions

  • 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.

Configuration

Create a .env file with the following variables:

SEPOLIA_RPC_URL=your_sepolia_rpc_url 
ETHERSCAN_API_KEY=your_etherscan_api_key  

Project Structure

├──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

Gas Optimization

The contract implements several gas optimization techniques:

  • Immutable owner variable
  • Constant minimum USD value
  • Efficient array resetting in withdrawal

📚 References

  • Foundry Book
  • Foundry Fundamentals

Learn More

  • Solidity Documentation
  • Foundry Documentation
  • Chainlink Documentation

Author

LegendaryCode

About

A decentralized smart contract 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 , and the smart contract keeps track of the doners in case they are to be rewarded in the future.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors