运行 "brownie test" 时我的测试失败。如何正确地将 transactionReeipt 与 python 中的浮点数进行比较?


from scripts.helpful_scripts import get_account
from scripts.deploy import deploy_donation
from web3 import Web3

def test_can_get_conversion_rate():
account = get_account()
donation = deploy_donation()


tx = donation.getConversionRate(100, {"from": account})
tx.wait(1)
assert tx < 0.075
assert tx > 0.06
print(f"The ethAmount is {tx}")

def main():
test_can_get_conversion_rate()

当我运行";布朗尼试验";在终端上:TypeError:"<"在"TransactionReceipt"one_answers"float"实例之间不支持

这是我正在努力测试的稳固合同。部署python脚本运行得很好,但我的测试脚本运行得不好。

// 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 Donation {
uint256 ethAmount;
address payable owner;
AggregatorV3Interface public priceFeed;
constructor(address _priceFeed) public {
priceFeed = AggregatorV3Interface(_priceFeed);
owner = msg.sender;
}
function donate(uint256 _amount) public payable {
ethAmount = getConversionRate(_amount);
owner.transfer(ethAmount);
}
function getConversionRate(uint256 rawUSD) public returns 
(uint256) {
uint256 ethUSD = (rawUSD / getPrice()) * 10**18;
return ethUSD;
}
function getPrice() internal returns (uint256) {
(, int256 answer, , , ) = priceFeed.latestRoundData();
return uint256(answer * 100000000000);
}
}

这是我可怜的剧本,请看一下。感谢

from brownie import Donation, accounts, config, network, MockV3Aggregator
from scripts.helpful_scripts import (
LOCAL_BLOCKCHAIN_ENVIRONMENTS,
deploy_mocks,
get_account,
)

def deploy_donation():
account = get_account()
if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
price_feed_address = config["networks"] 
[network.show_active()][
"eth_usd_price_feed_address"
]
else:
deploy_mocks()
price_feed_address = MockV3Aggregator[-1].address
donation = Donation.deploy(
price_feed_address,
{"from": account},
)
print(f"Contract deployed to {donation.address}")
return donation

def-main((:deploy_donation((

所以我发现我的"transactionReceipt"实际上是一个散列交易,这就是为什么它不能与float进行比较。A使我的getConversionRate((和getPrice((函数都成为公共视图函数。还对我的deploy.py脚本进行了一些调整,这样它就不会遇到更多错误。这解决了问题。

相关内容

  • 没有找到相关文章

最新更新