Lesson6全区块链稳定性课程:fund_and_withdraw - ValueError: Gas估计失败.<



我正在遵循完整的区块链可靠性课程。

我正在尝试使用brownie将fund_and_withdraw.py部署到Rinkeby,但我遇到以下错误:

ValueError: Gas estimation failed: 'execution reverted: You need to spend more ETH!'. This transaction will likely revert. If you wish to broadcast, you must set the gas limit manually.

我以前尝试在ganache-cli上部署它,但得到一个"索引超出范围"。

现在,我正试图在Rinkeby上运行它(Ganache UI没有连接到我的Brownie并且正在抛出整个合同),但它返回"ValueError"

另外,尝试更改FundMe.py中的小数。

我试着在Rinkby上部署,我不再得到"索引超出范围"。但是我得到的却是"ValueError">

我的代码如下:

fund_and_withdraw

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,
}
)
# 0.025000000000000000
def main():
fund()

Helpful_scripts.py

from brownie import network, config, accounts, MockV3Aggregator
from web3 import Web3
# Create Variable to store dev network list (Ex: ["development", "ganache", etc...])
LOCAL_BLOCKCHAIN_ENVIRONMENTS = "development"
DECIMALS = 8
STARTING_PRICE = 200000000000

def get_account():
# if network.show_active == "development":
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("Deploying Mocks...")
if len(MockV3Aggregator) <= 0:
MockV3Aggregator.deploy(
# DECIMALS, Web3.toWei(STARTING_PRICE, "ether"), {"from": get_account()}
DECIMALS,
STARTING_PRICE,
{"from": get_account()},
)
print("Mocks Deployed!")

FundMe.py

contract FundMe {
using SafeMath96 for uint256;
mapping(address => uint256) public addressToAmountFunded;
address[] public funders;
address public owner;
AggregatorV3Interface public priceFeed;
//constructor(address _priceFeed) public {
constructor(address _priceFeed) {
priceFeed = AggregatorV3Interface(_priceFeed);
owner = msg.sender;
}
function fund() public payable {
uint256 minimumUSD = 50 * 10**18;
require(
getConversionRate(msg.value) >= minimumUSD,
"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);
return uint256(answer * 100000000);
}
// 1000000000
function getConversionRate(uint256 ethAmount)
public
view
returns (uint256)
{
uint256 ethPrice = getPrice();
//uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
uint256 ethAmountInUsd = (ethPrice * ethAmount) / 100000000;
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 {
payable(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);
}
}

谢谢!

您的require语句在这一行失败了:

require(
getConversionRate(msg.value) >= minimumUSD,
"You need to spend more ETH!"
);

这可能意味着你的钱包里没有足够的以太币来发送给合约。因此,用更多的以太币为你的钱包注资,如果你正确部署了合约,它可能会起作用。

索引超出范围可能是get_account()函数中的错误。LOCAL_BLOCKCHAIN_ENVIRONMENTS应该是一个数组,而不是字符串。它也不包含ganache-local,所以你试图为你的本地区块链使用你的私钥,这可能就是为什么你得到索引超出范围的原因。

所以修复你的LOCAL_BLOCKCHAIN_ENVIRONMENTS看起来像这样:

LOCAL_BLOCKCHAIN_ENVIRONMENTS = ["development", "ganache-local"]

我想应该可以了。

from brownie import FundMe从脚本。Helpful_scripts import get_account

def fund():
_priceFeed = 0x8A753747A1Fa494EC906cE90E9f37563A8AF630e
account = get_account()
fund_me = FundMe.deploy(_priceFeed , {"from": account})
**_priceFeed is in your constructor , so you should pass it to your 
contract for deploy, i added it as hardcode , but its better to use yaml 
file for projects **
entrance_fee = fund_me.getEntranceFee()
print(entrance_fee)
print(f"The current entry fee is {entrance_fee}")
print("funding")
fund_tx = fund_me.fund(
{
"from": account,
"value": entrance_fee,
}
)
# 0.025000000000000000
fund_tx.wait(1)
** when you use functions that make transaction(not reading) , its better 
to use wait() method , its help your transaction to write in block **
def main():
fund()

最新更新