Solidity Smart Contract Example Development (1) (Explanation + Notes) - Donations and Withdrawals

General requirements of the project
* Allow any user to donate to this contract
* Allow the contract deployer to withdraw the money donated by the user a>
* You can see how much each user donated
* If a minimum amount is set if it is less than this amount, the donation will fail


Running environment: remix (version 0.8.13)

EndMe.sol

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

contract FundMe{

    address public  owner;
    uint public  donation;
    uint minimumUSD = 100;

//做一个合约的动态地址 记录每个捐款者的地址
    address[]  public  fundes;

    //定义一个mapping方法定义每一个地址捐款多少钱
    mapping (address => uint) public  Fund_address;

    constructor() {
        owner = msg.sender;
    }

    function Fund() public  payable {
        require(msg.value > 500 ,"too little must than 50wei");
        fundes.push(msg.sender);
        Fund_address[msg.sender] = msg.value;
    }

    function getFund() public  view returns (address[] memory){
        return (fundes);
    }
    //定义一个函数把合约创建的地址修改给其他地址
    function setAddress(address _newOwner) public onlyOwner{
        owner = _newOwner;
    } 

    modifier onlyOwner {
        require(msg.sender == owner,"you are not deployer");
        _;
    }

    //防止有人直接转钱给合约不通过function
    receive() external payable {
        Fund();
    }

    fallback() external payable {
        Fund();
    }
 
    function withdrawl() public onlyOwner{
        // require(msg.sender == owner, "you are not deployer");

        for (uint i ; i < fundes.length;i++){
            Fund_address[fundes[i]] = 0;
        }

        (bool cassSuccess,bytes memory data) =  payable (msg.sender).call{value:address(this).balance}("");
        require(cassSuccess,"transaction failed");

//将funders这个地址元组回到初始化
        fundes = new  address[](0);
    }
}

If you don’t understand, you can leave a message in the comment area directly. I will reply.

Guess you like

Origin blog.csdn.net/m0_72435337/article/details/131614391