Skip to main content

Events

Events are log entries emitted during execution. They're useful for tracking actions and state changes.

Defining Events

Use the event keyword to define an event structure:

event TransferEvent:
topic from Identifier
topic to Identifier
field amount U64
field timestamp U64
Limits
  • Up to 4 topics per event
  • Up to 256 fields per event
  • Only primitive types allowed

Emitting Events

Direct Emit

emit TransferEvent{
from: sender_id,
to: recipient_id,
amount: 100,
timestamp: now
}

Emit to Actor Context

By default, events log to the logic context. Use -> to log to an actor's context:

emit TransferEvent{...} -> Sender     // Log to sender's context
emit TransferEvent{...} -> recipient // Log to specific actor

Classes with Events

Classes can implement __event__ to be emitted directly:

class Registration:
field name String
field counter U64

method __event__() -> (ev RegistrationEvent):
ev = RegistrationEvent{
user_name: self.name,
count: self.counter
}

event RegistrationEvent:
topic user_name String
field count U64

Then emit the class directly:

memory reg = Registration{name: "Alice", counter: 1}
emit reg -> Sender // Calls __event__ automatically

String Emits and builtin.Log

When you emit a string or f-string, Coco generates a builtin.Log event:

emit "Simple log message"
emit f"Transferred {amount} tokens"

These builtin.Log events appear in event queries and can be used for debugging and audit trails.

Complete Example

coco Logger

event UserAction:
topic user Identifier
topic action String
field details String

endpoint dynamic LogAction(action String, details String):
emit UserAction{
user: Sender,
action: action,
details: details
}

// Or emit to sender's context
emit UserAction{
user: Sender,
action: action,
details: details
} -> Sender