Lesson 4: FundMe #4556
-
Hi, I have been using the code as described by the video. Here is my FundMe contract: // Get funds from users
// Withdraw funds
// Set a minimum funding value in USD
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
import "./PriceConverter.sol";
error NotOwner();
contract FundMe {
using PriceConverter for uint256;
uint256 public constant MINIMUM_USD = 10 * 1e18;
address[] public funders;
mapping(address => uint256) public addressToAmountFunded;
address public immutable i_owner;
constructor() {
i_owner = msg.sender;
}
function fund() public payable {
// We want to be able to set a mininmum fund amount
// 1. How do we send ETH to this contract?
require(msg.value.getConversionRate() >= MINIMUM_USD, "Didn't send enough ETH!");
funders.push(msg.sender);
addressToAmountFunded[msg.sender] = msg.value;
// TODO: check to see if funders address exists first and add to existing balance
}
function withdraw() public onlyOwner {
for (uint256 funderIndex = 0; funderIndex < funders.length; funderIndex = funderIndex++) {
address funder = funders[funderIndex];
addressToAmountFunded[funder] = 0;
}
// reset the array
funders = new address[](0);
// actually with draw the funds
// transfer
// payable(msg.sender).transfer(address(this).balance);
// send
// bool sendSuccess = payable(msg.sender).send(address(this).balance);
// require(sendSuccess, "Send failed");
// call
(bool callSuccess,) = payable(msg.sender).call{value: address(this).balance}("");
require(callSuccess, "Call failed");
}
modifier onlyOwner {
if (msg.sender != i_owner) { revert NotOwner();}
_;
}
// What happens if somebody sends ETH without calling the fund function
// receive()
// fallback()
receive() external payable {
fund();
}
fallback() external payable {
fund();
}
} Here is my PriceConverter contract: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
library PriceConverter {
function getPrice() internal view returns (uint256) {
// ABI
// Address 0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e
AggregatorV3Interface priceFeed = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e);
(,int256 price,,,) = priceFeed.latestRoundData();
// ETH in terms of USD
return uint256(price * 1e10);
}
function getConversionRate(uint256 ethAmount) internal view returns (uint256) {
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1e18;
return ethAmountInUsd;
}
function getVersion() internal view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e);
return priceFeed.version();
}
} ProblemI am able to fund the contract and all functionality works except the withdraw function. Every time I try to withdraw I get the error:
The gas prices range from 0.75 GoerliETH to 2.4 GoerliETH. The GoerliETH faucet only allows me to withdraw 0.1 GoerliETH every 24 hours. I would like to continue working through the course, but I can't test the contracts with gas prices this high on the test net and I can't get enough GoerliETH from the faucet to have enough gas. Is anybody else having this problem? What are my other options so I can continue the class? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@GratefulDave In the withdraw function's loop here |
Beta Was this translation helpful? Give feedback.
@GratefulDave In the withdraw function's loop here
for (uint256 funderIndex = 0; funderIndex < funders.length; funderIndex = funderIndex++)
you are usingfunderIndex = funderIndex++
convert it tofunderIndex++
. This could be the issue for the transaction. Try it and let me know.