0

Macbook Pro : Monterey

Intel Core i7

Brownie v1.17.2

I am learning solidity according to reference(https://www.youtube.com/watch?v=M576WGiDBdQ&t=25510s).

What I tried to do here, is use brownie to deploy a contract(FundMe) in a script (deploy.py),then write a test script(test_can_fund_and_withdraw.py)

The error occurs when I run the test script.I think it's because in the testing script import the function "deploy_fund_me()" which don't have the function "getEntranceFee()",should import from contract FundMe.sol but in the youtube course ,it works fine.

test script (test_can_fund_and_withdraw.py) enter image description here

deploy.py enter image description here error info enter image description here

contract FundMe.sol

// SPDX-License-Identifier: MIT

pragma solidity 0.8.0;

// we need tell brownie @chainlink means == what input in config,need to tell compiler

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) {
        // make price feed a parameter
        priceFeed = AggregatorV3Interface(_priceFeed);
        owner = msg.sender;
    }

    function fund() public payable {
        uint256 mimimumUSD = 50 * 10**18;
        require(
            getConversionRate(msg.value) >= mimimumUSD,
            "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) {
        // mimimumUSD
        uint256 mimimumUSD = 50 * 10**18;
        uint256 price = getPrice();
        uint256 precision = 1 * 10**18;
        return (mimimumUSD * 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);
    }
}


mike515
  • 183
  • 1
  • 14
  • Does this answer your question? [Why do I get AttributeError: 'NoneType' object has no attribute 'something'?](https://stackoverflow.com/questions/8949252/why-do-i-get-attributeerror-nonetype-object-has-no-attribute-something) – Ture Pålsson Jan 14 '22 at 10:26

1 Answers1

2

In deploy.py you are not returning anything. you should

return fund_me

when you call deploy_fund_me in the test script, it will return the contract so you can call the methods.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • Add the return statement fix the problem ,thank you very much! But I don't understand the logic here , why should add the return fund_me statement here.Isn't the FundMe.sol already imported so I can get the valve through the getEntranceFee feature. – mike515 Jan 17 '22 at 02:03
  • 2
    @mike515 FundMe imported from brownie but that is just the contract. Inorder to interact with the contract, we have to deploy it. That is what `deploy_fund_me` does – Yilmaz Jan 17 '22 at 02:11
  • 1
    You can compare print("Fundme",FundMe) and print("fund_me",fund_me) – Yilmaz Jan 17 '22 at 02:21
  • Thanks ! Let me work on that. – mike515 Jan 17 '22 at 02:31