111 lines
3.1 KiB
Solidity
111 lines
3.1 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
/*
|
|
Get funds from users
|
|
Withdraw funds
|
|
Set a minimum funding value in USD
|
|
*/
|
|
pragma solidity ^0.8.0;
|
|
|
|
import "./PriceConverter.sol";
|
|
// Save Gas by using constant, immutable for variables
|
|
|
|
error NotOwner();
|
|
|
|
// 816914
|
|
contract FundMe {
|
|
/*
|
|
Transaction Fields
|
|
Nonce
|
|
Gas Price
|
|
Gas Limit - Max Gas that this TX can use
|
|
To - Address
|
|
Value - Amount of wei to send
|
|
Data - What to send to the Address (Can Empty)
|
|
v, r, s - components of TX signature
|
|
*/
|
|
|
|
using PriceConverter for uint256;
|
|
uint256 public constant MINIMUM_USD = 50 * 1e18;
|
|
|
|
address[] public funders;
|
|
mapping(address => uint256) public addressToAmountFunded;
|
|
|
|
// immutable only can declare 1 time at contructor
|
|
address public immutable i_owner;
|
|
|
|
AggregatorV3Interface public priceFeed;
|
|
|
|
constructor(address priceFeedAddress) {
|
|
i_owner = msg.sender;
|
|
priceFeed = AggregatorV3Interface(priceFeedAddress);
|
|
}
|
|
|
|
function fund() public payable {
|
|
/*
|
|
Want to be able to set a minimum fund amount in USD
|
|
1. How do we send ETH to this contract
|
|
*/
|
|
//require (msg.value >= 1e18, "Didn't send enough"); // 1e18 = 1 * 10 * 18 = 1000000000000000000
|
|
require(
|
|
msg.value.getConversionRate(priceFeed) >= MINIMUM_USD,
|
|
"Didn't send enough"
|
|
);
|
|
funders.push(msg.sender);
|
|
addressToAmountFunded[msg.sender] = msg.value;
|
|
}
|
|
|
|
// Owner of this contract only can withdraw
|
|
function withdraw() public onlyOwner {
|
|
// Can use modifier to replace
|
|
// require(msg.sender == owner, "Sender is not owner");
|
|
|
|
/* starting index, eding index, step amount */
|
|
for (
|
|
uint256 funderIndex = 0;
|
|
funderIndex < funders.length;
|
|
funderIndex++
|
|
) {
|
|
address funder = funders[funderIndex];
|
|
addressToAmountFunded[funder] = 0;
|
|
}
|
|
// reset the array
|
|
funders = new address[](0);
|
|
|
|
/*
|
|
// transfer - over 2300 gas will cause exception
|
|
payable(msg.sender).transfer(address(this).balance);
|
|
// send - over 2300 gas will not cause exception, only will return true or false
|
|
bool sendSuccess = payable(msg.sender).send(address(this).balance);
|
|
require(sendSuccess, "Send failed");
|
|
*/
|
|
|
|
// call - lower level
|
|
(bool callSuccess, ) = payable(msg.sender).call{
|
|
value: address(this).balance
|
|
}("");
|
|
require(callSuccess, "Call failed");
|
|
}
|
|
|
|
modifier onlyOwner() {
|
|
// require(msg.sender == i_owner, "Sender is not owner!");
|
|
if (msg.sender != i_owner) {
|
|
revert NotOwner();
|
|
}
|
|
|
|
_; // doing the rest of the code
|
|
}
|
|
|
|
// What happens if someone send this contract ETH without calling the fund fucntion
|
|
/*
|
|
receive - people send money / empty money into this contract
|
|
fallback - cannot identify which function to run
|
|
*/
|
|
receive() external payable {
|
|
fund();
|
|
}
|
|
|
|
fallback() external payable {
|
|
fund();
|
|
}
|
|
}
|