简体   繁体   中英

Why am I getting this error ? “Gas estimation errored with the following message (see below). The transaction > execution will likely fail”

Trying to test solidity using Remix IDE. I keep getting the error:

Gas estimation errored with the following message (see below). The transaction > execution will likely fail. Do you want to force sending?

Does anybody have an idea about what might be giving me this error. It I am trying to sell products using the ethereum smart contracts. I have used the Remix IDE to create this contract with value = 0. I am successfully able to create the contract and add_product but I am unable to Buy. The last line give me the error mentionned above.

The solidity file I am testing againt is the following: As you can see I create a Sell contract which would allow a user to sell products using the blockchain and a buyer to retrieve the product paying the price in ethereum. If anyone has a better solution for me to use for this exact use-case I am opened to suggestions.

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


    }
}

That's a very generic Remix error message. Fortunately, today I'm seeing new error messages on Remix (nice update guys!), which makes it easier to debug the problem.

When someone tries to buy a product, you should check if the value passed (in wei) is the right amount to buy that product and quantity.

Since you're not checking that, a buyer can buy a product with an amout equals to 0, which means the contract will have no wei to send to the seller at the end of the buy() function. That will throw an exception and the transaction will be reverted.

I updated your code to run on solidity 0.4.23 (latest version), made some code refactoring and add a modifier to the buy() function to check if the amount passed is correct.

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

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

In your case you are trying to use a MODIFIER function with arguments but have not passed any parameters to it in the Buy function.

And in my case I was trying to trigger a non-payable function with some ether in the VALUE field of the Deploy and Run Transactions tab.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM