[Ethereum] Truffle – How to get event

truffle

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?

Best Answer

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

Related Topic