Home > database >  My test fails when i run "brownie test". How can I correctly compare transactionReeipt to
My test fails when i run "brownie test". How can I correctly compare transactionReeipt to

Time:08-25

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()

I keep getting this error when i run "brownie test" on the terminal: TypeError: '<' not supported between instances of 'TransactionReceipt' and 'float'

This is my solidity contract which i am trying to test. The deploy python script ran well but my test script is not.

// 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);
    }
}

This is my deploy.py script, please have a look. Thanks

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()

CodePudding user response:

So i found out my 'transactionReceipt' was actually a hashed transaction and that was why it couldn't be compared with a float. A made both my getConversionRate() and getPrice() functions a public view function. Also made a few adjustment to my deploy.py script so that it doesn't run into more errors. This solved it.

  • Related