Created
January 10, 2024 08:24
-
-
Save munanadi/eb3509ef507c22622b21e8488cd8ecf1 to your computer and use it in GitHub Desktop.
Fund me contract
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.18; | |
| import {PriceConvertor} from "./PriceConvertor.sol"; | |
| error NotOwner(); | |
| contract FundMe { | |
| using PriceConvertor for uint256; | |
| uint256 public constant minimumUsd = 5 * 1e18; | |
| address[] public funders; | |
| mapping(address => uint256) public addToAmtFunded; | |
| address public immutable i_admin; | |
| constructor() { | |
| i_admin = msg.sender; | |
| } | |
| function fund() public payable { | |
| // Send a min. of 5 dollars. | |
| require(msg.value.getConversionRate() > 1e18, "didn't send enough wei"); /* 1e18 = 1ETH = 1 * 10 ** 18 Wei */ | |
| funders.push(msg.sender); | |
| addToAmtFunded[msg.sender] += msg.value; | |
| } | |
| function withdraw() public onlyOwner { | |
| for (uint256 index = 0; index < funders.length; index++) { | |
| address funder = funders[index]; | |
| addToAmtFunded[funder] = 0; | |
| } | |
| // reset the funders array | |
| funders = new address[](0); | |
| // withdraw funds from the contract. | |
| // trasnfer, send, call | |
| (bool callSuccess, ) = payable(msg.sender).call{ | |
| value: address(this).balance | |
| }(""); | |
| require(callSuccess, "Call failed"); | |
| } | |
| receive() external payable { | |
| fund(); | |
| } | |
| fallback() external payable { | |
| fund(); | |
| } | |
| modifier onlyOwner() { | |
| // require(msg.sender == i_admin, "SEnder is not owner"); | |
| if (msg.sender != i_admin) { | |
| revert NotOwner(); | |
| } | |
| _; | |
| } | |
| } |
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.18; | |
| import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol"; | |
| library PriceConvertor { | |
| function getConversionRate(uint256 ethAmount) internal view returns(uint256) { | |
| uint256 ethPrice = getPrice(); | |
| uint256 ethAmtInUsd = (ethPrice * ethAmount) / 1e18; | |
| return ethAmtInUsd; | |
| } | |
| function getPrice() internal view returns(uint256) { | |
| // Address = 0x694AA1769357215DE4FAC081bf1f309aDC325306 | |
| // ABI - Get the interface. | |
| AggregatorV3Interface priceFeed = AggregatorV3Interface(0x694AA1769357215DE4FAC081bf1f309aDC325306); | |
| (,int256 answer,,,) = priceFeed.latestRoundData(); | |
| return uint256(answer * 1e10); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment