0

I'm confused about the difference between myContract.events.MyEvent() and web3.eth.subscribe('logs', ...); Both are subscriptions to events, correct?

2

They are both subscriptions to events, but filtered a little differently.

myContract.events.MyEvent() will only return MyEvent events from the specific contract address that you used to create the web3 contract myContract

web3.eth.subscribe('logs', ...) will subscribe to all blockchain events by default.

Per the web3 documentation you can use: web3.eth.subscribe('logs', options [, callback]). Within options, you can use the address and topics to filter this broad subscription to listen to the specific events you want to listen to. In this case, if you wanted to use web3.eth.subscribe listen to the same thing as myContract.events.MyEvent() you would use the options variable and set options.address to the contract address you used to create myContract options.topics to an array where the first topic in the array is MyEvent signature. This would look something like:

web3.eth.subscribe(
   'logs', 
   {
      address: myContractAddress,
      topics:  [Keccak-256 hash(MyEvent(~~parameters here~~))]
      
   },
   () => console.log(`Saw MyEvent`);
);

References

Subscribe to Logs & Options Reference:

https://web3js.readthedocs.io/en/v1.2.7/web3-eth-subscribe.html#subscribe-logs

Event Topics:

What are event topics?

4
  • Is it possible to pass arguments in an event as a string? if it is possible then how do you get the string from the event? smart contract-> emit myevent(string) node-> ?? – Mario Roma Dec 29 '20 at 10:44
  • Yes you can pass strings as event parameters and then use contract.events.MyEvent() or any other way of retrieving Logs to read the string. You can see an example here. If you want extra info please post a question. – Peri Kost Dec 29 '20 at 14:33
  • 1
    You can use a string as an event input, but this could result in users trying to store very large values in the contract resulting in high gas prices or transaction failures. You may want to use a bytes32 or bytes32 array depending on the length limit of what you want to store. Whatever type you use, when your subscription returns an event, the parameters of that event will be within the object. – Steven V Dec 29 '20 at 14:55
  • @StevenV I am working on an ethereum project but I have some doubts. I have a backend that connects to the blockchain via web3.js. I have a backend componnt-->web3.js-->geth-->smart contract. What is the component sending the transaction? Is it the backend component or the geth node? Then suppose that another smart contract in the network emits an event that I want to capture. What is the component that captures the event? Is it the backend component or the geth node? – Mario Roma Jan 21 at 11:25

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.