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:
Emitting an event from a function
Creating an event trigger to trigger another function from the event
Reading the event data in the triggered function
Pre-reqs: You’ve already read the basic function guide and can
mimic codegen,mimic compile, andmimic deploy.
Emit a custom event
Goal: Add an event with arbitrary information to an operation so it's emitted on-chain when the intent is executed.
Events are attached to operations (not to the intent itself), so you call .addEvent() on any operation builder — EvmCallBuilder, SwapBuilder, or TransferBuilder — before calling .send().
import { Address, Bytes, ChainId, Ethereum, EvmCallBuilder, evm, EvmEncodeParam, TokenAmount } from '@mimicprotocol/lib-ts'
export default function main(): void {
const topic = evm.keccak('First function')
const data = evm.encode([EvmEncodeParam.fromValue('address', '0xSomeAddress')])
const fee = TokenAmount.fromStringDecimal(Ethereum.USDC, '1')
EvmCallBuilder.forChain(ChainId.ETHEREUM)
.addCall(Address.fromString('0xContractAddress'), callData)
.addEvent(Bytes.fromHexString(topic), Bytes.fromHexString(data))
.addUser('0xSmartAccountAddress')
.send(fee)
}The same .addEvent() method is available on SwapBuilder and TransferBuilder:
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