0

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 :)

2
  • 1
    The default websocket port is 8546. The URL should be ws://127.0.0.1:8546. See here ethereum.stackexchange.com/questions/11527/… for other possible issues.
    – Ismael
    Commented Jun 21, 2019 at 16:16
  • Updated it, still on a function call this error occurs.
    – angelmaria
    Commented Jul 1, 2019 at 6:53

1 Answer 1

1

Since all of the eventinloop events will be emitted in a single transaction, you will see the list of all events in the single calc() transaction.

There is no way to react to the first event in the loop, before the second loop is executed (as transactions are atomic, so all events are emitted in a single transaction).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.