PatrickAlphaC / brownie_fund_me

26 stars 64 forks source link

Beginning of fund_and_withdraw.py Section, Unable to come up with the same print(entrance_fee) value as in the video. #27

Closed Liam-Hughes closed 2 years ago

Liam-Hughes commented 2 years ago

I keep coming up with 2500000 when running scripts.fund_and_withdraw.py.

I've seen some other people running into this but their fixes did not work for me.

Please let it be something bleedingly obvious wrong with me code. fingers crossed.

Here is my code:

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)

def main(): fund()

FundMe.sol:

// SPDX-License-Identifier: MIT

pragma solidity ^0.6.6;

import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol"; import "@chainlink/contracts/src/v0.6/vendor/SafeMathChainlink.sol";

contract FundMe { using SafeMathChainlink for uint256;

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

// if you're following along with the freecodecamp video
// Please see https://github.com/PatrickAlphaC/fund_me
// to get the starting solidity contract code, it'll be slightly different than this!
constructor(address _priceFeed) public {
    priceFeed = AggregatorV3Interface(_priceFeed);
    owner = msg.sender;
}

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

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

function getPrice() public view returns (uint256) {
    (, 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;
}

function getEntranceFee() public view returns (uint256) {
    // minimumUSD
    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 {
    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);
}

}

deploy.py:

// SPDX-License-Identifier: MIT

pragma solidity ^0.6.6;

import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol"; import "@chainlink/contracts/src/v0.6/vendor/SafeMathChainlink.sol";

contract FundMe { using SafeMathChainlink for uint256;

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

// if you're following along with the freecodecamp video
// Please see https://github.com/PatrickAlphaC/fund_me
// to get the starting solidity contract code, it'll be slightly different than this!
constructor(address _priceFeed) public {
    priceFeed = AggregatorV3Interface(_priceFeed);
    owner = msg.sender;
}

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

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

function getPrice() public view returns (uint256) {
    (, 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;
}

function getEntranceFee() public view returns (uint256) {
    // minimumUSD
    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 {
    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);
}

}

helpful_scripts:

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("Deploying Mocks") if len(MockV3Aggregator) <= 0: MockV3Aggregator.deploy(DECIMALS, STARTING_PRICE, {"from": get_account()}) print("Mocks Deployed!")

Any and all advice is appreciated!

jeremiasivan commented 2 years ago

For me, grouping every exponentiation in FundMe.sol somehow works (e.g. uint256 minimumUSD = 50 * (10**18);. Can you try?

PatrickAlphaC commented 2 years ago

Hi all! Could you please ask these questions on the main repo? Thank you!

https://github.com/smartcontractkit/full-blockchain-solidity-course-py

ArnoldTingSu commented 2 years ago

I asked this question in the main repo: https://github.com/PatrickAlphaC/brownie_fund_me/issues/34