Events and Function chaining

This page shows how to link your Mimic functions by emitting and listening to custom events. You’ll learn the full flow:

  1. Emitting an event from a function

  2. Creating an event trigger to trigger another function from the event

  3. Reading the event data in the triggered function

Pre-reqs: You’ve already read the basic function guide and can mimic codegen, mimic compile, and mimic deploy.


Emit a custom event

Goal: Add an event with arbitrary information to an intent so it's emitted on-chain.

import { Bytes, evm, EvmEncodeParam } from '@mimicprotocol/lib-ts'

export default function main(): void {
  const topic = evm.keccak('First function')
  const data = evm.encode([EvmEncodeParam.fromValue('address', '0xSomeAddress')])

  builder
    .addEvent(Bytes.fromHexString(topic), Bytes.fromHexString(data))
    .addUser('0xUserAddress')
}

Trigger a function from a custom event

Goal: Automatically execute a second function every time an intent from the first function is executed.

Notes

  • The code above is a backend script, not a function.

  • This section shows how to trigger a function from a custom event. However, it's also possible to trigger functions from any smart contract event, such as ERC-20 token transfers.


Read event data

Goal: Access information included in the event.


Use case

For a complete use case using custom events, see Bridge and invest in Aave, where one function transfers tokens between chains and emits an event, and another function reacts to that event by investing the bridged tokens.

Last updated