1

I have received same message as this question : Warning! Error encountered during contract execution [Bad instruction] However, my contract concept is different. Here is my contract code :

pragma solidity 0.4.23; 

contract RFID {

    struct StateStruct {
        bytes32 description;
        mapping(bytes32 => bytes32) sub_state;
    }

    struct ObjectStruct {
        StateStruct state;
        address owner; 
        bool isObject;
    }

    mapping(bytes32 => ObjectStruct) objectStructs;
    bytes32[] public objectList;

    event LogNewObject(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values, address owner);
    event LogChangeObjectState(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values);
    event LogChangeObjectOwner(address sender, bytes32 indexed id, address newOwner);

    function isObject(bytes32 _id) public view returns(bool isIndeed) {
        return objectStructs[_id].isObject;
    }

    function getObjectCount() public view returns(uint count) {
        return objectList.length;
    }

    /*function setArraySize(uint256 _number_of_sub_states) public {

        number_of_sub_states = _number_of_sub_states;

    }

    function getArraySize() view public returns (uint256) {
       return number_of_sub_states;
    }*/

    function newObject(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values, address _owner) public returns(bool success) {
        require(!isObject(_id));

        uint256 counter=0;
        for(counter; counter < number_of_sub_states; counter++) {

            objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

            emit LogNewObject(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]), _owner);

        }

        objectStructs[_id].owner = _owner;
        objectStructs[_id].isObject = true;

        objectList.push(_id);

        return true;
    }

    function changeObjectState(bytes32 _id, uint256 number_of_sub_states, bytes32[10] sub_states_types, bytes32[10] sub_states_values) public returns(bool success) {
        require(isObject(_id));
        uint256 counter=0;
        for(counter; counter < number_of_sub_states; counter++) {

            objectStructs[_id].state.sub_state[sub_states_types[counter]] = sub_states_values[counter];

            emit LogChangeObjectState(msg.sender, _id, bytes32(sub_states_types[counter]), bytes32(sub_states_values[counter]));

        }
        //objectStructs[_id].state = StateStruct(_newState);
        //emit LogChangeObjectState(msg.sender, _id, _newState);
        return true;
    }

    function changeObjectOwner(bytes32 _id, address _newOwner) public returns(bool success) {
        require(isObject(_id));
        objectStructs[_id].owner = _newOwner;
        emit LogChangeObjectOwner(msg.sender, _id, _newOwner);
        return true;
    }

} 

And I invoked function newObject as follows :

> web3.eth.personal.unlockAccount('0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773', 'logic');
Promise {
  <pending>,
  domain: 
   Domain {
     domain: null,
     _events: 
      { removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [] } }
> contractInstance.methods.newObject(web3.utils.asciiToHex("50"),web3.utils.asciiToHex("3"),[web3.utils.asciiToHex("location"),web3.utils.asciiToHex("price"),web3.utils.asciiToHex("sold"),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex("")],[web3.utils.asciiToHex("Paris"),web3.utils.asciiToHex("50"),web3.utils.asciiToHex("No"),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex(""),web3.utils.asciiToHex("")], '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773').send({ from: '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773' }, function(error, result) {
...     console.log(error);
...     console.log(result)
... });
Promise {
  <pending>,
  domain: 
   Domain {
     domain: null,
     _events: 
      { removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [] },
  _events: undefined,
  emit: [Function: emit],
  on: [Function: on],
  once: [Function: once],
  off: [Function: removeListener],
  listeners: [Function: listeners],
  addListener: [Function: on],
  removeListener: [Function: removeListener],
  removeAllListeners: [Function: removeAllListeners] }
> null
0xa270ed1bda636e4256327683c8d4e5689d81d44c0536a78d4acdb295c9d778e2
(node:11166) UnhandledPromiseRejectionWarning: Error: Transaction ran out of gas. Please provide more gas:
{
  "blockHash": "0x82f1d259bc06749c886016e978152d11c65730dd8b1a6388e738fca620db5be7",
  "blockNumber": 4286599,
  "contractAddress": null,
  "cumulativeGasUsed": 965036,
  "gasUsed": 940000,
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  "root": null,
  "status": false,

I got transaction hash. However, when I search transaction status in https://tobalaba.etherscan.com/tx/0xa270ed1bda636e4256327683c8d4e5689d81d44c0536a78d4acdb295c9d778e2

It is mentioned : "Warning! Error encountered during contract execution [Bad instruction]"

When, I invoke function newObject with remix, it works without any problem.

Is there any problem in my contract ?

1

You make a call to your contract with:

contractInstance.methods.newObject(
    web3.utils.asciiToHex("50"),
    web3.utils.asciiToHex("3"),...

In your contract it is declared

function newObject(bytes32 _id, uint256 number_of_sub_states, ..) 

But web3.utils.asciiToHex("3") returns 0x33. Meaning number_of_sub_states = 51 in your function. This causes an index out of bound when trying to access elements beyond 10 in your function.

You can either pass numbers directly

contractInstance.methods.newObject(
    web3.utils.asciiToHex("50"),
    3,

Or use web3.utils.toHex()

contractInstance.methods.newObject(
    web3.utils.asciiToHex("50"),
    web3.utils.toHex("3"),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.