Web3js
如何獲得類似於“混音”的“事件日誌”?
我有一個事件如下:
event LogNewObject(address sender, bytes32 indexed id, bytes32 sub_states_types, bytes32 sub_states_values, address owner);
此事件用於以下功能:
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; }
我想得到像remix這樣的日誌,如下圖所示:
但是,當我使用以下程式碼獲取事件日誌時:
var Ev = contractInstance.LogNewObject({}, {fromBlock: 0, toBlock: 'latest'}); Ev.get((error, events) => { if (!error) { function ShowResults(event) { console.log('sub_states_types: ' + event.args.sub_states_types); console.log('sub_states_values: ' + event.args.sub_states_values); } events.forEach(ShowResults); } else { console.log('Error'); } });
我收到以下錯誤:
TypeError:contractInstance.LogNewObject 不是函式
如上圖所示,我如何呼叫
event LogNewObject
以獲得像remix logs 輸出一樣的結果?注意:我根據使用者“oktapodia”的回答修改了我的命令。第一個 spep 現在沒有錯誤,但是我收到另一個錯誤,如下所示:
> var Ev = contractInstance.events.LogNewObject({}, {fromBlock: 0, toBlock: 'latest'}); undefined > Ev.events.get((error, events) => { ... if (!error) { ..... function ShowResults(event) { ....... console.log('sub_states_types: ' + event.args.sub_states_types); ....... console.log('sub_states_values: ' + event.args.sub_states_values); ....... } ..... events.forEach(ShowResults); ..... } else { ..... console.log('Error'); ..... } ... }); TypeError: Cannot read property 'get' of undefined
如果我使用以下格式,我會再次收到以前的錯誤:
> var Ev = contractInstance.events.LogNewObject({}, {fromBlock: 0, toBlock: 'latest'}); undefined > Ev.get((error, events) => { ... if (!error) { ..... function ShowResults(event) { ....... console.log('sub_states_types: ' + event.args.sub_states_types); ....... console.log('sub_states_values: ' + event.args.sub_states_values); ....... } ..... events.forEach(ShowResults); ..... } else { ..... console.log('Error'); ..... } ... }); TypeError: Ev.get is not a functionI also used the command proposed by user "oktapodia", however I receive this error :
我還使用了該命令,因為他的回答中解釋了使用者“oktapodia”。但是我收到如下新錯誤:“TypeError:無法讀取未定義的屬性’LogNewObject’”
重要提示:這是我的智能合約程式碼:
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; } }
function newObject
並使用以下命令呼叫:contractInstance.methods.newObject(web3.utils.asciiToHex("50"),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) });
看起來您正在使用具有新語法的 web3 1.0 版(此處的文件)
更改
contractInstance.LogNewObject
為contractInstance.events.LogNewObject
答案太長,無法在單個評論中添加。
因為您是直接在終端中而不是從文件中執行它,所以在 NodeJS 中,分號不是強制性的,並且您的終端將 解釋
.on
為新範圍內的新行而不是函式 bind tocontractInstance.events.LogNewObject
,以下範例應直接從你的終端,但不是很漂亮:)contractInstance.events.LogNewObject({ fromBlock: 0, }, function(error, event){ console.log(event); }).on('data', function(event){ console.log(event); // same results as the optional callback above }).on('changed', function(event){ // remove event from local database }).on('error', console.error);