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

18,062

Solution 1

In my case, I needed to fund my contract so it can perform operations.

Solution 2

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);
    }
}
Share:
18,062
Traoré Moussa
Author by

Traoré Moussa

Computer engineer from Polytechnique Montreal.

Updated on December 05, 2022

Comments

  • Traoré Moussa
    Traoré Moussa over 1 year

    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);
    
    
        }
    }
    
  • aymusbond
    aymusbond almost 4 years
    I tried running the new code provided by you but remix still shows the same warning. Any idea why?