Tenemos un contrato de la siguiente manera:
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;
}
}
Cuando llamo function newObject
a remix, recibo el siguiente error:
Invoque newObject en remix usando el cuadro de texto relacionado: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.
¿Cuál es la razón de este error?
NOTA IMPORTANTE : edité mi pregunta con respecto a la respuesta del usuario Jesse Busman.
Veo cuatro problemas potenciales con su llamada de función:
Debe usar corchetes []
para los literales de matriz en lugar de llaves {}
:
newObject(100, 3, ["location","price","sold"], ["Paris","50","yes"], '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773');
La newObject
función toma matrices de tamaño dinámico como argumentos. De la documentación de Solidez:
Tenga en cuenta que, actualmente, las matrices de memoria de tamaño fijo no se pueden asignar a matrices de memoria de tamaño dinámico, es decir, lo siguiente no es posible:
// 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]; } }
Una solución es cambiar la firma de la función newObject
para aceptar solo matrices de tamaño 3:
..., uint256 number_of_sub_states, bytes32[3] sub_states_types, bytes32[3] sub_states_values, ...
Una matriz literal de cadenas no se puede convertir implícitamente en una matriz de bytes32
. Puede convertir los valores en la llamada de función manualmente:
newObject(100, 3, [bytes32("location"),bytes32("price"),bytes32("sold")], [bytes32("Paris"),bytes32("50"),bytes32("yes")], '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773');
Las direcciones en Solidity se escriben como constantes hexadecimales convertidas en address
. Así que deberías escribir esto:address(0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773)
en lugar de esto:'0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773'
Debo invocar la función newObject en remix en el siguiente formato:
40,3,["location","price","sold","","","","","","",""],["Paris","50","No","","","","","","",""], 0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773
Ahora, el resultado es verdadero.
Preguntador
Preguntador
Preguntador