Skip to content

Mahmoud-Mourad-Dev/FundMe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

Solidity smart contract example fundMe application


fundMe1 example

1- Users can contribute funds by calling the contribute function and sending Ether with the transaction.

2- The contributed amount is stored in the contributions mapping, which maps addresses to their contributed amounts.

3- The addresses of contributors are stored in the contributors array.

4- The owner of the contract (deployer) can withdraw the funds by calling the withdraw function, which transfers the contract's balance to the owner's address.

5- This is a basic example. Depending on your requirements, you might want to add more features such as withdrawal limits, fundraising goals, or refund functionality.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

contract fundMe1 {
    // mapping store the amount of contributed
    mapping (address => uint256) public contributions;
    // array store address of contributors
    address [] public contributors;
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    // the main two function in this contract 
    function contribute() public payable{
        //contributions must be greater than 0
        require(msg.value>0,"contributions must be greater than 0");
        // add address of contributions to mapping
        if (contributions[msg.sender]==0){
            contributors.push(msg.sender);


        }
    }


    function withdraw() public{
        require(msg.sender== owner,"owner can only withdraw this fund");
        // withdraw all balance 
        payable(msg.sender).transfer(address(this).balance); 

    }

    }

fundMe2 example

Another basic example fundMe smart contract that allow user to contribute funds

// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
contract fundMe2{
    // address of the owner of the contract
    address public owner;

    // track total amount of fund raised
    uint256 public totalFund;

    // mapping to store contributions amount of address
    mapping(address=> uint256) public contributions;

    // constructor to set owner
    constructor() {
        owner=msg.sender;
    }

    // function to allow user to contribute
    function contribute() public payable{
        require(msg.value>0,"contributions must be greater than 0");
        contributions[msg.sender]+= msg.value;
        totalFund+=msg.value;

    }

    //function to withdraw fund by the owner
    function withdraw() public payable onlyOwner{
        payable(owner).transfer(address(this).balance);

    }

    // Modifier to restrict functions to the owner
    modifier onlyOwner() {
        require(msg.sender == owner, "Only owner can withdraw funds");
        _; // This line allows the function code to run after the modifier check.
    }

}

fundMe3 Advanced

I take this contract example from patric collins course

First we start with two main function

Main Two Function

// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
contract fundMe3{

    /*  1- Function allow user to send money 

    */
    function fund() public {
        
    }

    function withdraw() public{

    }
}

1- We have to make fund function to recieve and sent ethereum to do that we use payable keyword

2- user should be send 1 ether at least we use require to do that

3- the require statement has the power to control the behavior of the transaction

4- soldity have global variable like (msg.value) number of wei send with transaction

5- 1ehereum = 1e18 wei site converter

fund function

// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
contract fundMe3{

    function fund() public payable {
        require (msg.value>=1e18,"you have to send at least 1 eth");
        
    }

    function withdraw() public{

    }
}

1- replace minimum fund from 1 eth to 5 dollar

// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
contract fundMe3{
    uint256 minimumUsd=5;

    function fund() public payable {
        require (msg.value>=minimumUsd,"you have to send at least 1 eth");
        
    }

    function withdraw() public{

    }
}

Convert eth to usd

1- how to convert ethereum to dollar using chainlink data feeds

2- This conversion requires us to identify the price of Ethereum

3- chainlink data feeds

4- chainlink Price feeds address

// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

contract fundMe3{
    uint256 minimumUsd=5;

    function fund() public payable {
        require (msg.value>=minimumUsd,"you have to send at least 1 eth");
        
    }

    function withdraw() public{

    }

    function getPrice() public{

    }

    function getConversionRate() public{

    }
}

solidity interface

1- Chainlink Data Feeds sepolia testnet address 0x694AA1769357215DE4FAC081bf1f309aDC325306

Solidity interface  A Mode of interaction

1- Copy the Aggregator V3 Interface from Chainlink's GitHub

2- The interface allows us to call these functions on the contract without needing the contract code

3- This interface is what we need to interact with the contract for our task

4- It contains the getVersion() function, among others

5- Aggregator to interact with the Chainlink Price Feed contract and retrieve the current version.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
interface AggregatorV3Interface {

  function decimals() external view returns (uint8);

  function description()external view returns (string memory );

  function version() external view returns ( uint256  );

  function getRoundData(uint80 _roundId ) external view  
  returns (
      uint80 roundId,
      int256 answer,
      uint256 startedAt,
      uint256 updatedAt,
      uint80 answeredInRound
    );

  function latestRoundData()external view
    returns (
      uint80 roundId,
      int256 answer,
      uint256 startedAt,
      uint256 updatedAt,
      uint80 answeredInRound
    );

}

contract fundMe3{
    uint256 minimumUsd=5;

    function fund() public payable {
        require (msg.value>=minimumUsd,"you have to send at least 1 eth");
        
    }

    function withdraw() public{

    }

    function getPrice() public{

    }

    function getConversionRate() public{

    }

    function getVersion() public view returns(uint256){
        return AggregatorV3Interface(0x694AA1769357215DE4FAC081bf1f309aDC325306).version();
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published