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 Mimic Protocol’s CLI:

yarn global add @mimicprotocol/cli

Make sure that yarn's global bin directory is in your PATH environment variable so that the mimic command can be found in your terminal (check yarn global docs).

Workflow overview

The process consists of the following steps:

  1. Initialize your project: Start a working directory to develop your task from scratch.

  2. Define the manifest: Specify task inputs, ABIs, and metadata in a manifest file.

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

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

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


Initialize your project

Let's start a new working directory to develop your task. To do that you can run the following command:

mimic init -d my-mimic-task

Let's continue analyzing the structure of this project.


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
  - fee: uint256
  - recipient: address 
  - thresholdUSD: uint32
abis: 
  - ERC20: './abis/ERC20.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.

Note that inputs and abis can be written in YAML as lists of single-key objects (as shown above). They are merged into maps during validation, and duplicate keys are rejected. ABI paths are resolved relative to the directory of your manifest.yaml.

Additionally, each input may include an optional description using the object form:

inputs:
  - amount:
      type: uint256
      description: Amount to transfer in wei
  - recipient:
      type: address
      description: Target account address

For the purpose of this example you will the ERC20 ABI, you can copy it from here:

4KB
Open

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 ./src/types 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 src/types

If you pass the --clean flag, you will be asked to confirm before deleting the existing contents of the output directory.


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

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

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

  const token = ERC20Token.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(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/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 --manifest manifest.yaml --output build

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

build/
├── 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.

By default, this command will run code generation and compilation, then deploy the generated artifacts from the build directory. You can skip the build steps by passing --skip-compile if you already have up-to-date artifacts.

You can specify a different input/output directory using the following parameters:

mimic deploy --input build --output build

This command will upload your artifacts to the Mimic Registry (which stores them on IPFS) and pin the resultant CID so it can be discovered by others. The CID is also written to CID.json in the specified output directory.


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