Solidity – Encountering ‘Bad Instruction’ Error During Contract Execution

contract-developmentsoliditytransactions

I don't understand why if i execute my function with web3.js i obtain this error in the transaction:

Warning! Error encountered during contract execution [Bad instruction]

I read this other question about the same error. I don't understand why with this code its happening the same:

contract Music is owned{

    string public themeMusic;
    string public idMusic;
    int public money;

    function Music (string setThemeMusic, string setIdMusic, int setmoney) {
        themeMusic = setThemeMusic;
        idMusic = setIdMusic;
        money = setmoney;
    }

    function setMoney(int moneyUpdate) onlyOwner {
        money = moneyUpdate;
    }
}

The error is with the setMoney function, is for the onlyOwner? i need to make it payable anyway? I don't want to make it payable, just only use it for set the money and this function only for the owned.

More information:

  • I make other contracts with similar functions and don't have this problem.
  • This contract is created by other contract like the typical example Factory.
  • Owned is a contrac where i store the address and test if is the owner.
  • I'm using Meteor.js and Web3.js.

How do you interact with the contract? For interact with the contract i'm using the Web3.js APi of ethereum.

What command do you use? I'm defining the contract with the ABI and the address of the contract like this myContract = web3.eth.contract(ABIArray).at(contractAddress); and then i call the function of the contract like this myContract.setMoney(money); (and the callback function).

Do you use production blockchain? I'm working in Ropsten Test-net.

My Music company:

contract musicCompany is owned {

    address[] public listofmusic;

    function addMusic(string setThemeMusic, string setIdMusic, int setmoney) onlyOwner {
        address newMusic = new Music(setThemeMusic, setIdMusic, setmoney);
        listofmusic.push(newMusic);
    }
}

This contract can create a new Music contracts.

My Owned contract:

contract owned {

    address public owner;

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

    modifier onlyOwner {
        if (msg.sender != owner) throw;
        _;
    }

}

My Web3.js code:

'click .setMoney'(event, instance) {
     var price = document.getElementById('priceUpdate').value;
     event.preventDefault();
     alert("contract price");
     myContract.setMoney(price, function(error, result){
      if(!error)
          console.log(result);
      else
          console.error(error);
      })

Screenshot of the metamask transactions without any problem:

Screenshot of the metamask transactions without any problem

Screenshot of the EtherScan.io transaction with the error:
Screenshot of the EtherScan.io transaction with the error:

Update 1: I try using unsigned integers and bytes32 instead of int, the result is the same. Maybe the problem is in the modifier, could it be?

Update 2: I tried to delete the onlyOwnermodifier of the function and doesn't work …

Update 3: I tried to delete all the modifier, is owned and onlyOwner and still doesn't working. I think the problem is with the builder function, i will starting now to make test with it…

Update 4: By doing Test, i know that moneyUpdate have the value correctly and money the same, but I have discovered that the value of money don't change. And I have discovered too that when i print the result variable of my callback inside "My Web3.js code" i obtain the adress of the transaction 0x0fda5d31d02c87aa20c45a54f6859c7576b4f0aae4a639c47ec23e5c4eā€Œā€‹3d9953. In principle, the transaction is performed but the error prevents the change.

Some transactions (all are mine) for view the error:

0x52ef9164750c4e174cba1a5f45343220458f1cedca25380f0aaf102df0a8f895

0x4e00d9d67ffb906e0036434ea2e14acea82a6aabb4d8c99d94c0b2bcdef24f7a

0x4663fa874993d54989d073f72d6846cae652a454349e62882dccc96d78528a8e

0x69fbf54ffd81ee114efcd750d4e97bb528ecc868140abdecf218e9ae66391a5

Best Answer

The code is so obviously simple that the only problem would seem to be with onlyOwner. I would check the value of the owner variable. I bet it's not what you think it is. (It's either zero or the value of the sender who deployed the contract). You didn't provide the address of the contract, so I can't check the value of owner on Etherscan. Plus you're not providing the address of the calling account. If they don't match, the transaction will throw, which (as is true of all Ethereum errors) will report out of gas. If the code is really as simple as you say, the only possible problem is with owner. I would check that.

Related Topic