cadence icon indicating copy to clipboard operation
cadence copied to clipboard

Event listeners

Open cybercent opened this issue 3 years ago • 4 comments

Issue To Be Solved

Allow contracts to listen to events emitted by other contracts.

Suggested Solution

When an event is emitted from Ping

pub contract Ping {
   event Pinged(with: String)
}

Pong's listener would be triggered.

import Ping from 0x01

pub contract Pong {

  pub listener PingListener(on: Ping.Pinged, fun(with: String){
    log(with)
  })
  
}

cybercent avatar Aug 16 '22 18:08 cybercent

Maybe this could be implemented with delegates.

Ping would have a method to register a delegate.

Pong would call a method on Ping to add itself as a delegate. When an event is emitted by Ping all delegates would receive the event.

cybercent avatar Aug 17 '22 21:08 cybercent

Which listeners would get invoked? Say a transaction calls into a contract that emits an event – would all listeners on the chain get invoked? What if they fail?

turbolent avatar Aug 18 '22 00:08 turbolent

This is not possible I guess

  • Technically requires something like nested transactions to work.
  • Can create very big performance issues. What if listener also emits an event ?
  • Events are in general already messed up: we have big event spam, AN event support is so primitive you should use 3rd party etc

bluesign avatar Aug 18 '22 19:08 bluesign

Maybe:

  • When event E is emitted , all listeners of event E would be invoked.
  • Instead of having sub transactions, when a listener is invoked it could enqueue a new transaction.
  • The gas for the execution of the enqueued transaction could be subtracted from the contract that defined the listener.
  • 🤔 I'm not sure who the authoriser would be

cybercent avatar Aug 25 '22 12:08 cybercent