-
Notifications
You must be signed in to change notification settings - Fork 0
/
FundMe.sol
61 lines (49 loc) · 2.08 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
import "@chainlink/contracts/src/v0.6/vendor/SafeMathChainlink.sol";
contract FundMe {
using SafeMathChainlink for uint256;
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
address public owner;
// constructor gets automatically triggered once the contract gets deployed
constructor() public {
owner = msg.sender;
}
function fund() public payable {
// 50$
uint256 minimumUSD = 50 * 10 ** 18;
require(getConversionRate(msg.value) >= minimumUSD, "You need to send at least 50$ worth of ETH");
addressToAmountFunded[msg.sender] += msg.value;
funders.push(msg.sender);
}
function getVersion() public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
return priceFeed.version();
}
function getPrice() public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(,int256 answer,,,) = priceFeed.latestRoundData();
return uint256(answer * 10000000000);
}
function getConversionRate(uint256 ethAmount) public view returns (uint256) {
uint256 ethPrice = getPrice();
uint256 ethAmountInUSD = (ethAmount * ethPrice) / 1000000000000000000;
return ethAmountInUSD;
}
modifier onlyOwner {
require(msg.sender == owner, "Only the owner is authorized to trigger this function");
_;
}
function withDraw() payable onlyOwner public {
msg.sender.transfer(address(this).balance);
// update the balance of each funder after withdrawal
for (uint256 funderIndex=0; funderIndex<funders.length; funderIndex++) {
address funder = funders[funderIndex];
addressToAmountFunded[funder] = 0;
}
// instantiate a new funders array
funders = new address[](0);
}
}