【问题标题】:Why am I getting this error ? "Gas estimation errored with the following message (see below). The transaction > execution will likely fail"为什么我会收到此错误? “气体估算错误,出现以下消息(见下文)。交易 > 执行可能会失败”
【发布时间】:2018-10-15 22:46:27
【问题描述】:

尝试使用 Remix IDE 测试可靠性。我不断收到错误:

气体估算错误并显示以下消息(见下文)。事务 > 执行可能会失败。是否要强制发送?

有没有人知道什么可能给我这个错误。我正在尝试使用以太坊智能合约销售产品。我使用 Remix IDE 创建了这个 value = 0 的合约。 我能够成功创建合同和 add_product 但我无法购买。最后一行给了我上面提到的错误。

我正在测试的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);


    }
}

【问题讨论】:

    标签: blockchain ethereum smartcontracts remix


    【解决方案1】:

    这是一个非常通用的 Remix 错误消息。幸运的是,今天我在 Remix 上看到了新的错误消息(好消息!),这使得调试问题变得更加容易。

    当有人尝试购买产品时,您应该检查传递的值(以 wei 为单位)是否是购买该产品的正确数量和数量。

    由于您没有检查,买家可以购买金额等于 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 仍然显示相同的警告。知道为什么吗?
    【解决方案2】:

    就我而言,我需要为我的合同提供资金,以便它可以执行操作。

    【讨论】:

      【解决方案3】:

      在您的情况下,您尝试使用带参数的 MODIFIER 函数,但未在 Buy 函数中将任何参数传递给它。

      在我的例子中,我试图在 Deploy and Run Transactions 选项卡的 VALUE 字段中使用一些以太币触发非支付函数。

      【讨论】:

        猜你喜欢
        • 2022-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-12
        • 2015-01-25
        • 1970-01-01
        • 1970-01-01
        • 2020-11-03
        相关资源
        最近更新 更多