Lesson 6: FAILED tests/test_fund_me.py::test_can_fund_and_withdraw - AttributeError: 'int' objec... #1266
-
|
I was doing the Patrick Collins tutorial link and got this error and I got this error: My files: test_fund_me.py from scripts.helpful_scripts import get_account
from scripts.deploy import deploy_fund_me
def test_can_fund_and_withdraw():
account = get_account()
fund_me = deploy_fund_me()
entrance_fee = fund_me.getEntranceFee()
tx = fund_me.fund({"from": account, "value": entrance_fee})
tx.wait(1)
assert fund_me.addressToAmountFunded(account.address) == entrance_fee
tx2 = fund_me.withdraw({"from": account})
tx2.wait(1)
assert fund_me.addressToAmountFunded(account.address) == 0fund_and_withdraw.py from brownie import FundMe
from scripts.helpful_scripts import get_account
def fund():
fund_me = FundMe[-1]
account = get_account()
entrance_fee = fund_me.getEntranceFee()
print(entrance_fee)
print(f"The current entry fee is {entrance_fee}")
print("Funding")
fund_me.fund({"from": account, "value": entrance_fee})
def withdraw():
fund_me = FundMe[-1]
account = get_account()
fund_me.withdraw({"from": account})
def main():
fund()
withdraw()deploy.py from brownie import FundMe, MockV3Aggregator, network, config
from scripts.helpful_scripts import (
get_account,
deploy_mocks,
LOCAL_BLOCKCHAIN_ENVIRONMENTS,
)
def deploy_fund_me():
account = get_account()
if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
price_feed_address = config["networks"][network.show_active()][
"eth_usd_price_feed"
]
else:
deploy_mocks()
price_feed_address = MockV3Aggregator[-1].address
fund_me = FundMe.deploy(
price_feed_address,
{"from": account},
publish_source=config["networks"][network.show_active()].get("verify"),
)
print(f"Contract deployed to {fund_me.address}")
return fund_me
def main():
deploy_fund_me()helpfull_scripts.py from brownie import network, config, accounts, MockV3Aggregator
from web3 import Web3
LOCAL_BLOCKCHAIN_ENVIRONMENTS = ["development", "ganache-local"]
DECIMALS = 8
STARTING_PRICE = 200000000
def get_account():
if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
return accounts[0]
else:
return accounts.add(config["wallets"]["from_key"])
def deploy_mocks():
print(f"The active network is {network.show_active()}")
print(f"Deploying Mocks...")
if len(MockV3Aggregator) <= 0:
MockV3Aggregator.deploy(
DECIMALS, Web3.toWei(DECIMALS, STARTING_PRICE), {"from": get_account()}
)
print("Mocks Deployed!")
FundMe.sol // 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 FundMe {
using SafeMathChainlink for uint256;
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
address public owner;
AggregatorV3Interface public priceFeed;
constructor(address _priceFeed) public {
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) {
// minimumUSD
uint256 minimumUSD = 50 * 10**18;
uint256 price = getPrice();
uint256 precision = 1 * 10**18;
// return (minimumUSD * precision) / price;
// We fixed a rounding error found in the video by adding one!
return ((minimumUSD * precision) / price) + 1;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function withdraw() public payable onlyOwner {
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);
}
}
brownie-config.yalm dependencies:
# - <organization/repo>@<version>
- smartcontractkit/chainlink-brownie-contracts@1.1.1
compiler:
solc:
remappings:
- '@chainlink=smartcontractkit/chainlink-brownie-contracts@1.1.1'
dotenv: .env
networks:
rinkeby:
eth_usd_price_feed: '0x8A753747A1Fa494EC906cE90E9f37563A8AF630e'
verify: True
development:
verify: False
ganache-local:
verify: False
wallets:
from_key: ${PRIVATE_KEY}I also have .env file with |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
Beta Was this translation helpful? Give feedback.
-
|
Just a hunch: I am wondering if you are really getting the exact value you are expecting here, so cute! |
Beta Was this translation helpful? Give feedback.
-
|
@n4n0b1t3 is right should be the test Anyway, could you please share the detailed error message? as for me is related to a non well declared variable. |
Beta Was this translation helpful? Give feedback.
-
|
this is nonsense here in your helpfull_scripts.py Just compare it with the original: https://github.com/PatrickAlphaC/brownie_fund_me/blob/main/scripts/helpful_scripts.py Pythons toWei() is used like this: https://web3py.readthedocs.io/en/stable/examples.html?highlight=towei#converting-currency-denominations If you'd like to understand the details of what Decimals are used for, read this post: #1153 |
Beta Was this translation helpful? Give feedback.



this is nonsense here in your helpfull_scripts.py
Web3.toWei(DECIMALS, STARTING_PRICE)Just compare it with the original: https://github.com/PatrickAlphaC/brownie_fund_me/blob/main/scripts/helpful_scripts.py
Pythons toWei() is used like this: https://web3py.readthedocs.io/en/stable/examples.html?highlight=towei#converting-currency-denominations
If you'd like to understand the details of what Decimals are used for, read this post: #1153