[Ethereum] How to access the event log by knowing the contract address (web3)

eventslogsweb3js

How can I access a log stored in one of the tx of the contract via web3?

Code example:

event newtest(string indexed name, uint indexed idlevel,string indexed multib, string objmulti, uint objnm);

and

newtest('test',5,'testj','obj2',30);

Let's say contract address is 0x00. How do I get all the logs that are stored in this contract address with web3?

P.s. I don't need to listen on events in real time. I just need to get all the logs of a contract, based on filters when I need it.

Best Answer

Take a look at web3.eth.filter and watch.

Something like this:

const filter = web3.eth.filter({
  fromBlock: 0,
  toBlock: 'latest',
  address: contractAddress,
  topics: [web3.sha3('newtest(string,uint256,string,string,uint256)')]
})

filter.watch((error, result) => {
   //
})

Note the part that "In Solidity: The first topic is the hash of the signature of the event." Canonical types, such as uint256 have to be used in signatures.

EDIT: Per @plingamp's comment web3.sha3 now includes the '0x'.