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.
Best Answer
I see four potential problems with your function call:
You should use square brackets
[]
for array literals instead of curly braces{}
:The
newObject
function takes dynamically sized arrays as arguments. From the Solidity documentation:One solution is to change the function signature of
newObject
to accept only arrays of size 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:Addresses in Solidity are written as hexadecimal constants casted to
address
. So you should write this:address(0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773)
instead of this:
'0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773'