Skill: Writing a Function

A Mimic function is an AssemblyScript module compiled to WebAssembly. It runs inside a sandboxed environment on every trigger execution, receives typed inputs from the manifest, queries on-chain and off-chain data, and emits intents — declarations of what you want to happen on-chain.


Project anatomy

my-function/
├── manifest.yaml          # Inputs, ABIs, and metadata
├── src/
│   ├── function.ts        # Your function logic (entry point)
│   └── types/             # Auto-generated by `mimic codegen`
│       ├── index.ts       # Typed inputs
│       └── ERC20.ts       # Generated ABI wrapper (one per ABI)
└── build/
    ├── function.wasm      # Compiled output
    └── manifest.json      # Validated manifest

Manifest

The manifest describes your function's metadata, inputs, and contract ABIs. The CLI uses it to validate, generate types, compile, and deploy.

version: 1.0.0
name: My Automation Function
description: Swaps USDC to ETH when balance exceeds a threshold.
inputs:
  - chainId: uint32
  - account: address
  - tokenIn: address
  - tokenOut: address
  - threshold: uint256
  - slippage: uint32
abis:
  - ERC20: "./abis/ERC20.json"

Inputs can optionally include a description:

  • inputs and abis are merged into maps during validation — duplicate keys are rejected.

  • ABI paths are resolved relative to the directory of your manifest.yaml.


Function structure

Every function must export a main function. The inputs type is auto-generated from your manifest.


Library reference (@mimicprotocol/lib-ts)

Primitives


Tokens and amounts

Built-in chain namespaces export known tokens and the chain's CHAIN_ID:


Result type

All queries (except getContext) return Result<V, string>. Always handle errors.


Environment queries

Token price

Relevant token balances

Returns all token balances for an address, with optional chain, allow/deny list, and USD minimum filters.

EVM contract read (raw)

Use generated ABI wrappers (see below) instead of raw calls where possible.

Native token balance

Account code

Subgraph query

Execution context

getContext() does not return a Result — it cannot fail.


Generated ABI wrappers

After running mimic codegen, each ABI declared in the manifest produces a typed wrapper in src/types/<ContractName>.ts.

Read methods call the contract and return Result<T, string>:

Write methods return an EvmCallBuilder — they do not call the environment directly:

Type mapping summary

Solidity
AssemblyScript

address

Address

bool

bool

string

string

bytes / bytesN

Bytes

uint8uint16, int8int16

u8/u16/i8/i16

uint32uint64, int32int64

u32/u64/i32/i64

uint256, int256 (N ≥ 24)

BigInt

T[]

T[]

tuple / struct

generated class


Intent builders

Intents are declarations of what you want to happen. The protocol finds the best way to fulfill them.

Transfer — move tokens between addresses

Shorthand for a single-token transfer:

All input amounts (token, fee) must be on the same chain as the builder.

Swap — exchange tokens

The tokenOut amount is a minimum — relayers may deliver more.

Shorthand for a simple single-chain swap (recipient defaults to ctx.user):

EVM Call — execute contract functions

Common builder options

All intent builders support these optional methods before .build():

Method
Purpose

.addSettler(address)

Override the settler contract (defaults to context settler)

.addUser(address)

Override the user (defaults to ctx.user)

.addDeadline(timestamp)

Override the deadline (defaults to 5 minutes from now)

.addNonce(string)

Override the nonce (defaults to auto-generated)

.addEvent(topic, data)

Attach an on-chain event to the intent

.addEvents(events[])

Attach multiple events


Persistent storage

Use the storage namespace to read and write arbitrary bytes to the user's on-chain storage via the Mimic Helper contract. Useful for maintaining state between executions.


Logging

Use {} as a placeholder; arguments are injected in order. Levels: DEBUG < INFO < WARNING < ERROR < CRITICAL.


EVM utilities


Putting it together — annotated example

Last updated