Getting error: execution reverted ass gas estimation failed #2252
-
This is my code. It is getting deployed but when I click on fund by entering 0.05 eth in wei, it doesn't run. please help! // get funds from users // 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 = 50 * 1e18 ; // constant is used for the variables that set their value once and do not change. Adding constant before them makes contract gas efficient.
// Constant variable has all caps and underscore. it saves gas because it saves the variable in bytecode not in memory
address[] public funders;
mapping(address => uint256) public addressToAmountFunded ;
address public immutable i_owner; // immutable variable starts with i_. it is similar to constructor but not used for global variables.
constructor() { // constructor is the function that gets immediately called with the contract
i_owner = msg.sender ;
}
function fund() public payable {
// want to be able to set a minimum fund amount in USD.
// 1. How do we send ETH to this contract?
// require(msg.value > 1e18, "Didn't send enough!") ; // 1e18 == 1 * 10^18. that is how much 1 eth is in wei. we have set min value here.
// msg.value.getConversionRate() ;
require(msg.value.getConversionRate() >= MINIMUM_USD, "Didn't send enough!") ;
// 18 decimals
funders.push(msg.sender) ;
addressToAmountFunded[msg.sender] += msg.value ;
}
function withdraw() public onlyOwner { // onlyOwner is the modifier created below.
// require(msg.sender == owner , "sender is not owener!") ; // == is used to check whether the values are equal.
// there might be many functions where wee need to check but we can't copy paste this code in every function.
// hence we use modifiers. this is why i have commented first line
for(uint256 funderIndex = 0; funderIndex < funders.length; funderIndex++) {
address funder = funders[funderIndex] ; // derive address from funderindex.
addressToAmountFunded[funder] = 0; // setting amount to 0 after withdraw.
}
// reset the array
funders = new address[] (0);
// actually withdraw the funds.
// transfer
// send
// call
// msg.sender = address
// payable(msg.sender) = payable address
payable(msg.sender).transfer(address(this).balance); // transfer has 2300 upperlimit on gas. it automatically reverts if transaction fails.
bool sendSuccess = payable(msg.sender).send(address(this).balance); // send also has upperlimit of gas as 25300
require (sendSuccess, "Send failed"); // require needs to be added to revert back if the transaction fails.
(bool callSuccess, ) = payable(msg.sender).call{value: address(this).balance}("") ; // bytes are arrays
require(callSuccess, "call failed") ;
revert() ;
}
modifier onlyOwner {
//require(msg.sender == i_owner, "sender is not owner!");
if(msg.sender != i_owner) {revert NotOwner();} // this makes more gas efficient as string takes a lot of gas which has been avoided here.
_; // this tells the function to run the cde inside function. since we have written require first, so firstrequire statement will run and then the code will run.
}
// what happens if someone sends this contract ETH without calling the fund function
receive() external payable {
fund();
}
fallback() external payable {
fund() ;
}
} error
Here is also my Price Convertor file. //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0 ;
// aggregator interface is used to refer to contract for obtaining price feed (API).
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
library PriceConverter {
function getPrice() internal view returns(uint256) { // we will be getting the price from a different contract.
// ABI
// Address 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
(, int price,,,) = priceFeed.latestRoundData();
// ETH in terms of USD
//3000.00000000
return uint256(price * 1e10);
}
function getversion() internal view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
return priceFeed.version() ;
}
function getConversionRate(uint256 ethAmount) internal view returns (uint256) {
uint256 ethPrice = getPrice();
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1e18;
return ethAmountInUsd;
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
alymurtazamemon
Sep 1, 2022
Replies: 1 comment 2 replies
-
@Tarang-16 Also add your price converter file code above. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
alymurtazamemon
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Tarang-16 Also add your price converter file code above.