-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFundMe.sol
83 lines (67 loc) · 2.61 KB
/
FundMe.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Get funds from users
// Withdraw funds
// Set a minmum fundinng value in USD
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import { PriceConverter } from './PriceConverter.sol';
// Custom Errors in Solidity https://soliditylang.org/blog/2021/04/21/custom-errors/
error NotOwner();
contract FundMe {
// allow user to send $
// have a minimun $ send
using PriceConverter for uint256;
// Immutability and constants
uint256 public constant MINIMUN_USD = 1e18;
address[] public funders;
mapping(address funder => uint256 amountFuned) public addressToAmountFuned;
address public immutable i_owner;
// when contract init run this code
constructor() {
i_owner = msg.sender;
}
function fund() public payable{
// 1. how do we sent ETH to this contract
// 1 ether = 1000000000000000000 wei = 1*10**18 wei = 1e18 wei
// require(msg.value > 1e18, "Didn't send enough ETH");
// require(msg.value > 1 ether, "Didn't send enough ETH");
// require(getConvertionRate(msg.value) >= MINIMUN_USD, "Didn't send enough ETH");
require(msg.value.getConvertionRate() >= MINIMUN_USD, "Didn't send enough ETH");
// msg.sender
funders.push(msg.sender);
addressToAmountFuned[msg.sender] = addressToAmountFuned[msg.sender] + msg.value;
}
function withdraw() public onlyOwner {
for (uint256 funderIndex = 0; funderIndex < funders.length; funderIndex++) {
// get funders address
address funder = funders[funderIndex];
addressToAmountFuned[funder] = 0;
}
// reset array
funders = new address[](0);
// withdraw the funds
// https://solidity-by-example.org/sending-ether/ transfer send call
// transfer
// payable(msg.sender).transfer(address(this).balance);
// // send
// bool sendSuccess = payable(msg.sender).send(address(this).balance);
// require(sendSuccess, "Send fail");
// call
(bool callSuccess,) = payable(msg.sender).call{value: address(this).balance}("");
require(callSuccess, "call fail");
}
modifier onlyOwner () {
// first solution
// require(i_owner == msg.sender, "Must be owner!");
// section solution
if(i_owner == msg.sender) { revert NotOwner();}
_;
}
// what happens if somebody send this contract ETH without calling the fund function?
// two sepesceil function receive() fallback()
receive() external payable {
fund();
}
fallback() external payable {
fund();
}
}