Last active
March 31, 2022 06:53
-
-
Save amkurian/5b67c768791e6c80256490132559fee2 to your computer and use it in GitHub Desktop.
A sample crowdfunding app using solidity
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.6.6 <0.9.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; | |
address public owner; | |
address[] public funders; | |
constructor() public { | |
owner = msg.sender; | |
} | |
mapping(address => uint256) public addressToAmountFunded; | |
function fund() public payable { | |
uint256 minimumUSD = 20 * 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(0x9326BFA02ADD2366b30bacB125260Af641031331); | |
return priceFeed.version(); | |
} | |
function getPrice() public view returns (uint256){ | |
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331); | |
(,int256 answer,,,) = priceFeed.latestRoundData(); | |
return uint256(answer * 10000000000); | |
} | |
function getConversionRate(uint256 ethAmount) public view returns (uint256){ | |
uint256 ethPrice = getPrice(); | |
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000; | |
// the actual ETH/USD conversation rate, after adjusting the extra 0s. | |
return ethAmountInUsd; | |
} | |
modifier onlyOwner { | |
require(msg.sender == owner); | |
_; | |
} | |
function withdraw() payable onlyOwner public { | |
msg.sender.transfer(address(this).balance); | |
for (uint256 funderIndex=0; funderIndex < funders.length; funderIndex++){ | |
address funder = funders[funderIndex]; | |
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