Skip to content

Instantly share code, notes, and snippets.

@hannesroth88
Created September 28, 2022 22:16
Show Gist options
  • Save hannesroth88/68469e77ae7bac14f42d267ca13a61f4 to your computer and use it in GitHub Desktop.
Save hannesroth88/68469e77ae7bac14f42d267ca13a61f4 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.8.8+commit.dddeac2f.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17; // Hi I'am new to Solidity
import "./PriceConverter.sol";
contract FundMe{
using PriceConverter for uint256;
uint256 public minimumUsd = 10 * 1e18;
address[] public funders;
mapping(address => uint256) public addressesToAmountFunded;
address public owner;
constructor(){
owner = msg.sender;
}
function fund() public payable {
require( msg.value.getConversionRate() > minimumUsd, "didn't send enough");
funders.push(msg.sender);
addressesToAmountFunded[msg.sender] = msg.value;
}
function withdraw() public onlyOwner{
for (uint256 i=0; i < funders.length; i++) {
address funder = funders[i];
addressesToAmountFunded[funder] = 0;
}
// reset array
funders= new address[](0);
// withdraw the funds
// 1. transfer, on error reverts
//payable(msg.sender).transfer(address(this).balance);
// 2. send if error should be thrown use require
//bool sendSuccess = payable(msg.sender).send(address(this).balance);
//require(sendSuccess,"Send failed");
// 3. call, can add data, set gas, ...
(bool callSuccess,) = payable(msg.sender).call{value: address(this).balance}("");
require(callSuccess, "Call failed");
}
modifier onlyOwner{
require(msg.sender == owner, "Sender is not the contract owner");
_; // require() first then run all code of original funciton
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17; // Hi I'am new to Solidity
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
library PriceConverter {
function getPrice() internal view returns (uint256) {
// Goerli ETH / USD Address
// https://docs.chain.link/docs/ethereum-addresses/
AggregatorV3Interface priceFeed = AggregatorV3Interface(
0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
);
(, int256 answer, , , ) = priceFeed.latestRoundData();
// ETH/USD rate in 18 digit
return uint256(answer * 10000000000);
}
// 1000000000
function getConversionRate(uint256 ethAmount)
internal
view
returns (uint256)
{
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
// the actual ETH/USD conversion rate, after adjusting the extra 0s.
return ethAmountInUsd;
}
function getVersion() internal view returns(uint256) {
// ETHUSD Price Feed Address = 0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
AggregatorV3Interface priceFeed = AggregatorV3Interface(
0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
);
return priceFeed.version();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment