smartcontractkit / full-blockchain-solidity-course-py

Ultimate Solidity, Blockchain, and Smart Contract - Beginner to Expert Full Course | Python Edition
MIT License
10.74k stars 2.9k forks source link

Gas estimation failed? #615

Open geunee20 opened 2 years ago

geunee20 commented 2 years ago

Hi, I'm following your course and got an issue at lesson 6.

It's about beginning of fund_and_withdraw.py Here is the error code

Deploying works well but I idk why I got entry fee as 0

++ When I changed to "fund_me.fund({"from": account, "value": 2500000})" in the fund_me_withdraw.py the code works well. I think this problem is caused by getting 0 from fund_me.getEntranceFee() So please go over my FundMe.sol

++ I changed function getEntranceFee() to following code and it works Idk why it works tho. Any explanation?

    function getEntranceFee() public view returns (uint256) {
        uint256 minimumUSD = 50 * 10**18;
        uint256 price = getPrice();
        uint256 precision = 1 * 10**18;
        return (minimumUSD * precision *10**18) / price;
    }                                   ^^^^^^^

ERROR CODE

$ brownie run scripts/fund_and_withdraw.py --network ganache-local

 INFO: Could not find files for the given pattern(s).
 Brownie v1.17.2 - Python development framework for Ethereum

 BrownieFundmeProject is the active project.

 Running 'scripts\fund_and_withdraw.py::main'...
 0
 The current entry fee is 0
 Funding
   File "C:\Users\name\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\_cli\run.py", line 50, in main
     return_value, frame = run(
   File "C:\Users\name\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\project\scripts.py", line 103, in run
     return_value = f_locals[method_name](*args, **kwargs)
   File ".\scripts\fund_and_withdraw.py", line 16, in main
     fund()
   File ".\scripts\fund_and_withdraw.py", line 12, in fund
     fund_me.fund({"from": account, "value": entrance_fee})
   File "C:\Users\name\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\network\contract.py", line 1629, in __call__
     return self.transact(*args)
   File "C:\Users\name\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\network\contract.py", line 1502, in transact
     return tx["from"].transfer(
   File "C:\Users\name\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\network\account.py", line 644, in transfer
     receipt, exc = self._make_transaction(
   File "C:\Users\name\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\network\account.py", line 727, in _make_transaction
     raise VirtualMachineError(e) from None
   File "C:\Users\name\.local\pipx\venvs\eth-brownie\lib\site-packages\brownie\exceptions.py", line 121, in __init__
     raise ValueError(str(exc)) from None
 ValueError: Gas estimation failed: 'execution reverted: VM Exception while processing transaction: revert You need spend more'. This transaction will likely revert. If you wish to broadcast, you must set the gas limit manually.
geunee20 commented 2 years ago

helpful_scripts.py is here

from brownie import network, config, accounts, MockV3Aggregator
from web3 import Web3

DECIMALS = 8
STARTING_PRICE = 200000000000
LOCAL_BLOCKCHAIN_ENVIRONMENTS = ["development", "ganache-local"]

def get_account():
    if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
        return accounts[0]
    else:
        return accounts.add(config["wallets"]["from_key"])

def deploy_mocks():
    print(f"The active network is {network.show_active()}")
    print(f"Deploying Mocks...")
    if len(MockV3Aggregator) <= 0:
        mock_aggregator = MockV3Aggregator.deploy(
            DECIMALS, Web3.toWei(STARTING_PRICE, "ether"), {"from": get_account()}
        )
    print("Mocks Deployed!")

fund_and_withdraw.py

from brownie import FundMe
from scripts.helpful_scripts import get_account

def fund():
    fund_me = FundMe[-1]
    account = get_account()
    entrance_fee = fund_me.getEntranceFee()
    print(entrance_fee)
    print(f"The current entry fee is {entrance_fee}")
    print("Funding")
    fund_me.fund({"from": account, "value": entrance_fee})

def main():
    fund()

deploy.py

from brownie import FundMe, MockV3Aggregator, network, config
from scripts.helpful_scripts import (
get_account,
deploy_mocks,
LOCAL_BLOCKCHAIN_ENVIRONMENTS,
)
from web3 import Web3

def deploy_fund_me():
account = get_account()
# pass the price feed address to our fundme contract

# if we are on a persistent network like rinkeby, use the associated address
# otherwise, deploy mocks

if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
    price_feed_address = config["networks"][network.show_active()][
        "eth_usd_price_feed"
    ]
else:
    deploy_mocks()
    price_feed_address = MockV3Aggregator[-1].address

fund_me = FundMe.deploy(
    price_feed_address,
    {"from": account},
    publish_source=config["networks"][network.show_active()].get("verify"),
)
print(f"Contract deployed to {fund_me.address}")
def main():
deploy_fund_me()

FundMe.sol

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract FundMe {
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
address public owner;
AggregatorV3Interface public priceFeed;

constructor(address _priceFeed) {
    priceFeed = AggregatorV3Interface(_priceFeed);
    owner = msg.sender;
}

function fund() public payable {
    uint256 minimumUSD = 50 * 10**18;
    require(
        getConversionRate(msg.value) >= minimumUSD,
        "You need spend more"
    );
    addressToAmountFunded[msg.sender] += msg.value;
    funders.push(msg.sender);
    // what the ETH -> USD conversion rate
}

function getVersion() public view returns (uint256) {
    return priceFeed.version();
}

function getPrice() public view returns (uint256) {
    (, int256 answer, , , ) = priceFeed.latestRoundData();
    return uint256(answer * 10**10);
}

function getConversionRate(uint256 ethAmount)
    public
    view
    returns (uint256)
{
    uint256 ethPrice = getPrice();
    uint256 ethAmountInUsd = (ethPrice * ethAmount) / 10**18;
    return ethAmountInUsd;
}

function getEntranceFee() public view returns (uint256) {
    uint256 minimumUSD = 50 * 10**18;
    uint256 price = getPrice();
    uint256 precision = 1 * 10**18;
    return (minimumUSD * precision) / price;
}

modifier onlyOwner() {
    require(msg.sender == owner);
    _;
}

function withdraw() public payable onlyOwner {
    payable(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);
}
}

brownie-config.yaml

dependencies:
#  - <organization/repo>@<version>
  - smartcontractkit/chainlink-brownie-contracts@0.2.2

compiler:
  solc:
    remappings:
      - '@chainlink=smartcontractkit/chainlink-brownie-contracts@0.2.2'

dotenv: .env

networks:
  rinkeby:
    eth_usd_price_feed: '0x8A753747A1Fa494EC906cE90E9f37563A8AF630e'
    verify: True
  development:
    verify: False
  ganache-local:
    verify: False

wallets:
  from_key: ${PRIVATE_KEY}
gYomii commented 2 years ago

try in FundMe.sol getConversionRate(msg.value) <= minimumUSD, "You need spend more"