2

I can't figure out how can I get event. I was trying with "contract.NewVoting().watch{}" but it says that watch is not a function.

I found out that watch has been removed and there is EventEmitter instead.

Here is a part of my not working yet code:

Solidity:

contract AddNewVotingBuilder is BaseBuilder
{
    function build()
        external
    {
        emit NewVoting(address(new AddNewVoting()));
    }
}

Truffle:

console.log("building add voting contract...");
await deployer.deploy(AddNewVotingBuilder);
let builder = await BaseBuilder.at(AddNewVotingBuilder.address);
await builder.build(); // doesn't work even without await
await builder.NewVoting()
    .on('data', event => console.log(event));

It completely ignores the code and doesn't show the event.

How can I make it working?

1

Listening for events is something you do in the background, so you should make sure you are watching for the events before calling the function that emits the event.

Change the code to:

console.log("building add voting contract...");

await deployer.deploy(AddNewVotingBuilder);
let builder = await BaseBuilder.at(AddNewVotingBuilder.address);

builder.NewVoting()
  .on('data', event => console.log(event));

await builder.build();
1

If you're on version 5 of Truffle you can use getPastEvents() from Web3js :

for example something like:

await builder.getPastEvents( 'NewVoting', { fromBlock: 0, toBlock: 'latest' } )

ref: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#getpastevents

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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