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​

You can also emit a string or f-string, which is handy for debugging and audit trails:

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

A string emit produces an event with no name and no topics. The message is its only data, and Cocolab prints it as value:<message>:

Execution Logs:
>> [0x86ef...c5d2] value:Transferred 5 tokens

The address in brackets is where the event was emitted to. A string emit has no target, so it's the emitting logic's address.

Because the event has no name, filters that match an event name or topics, like get events.<Name> or get events with {…}, never return it. Query string emits in Cocolab with plain get events, or with from <logic>, of <ixHash> or on <logic>.

Complete Example​

coco Logger

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

// Emitting needs no state access, so the endpoint is pure
endpoint 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