当我试图测试下面的项目时,它会出现以下错误。对考试失败的原因有什么想法吗,我是不是遗漏了什么?我正在关注帕特里克·柯林斯( Patrick )提出的“自由意志”(freecodecamp`)16个小时的坚实课程。
实体代码:
//SPDX-License-Identifier: MIT
pragma solidity ^0.6.6;
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract Lottery {
address payable[] public players;
uint256 public usdEntryFee;
AggregatorV3Interface internal ethUsdPriceFeed;
constructor(address _priceFeedAddress) public {
usdEntryFee = 50 * (10**18);
ethUsdPriceFeed = AggregatorV3Interface(_priceFeedAddress);
}
function enter() public payable {
//en az 50 dolar
require(msg.value >= getEntranceFee(), "Not enough ETH!");
players.push(msg.sender);
}
function getEntranceFee() public view returns (uint256) {
(, int256 price, , , ) = ethUsdPriceFeed.latestRoundData();
uint256 adjustedPrice = uint256(price) * 10**10; //18 decimals
uint256 costToEnter = (usdEntryFee * 10**18) / adjustedPrice;
return costToEnter;
}
function startLottery() public {}
function endLottery() public {}
}测试代码:
from brownie import Lottery, accounts, config, network
from web3 import Web3
# 0.019 ETH
# 19000000000000000 WEI
def test_get_entrance_fee():
account = accounts[0]
lottery = Lottery.deploy(
config["networks"]["mainnet-fork"]["eth_usd_price_feed"],
{"from": account},
)
# lottery.tx.wait(1)
# print(lottery)
assert lottery.getEntranceFee() > Web3.toWei(0.017, "ether")
assert lottery.getEntranceFee() < Web3.toWei(0.020, "ether")配置文件:
dependencies:
- smartcontractkit/chainlink-brownie-contracts@1.1.1
compiler:
solc:
remappings:
- '@chainlink=smartcontractkit/chainlink-brownie-contracts@1.1.1'
networks:
mainnet-fork:
eth_usd_price_feed: '0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419'我得到的错误打印: PS C:\\Aybar\SmartContrac-抽签> brownie -网络主机-叉信息:无法找到给定模式的文件。Browniev1.16.4- Ethereum的Python开发框架
====================================================== test session starts ======================================================
platform win32 -- Python 3.10.2, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: C:\Users\Aybars\smartcontract-lottery
plugins: eth-brownie-1.16.4, hypothesis-6.21.6, forked-1.3.0, xdist-1.34.0, web3-5.23.1
collected 1 item
Attached to local RPC client listening at '127.0.0.1:7545'...
tests\test_lottery.py F [100%]
=========================================================== FAILURES ============================================================
_____________________________________________________ test_get_entrance_fee _____________________________________________________
def test_get_entrance_fee():
account = accounts[0]
lottery = Lottery.deploy(
config["networks"]["mainnet-fork"]["eth_usd_price_feed"],
{"from": account},
)
# lottery.tx.wait(1)
# print(lottery)
> assert lottery.getEntranceFee() > Web3.toWei(0.017, "ether")
tests\test_lottery.py:16:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\AppData\Local\Programs\Python\Python310\lib\site-packages\brownie\network\multicall.py:115: in _proxy_call
result = ContractCall.__call__(*args, **kwargs) # type: ignore
..\AppData\Local\Programs\Python\Python310\lib\site-packages\brownie\network\contract.py:1729: in __call__
return self.call(*args, block_identifier=block_identifier)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <ContractCall 'getEntranceFee()'>, block_identifier = None, args = ()
tx = {'allow_revert': None, 'data': '0x09bc33a7', 'from': '0xaE72Ea06d543D50B5E8DD07CDA2De4934926CdF4', 'gas': None, ...}
def call(self, *args: Tuple, block_identifier: Union[int, str, bytes] = None) -> Any:
"""
Call the contract method without broadcasting a transaction.
Arguments
---------
*args
Contract method inputs. You can optionally provide a
dictionary of transaction properties as the last arg.
block_identifier : int | str | bytes, optional
A block number or hash that the call is executed at. If not given, the
latest block used. Raises `ValueError` if this value is too far in the
past and you are not using an archival node.
Returns
-------
Contract method return value(s).
"""
args, tx = _get_tx(self._owner, args)
if tx["from"]:
tx["from"] = str(tx["from"])
del tx["required_confs"]
tx.update({"to": self._address, "data": self.encode_input(*args)})
try:
data = web3.eth.call({k: v for k, v in tx.items() if v}, block_identifier)
except ValueError as e:
> raise VirtualMachineError(e) from None
E brownie.exceptions.VirtualMachineError: revert
..\AppData\Local\Programs\Python\Python310\lib\site-packages\brownie\network\contract.py:1533: VirtualMachineError
======================================================= warnings summary ========================================================
..\AppData\Local\Programs\Python\Python310\lib\site-packages\brownie\network\main.py:44
C:\Users\Aybars\AppData\Local\Programs\Python\Python310\lib\site-packages\brownie\network\main.py:44: BrownieEnvironmentWarning: Development network has a block height of 49
warnings.warn(
-- Docs: https://docs.pytest.org/en/stable/warnings.html
==================================================== short test summary info ====================================================
FAILED tests/test_lottery.py::test_get_entrance_fee - brownie.exceptions.VirtualMachineError: revert
================================================= 1 failed, 1 warning in 2.45s ==================================================发布于 2022-05-18 09:46:32
我在上这门课的时候也遇到了同样的问题。在这里找到了答案:https://github.com/PatrickAlphaC/smartcontract-lottery/issues/28。我运行了一个ganache实例,因此关闭它解决了问题。
发布于 2022-06-21 14:45:16
当我关闭ganache实例时,我遇到了同样的问题并得到了解决。请记住在使用mainnet-叉进行测试时关闭ganache实例。否则,Brownie将自动附加到不属于您分叉的ganache实例。
发布于 2022-07-13 13:05:12
测试未能满足getEntranceFee() > Web3.toWei(0.017,"ether")的断言
您可以做的是检查ETH/美元价格的当前值,并得到它的逆值,然后在Web3.toWei(<current value>,"ether")上替换它。
还可以使用with raises(exceptions.VirtualMachineError):进行更有效的错误处理。
https://stackoverflow.com/questions/71472909
复制相似问题