Solidity
錯誤編碼參數(混音)
我們的契約如下:
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; } }
當我呼叫
function newObject
remix 時,我收到以下錯誤:使用相關文本框在 remix 中呼叫 newObject :
100, 3, [bytes32("location"),bytes32("price"),bytes32("sold")], [bytes32("Paris"),bytes32("50"),bytes32("yes")], address(0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773)
錯誤 :
error encoding arguments syntax error unexpected token b in JSON at position 12.
這個錯誤的原因是什麼?
重要提示:我編輯了關於使用者 Jesse Busman 回答的問題。
我發現您的函式呼叫存在四個潛在問題:
- 您應該
[]
對數組文字使用方括號而不是花括號{}
:newObject(100, 3, ["location","price","sold"], ["Paris","50","yes"], '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773');
- 該
newObject
函式將動態大小的數組作為參數。來自 Solidity 文件:請注意,目前,固定大小的記憶體數組不能分配給動態大小的記憶體數組,即以下是不可能的:
// 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]; } }
一種解決方案是將函式簽名更改為
newObject
僅接受大小為 3 的數組:..., uint256 number_of_sub_states, bytes32[3] sub_states_types, bytes32[3] sub_states_values, ...
- 字元串的數組文字不能隱式轉換為
bytes32
. 您可以手動轉換函式呼叫中的值:newObject(100, 3, [bytes32("location"),bytes32("price"),bytes32("sold")], [bytes32("Paris"),bytes32("50"),bytes32("yes")], '0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773');
- Solidity 中的地址被寫入為轉換為
address
. 所以你應該這樣寫:address(0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773)
而不是這個:
'0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773'
我必須以以下格式在 remix 中呼叫 newObject 函式:
40,3,["location","price","sold","","","","","","",""],["Paris","50","No","","","","","","",""], 0xE07b6e5a2026CC916A4E2Beb03767ae0ED6af773
現在,結果是真的。