Skip to content

Instantly share code, notes, and snippets.

@Oliver-ke
Created December 27, 2021 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oliver-ke/80a85150a67cb34dac0420abcd0566ef to your computer and use it in GitHub Desktop.
Save Oliver-ke/80a85150a67cb34dac0420abcd0566ef to your computer and use it in GitHub Desktop.
Basic solidity code to fund account
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract FundMe {
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
address public owner;
address public ethFeedAddr = 0x9326BFA02ADD2366b30bacB125260Af641031331;
constructor() {
owner = msg.sender;
}
function fund() public payable {
uint256 minimumUSD = 4 * 10 ** 18;
require(getConversionRate(msg.value) >= minimumUSD, "You need to spend more ETH!");
if(addressToAmountFunded[msg.sender] == 0) {
funders.push(msg.sender);
}
addressToAmountFunded[msg.sender] += msg.value;
}
function getVersion() public view returns (uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(ethFeedAddr);
return priceFeed.version();
}
function getPrice() public view returns(uint256){
AggregatorV3Interface priceFeed = AggregatorV3Interface(ethFeedAddr);
(,int256 answer,,,) = priceFeed.latestRoundData();
return uint256(answer * 10000000000);
}
// 1000000000
function getConversionRate(uint256 ethAmount) public view returns (uint256){
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
return ethAmountInUsd;
}
modifier onlyOwner {
require(msg.sender == owner, "Only Owner Can withdraw");
_;
}
function withdraw() payable onlyOwner public {
address payable _to = payable(msg.sender);
_to.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