Created
February 17, 2022 17:23
-
-
Save rwehresmann/5c2d0cfbf244e684c5a47f49c0e1d737 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.0; | |
| import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | |
| contract FundMe { | |
| mapping(address => uint256) public addressToAmountFunded; | |
| address[] public funders; | |
| address public owner; | |
| constructor() { | |
| owner = msg.sender; | |
| } | |
| function fund() public payable { | |
| uint256 minimumUSD = 50 * 10 ** 18; | |
| require(getConversionRate(msg.value) >= minimumUSD, "You need to spend more 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 = (ethPrice * ethAmount) / (10**18); | |
| return ethAmountInUsd; | |
| } | |
| modifier onlyOwner { | |
| require(msg.sender == owner, "You're not the owner of this contract!"); | |
| _; | |
| } | |
| function withdraw() payable onlyOwner public { | |
| payable(msg.sender).transfer(address(this).balance); | |
| for (uint256 funderIdx=0; funderIdx < funders.length; funderIdx++) { | |
| address funder = funders[funderIdx]; | |
| addressToAmountFunded[funder] = 0; | |
| } | |
| funders = new address[](0); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment