Blockchain 为什么我会犯这个错误&引用;气体估算错误,显示以下信息(见下文)。交易>;执行可能会失败”;
尝试使用混音IDE测试坚固性。我不断得到错误: 气体估算错误,显示以下信息(见下文)。事务>执行可能会失败。您想强制发送吗 有人知道是什么让我犯了这个错误吗。我正在尝试使用以太坊智能合约销售产品。我使用混音IDE创建了这个值为0的合同。 我能够成功地创建合同并添加产品,但我无法购买。最后一行告诉我上面提到的错误 我正在测试的solidity文件如下:如您所见,我创建了一个销售合同,允许用户使用区块链销售产品,买方在以太坊中取回支付价格的产品。如果有人能为我提供一个更好的解决方案,我愿意接受建议Blockchain 为什么我会犯这个错误&引用;气体估算错误,显示以下信息(见下文)。交易>;执行可能会失败”;,blockchain,ethereum,smartcontracts,remix,Blockchain,Ethereum,Smartcontracts,Remix,尝试使用混音IDE测试坚固性。我不断得到错误: 气体估算错误,显示以下信息(见下文)。事务>执行可能会失败。您想强制发送吗 有人知道是什么让我犯了这个错误吗。我正在尝试使用以太坊智能合约销售产品。我使用混音IDE创建了这个值为0的合同。 我能够成功地创建合同并添加产品,但我无法购买。最后一行告诉我上面提到的错误 我正在测试的solidity文件如下:如您所见,我创建了一个销售合同,允许用户使用区块链销售产品,买方在以太坊中取回支付价格的产品。如果有人能为我提供一个更好的解决方案,我愿意接受建议
pragma solidity ^0.4.0;
contract Sell {
struct Product_Quantity{
string _product_name;
uint256 _product_quantity;
uint256 _price_unity;
bool isValue;
}
struct Seller{
address _id;
mapping(string => Product_Quantity) products;
}
Seller public seller;
mapping (address => Product_Quantity) product_owners;
function Sell(){
seller._id = msg.sender;
}
function add_product(string product_name, uint256 product_quantity, uint256 price_unity) {
if(msg.sender != seller._id) throw;
if(seller.products[product_name].isValue){
seller.products[product_name]._product_quantity += product_quantity;
}
else{
seller.products[product_name] = Product_Quantity(product_name, product_quantity, price_unity, true);
}
}
function Buy( string product_name, uint256 quantity) payable {
if(product_owners[msg.sender].isValue){
product_owners[msg.sender]._product_quantity += quantity;
}
else{
product_owners[msg.sender] = Product_Quantity(product_name, quantity, seller.products[product_name]._price_unity, true);
}
seller.products[product_name]._product_quantity -= quantity;
seller._id.transfer(seller.products[product_name]._price_unity * quantity);
}
}
这是一个非常通用的混音错误消息。幸运的是,今天我在Remix上看到了新的错误消息(很好的更新人员!),这使得调试问题变得更容易 当有人试图购买一种产品时,你应该检查通过的价值(单位:wei)是否是购买该产品的正确金额和数量 因为您没有检查它,所以买家可以购买amout等于0的产品,这意味着在buy()函数结束时,合同将没有要发送给卖家的wei。这将引发异常,事务将被还原 我将您的代码更新为在solidity 0.4.23(最新版本)上运行,进行了一些代码重构,并向buy()函数添加了一个修饰符,以检查传递的量是否正确
pragma solidity ^0.4.23;
contract Sell {
struct Product_Quantity{
string _product_name;
uint256 _product_quantity;
uint256 _price_unity;
bool isValue;
}
mapping (address => Product_Quantity) product_owners;
struct Seller{
address _id;
mapping(string => Product_Quantity) products;
}
Seller public seller;
constructor() public {
seller._id = msg.sender;
}
function add_product (string product_name, uint256 product_quantity, uint256 price_unity) public {
require(msg.sender == seller._id);
if (seller.products[product_name].isValue) {
seller.products[product_name]._product_quantity += product_quantity;
}
else{
seller.products[product_name] = Product_Quantity(product_name, product_quantity, price_unity, true);
}
}
modifier hasEnoughEther (string product_name, uint256 quantity) {
require (seller.products[product_name].isValue); // does the product exists?
uint256 neededEther = seller.products[product_name]._price_unity * quantity;
require (msg.value == neededEther); // did the buyer sent the correct value?
_;
}
function buy (string product_name, uint256 quantity) payable public hasEnoughEther (product_name, quantity) {
if (product_owners[msg.sender].isValue) {
product_owners[msg.sender]._product_quantity += quantity;
} else {
product_owners[msg.sender] = Product_Quantity(product_name, quantity, seller.products[product_name]._price_unity, true);
}
seller.products[product_name]._product_quantity -= quantity;
seller._id.transfer(seller.products[product_name]._price_unity * quantity);
}
}
在我的情况下,我需要为我的合同提供资金,以便它能够执行操作。我尝试运行您提供的新代码,但remix仍然显示相同的警告。知道为什么吗?