Build a simple task

This guide provides step-by-step instructions on how to build and deploy a task to automate a blockchain operation using Mimic Protocol.

Before you begin, ensure you have installed both Mimic’s lib and CLI:

$ yarn add -D @mimicprotocol/cli
$ yarn add -D @mimicprotocol/lib-ts

Workflow overview

The process consists of the following steps:

  1. Define the manifest: Specify task inputs, triggers, and metadata in a manifest file.

  2. Write the task logic: Implement your task logic.

  3. Build: Validate the manifest, generate supporting artifacts, and compile the task logic.

  4. Deploy: Upload your build output to a task registry, making it available for relayers to execute.


Manifest definition

The manifest file provides the configuration for your task, including:

  • Metadata: Name, description, and version of the task.

  • Triggers: When the task should run (e.g., cron schedules or event-based triggers).

  • Inputs: Parameters required by the task logic.

  • ABIs: Smart contract ABIs to generate type-safe interfaces for the task.

Save this configuration in a manifest.yaml file:

version: 1.0.0
name: Balance Monitoring Task
description: Monitors an account's token balance and creates a swap intent if conditions are met.
trigger:
  - type: cron
inputs:
  - chainId: number
  - account: address
  - tokenIn: address
  - tokenOut: address
  - slippage: number
  - threshold: number
abis: 
  - ERC20: './abis/IERC20.json'

This manifest file defines different inputs that will be accessible from the task logic code thanks to the types generation process that will be explained in the next section.

Additionally, it defines a trigger option using a cron schedule. For now, only cron schedule configurations are allowed, later on event triggered tasks will be supported too.

Finally it declares the path for the ERC20 ABI required by the task logic. Similar to the inputs list, the ABIs will be processed to generate the proper types to access them from the codebase.


Generate types

This steps allows you to validate the manifest definitions and generate the corresponding code to access both your declared inputs and the contract objects for your declared ABIs.

To do this you can run the codegen command using the CLI:

$ mimic codegen

This command will output the generated types to the types directory by default. It will also assume your manifest file is called manifest.yaml. However, you can specify a different manifest or output paths using the following parameters:

$ mimic codegen --manifest manifest.yml --output types

Writing the task logic

The task logic is implemented in AssemblyScript and must export:

  1. Input type: The generated TaskInput type from the previous step.

  2. Main function: The core task logic, which receives the inputs as an argument.

Create a file ./src/task.ts and implement the logic:

import { BigNumber, Environment, log } from '@mimicprotocol/lib-ts'
import { ERC20, TaskInput } from './types'

export default function main(environment: Environment, input: TaskInput): void {
  // Load the token and fetch the account balance
  const tokenIn = ERC20.load(input.chainId, input.tokenIn)
  const balance = tokenIn.balanceOf(input.account)

  // Convert balance to USD and calculate minimum output amount  
  const valueInUsd = environment.convertToUsd(tokenIn, balance)
  const minAmountOut = environment.getMinAmountOut(input.tokenIn, balance, input.tokenOut, input.slippage)

  // Create a swap intent if the threshold is met
  if (valueInUsd > input.threshold) {
    environment.swap({
      from: input.account,
      to: input.account,
      tokenIn: input.tokenIn,
      tokenOut: input.tokenOut,
      amount: balance,
      minAmountOut: minAmountOut,
    })
    log.info('Intent created')
  } else {
    log.debug('Threshold not met')
  }
}

As you can see, the generated contract objects can be accessed from your task code.

For now Mimic tasks allows creating three types of intents:

  • Transfers

  • Single-chain swaps

  • Cross-chain swaps


Build process

The build process converts your task logic and manifest into deployable artifacts:

  • task.wasm: The compiled WebAssembly binary of your task.

  • manifest.yaml: The validated manifest.

  • inputs.json: A list of required dependencies for relayers.

Run the build command:

$ mimic build

By default, outputs are saved in the build directory. You can customize the paths:

$ mimic build --task src/task.ts --output build

Here is an example of the output produced by this command:

build/
├── task.wasm         # Compiled WASM binary
├── manifest.json     # Validated manifest
├── inputs.json       # External dependency blueprint

Deploy your task

This is where you upload your task artifacts to the network so others can discover it. To do this you can run the deploy command using the CLI:

$ mimic deploy --key [DEPLOYMENT_KEY]

You can generate a deployment key from the explorer app, where you can login using your wallet.

This command will deploy the generated artifacts from the build directory by default. However, you can specify a path using the following parameter:

$ mimic deploy --input build

This command will basically upload your artifacts to IPFS and pin the resultant CID into the off-chain tasks registry so it can be discovered by others.


Add your task to your project

Once you’ve deployed the task, the next step is to associate it with a project. A project is a container in the Mimic explorer where you can manage multiple tasks, configs, and access controls. In order to create a project simply:

  1. Go to the Mimic explorer.

  2. Sign in with your wallet.

  3. Create a new project or choose an existing one.

Finally, link your deployed task by running:

$ mimic link --project [PROJECT_KEY] --key [DEPLOYMENT_KEY]

Replace [PROJECT_KEY] with the key of your newly created project, and [DEPLOYMENT_KEY] with your account’s deployment key from the explorer.


Config your task

After linking, you need to configure your task to tell which config relayers should use to run your task. This means defining the parameters declared in your manifest.yml file. This configuration is typically done in the explorer UI where you will be requested to sign your config with your wallet.

  1. Open your project in the explorer and locate the task you just linked under the tasks section.

  2. Add or edit your config parameters

  3. Sign the new config

This signature ensures relayers know the config is authorized by the task owner.

You can update or deprecate this config at any point in time to reflect changes, without needing to redeploy the task code again. Just sign the new config in the explorer, and relayers will pick up the latest version.


Next steps

  1. Read more about the Lib

    Learn about available APIs for creating intents and interacting with contracts.

  2. Learn more about the CLI

    Discover advanced CLI commands for testing, validating, and managing tasks.

  3. Build more complex use cases Expand your automation to include cross-chain swaps, event triggers, and more.

Last updated