# 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.

```typescript
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.

```typescript
import { Chains, Client, TriggerType } from '@mimicprotocol/sdk'
import { AbiCoder, keccak256, toUtf8Bytes } from 'ethers'

async function main(): Promise<void> {
  const MIMIC_PROTOCOL_SETTLER = '0xSettlerAddress'
  const INTENT_EXECUTED_TOPIC = '0xee2c98c99b683f71058b8744fea294a411482f07d55c98b392917e9286f22f13'
  const USER_TOPIC = AbiCoder.defaultAbiCoder().encode(['address'], ['0xUserAddress'])
  const FIRST_FUNCTION_TOPIC = keccak256(toUtf8Bytes('First function'))

  const config = {
    type: TriggerType.Event
    chainId: Chains.SomeChain,
    contract: MIMIC_PROTOCOL_SETTLER,
    topics: [
      [INTENT_EXECUTED_TOPIC],
      [USER_TOPIC],
      [FIRST_FUNCTION_TOPIC],
    ],
  }

  const client = new Client({ signer })
  await client.triggers.signAndCreate({ SECOND_FUNCTION_CID, config, ... })
}
```

**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.

```typescript
import { Address, environment, evm } from '@mimicprotocol/lib-ts'
import { IntentExecutedEvent } from './types/Settler'

export default function main(): void {
  const trigger = environment.getContext().triggerPayload.getEventData()
  const event = IntentExecutedEvent.decode(trigger.topics, trigger.eventData)
  const str = evm.decode(new EvmDecodeParam('address', event.data.toHexString()))
  const someAddress = Address.fromString(str) // 0xSomeAddress
}
```

***

### Use case

For a complete use case using custom events, see [Bridge and invest in Aave](https://docs.mimic.fi/use-cases/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.
