Skip to content

Instantly share code, notes, and snippets.

@r4VP4
Created November 12, 2021 20:49
Show Gist options
  • Select an option

  • Save r4VP4/6476dbeaae16fe488cc93a8a5f85e9b3 to your computer and use it in GitHub Desktop.

Select an option

Save r4VP4/6476dbeaae16fe488cc93a8a5f85e9b3 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.6.9+commit.3e3065ac.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.6 <0.9.0;
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract FundMe{
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
address public owner;
constructor() public {
owner = msg.sender;
}
function fund() public payable {
// 50$
uint256 minimumUSD = 50* 10**18;
uint256 actualUSD = getConversionRate(msg.value);
require (actualUSD > minimumUSD, "not enough ETH!");
addressToAmountFunded[msg.sender] += msg.value;
funders.push(msg.sender);
}
function getVersion() public view returns(uint256){
// that says we have a contract that is located at this address and has the functiones deifned in the interface
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
return priceFeed.version();
}
function getDecimals() public view returns(uint8){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
return priceFeed.decimals();
}
function getPrice() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
(, //uint80 roundId,
int256 answer
, //uint256 startedAt,
, //uint256 updatedAt,
, //uint80 answeredInRound
) = priceFeed.latestRoundData();
return uint256(answer * 1000000000);
}
function getConversionRate(uint256 ethAmount) public view returns(uint256){
// meins return getPrice() * addressToAmountFunded[msg.sender];
uint256 ethPrice = getPrice();
uint256 ethAmountInUSD = (ethPrice * ethAmount) / 100000000000000000;
return ethAmountInUSD;
}
modifier onlyOwner {
// only the owner
require(msg.sender == owner, "not allowed");
_;
}
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