Events and Task chaining
This page shows how to link your Mimic tasks by emitting and listening to custom events. You’ll learn the full flow:
Emitting an event from a task
Creating a configuration to trigger another task from the event
Reading the event data in the triggered task
Pre-reqs: You’ve already read the basic task guide and can
mimic codegen,mimic compile, andmimic 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 task')
const data = evm.encode([EvmEncodeParam.fromValue('address', '0xSomeAddress')])
builder
.addEvent(Bytes.fromHexString(topic), Bytes.fromHexString(data))
.addUser('0xUserAddress')
}Trigger a task from a custom event
Goal: Automatically execute a second task every time an intent from the first task is executed.
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_TASK_TOPIC = keccak256(toUtf8Bytes('First task'))
const trigger = {
type: TriggerType.Event
chainId: Chains.SomeChain,
contract: MIMIC_PROTOCOL_SETTLER,
topics: [
[INTENT_EXECUTED_TOPIC],
[USER_TOPIC],
[FIRST_TASK_TOPIC],
],
}
const client = new Client({ signer })
await client.configs.signAndCreate({ SECOND_TASK_CID, trigger, ... })
}Notes
The code above is a backend script, not a task.
This section shows how to trigger a task from a custom event. However, it's also possible to trigger tasks from any smart contract event, such as ERC-20 token transfers.
Read event data
Goal: Access information included in the event.
import { Address, environment, evm } from '@mimicprotocol/lib-ts'
import { IntentExecutedEvent } from './types/Settler'
export default function main(): void {
const trigger = environment.getContext().trigger.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, where one task transfers tokens between chains and emits an event, and another task reacts to that event by investing the bridged tokens.
Last updated