2

We have a contract as follows:

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;
        //uint256 public number_of_sub_states = 3;

        event LogNewObject(address sender, bytes32 id, bytes32 sub_states_types, bytes32 sub_states_values, address owner);
        event LogChangeObjectState(address sender, bytes32 uid, bytes32 newState);
        event LogChangeObjectOwner(address sender, bytes32 uid, 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, 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, bytes32 _newState) public returns(bool success) {
            require(isObject(_id));
            //objectStructs[_id].location = _newState;
            objectStructs[_id].state = StateStruct(_newState);
            emit LogChangeObjectState(msg.sender, _id, _newState);
            return true;
        }

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

    }

When, I call function newObject in remix, I receive the following error:

Invoke newObject in remix using related text box : 100, 3, [bytes32("location"),bytes32("price"),bytes32("sold")], [bytes32("Paris"),bytes32("50"),bytes32("yes")], address(0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773)

ERROR : error encoding arguments syntax error unexpected token b in JSON at position 12.

What is the reason of this error ?

IMPORTANT NOTE : I edited my question regarding to the answer of user Jesse Busman.

2

I see four potential problems with your function call:

  1. You should use square brackets [] for array literals instead of curly braces {}:

    newObject(100, 3, ["location","price","sold"], ["Paris","50","yes"], '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773');
    
  2. The newObject function takes dynamically sized arrays as arguments. From the Solidity documentation:

    Note that currently, fixed size memory arrays cannot be assigned to dynamically-sized memory arrays, i.e. the following is not possible:

    // This will not compile.
    
    pragma solidity ^0.4.0;
    
    contract C {
        function f() public {
            // The next line creates a type error because uint[3] memory
            // cannot be converted to uint[] memory.
            uint[] x = [uint(1), 3, 4];
        }
    }
    

    One solution is to change the function signature of newObject to accept only arrays of size 3:

    ..., uint256 number_of_sub_states, bytes32[3] sub_states_types, bytes32[3] sub_states_values, ...
    
  3. An array literal of strings cannot be implicitly converted to an array of bytes32. You can cast the values in the function call manually:

    newObject(100, 3, [bytes32("location"),bytes32("price"),bytes32("sold")], [bytes32("Paris"),bytes32("50"),bytes32("yes")], '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773');
    
  4. Addresses in Solidity are written as hexadecimal constants casted to address. So you should write this: address(0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773)

    instead of this: '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773'

| improve this answer | |
1

I must invoke newObject function in remix in following format:

40,3,["location","price","sold","","","","","","",""],["Paris","50","No","","","","","","",""], 0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773

Now, the result is true.

| improve this answer | |

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.