I have a contract function in private network with a for loop and emitting event inside the loop. Following is the code :
pragma solidity ^0.4.20;
contract Eventsloop{
event loopidx(uint idx,uint jdx);
function watcheventloop() public payable returns(bool){
for(uint j =0;j<5;j++){
for(uint i=0;i<20;i++){
emit loopidx(i,j);
}
}
}
}
JS:
const Web3 = require('web3');
web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
const abidata = //abidata;
var deployedContract = new web3.eth.Contract(abidata,contractaddress,{
defaultAccount: account, // default from address
defaultGasPrice: '100000000000'
});
deployedContract.methods.watcheventloop().send({from:account,gas: 400000000})
.on('receipt',(receipt)=>{
console.log("Tx complted"+JSON.stringify(receipt))
}).catch(function(e){
console.log("ERror "+JSON.stringify(e))
})
deployedContract.events.loopidx({
fromBlock:58619,
toBlock:'latest'
}).on('data',event => {
console.log('new event ',event)//for each iteration an action should occur.
}).on('error', error =>{
console.log("Error ",error);
})
Is it possible to watch event in each iteration,so that some action could be done based on emitted values(i,j).
When tested with single looping,I was getting event log of all iteration together(iteration can go up to 500) and will be difficult to handle as another loop has to be written to fetch values from event logs in JS file. Is there any method for each iteration in contract, some action could be triggered in JS file?
Based on my requirements I need double looping but, watching event stopped in between with an error as follows:
Error: Subscriptions are not supported with the HttpProvider
However, transaction receipt of function_call displayed all eventlogs.
So then I updated code with
web3 = new Web3()
const web3 = new Web3(new Web3.providers.WebsocketProvider('http://127.0.0.1:8545'));
and got another error :
{"type":"error","isTrusted":false,"_yaeti":true,"target":{"_listeners":{"message":[null],"open":[null,null],"close":[null],"error":[null]},"_url":"http://127.0.0.1:8545","_readyState":3,"_extensions":"","_bufferedAmount":0,"_binaryType":"arraybuffer","_client":{"domain":null,"_events":{},"_eventsCount":0,"config":{"maxReceivedFrameSize":1048576,"maxReceivedMessageSize":8388608,"fragmentOutgoingMessages":true,"fragmentationThreshold":16384,"webSocketVersion":13,"assembleFragments":true,"disableNagleAlgorithm":true,"closeTimeout":5000,"tlsOptions":{}},"_req":null,"protocols":[],"origin":null,"url":{"protocol":"http:","slashes":true,"auth":null,"host":"127.0.0.1:8545","port":"8545","hostname":"127.0.0.1","hash":null,"search":null,"query":null,"pathname":"/","path":"/","href":"http://127.0.0.1:8545/"},"secure":false,"base64nonce":"fS/r0k1e2t5EisMDqV2Pjw=="}},"cancelable":true}
Help will be appreciated :)
ws://127.0.0.1:8546
. See here ethereum.stackexchange.com/questions/11527/… for other possible issues.