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.

  • 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: Fulfill an account's balance based on threshold in USD
inputs:
  - chainId: uint32
  - token: address
  - amount: uint256
  - recipient: address  
  - thresholdUSD: uint32
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. However, other types of task triggers can be used like event or balance threshold triggers.

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 inputs 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 { Token, Transfer, USD, TokenAmount } from '@mimicprotocol/lib-ts'

import { inputs } from './types'
import { ERC20 } from './types/ERC20'

export default function main(): void {
  const tokenContract = new ERC20(inputs.token, inputs.chainId)
  const balance = tokenContract.balanceOf(inputs.recipient)

  const token = Token.fromAddress(inputs.token, inputs.chainId)
  const balanceInUsd = TokenAmount.fromBigInt(token, balance).toUsd()
  const thresholdUsd = USD.fromI32(inputs.thresholdUSD)
  console.log('Balance in USD: ' + balanceInUsd.toString())

  if (balanceInUsd.lt(thresholdUsd)) {
    Transfer
      .create(inputs.chainId, inputs.token, inputs.amount, inputs.recipient, inputs.fee)
      .send()
  }
}

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

For now Mimic tasks allows creating three types of intents:

  • Transfers

  • Generic calls

  • Cross-chain swaps


Compile process

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

  • build/task.wasm - Compiled WebAssembly binary

  • build/task.wat - WebAssembly text format (human-readable)

  • build/manifest.json - Processed manifest configuration

Run the compile command:

$ mimic compile

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

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

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

build/
├── task.wat          # WASM text format
├── task.wasm         # Compiled WASM binary
├── manifest.json     # Validated manifest

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.


Config your task

After deploying your task, you can now configure it 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 done in the explorer UI where you will be requested to sign your config with your wallet.

  1. Open the explorer and locate the task you just deployed 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 examples.

Last updated