smartcontractkit / full-blockchain-solidity-course-py

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

lottery test lesson7 time 6:29:24 in video,, i get AssertionError: assert 1428382801236009805 < 15000000000000000 #609

Open WeTheArtist opened 2 years ago

WeTheArtist commented 2 years ago

Hi ,

While running in the terminal brownie test --network mainnet-fork, I get the following error. I am a bit confused asI had a similar problem earlier with the number of 0's output as entracefee. So it looks like the 14number is having a 0 more than the 15 number. My code is below, since it is just the start I hope it will be easy to identify where the problem is.

PS D:\Python projects\Solidity dev\demo\smartcontract-lottery> brownie test --network mainnet-fork
INFO: Could not find files for the given pattern(s).
Brownie v1.17.2 - Python development framework for Ethereum

======================================================================================= test session starts ========================================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: D:\Python projects\Solidity dev\demo\smartcontract-lottery
plugins: eth-brownie-1.17.2, hypothesis-6.27.3, forked-1.3.0, xdist-1.34.0, web3-5.25.0
collected 1 item

Launching 'ganache-cli.cmd --accounts 10 --fork https://eth-mainnet.alchemyapi.io/v2/ptTVJ3Vfd1HYNdJinrl45Q-yR_Of18O4 --mnemonic brownie --port 8545 --hardfork istanbul'...

tests\test_lottery.py F                                                                                                                                                                       [100%]

============================================================================================= FAILURES =============================================================================================
______________________________________________________________________________________ test_get_entrance_fee _______________________________________________________________________________________ 

    def test_get_entrance_fee():
        account = accounts[0]
        lottery = Lottery.deploy(
            config["networks"][network.show_active()]["eth_usd_price_feed"],
        )

        assert lottery.getEntraceFee() > Web3.toWei(0.011, "ether")
>       assert lottery.getEntraceFee() < Web3.toWei(0.015, "ether")
E       AssertionError: assert 1428382801236009805 < 15000000000000000
E        +  where 1428382801236009805 = <ContractCall 'getEntraceFee()'>()
E        +    where <ContractCall 'getEntraceFee()'> = <Lottery Contract '0xE7eD6747FaC5360f88a2EFC03E00d25789F69291'>.getEntraceFee
E        +  and   15000000000000000 = <function to_wei at 0x0000023DBEF110D0>(0.015, 'ether')
E        +    where <function to_wei at 0x0000023DBEF110D0> = Web3.toWei

tests\test_lottery.py:18: AssertionError
===================================================================================== short test summary info ====================================================================================== 
FAILED tests/test_lottery.py::test_get_entrance_fee - AssertionError: assert 1428382801236009805 < 15000000000000000
======================================================================================== 1 failed in 13.77s ======================================================================================== 
Terminating local RPC client...

my test_lottery code:

# 0,01243301712
# 12000000000000000

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

# from brownie.network import account

def test_get_entrance_fee():
    account = accounts[0]
    lottery = Lottery.deploy(
        config["networks"][network.show_active()]["eth_usd_price_feed"],
        {"from": account},
    )

    assert lottery.getEntraceFee() > Web3.toWei(0.011, "ether")
    assert lottery.getEntraceFee() < Web3.toWei(0.015, "ether")

Lottery.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

contract Lottery {
    uint256 usdEntryFee;
    address payable[] public players;
    AggregatorV3Interface internal ethUsdPriceFeed;

    constructor(address _priceFeedAddress) public {
        usdEntryFee = 50 * (10**18);
        ethUsdPriceFeed = AggregatorV3Interface(_priceFeedAddress);
    }

    function enter() public payable {
        //$50 min
        // require();
        players.push(payable(msg.sender));
    }

    function getEntraceFee() public view returns (uint256) {
        (, int256 price, , , ) = ethUsdPriceFeed.latestRoundData();
        uint256 adjustedPrice = uint256(price) * 10**10; //18 decimals
        //$50, 2000 ETH
        //50/2000
        //50*10000/2000
        uint256 costToEnter = (usdEntryFee * 10**18) / adjustedPrice;
        return costToEnter;
    }

    function startLottery() public {}

    function endLottery() public {}
}

brownie-config.yaml:

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

networks:
  mainnet-fork:
    eth_usd_price_feed: '0xaEA2808407B7319A31A383B6F8B60f04BCa23cE2'
WeTheArtist commented 2 years ago

I can correct it by editing the function with 10^12 instead of 10^10, but I am not sure if that is correct, I wanted to check what the value of adjustedPrice is but I dont know how, can you please help me?

   function getEntraceFee() public view returns (uint256) {
        (, int256 price, , , ) = ethUsdPriceFeed.latestRoundData();
        uint256 adjustedPrice = uint256(price) * 10**12; //18 decimals
        //$50, 2000 ETH
        //50/2000
        //50*10000/2000
        uint256 costToEnter = (usdEntryFee * 10**18) / adjustedPrice;
        return costToEnter;
    }
br0wnD3v commented 2 years ago

To view the value of adjustedPrice you can create another

function getPrice() public view (returns uint256){
    (, int256 price, , , ) = ethUsdPriceFeed.latestRoundData();
    uint256 adjustedPrice = uint256(price) * 10**10;
    return adjustedPrice;
}

and run it on remix IDE for a faster + clear view.

DarrenWayn commented 2 years ago

i have the same problem do ufix this already? @WeTheArtist