Solidity geth Error encountered during contract execution Bad instruction

0 votes

I have a contract that gets deployed on Ropsten and then I'm trying to call function 'addRecipe' with the following code on geth:

recipes.addRecipe(300, "zupa", "zupa z trupa", {from:web3.eth.accounts[0], gas: 20000000})

The function looks as following:

function addRecipe(uint256 _price, string _name, string _content) public {

    recipes[recipeCount].price = _price;
    recipes[recipeCount].name = _name;
    recipes[recipeCount].content = _content;
    recipes[recipeCount].recipeOwner = msg.sender;

    recipeCount++;
}

I get the TX hash but looking up the transaction in Etherscan gives

 Warning! Error encountered during contract execution [Bad instruction] 

Adding 'payable' modifier to the function or using the following command doesn't give any better results...

recipes.addRecipe.sendTransaction(300, "zupa", "zupa z trupa", {from:web3.eth.accounts[0], gas: 200000000})

The whole contract:

pragma solidity ^0.4.24;

contract Recipes {

    address owner;
    uint256 recipeCount = 0;

    struct Recipe {
        string name;
        string content;
        uint256 price;
        address recipeOwner;
    }

    Recipe[] public recipes;

    function () public {
        owner = msg.sender;
    }

    function kill() public {
        require (msg.sender == owner);
        selfdestruct(owner);
    }


    function addRecipe(uint256 _price, string _name, string _content) public {

        recipes[recipeCount].price = _price;
        recipes[recipeCount].name = _name;
        recipes[recipeCount].content = _content;
        recipes[recipeCount].recipeOwner = msg.sender;

        recipeCount++;
    }

    function showRecipes(uint256 _id) constant returns(string) {

        return recipes[_id].content;

    }

}

Oct 15, 2018 in Blockchain by slayer
• 29,370 points

retagged Nov 22, 2018 by Priyaj 1,467 views

1 answer to this question.

0 votes

recipes is a dynamic storage array. You need to change the size of the array to add new elements to it. You can either do this by explicitly increasing the length of the array or by pushing a new element into the array.

function addRecipe(uint256 _price, string _name, string _content) public {
    recipes.length++;
    recipes[recipeCount].price = _price;
    recipes[recipeCount].name = _name;
    recipes[recipeCount].content = _content;
    recipes[recipeCount].recipeOwner = msg.sender;

    recipeCount++;
}

Or

function addRecipe(uint256 _price, string _name, string _content) public {
    Recipe memory r;
    r.price = _price;
    r.name = _name;
    r.content = _content;
    r.recipeOwner = msg.sender;

    recipes.push(r);

    recipeCount++;
}
answered Oct 15, 2018 by Omkar
• 69,220 points

Related Questions In Blockchain

0 votes
1 answer

Invalid opcode error in a simple Solidity contract and script

You don't need to call .at() if you're using .new(). ...READ MORE

answered Sep 14, 2018 in Blockchain by slayer
• 29,370 points
4,824 views
0 votes
1 answer
0 votes
1 answer

Solidity mocha : address is not a contract error

Try the following: // Initialize contract variable with ...READ MORE

answered Oct 1, 2018 in Blockchain by digger
• 26,740 points
1,069 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,927 views
0 votes
0 answers

Error: Unexpected token o in JSON at position 1

I have been working with Interacting a ...READ MORE

Mar 6, 2019 in Blockchain by saeedi
• 120 points

edited Mar 6, 2019 by Omkar 2,477 views
0 votes
2 answers

Why is network already up to date while trying to deploy a contract on truffle?

I guess you have ganache running already ...READ MORE

answered Apr 24, 2018 in Blockchain by Shashank
• 10,400 points
4,572 views
0 votes
1 answer

How can I deploy a HelloWorld contract on my testrpc network?

The problem lies in the command: truffle migrate Your truffle migrate command ...READ MORE

answered Apr 27, 2018 in Blockchain by Perry
• 17,100 points

edited Aug 10, 2018 by Omkar 2,847 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP